1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "git.arvados.org/arvados.git/sdk/go/arvadostest"
22 "git.arvados.org/arvados.git/sdk/go/ctxlog"
23 "git.arvados.org/arvados.git/sdk/go/httpserver"
24 "github.com/prometheus/client_golang/prometheus"
25 check "gopkg.in/check.v1"
28 var forceLegacyAPI14 bool
30 // Gocheck boilerplate
31 func Test(t *testing.T) {
32 for _, forceLegacyAPI14 = range []bool{false, true} {
37 var _ = check.Suite(&HandlerSuite{})
39 type HandlerSuite struct {
40 cluster *arvados.Cluster
43 cancel context.CancelFunc
46 func (s *HandlerSuite) SetUpTest(c *check.C) {
47 s.ctx, s.cancel = context.WithCancel(context.Background())
48 s.ctx = ctxlog.Context(s.ctx, ctxlog.New(os.Stderr, "json", "debug"))
49 s.cluster = &arvados.Cluster{
51 PostgreSQL: integrationTestCluster().PostgreSQL,
52 ForceLegacyAPI14: forceLegacyAPI14,
54 s.cluster.TLS.Insecure = true
55 arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
56 arvadostest.SetServiceURL(&s.cluster.Services.Controller, "http://localhost:/")
57 s.handler = newHandler(s.ctx, s.cluster, "", prometheus.NewRegistry())
60 func (s *HandlerSuite) TearDownTest(c *check.C) {
64 func (s *HandlerSuite) TestConfigExport(c *check.C) {
65 s.cluster.ManagementToken = "secret"
66 s.cluster.SystemRootToken = "secret"
67 s.cluster.Collections.BlobSigning = true
68 s.cluster.Collections.BlobSigningTTL = arvados.Duration(23 * time.Second)
69 for _, method := range []string{"GET", "OPTIONS"} {
70 req := httptest.NewRequest(method, "/arvados/v1/config", nil)
71 resp := httptest.NewRecorder()
72 s.handler.ServeHTTP(resp, req)
73 c.Check(resp.Code, check.Equals, http.StatusOK)
74 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, `*`)
75 c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Matches, `.*\bGET\b.*`)
76 c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Matches, `.+`)
77 if method == "OPTIONS" {
78 c.Check(resp.Body.String(), check.HasLen, 0)
81 var cluster arvados.Cluster
82 c.Log(resp.Body.String())
83 err := json.Unmarshal(resp.Body.Bytes(), &cluster)
84 c.Check(err, check.IsNil)
85 c.Check(cluster.ManagementToken, check.Equals, "")
86 c.Check(cluster.SystemRootToken, check.Equals, "")
87 c.Check(cluster.Collections.BlobSigning, check.DeepEquals, true)
88 c.Check(cluster.Collections.BlobSigningTTL, check.Equals, arvados.Duration(23*time.Second))
92 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
93 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
94 resp := httptest.NewRecorder()
95 s.handler.ServeHTTP(resp, req)
96 c.Check(resp.Code, check.Equals, http.StatusOK)
97 var dd arvados.DiscoveryDocument
98 err := json.Unmarshal(resp.Body.Bytes(), &dd)
99 c.Check(err, check.IsNil)
100 c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
101 c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
102 c.Check(len(dd.Resources), check.Not(check.Equals), 0)
103 c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
106 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
107 s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond)
108 req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
109 resp := httptest.NewRecorder()
110 s.handler.ServeHTTP(resp, req)
111 c.Check(resp.Code, check.Equals, http.StatusBadGateway)
112 var jresp httpserver.ErrorResponse
113 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
114 c.Check(err, check.IsNil)
115 c.Assert(len(jresp.Errors), check.Equals, 1)
116 c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
119 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
120 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
121 resp := httptest.NewRecorder()
122 s.handler.ServeHTTP(resp, req)
123 c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
124 jresp := map[string]interface{}{}
125 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
126 c.Check(err, check.IsNil)
127 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
130 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
131 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
132 req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
133 resp := httptest.NewRecorder()
134 s.handler.ServeHTTP(resp, req)
135 c.Check(resp.Code, check.Equals, http.StatusOK)
137 err := json.Unmarshal(resp.Body.Bytes(), &u)
138 c.Check(err, check.IsNil)
139 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
142 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
143 req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
145 "api_token": {arvadostest.ActiveToken},
147 req.Header.Set("Content-type", "application/x-www-form-urlencoded")
148 resp := httptest.NewRecorder()
149 s.handler.ServeHTTP(resp, req)
150 c.Check(resp.Code, check.Equals, http.StatusOK)
152 err := json.Unmarshal(resp.Body.Bytes(), &u)
153 c.Check(err, check.IsNil)
154 c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
157 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
158 req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
159 resp := httptest.NewRecorder()
160 s.handler.ServeHTTP(resp, req)
161 c.Check(resp.Code, check.Equals, http.StatusNotFound)
162 jresp := map[string]interface{}{}
163 err := json.Unmarshal(resp.Body.Bytes(), &jresp)
164 c.Check(err, check.IsNil)
165 c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
168 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
169 s.cluster.Login.ProviderAppID = "test"
170 s.cluster.Login.ProviderAppSecret = "test"
171 req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
172 resp := httptest.NewRecorder()
173 s.handler.ServeHTTP(resp, req)
174 if !c.Check(resp.Code, check.Equals, http.StatusFound) {
175 c.Log(resp.Body.String())
177 // Old "proxy entire request" code path returns an absolute
178 // URL. New lib/controller/federation code path returns a
180 c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
183 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
184 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
185 user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
186 c.Assert(err, check.IsNil)
187 c.Check(ok, check.Equals, true)
188 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
189 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
190 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
191 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
194 func (s *HandlerSuite) TestValidateV2APIToken(c *check.C) {
195 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
196 user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
197 c.Assert(err, check.IsNil)
198 c.Check(ok, check.Equals, true)
199 c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
200 c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
201 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
202 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
203 c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
206 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
207 req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
208 auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
209 c.Assert(err, check.IsNil)
210 c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
212 user, ok, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
213 c.Assert(err, check.IsNil)
214 c.Check(ok, check.Equals, true)
215 c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
216 c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
217 c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
218 c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
219 c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())
222 func (s *HandlerSuite) CheckObjectType(c *check.C, url string, token string, skippedFields map[string]bool) {
223 var proxied, direct map[string]interface{}
226 // Get collection from controller
227 req := httptest.NewRequest("GET", url, nil)
228 req.Header.Set("Authorization", "Bearer "+token)
229 resp := httptest.NewRecorder()
230 s.handler.ServeHTTP(resp, req)
231 c.Check(resp.Code, check.Equals, http.StatusOK)
232 err = json.Unmarshal(resp.Body.Bytes(), &proxied)
233 c.Check(err, check.Equals, nil)
235 // Get collection directly from railsAPI
236 client := &http.Client{
237 Transport: &http.Transport{
238 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
241 resp2, err := client.Get(s.cluster.Services.RailsAPI.ExternalURL.String() + url + "/?api_token=" + token)
242 c.Check(err, check.Equals, nil)
243 defer resp2.Body.Close()
244 db, err := ioutil.ReadAll(resp2.Body)
245 c.Check(err, check.Equals, nil)
246 err = json.Unmarshal(db, &direct)
247 c.Check(err, check.Equals, nil)
249 // Check that all RailsAPI provided keys exist on the controller response.
250 for k := range direct {
251 if _, ok := skippedFields[k]; ok {
253 } else if val, ok := proxied[k]; ok {
254 if direct["kind"] == "arvados#collection" && k == "manifest_text" {
255 // Tokens differ from request to request
256 c.Check(strings.Split(val.(string), "+A")[0], check.Equals, strings.Split(direct[k].(string), "+A")[0])
258 c.Check(val, check.DeepEquals, direct[k],
259 check.Commentf("RailsAPI %s key %q's value %q differs from controller's %q.", direct["kind"], k, direct[k], val))
262 c.Errorf("%s's key %q missing on controller's response.", direct["kind"], k)
267 func (s *HandlerSuite) TestGetObjects(c *check.C) {
268 testCases := map[string]map[string]bool{
269 "api_clients/" + arvadostest.TrustedWorkbenchAPIClientUUID: map[string]bool{},
270 "api_client_authorizations/" + arvadostest.AdminTokenUUID: map[string]bool{},
271 "authorized_keys/" + arvadostest.AdminAuthorizedKeysUUID: map[string]bool{},
272 "collections/" + arvadostest.FooCollection: map[string]bool{"href": true},
273 "containers/" + arvadostest.RunningContainerUUID: map[string]bool{},
274 "container_requests/" + arvadostest.QueuedContainerRequestUUID: map[string]bool{},
275 "groups/" + arvadostest.AProjectUUID: map[string]bool{},
276 "keep_services/" + arvadostest.KeepServiceZeroUUID: map[string]bool{},
277 "links/" + arvadostest.ActiveUserCanReadAllUsersLinkUUID: map[string]bool{},
278 "logs/" + arvadostest.CrunchstatForRunningJobLogUUID: map[string]bool{},
279 "nodes/" + arvadostest.IdleNodeUUID: map[string]bool{},
280 "repositories/" + arvadostest.ArvadosRepoUUID: map[string]bool{},
281 "users/" + arvadostest.ActiveUserUUID: map[string]bool{"href": true},
282 "virtual_machines/" + arvadostest.TestVMUUID: map[string]bool{},
283 "workflows/" + arvadostest.WorkflowWithDefinitionYAMLUUID: map[string]bool{},
285 for url, skippedFields := range testCases {
286 s.CheckObjectType(c, "/arvados/v1/"+url, arvadostest.AdminToken, skippedFields)