1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.curoverse.com/arvados.git/sdk/go/arvados"
19 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
20 "git.curoverse.com/arvados.git/sdk/go/ctxlog"
21 "git.curoverse.com/arvados.git/sdk/go/httpserver"
22 check "gopkg.in/check.v1"
25 var enableBetaController14287 bool
27 // Gocheck boilerplate
28 func Test(t *testing.T) {
29 for _, enableBetaController14287 = range []bool{false, true} {
34 var _ = check.Suite(&HandlerSuite{})
36 type HandlerSuite struct {
37 cluster *arvados.Cluster
40 cancel context.CancelFunc
43 func (s *HandlerSuite) SetUpTest(c *check.C) {
44 s.ctx, s.cancel = context.WithCancel(context.Background())
45 s.ctx = ctxlog.Context(s.ctx, ctxlog.New(os.Stderr, "json", "debug"))
46 s.cluster = &arvados.Cluster{
48 PostgreSQL: integrationTestCluster().PostgreSQL,
50 EnableBetaController14287: enableBetaController14287,
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, "")
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 req := httptest.NewRequest("GET", "/arvados/v1/config", nil)
68 resp := httptest.NewRecorder()
69 s.handler.ServeHTTP(resp, req)
70 c.Check(resp.Code, check.Equals, http.StatusOK)
71 var cluster arvados.Cluster
72 c.Log(resp.Body.String())
73 err := json.Unmarshal(resp.Body.Bytes(), &cluster)
74 c.Check(err, check.IsNil)
75 c.Check(cluster.ManagementToken, check.Equals, "")
76 c.Check(cluster.SystemRootToken, check.Equals, "")
77 c.Check(cluster.Collections.BlobSigning, check.DeepEquals, true)
78 c.Check(cluster.Collections.BlobSigningTTL, check.Equals, arvados.Duration(23*time.Second))
81 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
82 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
83 resp := httptest.NewRecorder()
84 s.handler.ServeHTTP(resp, req)
85 c.Check(resp.Code, check.Equals, http.StatusOK)
86 var dd arvados.DiscoveryDocument
87 err := json.Unmarshal(resp.Body.Bytes(), &dd)
88 c.Check(err, check.IsNil)
89 c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
90 c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
91 c.Check(len(dd.Resources), check.Not(check.Equals), 0)
92 c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
95 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
96 s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond)
97 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
98 resp := httptest.NewRecorder()
99 s.handler.ServeHTTP(resp, req)
100 c.Check(resp.Code, check.Equals, http.StatusBadGateway)
101 var jresp httpserver.ErrorResponse
102 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
103 c.Check(err, check.IsNil)
104 c.Assert(len(jresp.Errors), check.Equals, 1)
105 c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
108 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
109 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
110 resp := httptest.NewRecorder()
111 s.handler.ServeHTTP(resp, req)
112 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
113 jresp := map[string]interface{}{}
114 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
115 c.Check(err, check.IsNil)
116 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
119 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
120 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
121 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
122 resp := httptest.NewRecorder()
123 s.handler.ServeHTTP(resp, req)
124 c.Check(resp.Code, check.Equals, http.StatusOK)
126 err := json.Unmarshal(resp.Body.Bytes(), &u)
127 c.Check(err, check.IsNil)
128 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
131 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
132 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
134 "api_token": {arvadostest.ActiveToken},
136 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
137 resp := httptest.NewRecorder()
138 s.handler.ServeHTTP(resp, req)
139 c.Check(resp.Code, check.Equals, http.StatusOK)
141 err := json.Unmarshal(resp.Body.Bytes(), &u)
142 c.Check(err, check.IsNil)
143 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
146 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
147 req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
148 resp := httptest.NewRecorder()
149 s.handler.ServeHTTP(resp, req)
150 c.Check(resp.Code, check.Equals, http.StatusNotFound)
151 jresp := map[string]interface{}{}
152 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
153 c.Check(err, check.IsNil)
154 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
157 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
158 req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
159 resp := httptest.NewRecorder()
160 s.handler.ServeHTTP(resp, req)
161 c.Check(resp.Code, check.Equals, http.StatusFound)
162 c.Check(resp.Header().Get("Location"), check.Matches, `https://0.0.0.0:1/auth/joshid\?return_to=%2Cfoo&?`)
165 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
166 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
167 user, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
168 c.Assert(err, check.IsNil)
169 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
170 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
171 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
172 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
175 func (s *HandlerSuite) TestValidateV2APIToken(c *check.C) {
176 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
177 user, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
178 c.Assert(err, check.IsNil)
179 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
180 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
181 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
182 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
183 c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
186 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
187 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
188 auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
189 c.Assert(err, check.IsNil)
190 c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
192 user, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
193 c.Assert(err, check.IsNil)
194 c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
195 c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
196 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
197 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
198 c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())