1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.arvados.org/arvados.git/sdk/go/arvados"
19 "git.arvados.org/arvados.git/sdk/go/arvadostest"
20 "git.arvados.org/arvados.git/sdk/go/ctxlog"
21 "git.arvados.org/arvados.git/sdk/go/httpserver"
22 "github.com/prometheus/client_golang/prometheus"
23 check "gopkg.in/check.v1"
26 var forceLegacyAPI14 bool
28 // Gocheck boilerplate
29 func Test(t *testing.T) {
30 for _, forceLegacyAPI14 = range []bool{false, true} {
35 var _ = check.Suite(&HandlerSuite{})
37 type HandlerSuite struct {
38 cluster *arvados.Cluster
41 cancel context.CancelFunc
44 func (s *HandlerSuite) SetUpTest(c *check.C) {
45 s.ctx, s.cancel = context.WithCancel(context.Background())
46 s.ctx = ctxlog.Context(s.ctx, ctxlog.New(os.Stderr, "json", "debug"))
47 s.cluster = &arvados.Cluster{
49 PostgreSQL: integrationTestCluster().PostgreSQL,
50 ForceLegacyAPI14: forceLegacyAPI14,
52 s.cluster.TLS.Insecure = true
53 arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
54 arvadostest.SetServiceURL(&s.cluster.Services.Controller, "http://localhost:/")
55 s.handler = newHandler(s.ctx, s.cluster, "", prometheus.NewRegistry())
58 func (s *HandlerSuite) TearDownTest(c *check.C) {
62 func (s *HandlerSuite) TestConfigExport(c *check.C) {
63 s.cluster.ManagementToken = "secret"
64 s.cluster.SystemRootToken = "secret"
65 s.cluster.Collections.BlobSigning = true
66 s.cluster.Collections.BlobSigningTTL = arvados.Duration(23 * time.Second)
67 for _, method := range []string{"GET", "OPTIONS"} {
68 req := httptest.NewRequest(method, "/arvados/v1/config", nil)
69 resp := httptest.NewRecorder()
70 s.handler.ServeHTTP(resp, req)
71 c.Check(resp.Code, check.Equals, http.StatusOK)
72 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, `*`)
73 c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Matches, `.*\bGET\b.*`)
74 c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Matches, `.+`)
75 if method == "OPTIONS" {
76 c.Check(resp.Body.String(), check.HasLen, 0)
79 var cluster arvados.Cluster
80 c.Log(resp.Body.String())
81 err := json.Unmarshal(resp.Body.Bytes(), &cluster)
82 c.Check(err, check.IsNil)
83 c.Check(cluster.ManagementToken, check.Equals, "")
84 c.Check(cluster.SystemRootToken, check.Equals, "")
85 c.Check(cluster.Collections.BlobSigning, check.DeepEquals, true)
86 c.Check(cluster.Collections.BlobSigningTTL, check.Equals, arvados.Duration(23*time.Second))
90 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
91 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
92 resp := httptest.NewRecorder()
93 s.handler.ServeHTTP(resp, req)
94 c.Check(resp.Code, check.Equals, http.StatusOK)
95 var dd arvados.DiscoveryDocument
96 err := json.Unmarshal(resp.Body.Bytes(), &dd)
97 c.Check(err, check.IsNil)
98 c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
99 c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
100 c.Check(len(dd.Resources), check.Not(check.Equals), 0)
101 c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
104 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
105 s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond)
106 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
107 resp := httptest.NewRecorder()
108 s.handler.ServeHTTP(resp, req)
109 c.Check(resp.Code, check.Equals, http.StatusBadGateway)
110 var jresp httpserver.ErrorResponse
111 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
112 c.Check(err, check.IsNil)
113 c.Assert(len(jresp.Errors), check.Equals, 1)
114 c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
117 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
118 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
119 resp := httptest.NewRecorder()
120 s.handler.ServeHTTP(resp, req)
121 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
122 jresp := map[string]interface{}{}
123 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
124 c.Check(err, check.IsNil)
125 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
128 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
129 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
130 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
131 resp := httptest.NewRecorder()
132 s.handler.ServeHTTP(resp, req)
133 c.Check(resp.Code, check.Equals, http.StatusOK)
135 err := json.Unmarshal(resp.Body.Bytes(), &u)
136 c.Check(err, check.IsNil)
137 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
140 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
141 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
143 "api_token": {arvadostest.ActiveToken},
145 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
146 resp := httptest.NewRecorder()
147 s.handler.ServeHTTP(resp, req)
148 c.Check(resp.Code, check.Equals, http.StatusOK)
150 err := json.Unmarshal(resp.Body.Bytes(), &u)
151 c.Check(err, check.IsNil)
152 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
155 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
156 req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
157 resp := httptest.NewRecorder()
158 s.handler.ServeHTTP(resp, req)
159 c.Check(resp.Code, check.Equals, http.StatusNotFound)
160 jresp := map[string]interface{}{}
161 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
162 c.Check(err, check.IsNil)
163 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
166 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
167 s.cluster.Login.ProviderAppID = "test"
168 s.cluster.Login.ProviderAppSecret = "test"
169 req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
170 resp := httptest.NewRecorder()
171 s.handler.ServeHTTP(resp, req)
172 if !c.Check(resp.Code, check.Equals, http.StatusFound) {
173 c.Log(resp.Body.String())
175 // Old "proxy entire request" code path returns an absolute
176 // URL. New lib/controller/federation code path returns a
178 c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
181 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
182 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
183 user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
184 c.Assert(err, check.IsNil)
185 c.Check(ok, check.Equals, true)
186 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
187 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
188 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
189 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
192 func (s *HandlerSuite) TestValidateV2APIToken(c *check.C) {
193 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
194 user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
195 c.Assert(err, check.IsNil)
196 c.Check(ok, check.Equals, true)
197 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
198 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
199 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
200 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
201 c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
204 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
205 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
206 auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
207 c.Assert(err, check.IsNil)
208 c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
210 user, ok, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
211 c.Assert(err, check.IsNil)
212 c.Check(ok, check.Equals, true)
213 c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
214 c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
215 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
216 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
217 c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())