16141: Fixes test to use the 1st keep_service's uuid the test server returns.
[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/ctxlog"
23         "git.arvados.org/arvados.git/sdk/go/httpserver"
24         "github.com/prometheus/client_golang/prometheus"
25         check "gopkg.in/check.v1"
26 )
27
28 var forceLegacyAPI14 bool
29
30 // Gocheck boilerplate
31 func Test(t *testing.T) {
32         for _, forceLegacyAPI14 = range []bool{false, true} {
33                 check.TestingT(t)
34         }
35 }
36
37 var _ = check.Suite(&HandlerSuite{})
38
39 type HandlerSuite struct {
40         cluster *arvados.Cluster
41         handler http.Handler
42         ctx     context.Context
43         cancel  context.CancelFunc
44 }
45
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{
50                 ClusterID:        "zzzzz",
51                 PostgreSQL:       integrationTestCluster().PostgreSQL,
52                 ForceLegacyAPI14: forceLegacyAPI14,
53         }
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())
58 }
59
60 func (s *HandlerSuite) TearDownTest(c *check.C) {
61         s.cancel()
62 }
63
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)
79                         continue
80                 }
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))
89         }
90 }
91
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)
104 }
105
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.*`)
117 }
118
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{}{})
128 }
129
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)
136         var u arvados.User
137         err := json.Unmarshal(resp.Body.Bytes(), &u)
138         c.Check(err, check.IsNil)
139         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
140 }
141
142 func (s *HandlerSuite) TestProxyWithTokenInRequestBody(c *check.C) {
143         req := httptest.NewRequest("POST", "/arvados/v1/users/current", strings.NewReader(url.Values{
144                 "_method":   {"GET"},
145                 "api_token": {arvadostest.ActiveToken},
146         }.Encode()))
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)
151         var u arvados.User
152         err := json.Unmarshal(resp.Body.Bytes(), &u)
153         c.Check(err, check.IsNil)
154         c.Check(u.UUID, check.Equals, arvadostest.ActiveUserUUID)
155 }
156
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{}{})
166 }
167
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())
176         }
177         // Old "proxy entire request" code path returns an absolute
178         // URL. New lib/controller/federation code path returns a
179         // relative URL.
180         c.Check(resp.Header().Get("Location"), check.Matches, `(https://0.0.0.0:1)?/auth/joshid\?return_to=%2Cfoo&?`)
181 }
182
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)
192 }
193
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)
204 }
205
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"})
211
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())
220 }
221
222 func (s *HandlerSuite) CheckObjectType(c *check.C, url string, token string, skippedFields map[string]bool) {
223         var proxied, direct map[string]interface{}
224         var err error
225
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.Assert(resp.Code, check.Equals, http.StatusOK,
232                 check.Commentf("Wasn't able to get data from the controller at %q", url))
233         err = json.Unmarshal(resp.Body.Bytes(), &proxied)
234         c.Check(err, check.Equals, nil)
235
236         // Get collection directly from RailsAPI
237         client := &http.Client{
238                 Transport: &http.Transport{
239                         TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
240                 },
241         }
242         resp2, err := client.Get(s.cluster.Services.RailsAPI.ExternalURL.String() + url + "/?api_token=" + token)
243         c.Check(err, check.Equals, nil)
244         defer resp2.Body.Close()
245         db, err := ioutil.ReadAll(resp2.Body)
246         c.Check(err, check.Equals, nil)
247         err = json.Unmarshal(db, &direct)
248         c.Check(err, check.Equals, nil)
249
250         // Check that all RailsAPI provided keys exist on the controller response.
251         for k := range direct {
252                 if _, ok := skippedFields[k]; ok {
253                         continue
254                 } else if val, ok := proxied[k]; ok {
255                         if direct["kind"] == "arvados#collection" && k == "manifest_text" {
256                                 // Tokens differ from request to request
257                                 c.Check(strings.Split(val.(string), "+A")[0], check.Equals, strings.Split(direct[k].(string), "+A")[0])
258                         } else {
259                                 c.Check(val, check.DeepEquals, direct[k],
260                                         check.Commentf("RailsAPI %s key %q's value %q differs from controller's %q.", direct["kind"], k, direct[k], val))
261                         }
262                 } else {
263                         c.Errorf("%s's key %q missing on controller's response.", direct["kind"], k)
264                 }
265         }
266 }
267
268 func (s *HandlerSuite) TestGetObjects(c *check.C) {
269         // Get the 1st keep service's uuid from the running test server.
270         req := httptest.NewRequest("GET", "/arvados/v1/keep_services/", nil)
271         req.Header.Set("Authorization", "Bearer "+arvadostest.AdminToken)
272         resp := httptest.NewRecorder()
273         s.handler.ServeHTTP(resp, req)
274         c.Assert(resp.Code, check.Equals, http.StatusOK)
275         var ksList arvados.KeepServiceList
276         json.Unmarshal(resp.Body.Bytes(), &ksList)
277         c.Assert(len(ksList.Items), check.Not(check.Equals), 0)
278         ksUUID := ksList.Items[0].UUID
279
280         testCases := map[string]map[string]bool{
281                 "api_clients/" + arvadostest.TrustedWorkbenchAPIClientUUID:     map[string]bool{},
282                 "api_client_authorizations/" + arvadostest.AdminTokenUUID:      map[string]bool{},
283                 "authorized_keys/" + arvadostest.AdminAuthorizedKeysUUID:       map[string]bool{},
284                 "collections/" + arvadostest.FooCollection:                     map[string]bool{"href": true},
285                 "containers/" + arvadostest.RunningContainerUUID:               map[string]bool{},
286                 "container_requests/" + arvadostest.QueuedContainerRequestUUID: map[string]bool{},
287                 "groups/" + arvadostest.AProjectUUID:                           map[string]bool{},
288                 "keep_services/" + ksUUID:                                      map[string]bool{},
289                 "links/" + arvadostest.ActiveUserCanReadAllUsersLinkUUID:       map[string]bool{},
290                 "logs/" + arvadostest.CrunchstatForRunningJobLogUUID:           map[string]bool{},
291                 "nodes/" + arvadostest.IdleNodeUUID:                            map[string]bool{},
292                 "repositories/" + arvadostest.ArvadosRepoUUID:                  map[string]bool{},
293                 "users/" + arvadostest.ActiveUserUUID:                          map[string]bool{"href": true},
294                 "virtual_machines/" + arvadostest.TestVMUUID:                   map[string]bool{},
295                 "workflows/" + arvadostest.WorkflowWithDefinitionYAMLUUID:      map[string]bool{},
296         }
297         for url, skippedFields := range testCases {
298                 s.CheckObjectType(c, "/arvados/v1/"+url, arvadostest.AdminToken, skippedFields)
299         }
300 }