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 "github.com/prometheus/client_golang/prometheus"
23 check "gopkg.in/check.v1"
26 var enableBetaController14287 bool
28 // Gocheck boilerplate
29 func Test(t *testing.T) {
30 for _, enableBetaController14287 = 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,
51 EnableBetaController14287: enableBetaController14287,
53 s.cluster.TLS.Insecure = true
54 arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
55 arvadostest.SetServiceURL(&s.cluster.Services.Controller, "http://localhost:/")
56 s.handler = newHandler(s.ctx, s.cluster, "", prometheus.NewRegistry())
59 func (s *HandlerSuite) TearDownTest(c *check.C) {
63 func (s *HandlerSuite) TestConfigExport(c *check.C) {
64 s.cluster.ManagementToken = "secret"
65 s.cluster.SystemRootToken = "secret"
66 s.cluster.Collections.BlobSigning = true
67 s.cluster.Collections.BlobSigningTTL = arvados.Duration(23 * time.Second)
68 for _, method := range []string{"GET", "OPTIONS"} {
69 req := httptest.NewRequest(method, "/arvados/v1/config", nil)
70 resp := httptest.NewRecorder()
71 s.handler.ServeHTTP(resp, req)
72 c.Check(resp.Code, check.Equals, http.StatusOK)
73 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, `*`)
74 c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Matches, `.*\bGET\b.*`)
75 c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Matches, `.+`)
76 if method == "OPTIONS" {
77 c.Check(resp.Body.String(), check.HasLen, 0)
80 var cluster arvados.Cluster
81 c.Log(resp.Body.String())
82 err := json.Unmarshal(resp.Body.Bytes(), &cluster)
83 c.Check(err, check.IsNil)
84 c.Check(cluster.ManagementToken, check.Equals, "")
85 c.Check(cluster.SystemRootToken, check.Equals, "")
86 c.Check(cluster.Collections.BlobSigning, check.DeepEquals, true)
87 c.Check(cluster.Collections.BlobSigningTTL, check.Equals, arvados.Duration(23*time.Second))
91 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
92 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
93 resp := httptest.NewRecorder()
94 s.handler.ServeHTTP(resp, req)
95 c.Check(resp.Code, check.Equals, http.StatusOK)
96 var dd arvados.DiscoveryDocument
97 err := json.Unmarshal(resp.Body.Bytes(), &dd)
98 c.Check(err, check.IsNil)
99 c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
100 c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
101 c.Check(len(dd.Resources), check.Not(check.Equals), 0)
102 c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
105 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
106 s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond)
107 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
108 resp := httptest.NewRecorder()
109 s.handler.ServeHTTP(resp, req)
110 c.Check(resp.Code, check.Equals, http.StatusBadGateway)
111 var jresp httpserver.ErrorResponse
112 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
113 c.Check(err, check.IsNil)
114 c.Assert(len(jresp.Errors), check.Equals, 1)
115 c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
118 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
119 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
120 resp := httptest.NewRecorder()
121 s.handler.ServeHTTP(resp, req)
122 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
123 jresp := map[string]interface{}{}
124 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
125 c.Check(err, check.IsNil)
126 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
129 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
130 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
131 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
132 resp := httptest.NewRecorder()
133 s.handler.ServeHTTP(resp, req)
134 c.Check(resp.Code, check.Equals, http.StatusOK)
136 err := json.Unmarshal(resp.Body.Bytes(), &u)
137 c.Check(err, check.IsNil)
138 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
141 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
142 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
144 "api_token": {arvadostest.ActiveToken},
146 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
147 resp := httptest.NewRecorder()
148 s.handler.ServeHTTP(resp, req)
149 c.Check(resp.Code, check.Equals, http.StatusOK)
151 err := json.Unmarshal(resp.Body.Bytes(), &u)
152 c.Check(err, check.IsNil)
153 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
156 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
157 req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
158 resp := httptest.NewRecorder()
159 s.handler.ServeHTTP(resp, req)
160 c.Check(resp.Code, check.Equals, http.StatusNotFound)
161 jresp := map[string]interface{}{}
162 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
163 c.Check(err, check.IsNil)
164 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
167 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
168 s.cluster.Login.ProviderAppID = "test"
169 s.cluster.Login.ProviderAppSecret = "test"
170 req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
171 resp := httptest.NewRecorder()
172 s.handler.ServeHTTP(resp, req)
173 if !c.Check(resp.Code, check.Equals, http.StatusFound) {
174 c.Log(resp.Body.String())
176 // Old "proxy entire request" code path returns an absolute
177 // URL. New lib/controller/federation code path returns a
179 c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
182 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
183 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
184 user, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
185 c.Assert(err, check.IsNil)
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, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
195 c.Assert(err, check.IsNil)
196 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
197 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
198 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
199 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
200 c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
203 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
204 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
205 auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
206 c.Assert(err, check.IsNil)
207 c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
209 user, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
210 c.Assert(err, check.IsNil)
211 c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
212 c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
213 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
214 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
215 c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())