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