17119: one more change after review feedback.
[arvados.git] / lib / controller / handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package controller
6
7 import (
8         "context"
9         "crypto/tls"
10         "encoding/json"
11         "io/ioutil"
12         "net/http"
13         "net/http/httptest"
14         "net/url"
15         "os"
16         "strings"
17         "testing"
18         "time"
19
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/auth"
23         "git.arvados.org/arvados.git/sdk/go/ctxlog"
24         "git.arvados.org/arvados.git/sdk/go/httpserver"
25         "github.com/prometheus/client_golang/prometheus"
26         check "gopkg.in/check.v1"
27 )
28
29 var forceLegacyAPI14 bool
30
31 // Gocheck boilerplate
32 func Test(t *testing.T) {
33         for _, forceLegacyAPI14 = range []bool{false, true} {
34                 check.TestingT(t)
35         }
36 }
37
38 var _ = check.Suite(&HandlerSuite{})
39
40 type HandlerSuite struct {
41         cluster *arvados.Cluster
42         handler http.Handler
43         ctx     context.Context
44         cancel  context.CancelFunc
45 }
46
47 func (s *HandlerSuite) SetUpTest(c *check.C) {
48         s.ctx, s.cancel = context.WithCancel(context.Background())
49         s.ctx = ctxlog.Context(s.ctx, ctxlog.New(os.Stderr, "json", "debug"))
50         s.cluster = &arvados.Cluster{
51                 ClusterID:        "zzzzz",
52                 PostgreSQL:       integrationTestCluster().PostgreSQL,
53                 ForceLegacyAPI14: forceLegacyAPI14,
54         }
55         s.cluster.API.RequestTimeout = arvados.Duration(5 * time.Minute)
56         s.cluster.TLS.Insecure = true
57         arvadostest.SetServiceURL(&s.cluster.Services.RailsAPI, "https://"+os.Getenv("ARVADOS_TEST_API_HOST"))
58         arvadostest.SetServiceURL(&s.cluster.Services.Controller, "http://localhost:/")
59         s.handler = newHandler(s.ctx, s.cluster, "", prometheus.NewRegistry())
60 }
61
62 func (s *HandlerSuite) TearDownTest(c *check.C) {
63         s.cancel()
64 }
65
66 func (s *HandlerSuite) TestConfigExport(c *check.C) {
67         s.cluster.ManagementToken = "secret"
68         s.cluster.SystemRootToken = "secret"
69         s.cluster.Collections.BlobSigning = true
70         s.cluster.Collections.BlobSigningTTL = arvados.Duration(23 * time.Second)
71         for _, method := range []string{"GET", "OPTIONS"} {
72                 req := httptest.NewRequest(method, "/arvados/v1/config", nil)
73                 resp := httptest.NewRecorder()
74                 s.handler.ServeHTTP(resp, req)
75                 c.Log(resp.Body.String())
76                 if !c.Check(resp.Code, check.Equals, http.StatusOK) {
77                         continue
78                 }
79                 c.Check(resp.Header().Get("Access-Control-Allow-Origin"), check.Equals, `*`)
80                 c.Check(resp.Header().Get("Access-Control-Allow-Methods"), check.Matches, `.*\bGET\b.*`)
81                 c.Check(resp.Header().Get("Access-Control-Allow-Headers"), check.Matches, `.+`)
82                 if method == "OPTIONS" {
83                         c.Check(resp.Body.String(), check.HasLen, 0)
84                         continue
85                 }
86                 var cluster arvados.Cluster
87                 err := json.Unmarshal(resp.Body.Bytes(), &cluster)
88                 c.Check(err, check.IsNil)
89                 c.Check(cluster.ManagementToken, check.Equals, "")
90                 c.Check(cluster.SystemRootToken, check.Equals, "")
91                 c.Check(cluster.Collections.BlobSigning, check.Equals, true)
92                 c.Check(cluster.Collections.BlobSigningTTL, check.Equals, arvados.Duration(23*time.Second))
93         }
94 }
95
96 func (s *HandlerSuite) TestProxyDiscoveryDoc(c *check.C) {
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.StatusOK)
101         var dd arvados.DiscoveryDocument
102         err := json.Unmarshal(resp.Body.Bytes(), &dd)
103         c.Check(err, check.IsNil)
104         c.Check(dd.BlobSignatureTTL, check.Not(check.Equals), int64(0))
105         c.Check(dd.BlobSignatureTTL > 0, check.Equals, true)
106         c.Check(len(dd.Resources), check.Not(check.Equals), 0)
107         c.Check(len(dd.Schemas), check.Not(check.Equals), 0)
108 }
109
110 func (s *HandlerSuite) TestRequestTimeout(c *check.C) {
111         s.cluster.API.RequestTimeout = arvados.Duration(time.Nanosecond)
112         req := httptest.NewRequest("GET", "/discovery/v1/apis/arvados/v1/rest", nil)
113         resp := httptest.NewRecorder()
114         s.handler.ServeHTTP(resp, req)
115         c.Check(resp.Code, check.Equals, http.StatusBadGateway)
116         var jresp httpserver.ErrorResponse
117         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
118         c.Check(err, check.IsNil)
119         c.Assert(len(jresp.Errors), check.Equals, 1)
120         c.Check(jresp.Errors[0], check.Matches, `.*context deadline exceeded.*`)
121 }
122
123 func (s *HandlerSuite) TestProxyWithoutToken(c *check.C) {
124         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
125         resp := httptest.NewRecorder()
126         s.handler.ServeHTTP(resp, req)
127         c.Check(resp.Code, check.Equals, http.StatusUnauthorized)
128         jresp := map[string]interface{}{}
129         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
130         c.Check(err, check.IsNil)
131         c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
132 }
133
134 func (s *HandlerSuite) TestProxyWithToken(c *check.C) {
135         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
136         req.Header.Set("Authorization", "Bearer "+arvadostest.ActiveToken)
137         resp := httptest.NewRecorder()
138         s.handler.ServeHTTP(resp, req)
139         c.Check(resp.Code, check.Equals, http.StatusOK)
140         var u arvados.User
141         err := json.Unmarshal(resp.Body.Bytes(), &u)
142         c.Check(err, check.IsNil)
143         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
144 }
145
146 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
147         req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
148                 "_method":   {"GET"},
149                 "api_token": {arvadostest.ActiveToken},
150         }.Encode()))
151         req.Header.Set("Content-type", "application/x-www-form-urlencoded")
152         resp := httptest.NewRecorder()
153         s.handler.ServeHTTP(resp, req)
154         c.Check(resp.Code, check.Equals, http.StatusOK)
155         var u arvados.User
156         err := json.Unmarshal(resp.Body.Bytes(), &u)
157         c.Check(err, check.IsNil)
158         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
159 }
160
161 func (s *HandlerSuite) TestProxyNotFound(c *check.C) {
162         req := httptest.NewRequest("GET", "/arvados/v1/xyzzy", nil)
163         resp := httptest.NewRecorder()
164         s.handler.ServeHTTP(resp, req)
165         c.Check(resp.Code, check.Equals, http.StatusNotFound)
166         jresp := map[string]interface{}{}
167         err := json.Unmarshal(resp.Body.Bytes(), &jresp)
168         c.Check(err, check.IsNil)
169         c.Check(jresp["errors"], check.FitsTypeOf, []interface{}{})
170 }
171
172 func (s *HandlerSuite) TestProxyRedirect(c *check.C) {
173         s.cluster.Login.SSO.Enable = true
174         s.cluster.Login.SSO.ProviderAppID = "test"
175         s.cluster.Login.SSO.ProviderAppSecret = "test"
176         req := httptest.NewRequest("GET", "https://0.0.0.0:1/login?return_to=foo", nil)
177         resp := httptest.NewRecorder()
178         s.handler.ServeHTTP(resp, req)
179         if !c.Check(resp.Code, check.Equals, http.StatusFound) {
180                 c.Log(resp.Body.String())
181         }
182         // Old "proxy entire request" code path returns an absolute
183         // URL. New lib/controller/federation code path returns a
184         // relative URL.
185         c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
186 }
187
188 func (s *HandlerSuite) TestLogoutSSO(c *check.C) {
189         s.cluster.Login.SSO.Enable = true
190         s.cluster.Login.SSO.ProviderAppID = "test"
191         req := httptest.NewRequest("GET", "https://0.0.0.0:1/logout?return_to=https://example.com/foo", nil)
192         resp := httptest.NewRecorder()
193         s.handler.ServeHTTP(resp, req)
194         if !c.Check(resp.Code, check.Equals, http.StatusFound) {
195                 c.Log(resp.Body.String())
196         }
197         c.Check(resp.Header().Get("Location"), check.Equals, "http://localhost:3002/users/sign_out?"+url.Values{"redirect_uri": {"https://example.com/foo"}}.Encode())
198 }
199
200 func (s *HandlerSuite) TestLogoutGoogle(c *check.C) {
201         if s.cluster.ForceLegacyAPI14 {
202                 // Google login N/A
203                 return
204         }
205         s.cluster.Login.Google.Enable = true
206         s.cluster.Login.Google.ClientID = "test"
207         req := httptest.NewRequest("GET", "https://0.0.0.0:1/logout?return_to=https://example.com/foo", nil)
208         resp := httptest.NewRecorder()
209         s.handler.ServeHTTP(resp, req)
210         if !c.Check(resp.Code, check.Equals, http.StatusFound) {
211                 c.Log(resp.Body.String())
212         }
213         c.Check(resp.Header().Get("Location"), check.Equals, "https://example.com/foo")
214 }
215
216 func (s *HandlerSuite) TestValidateV1APIToken(c *check.C) {
217         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
218         user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveToken)
219         c.Assert(err, check.IsNil)
220         c.Check(ok, check.Equals, true)
221         c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
222         c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
223         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
224         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
225 }
226
227 func (s *HandlerSuite) TestValidateV2APIToken(c *check.C) {
228         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
229         user, ok, err := s.handler.(*Handler).validateAPItoken(req, arvadostest.ActiveTokenV2)
230         c.Assert(err, check.IsNil)
231         c.Check(ok, check.Equals, true)
232         c.Check(user.Authorization.UUID, check.Equals, arvadostest.ActiveTokenUUID)
233         c.Check(user.Authorization.APIToken, check.Equals, arvadostest.ActiveToken)
234         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
235         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
236         c.Check(user.Authorization.TokenV2(), check.Equals, arvadostest.ActiveTokenV2)
237 }
238
239 func (s *HandlerSuite) TestValidateRemoteToken(c *check.C) {
240         saltedToken, err := auth.SaltToken(arvadostest.ActiveTokenV2, "abcde")
241         c.Assert(err, check.IsNil)
242         for _, trial := range []struct {
243                 code  int
244                 token string
245         }{
246                 {http.StatusOK, saltedToken},
247                 {http.StatusUnauthorized, "bogus"},
248         } {
249                 req := httptest.NewRequest("GET", "https://0.0.0.0:1/arvados/v1/users/current?remote=abcde", nil)
250                 req.Header.Set("Authorization", "Bearer "+trial.token)
251                 resp := httptest.NewRecorder()
252                 s.handler.ServeHTTP(resp, req)
253                 if !c.Check(resp.Code, check.Equals, trial.code) {
254                         c.Logf("HTTP %d: %s", resp.Code, resp.Body.String())
255                 }
256         }
257 }
258
259 func (s *HandlerSuite) TestCreateAPIToken(c *check.C) {
260         req := httptest.NewRequest("GET", "/arvados/v1/users/current", nil)
261         auth, err := s.handler.(*Handler).createAPItoken(req, arvadostest.ActiveUserUUID, nil)
262         c.Assert(err, check.IsNil)
263         c.Check(auth.Scopes, check.DeepEquals, []string{"all"})
264
265         user, ok, err := s.handler.(*Handler).validateAPItoken(req, auth.TokenV2())
266         c.Assert(err, check.IsNil)
267         c.Check(ok, check.Equals, true)
268         c.Check(user.Authorization.UUID, check.Equals, auth.UUID)
269         c.Check(user.Authorization.APIToken, check.Equals, auth.APIToken)
270         c.Check(user.Authorization.Scopes, check.DeepEquals, []string{"all"})
271         c.Check(user.UUID, check.Equals, arvadostest.ActiveUserUUID)
272         c.Check(user.Authorization.TokenV2(), check.Equals, auth.TokenV2())
273 }
274
275 func (s *HandlerSuite) CheckObjectType(c *check.C, url string, token string, skippedFields map[string]bool) {
276         var proxied, direct map[string]interface{}
277         var err error
278
279         // Get collection from controller
280         req := httptest.NewRequest("GET", url, nil)
281         req.Header.Set("Authorization", "Bearer "+token)
282         resp := httptest.NewRecorder()
283         s.handler.ServeHTTP(resp, req)
284         c.Assert(resp.Code, check.Equals, http.StatusOK,
285                 check.Commentf("Wasn't able to get data from the controller at %q", url))
286         err = json.Unmarshal(resp.Body.Bytes(), &proxied)
287         c.Check(err, check.Equals, nil)
288
289         // Get collection directly from RailsAPI
290         client := &http.Client{
291                 Transport: &http.Transport{
292                         TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
293                 },
294         }
295         resp2, err := client.Get(s.cluster.Services.RailsAPI.ExternalURL.String() + url + "/?api_token=" + token)
296         c.Check(err, check.Equals, nil)
297         c.Assert(resp2.StatusCode, check.Equals, http.StatusOK,
298                 check.Commentf("Wasn't able to get data from the RailsAPI at %q", url))
299         defer resp2.Body.Close()
300         db, err := ioutil.ReadAll(resp2.Body)
301         c.Check(err, check.Equals, nil)
302         err = json.Unmarshal(db, &direct)
303         c.Check(err, check.Equals, nil)
304
305         // Check that all RailsAPI provided keys exist on the controller response.
306         for k := range direct {
307                 if _, ok := skippedFields[k]; ok {
308                         continue
309                 } else if val, ok := proxied[k]; ok {
310                         if direct["kind"] == "arvados#collection" && k == "manifest_text" {
311                                 // Tokens differ from request to request
312                                 c.Check(strings.Split(val.(string), "+A")[0], check.Equals, strings.Split(direct[k].(string), "+A")[0])
313                         } else {
314                                 c.Check(val, check.DeepEquals, direct[k],
315                                         check.Commentf("RailsAPI %s key %q's value %q differs from controller's %q.", direct["kind"], k, direct[k], val))
316                         }
317                 } else {
318                         c.Errorf("%s's key %q missing on controller's response.", direct["kind"], k)
319                 }
320         }
321 }
322
323 func (s *HandlerSuite) TestGetObjects(c *check.C) {
324         // Get the 1st keep service's uuid from the running test server.
325         req := httptest.NewRequest("GET", "/arvados/v1/keep_services/", nil)
326         req.Header.Set("Authorization", "Bearer "+arvadostest.AdminToken)
327         resp := httptest.NewRecorder()
328         s.handler.ServeHTTP(resp, req)
329         c.Assert(resp.Code, check.Equals, http.StatusOK)
330         var ksList arvados.KeepServiceList
331         json.Unmarshal(resp.Body.Bytes(), &ksList)
332         c.Assert(len(ksList.Items), check.Not(check.Equals), 0)
333         ksUUID := ksList.Items[0].UUID
334
335         testCases := map[string]map[string]bool{
336                 "api_clients/" + arvadostest.TrustedWorkbenchAPIClientUUID:     nil,
337                 "api_client_authorizations/" + arvadostest.AdminTokenUUID:      nil,
338                 "authorized_keys/" + arvadostest.AdminAuthorizedKeysUUID:       nil,
339                 "collections/" + arvadostest.CollectionWithUniqueWordsUUID:     {"href": true},
340                 "containers/" + arvadostest.RunningContainerUUID:               nil,
341                 "container_requests/" + arvadostest.QueuedContainerRequestUUID: nil,
342                 "groups/" + arvadostest.AProjectUUID:                           nil,
343                 "keep_services/" + ksUUID:                                      nil,
344                 "links/" + arvadostest.ActiveUserCanReadAllUsersLinkUUID:       nil,
345                 "logs/" + arvadostest.CrunchstatForRunningJobLogUUID:           nil,
346                 "nodes/" + arvadostest.IdleNodeUUID:                            nil,
347                 "repositories/" + arvadostest.ArvadosRepoUUID:                  nil,
348                 "users/" + arvadostest.ActiveUserUUID:                          {"href": true},
349                 "virtual_machines/" + arvadostest.TestVMUUID:                   nil,
350                 "workflows/" + arvadostest.WorkflowWithDefinitionYAMLUUID:      nil,
351         }
352         for url, skippedFields := range testCases {
353                 s.CheckObjectType(c, "/arvados/v1/"+url, arvadostest.AdminToken, skippedFields)
354         }
355 }