13647: Use cluster config instead of custom keepstore config.
[arvados.git] / services / keepstore / handler_test.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // Tests for Keep HTTP handlers:
6 //
7 //     GetBlockHandler
8 //     PutBlockHandler
9 //     IndexHandler
10 //
11 // The HTTP handlers are responsible for enforcing permission policy,
12 // so these tests must exercise all possible permission permutations.
13
14 package main
15
16 import (
17         "bytes"
18         "context"
19         "encoding/json"
20         "fmt"
21         "net/http"
22         "net/http/httptest"
23         "os"
24         "regexp"
25         "strings"
26         "time"
27
28         "git.curoverse.com/arvados.git/lib/config"
29         "git.curoverse.com/arvados.git/sdk/go/arvados"
30         "git.curoverse.com/arvados.git/sdk/go/arvadostest"
31         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
32         "github.com/prometheus/client_golang/prometheus"
33         check "gopkg.in/check.v1"
34 )
35
36 var testServiceURL = func() arvados.URL {
37         return arvados.URL{Host: "localhost:12345", Scheme: "http"}
38 }()
39
40 func testCluster(t TB) *arvados.Cluster {
41         cfg, err := config.NewLoader(bytes.NewBufferString("Clusters: {zzzzz: {}}"), ctxlog.TestLogger(t)).Load()
42         if err != nil {
43                 t.Fatal(err)
44         }
45         cluster, err := cfg.GetCluster("")
46         if err != nil {
47                 t.Fatal(err)
48         }
49         cluster.SystemRootToken = arvadostest.DataManagerToken
50         cluster.ManagementToken = arvadostest.ManagementToken
51         cluster.Collections.BlobSigning = false
52         return cluster
53 }
54
55 var _ = check.Suite(&HandlerSuite{})
56
57 type HandlerSuite struct {
58         cluster *arvados.Cluster
59         handler *handler
60 }
61
62 func (s *HandlerSuite) SetUpTest(c *check.C) {
63         s.cluster = testCluster(c)
64         s.cluster.Volumes = map[string]arvados.Volume{
65                 "zzzzz-nyw5e-000000000000000": {Replication: 1, Driver: "mock"},
66                 "zzzzz-nyw5e-111111111111111": {Replication: 1, Driver: "mock"},
67         }
68         s.handler = &handler{}
69 }
70
71 // A RequestTester represents the parameters for an HTTP request to
72 // be issued on behalf of a unit test.
73 type RequestTester struct {
74         uri         string
75         apiToken    string
76         method      string
77         requestBody []byte
78 }
79
80 // Test GetBlockHandler on the following situations:
81 //   - permissions off, unauthenticated request, unsigned locator
82 //   - permissions on, authenticated request, signed locator
83 //   - permissions on, authenticated request, unsigned locator
84 //   - permissions on, unauthenticated request, signed locator
85 //   - permissions on, authenticated request, expired locator
86 //   - permissions on, authenticated request, signed locator, transient error from backend
87 //
88 func (s *HandlerSuite) TestGetHandler(c *check.C) {
89         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
90
91         vols := s.handler.volmgr.AllWritable()
92         err := vols[0].Put(context.Background(), TestHash, TestBlock)
93         c.Check(err, check.IsNil)
94
95         // Create locators for testing.
96         // Turn on permission settings so we can generate signed locators.
97         s.cluster.Collections.BlobSigning = true
98         s.cluster.Collections.BlobSigningKey = knownKey
99         s.cluster.Collections.BlobSigningTTL.Set("5m")
100
101         var (
102                 unsignedLocator  = "/" + TestHash
103                 validTimestamp   = time.Now().Add(s.cluster.Collections.BlobSigningTTL.Duration())
104                 expiredTimestamp = time.Now().Add(-time.Hour)
105                 signedLocator    = "/" + SignLocator(s.cluster, TestHash, knownToken, validTimestamp)
106                 expiredLocator   = "/" + SignLocator(s.cluster, TestHash, knownToken, expiredTimestamp)
107         )
108
109         // -----------------
110         // Test unauthenticated request with permissions off.
111         s.cluster.Collections.BlobSigning = false
112
113         // Unauthenticated request, unsigned locator
114         // => OK
115         response := IssueRequest(s.handler,
116                 &RequestTester{
117                         method: "GET",
118                         uri:    unsignedLocator,
119                 })
120         ExpectStatusCode(c,
121                 "Unauthenticated request, unsigned locator", http.StatusOK, response)
122         ExpectBody(c,
123                 "Unauthenticated request, unsigned locator",
124                 string(TestBlock),
125                 response)
126
127         receivedLen := response.Header().Get("Content-Length")
128         expectedLen := fmt.Sprintf("%d", len(TestBlock))
129         if receivedLen != expectedLen {
130                 c.Errorf("expected Content-Length %s, got %s", expectedLen, receivedLen)
131         }
132
133         // ----------------
134         // Permissions: on.
135         s.cluster.Collections.BlobSigning = true
136
137         // Authenticated request, signed locator
138         // => OK
139         response = IssueRequest(s.handler, &RequestTester{
140                 method:   "GET",
141                 uri:      signedLocator,
142                 apiToken: knownToken,
143         })
144         ExpectStatusCode(c,
145                 "Authenticated request, signed locator", http.StatusOK, response)
146         ExpectBody(c,
147                 "Authenticated request, signed locator", string(TestBlock), response)
148
149         receivedLen = response.Header().Get("Content-Length")
150         expectedLen = fmt.Sprintf("%d", len(TestBlock))
151         if receivedLen != expectedLen {
152                 c.Errorf("expected Content-Length %s, got %s", expectedLen, receivedLen)
153         }
154
155         // Authenticated request, unsigned locator
156         // => PermissionError
157         response = IssueRequest(s.handler, &RequestTester{
158                 method:   "GET",
159                 uri:      unsignedLocator,
160                 apiToken: knownToken,
161         })
162         ExpectStatusCode(c, "unsigned locator", PermissionError.HTTPCode, response)
163
164         // Unauthenticated request, signed locator
165         // => PermissionError
166         response = IssueRequest(s.handler, &RequestTester{
167                 method: "GET",
168                 uri:    signedLocator,
169         })
170         ExpectStatusCode(c,
171                 "Unauthenticated request, signed locator",
172                 PermissionError.HTTPCode, response)
173
174         // Authenticated request, expired locator
175         // => ExpiredError
176         response = IssueRequest(s.handler, &RequestTester{
177                 method:   "GET",
178                 uri:      expiredLocator,
179                 apiToken: knownToken,
180         })
181         ExpectStatusCode(c,
182                 "Authenticated request, expired locator",
183                 ExpiredError.HTTPCode, response)
184
185         // Authenticated request, signed locator
186         // => 503 Server busy (transient error)
187
188         // Set up the block owning volume to respond with errors
189         vols[0].Volume.(*MockVolume).Bad = true
190         vols[0].Volume.(*MockVolume).BadVolumeError = VolumeBusyError
191         response = IssueRequest(s.handler, &RequestTester{
192                 method:   "GET",
193                 uri:      signedLocator,
194                 apiToken: knownToken,
195         })
196         // A transient error from one volume while the other doesn't find the block
197         // should make the service return a 503 so that clients can retry.
198         ExpectStatusCode(c,
199                 "Volume backend busy",
200                 503, response)
201 }
202
203 // Test PutBlockHandler on the following situations:
204 //   - no server key
205 //   - with server key, authenticated request, unsigned locator
206 //   - with server key, unauthenticated request, unsigned locator
207 //
208 func (s *HandlerSuite) TestPutHandler(c *check.C) {
209         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
210
211         // --------------
212         // No server key.
213
214         s.cluster.Collections.BlobSigningKey = ""
215
216         // Unauthenticated request, no server key
217         // => OK (unsigned response)
218         unsignedLocator := "/" + TestHash
219         response := IssueRequest(s.handler,
220                 &RequestTester{
221                         method:      "PUT",
222                         uri:         unsignedLocator,
223                         requestBody: TestBlock,
224                 })
225
226         ExpectStatusCode(c,
227                 "Unauthenticated request, no server key", http.StatusOK, response)
228         ExpectBody(c,
229                 "Unauthenticated request, no server key",
230                 TestHashPutResp, response)
231
232         // ------------------
233         // With a server key.
234
235         s.cluster.Collections.BlobSigningKey = knownKey
236         s.cluster.Collections.BlobSigningTTL.Set("5m")
237
238         // When a permission key is available, the locator returned
239         // from an authenticated PUT request will be signed.
240
241         // Authenticated PUT, signed locator
242         // => OK (signed response)
243         response = IssueRequest(s.handler,
244                 &RequestTester{
245                         method:      "PUT",
246                         uri:         unsignedLocator,
247                         requestBody: TestBlock,
248                         apiToken:    knownToken,
249                 })
250
251         ExpectStatusCode(c,
252                 "Authenticated PUT, signed locator, with server key",
253                 http.StatusOK, response)
254         responseLocator := strings.TrimSpace(response.Body.String())
255         if VerifySignature(s.cluster, responseLocator, knownToken) != nil {
256                 c.Errorf("Authenticated PUT, signed locator, with server key:\n"+
257                         "response '%s' does not contain a valid signature",
258                         responseLocator)
259         }
260
261         // Unauthenticated PUT, unsigned locator
262         // => OK
263         response = IssueRequest(s.handler,
264                 &RequestTester{
265                         method:      "PUT",
266                         uri:         unsignedLocator,
267                         requestBody: TestBlock,
268                 })
269
270         ExpectStatusCode(c,
271                 "Unauthenticated PUT, unsigned locator, with server key",
272                 http.StatusOK, response)
273         ExpectBody(c,
274                 "Unauthenticated PUT, unsigned locator, with server key",
275                 TestHashPutResp, response)
276 }
277
278 func (s *HandlerSuite) TestPutAndDeleteSkipReadonlyVolumes(c *check.C) {
279         s.cluster.Volumes["zzzzz-nyw5e-000000000000000"] = arvados.Volume{Driver: "mock", ReadOnly: true}
280         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
281
282         s.cluster.SystemRootToken = "fake-data-manager-token"
283         IssueRequest(s.handler,
284                 &RequestTester{
285                         method:      "PUT",
286                         uri:         "/" + TestHash,
287                         requestBody: TestBlock,
288                 })
289
290         s.cluster.Collections.BlobTrash = true
291         IssueRequest(s.handler,
292                 &RequestTester{
293                         method:      "DELETE",
294                         uri:         "/" + TestHash,
295                         requestBody: TestBlock,
296                         apiToken:    s.cluster.SystemRootToken,
297                 })
298         type expect struct {
299                 volid     string
300                 method    string
301                 callcount int
302         }
303         for _, e := range []expect{
304                 {"zzzzz-nyw5e-000000000000000", "Get", 0},
305                 {"zzzzz-nyw5e-000000000000000", "Compare", 0},
306                 {"zzzzz-nyw5e-000000000000000", "Touch", 0},
307                 {"zzzzz-nyw5e-000000000000000", "Put", 0},
308                 {"zzzzz-nyw5e-000000000000000", "Delete", 0},
309                 {"zzzzz-nyw5e-111111111111111", "Get", 0},
310                 {"zzzzz-nyw5e-111111111111111", "Compare", 1},
311                 {"zzzzz-nyw5e-111111111111111", "Touch", 1},
312                 {"zzzzz-nyw5e-111111111111111", "Put", 1},
313                 {"zzzzz-nyw5e-111111111111111", "Delete", 1},
314         } {
315                 if calls := s.handler.volmgr.mountMap[e.volid].Volume.(*MockVolume).CallCount(e.method); calls != e.callcount {
316                         c.Errorf("Got %d %s() on vol %s, expect %d", calls, e.method, e.volid, e.callcount)
317                 }
318         }
319 }
320
321 // Test /index requests:
322 //   - unauthenticated /index request
323 //   - unauthenticated /index/prefix request
324 //   - authenticated   /index request        | non-superuser
325 //   - authenticated   /index/prefix request | non-superuser
326 //   - authenticated   /index request        | superuser
327 //   - authenticated   /index/prefix request | superuser
328 //
329 // The only /index requests that should succeed are those issued by the
330 // superuser. They should pass regardless of the value of RequireSignatures.
331 //
332 func (s *HandlerSuite) TestIndexHandler(c *check.C) {
333         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
334
335         // Include multiple blocks on different volumes, and
336         // some metadata files (which should be omitted from index listings)
337         vols := s.handler.volmgr.AllWritable()
338         vols[0].Put(context.Background(), TestHash, TestBlock)
339         vols[1].Put(context.Background(), TestHash2, TestBlock2)
340         vols[0].Put(context.Background(), TestHash+".meta", []byte("metadata"))
341         vols[1].Put(context.Background(), TestHash2+".meta", []byte("metadata"))
342
343         s.cluster.SystemRootToken = "DATA MANAGER TOKEN"
344
345         unauthenticatedReq := &RequestTester{
346                 method: "GET",
347                 uri:    "/index",
348         }
349         authenticatedReq := &RequestTester{
350                 method:   "GET",
351                 uri:      "/index",
352                 apiToken: knownToken,
353         }
354         superuserReq := &RequestTester{
355                 method:   "GET",
356                 uri:      "/index",
357                 apiToken: s.cluster.SystemRootToken,
358         }
359         unauthPrefixReq := &RequestTester{
360                 method: "GET",
361                 uri:    "/index/" + TestHash[0:3],
362         }
363         authPrefixReq := &RequestTester{
364                 method:   "GET",
365                 uri:      "/index/" + TestHash[0:3],
366                 apiToken: knownToken,
367         }
368         superuserPrefixReq := &RequestTester{
369                 method:   "GET",
370                 uri:      "/index/" + TestHash[0:3],
371                 apiToken: s.cluster.SystemRootToken,
372         }
373         superuserNoSuchPrefixReq := &RequestTester{
374                 method:   "GET",
375                 uri:      "/index/abcd",
376                 apiToken: s.cluster.SystemRootToken,
377         }
378         superuserInvalidPrefixReq := &RequestTester{
379                 method:   "GET",
380                 uri:      "/index/xyz",
381                 apiToken: s.cluster.SystemRootToken,
382         }
383
384         // -------------------------------------------------------------
385         // Only the superuser should be allowed to issue /index requests.
386
387         // ---------------------------
388         // BlobSigning enabled
389         // This setting should not affect tests passing.
390         s.cluster.Collections.BlobSigning = true
391
392         // unauthenticated /index request
393         // => UnauthorizedError
394         response := IssueRequest(s.handler, unauthenticatedReq)
395         ExpectStatusCode(c,
396                 "RequireSignatures on, unauthenticated request",
397                 UnauthorizedError.HTTPCode,
398                 response)
399
400         // unauthenticated /index/prefix request
401         // => UnauthorizedError
402         response = IssueRequest(s.handler, unauthPrefixReq)
403         ExpectStatusCode(c,
404                 "permissions on, unauthenticated /index/prefix request",
405                 UnauthorizedError.HTTPCode,
406                 response)
407
408         // authenticated /index request, non-superuser
409         // => UnauthorizedError
410         response = IssueRequest(s.handler, authenticatedReq)
411         ExpectStatusCode(c,
412                 "permissions on, authenticated request, non-superuser",
413                 UnauthorizedError.HTTPCode,
414                 response)
415
416         // authenticated /index/prefix request, non-superuser
417         // => UnauthorizedError
418         response = IssueRequest(s.handler, authPrefixReq)
419         ExpectStatusCode(c,
420                 "permissions on, authenticated /index/prefix request, non-superuser",
421                 UnauthorizedError.HTTPCode,
422                 response)
423
424         // superuser /index request
425         // => OK
426         response = IssueRequest(s.handler, superuserReq)
427         ExpectStatusCode(c,
428                 "permissions on, superuser request",
429                 http.StatusOK,
430                 response)
431
432         // ----------------------------
433         // BlobSigning disabled
434         // Valid Request should still pass.
435         s.cluster.Collections.BlobSigning = false
436
437         // superuser /index request
438         // => OK
439         response = IssueRequest(s.handler, superuserReq)
440         ExpectStatusCode(c,
441                 "permissions on, superuser request",
442                 http.StatusOK,
443                 response)
444
445         expected := `^` + TestHash + `\+\d+ \d+\n` +
446                 TestHash2 + `\+\d+ \d+\n\n$`
447         match, _ := regexp.MatchString(expected, response.Body.String())
448         if !match {
449                 c.Errorf(
450                         "permissions on, superuser request: expected %s, got:\n%s",
451                         expected, response.Body.String())
452         }
453
454         // superuser /index/prefix request
455         // => OK
456         response = IssueRequest(s.handler, superuserPrefixReq)
457         ExpectStatusCode(c,
458                 "permissions on, superuser request",
459                 http.StatusOK,
460                 response)
461
462         expected = `^` + TestHash + `\+\d+ \d+\n\n$`
463         match, _ = regexp.MatchString(expected, response.Body.String())
464         if !match {
465                 c.Errorf(
466                         "permissions on, superuser /index/prefix request: expected %s, got:\n%s",
467                         expected, response.Body.String())
468         }
469
470         // superuser /index/{no-such-prefix} request
471         // => OK
472         response = IssueRequest(s.handler, superuserNoSuchPrefixReq)
473         ExpectStatusCode(c,
474                 "permissions on, superuser request",
475                 http.StatusOK,
476                 response)
477
478         if "\n" != response.Body.String() {
479                 c.Errorf("Expected empty response for %s. Found %s", superuserNoSuchPrefixReq.uri, response.Body.String())
480         }
481
482         // superuser /index/{invalid-prefix} request
483         // => StatusBadRequest
484         response = IssueRequest(s.handler, superuserInvalidPrefixReq)
485         ExpectStatusCode(c,
486                 "permissions on, superuser request",
487                 http.StatusBadRequest,
488                 response)
489 }
490
491 // TestDeleteHandler
492 //
493 // Cases tested:
494 //
495 //   With no token and with a non-data-manager token:
496 //   * Delete existing block
497 //     (test for 403 Forbidden, confirm block not deleted)
498 //
499 //   With data manager token:
500 //
501 //   * Delete existing block
502 //     (test for 200 OK, response counts, confirm block deleted)
503 //
504 //   * Delete nonexistent block
505 //     (test for 200 OK, response counts)
506 //
507 //   TODO(twp):
508 //
509 //   * Delete block on read-only and read-write volume
510 //     (test for 200 OK, response with copies_deleted=1,
511 //     copies_failed=1, confirm block deleted only on r/w volume)
512 //
513 //   * Delete block on read-only volume only
514 //     (test for 200 OK, response with copies_deleted=0, copies_failed=1,
515 //     confirm block not deleted)
516 //
517 func (s *HandlerSuite) TestDeleteHandler(c *check.C) {
518         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
519
520         vols := s.handler.volmgr.AllWritable()
521         vols[0].Put(context.Background(), TestHash, TestBlock)
522
523         // Explicitly set the BlobSignatureTTL to 0 for these
524         // tests, to ensure the MockVolume deletes the blocks
525         // even though they have just been created.
526         s.cluster.Collections.BlobSigningTTL = arvados.Duration(0)
527
528         var userToken = "NOT DATA MANAGER TOKEN"
529         s.cluster.SystemRootToken = "DATA MANAGER TOKEN"
530
531         s.cluster.Collections.BlobTrash = true
532
533         unauthReq := &RequestTester{
534                 method: "DELETE",
535                 uri:    "/" + TestHash,
536         }
537
538         userReq := &RequestTester{
539                 method:   "DELETE",
540                 uri:      "/" + TestHash,
541                 apiToken: userToken,
542         }
543
544         superuserExistingBlockReq := &RequestTester{
545                 method:   "DELETE",
546                 uri:      "/" + TestHash,
547                 apiToken: s.cluster.SystemRootToken,
548         }
549
550         superuserNonexistentBlockReq := &RequestTester{
551                 method:   "DELETE",
552                 uri:      "/" + TestHash2,
553                 apiToken: s.cluster.SystemRootToken,
554         }
555
556         // Unauthenticated request returns PermissionError.
557         var response *httptest.ResponseRecorder
558         response = IssueRequest(s.handler, unauthReq)
559         ExpectStatusCode(c,
560                 "unauthenticated request",
561                 PermissionError.HTTPCode,
562                 response)
563
564         // Authenticated non-admin request returns PermissionError.
565         response = IssueRequest(s.handler, userReq)
566         ExpectStatusCode(c,
567                 "authenticated non-admin request",
568                 PermissionError.HTTPCode,
569                 response)
570
571         // Authenticated admin request for nonexistent block.
572         type deletecounter struct {
573                 Deleted int `json:"copies_deleted"`
574                 Failed  int `json:"copies_failed"`
575         }
576         var responseDc, expectedDc deletecounter
577
578         response = IssueRequest(s.handler, superuserNonexistentBlockReq)
579         ExpectStatusCode(c,
580                 "data manager request, nonexistent block",
581                 http.StatusNotFound,
582                 response)
583
584         // Authenticated admin request for existing block while BlobTrash is false.
585         s.cluster.Collections.BlobTrash = false
586         response = IssueRequest(s.handler, superuserExistingBlockReq)
587         ExpectStatusCode(c,
588                 "authenticated request, existing block, method disabled",
589                 MethodDisabledError.HTTPCode,
590                 response)
591         s.cluster.Collections.BlobTrash = true
592
593         // Authenticated admin request for existing block.
594         response = IssueRequest(s.handler, superuserExistingBlockReq)
595         ExpectStatusCode(c,
596                 "data manager request, existing block",
597                 http.StatusOK,
598                 response)
599         // Expect response {"copies_deleted":1,"copies_failed":0}
600         expectedDc = deletecounter{1, 0}
601         json.NewDecoder(response.Body).Decode(&responseDc)
602         if responseDc != expectedDc {
603                 c.Errorf("superuserExistingBlockReq\nexpected: %+v\nreceived: %+v",
604                         expectedDc, responseDc)
605         }
606         // Confirm the block has been deleted
607         buf := make([]byte, BlockSize)
608         _, err := vols[0].Get(context.Background(), TestHash, buf)
609         var blockDeleted = os.IsNotExist(err)
610         if !blockDeleted {
611                 c.Error("superuserExistingBlockReq: block not deleted")
612         }
613
614         // A DELETE request on a block newer than BlobSignatureTTL
615         // should return success but leave the block on the volume.
616         vols[0].Put(context.Background(), TestHash, TestBlock)
617         s.cluster.Collections.BlobSigningTTL = arvados.Duration(time.Hour)
618
619         response = IssueRequest(s.handler, superuserExistingBlockReq)
620         ExpectStatusCode(c,
621                 "data manager request, existing block",
622                 http.StatusOK,
623                 response)
624         // Expect response {"copies_deleted":1,"copies_failed":0}
625         expectedDc = deletecounter{1, 0}
626         json.NewDecoder(response.Body).Decode(&responseDc)
627         if responseDc != expectedDc {
628                 c.Errorf("superuserExistingBlockReq\nexpected: %+v\nreceived: %+v",
629                         expectedDc, responseDc)
630         }
631         // Confirm the block has NOT been deleted.
632         _, err = vols[0].Get(context.Background(), TestHash, buf)
633         if err != nil {
634                 c.Errorf("testing delete on new block: %s\n", err)
635         }
636 }
637
638 // TestPullHandler
639 //
640 // Test handling of the PUT /pull statement.
641 //
642 // Cases tested: syntactically valid and invalid pull lists, from the
643 // data manager and from unprivileged users:
644 //
645 //   1. Valid pull list from an ordinary user
646 //      (expected result: 401 Unauthorized)
647 //
648 //   2. Invalid pull request from an ordinary user
649 //      (expected result: 401 Unauthorized)
650 //
651 //   3. Valid pull request from the data manager
652 //      (expected result: 200 OK with request body "Received 3 pull
653 //      requests"
654 //
655 //   4. Invalid pull request from the data manager
656 //      (expected result: 400 Bad Request)
657 //
658 // Test that in the end, the pull manager received a good pull list with
659 // the expected number of requests.
660 //
661 // TODO(twp): test concurrency: launch 100 goroutines to update the
662 // pull list simultaneously.  Make sure that none of them return 400
663 // Bad Request and that pullq.GetList() returns a valid list.
664 //
665 func (s *HandlerSuite) TestPullHandler(c *check.C) {
666         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
667
668         // Replace the router's pullq -- which the worker goroutines
669         // started by setup() are now receiving from -- with a new
670         // one, so we can see what the handler sends to it.
671         pullq := NewWorkQueue()
672         s.handler.Handler.(*router).pullq = pullq
673
674         var userToken = "USER TOKEN"
675         s.cluster.SystemRootToken = "DATA MANAGER TOKEN"
676
677         goodJSON := []byte(`[
678                 {
679                         "locator":"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+12345",
680                         "servers":[
681                                 "http://server1",
682                                 "http://server2"
683                         ]
684                 },
685                 {
686                         "locator":"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb+12345",
687                         "servers":[]
688                 },
689                 {
690                         "locator":"cccccccccccccccccccccccccccccccc+12345",
691                         "servers":["http://server1"]
692                 }
693         ]`)
694
695         badJSON := []byte(`{ "key":"I'm a little teapot" }`)
696
697         type pullTest struct {
698                 name         string
699                 req          RequestTester
700                 responseCode int
701                 responseBody string
702         }
703         var testcases = []pullTest{
704                 {
705                         "Valid pull list from an ordinary user",
706                         RequestTester{"/pull", userToken, "PUT", goodJSON},
707                         http.StatusUnauthorized,
708                         "Unauthorized\n",
709                 },
710                 {
711                         "Invalid pull request from an ordinary user",
712                         RequestTester{"/pull", userToken, "PUT", badJSON},
713                         http.StatusUnauthorized,
714                         "Unauthorized\n",
715                 },
716                 {
717                         "Valid pull request from the data manager",
718                         RequestTester{"/pull", s.cluster.SystemRootToken, "PUT", goodJSON},
719                         http.StatusOK,
720                         "Received 3 pull requests\n",
721                 },
722                 {
723                         "Invalid pull request from the data manager",
724                         RequestTester{"/pull", s.cluster.SystemRootToken, "PUT", badJSON},
725                         http.StatusBadRequest,
726                         "",
727                 },
728         }
729
730         for _, tst := range testcases {
731                 response := IssueRequest(s.handler, &tst.req)
732                 ExpectStatusCode(c, tst.name, tst.responseCode, response)
733                 ExpectBody(c, tst.name, tst.responseBody, response)
734         }
735
736         // The Keep pull manager should have received one good list with 3
737         // requests on it.
738         for i := 0; i < 3; i++ {
739                 var item interface{}
740                 select {
741                 case item = <-pullq.NextItem:
742                 case <-time.After(time.Second):
743                         c.Error("timed out")
744                 }
745                 if _, ok := item.(PullRequest); !ok {
746                         c.Errorf("item %v could not be parsed as a PullRequest", item)
747                 }
748         }
749
750         expectChannelEmpty(c, pullq.NextItem)
751 }
752
753 // TestTrashHandler
754 //
755 // Test cases:
756 //
757 // Cases tested: syntactically valid and invalid trash lists, from the
758 // data manager and from unprivileged users:
759 //
760 //   1. Valid trash list from an ordinary user
761 //      (expected result: 401 Unauthorized)
762 //
763 //   2. Invalid trash list from an ordinary user
764 //      (expected result: 401 Unauthorized)
765 //
766 //   3. Valid trash list from the data manager
767 //      (expected result: 200 OK with request body "Received 3 trash
768 //      requests"
769 //
770 //   4. Invalid trash list from the data manager
771 //      (expected result: 400 Bad Request)
772 //
773 // Test that in the end, the trash collector received a good list
774 // trash list with the expected number of requests.
775 //
776 // TODO(twp): test concurrency: launch 100 goroutines to update the
777 // pull list simultaneously.  Make sure that none of them return 400
778 // Bad Request and that replica.Dump() returns a valid list.
779 //
780 func (s *HandlerSuite) TestTrashHandler(c *check.C) {
781         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
782         // Replace the router's trashq -- which the worker goroutines
783         // started by setup() are now receiving from -- with a new
784         // one, so we can see what the handler sends to it.
785         trashq := NewWorkQueue()
786         s.handler.Handler.(*router).trashq = trashq
787
788         var userToken = "USER TOKEN"
789         s.cluster.SystemRootToken = "DATA MANAGER TOKEN"
790
791         goodJSON := []byte(`[
792                 {
793                         "locator":"block1",
794                         "block_mtime":1409082153
795                 },
796                 {
797                         "locator":"block2",
798                         "block_mtime":1409082153
799                 },
800                 {
801                         "locator":"block3",
802                         "block_mtime":1409082153
803                 }
804         ]`)
805
806         badJSON := []byte(`I am not a valid JSON string`)
807
808         type trashTest struct {
809                 name         string
810                 req          RequestTester
811                 responseCode int
812                 responseBody string
813         }
814
815         var testcases = []trashTest{
816                 {
817                         "Valid trash list from an ordinary user",
818                         RequestTester{"/trash", userToken, "PUT", goodJSON},
819                         http.StatusUnauthorized,
820                         "Unauthorized\n",
821                 },
822                 {
823                         "Invalid trash list from an ordinary user",
824                         RequestTester{"/trash", userToken, "PUT", badJSON},
825                         http.StatusUnauthorized,
826                         "Unauthorized\n",
827                 },
828                 {
829                         "Valid trash list from the data manager",
830                         RequestTester{"/trash", s.cluster.SystemRootToken, "PUT", goodJSON},
831                         http.StatusOK,
832                         "Received 3 trash requests\n",
833                 },
834                 {
835                         "Invalid trash list from the data manager",
836                         RequestTester{"/trash", s.cluster.SystemRootToken, "PUT", badJSON},
837                         http.StatusBadRequest,
838                         "",
839                 },
840         }
841
842         for _, tst := range testcases {
843                 response := IssueRequest(s.handler, &tst.req)
844                 ExpectStatusCode(c, tst.name, tst.responseCode, response)
845                 ExpectBody(c, tst.name, tst.responseBody, response)
846         }
847
848         // The trash collector should have received one good list with 3
849         // requests on it.
850         for i := 0; i < 3; i++ {
851                 item := <-trashq.NextItem
852                 if _, ok := item.(TrashRequest); !ok {
853                         c.Errorf("item %v could not be parsed as a TrashRequest", item)
854                 }
855         }
856
857         expectChannelEmpty(c, trashq.NextItem)
858 }
859
860 // ====================
861 // Helper functions
862 // ====================
863
864 // IssueTestRequest executes an HTTP request described by rt, to a
865 // REST router.  It returns the HTTP response to the request.
866 func IssueRequest(handler http.Handler, rt *RequestTester) *httptest.ResponseRecorder {
867         response := httptest.NewRecorder()
868         body := bytes.NewReader(rt.requestBody)
869         req, _ := http.NewRequest(rt.method, rt.uri, body)
870         if rt.apiToken != "" {
871                 req.Header.Set("Authorization", "OAuth2 "+rt.apiToken)
872         }
873         handler.ServeHTTP(response, req)
874         return response
875 }
876
877 func IssueHealthCheckRequest(handler http.Handler, rt *RequestTester) *httptest.ResponseRecorder {
878         response := httptest.NewRecorder()
879         body := bytes.NewReader(rt.requestBody)
880         req, _ := http.NewRequest(rt.method, rt.uri, body)
881         if rt.apiToken != "" {
882                 req.Header.Set("Authorization", "Bearer "+rt.apiToken)
883         }
884         handler.ServeHTTP(response, req)
885         return response
886 }
887
888 // ExpectStatusCode checks whether a response has the specified status code,
889 // and reports a test failure if not.
890 func ExpectStatusCode(
891         c *check.C,
892         testname string,
893         expectedStatus int,
894         response *httptest.ResponseRecorder) {
895         if response.Code != expectedStatus {
896                 c.Errorf("%s: expected status %d, got %+v",
897                         testname, expectedStatus, response)
898         }
899 }
900
901 func ExpectBody(
902         c *check.C,
903         testname string,
904         expectedBody string,
905         response *httptest.ResponseRecorder) {
906         if expectedBody != "" && response.Body.String() != expectedBody {
907                 c.Errorf("%s: expected response body '%s', got %+v",
908                         testname, expectedBody, response)
909         }
910 }
911
912 // See #7121
913 func (s *HandlerSuite) TestPutNeedsOnlyOneBuffer(c *check.C) {
914         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
915
916         defer func(orig *bufferPool) {
917                 bufs = orig
918         }(bufs)
919         bufs = newBufferPool(ctxlog.TestLogger(c), 1, BlockSize)
920
921         ok := make(chan struct{})
922         go func() {
923                 for i := 0; i < 2; i++ {
924                         response := IssueRequest(s.handler,
925                                 &RequestTester{
926                                         method:      "PUT",
927                                         uri:         "/" + TestHash,
928                                         requestBody: TestBlock,
929                                 })
930                         ExpectStatusCode(c,
931                                 "TestPutNeedsOnlyOneBuffer", http.StatusOK, response)
932                 }
933                 ok <- struct{}{}
934         }()
935
936         select {
937         case <-ok:
938         case <-time.After(time.Second):
939                 c.Fatal("PUT deadlocks with MaxBuffers==1")
940         }
941 }
942
943 // Invoke the PutBlockHandler a bunch of times to test for bufferpool resource
944 // leak.
945 func (s *HandlerSuite) TestPutHandlerNoBufferleak(c *check.C) {
946         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
947
948         ok := make(chan bool)
949         go func() {
950                 for i := 0; i < s.cluster.API.MaxKeepBlockBuffers+1; i++ {
951                         // Unauthenticated request, no server key
952                         // => OK (unsigned response)
953                         unsignedLocator := "/" + TestHash
954                         response := IssueRequest(s.handler,
955                                 &RequestTester{
956                                         method:      "PUT",
957                                         uri:         unsignedLocator,
958                                         requestBody: TestBlock,
959                                 })
960                         ExpectStatusCode(c,
961                                 "TestPutHandlerBufferleak", http.StatusOK, response)
962                         ExpectBody(c,
963                                 "TestPutHandlerBufferleak",
964                                 TestHashPutResp, response)
965                 }
966                 ok <- true
967         }()
968         select {
969         case <-time.After(20 * time.Second):
970                 // If the buffer pool leaks, the test goroutine hangs.
971                 c.Fatal("test did not finish, assuming pool leaked")
972         case <-ok:
973         }
974 }
975
976 type notifyingResponseRecorder struct {
977         *httptest.ResponseRecorder
978         closer chan bool
979 }
980
981 func (r *notifyingResponseRecorder) CloseNotify() <-chan bool {
982         return r.closer
983 }
984
985 func (s *HandlerSuite) TestGetHandlerClientDisconnect(c *check.C) {
986         s.cluster.Collections.BlobSigning = false
987         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
988
989         defer func(orig *bufferPool) {
990                 bufs = orig
991         }(bufs)
992         bufs = newBufferPool(ctxlog.TestLogger(c), 1, BlockSize)
993         defer bufs.Put(bufs.Get(BlockSize))
994
995         if err := s.handler.volmgr.AllWritable()[0].Put(context.Background(), TestHash, TestBlock); err != nil {
996                 c.Error(err)
997         }
998
999         resp := &notifyingResponseRecorder{
1000                 ResponseRecorder: httptest.NewRecorder(),
1001                 closer:           make(chan bool, 1),
1002         }
1003         if _, ok := http.ResponseWriter(resp).(http.CloseNotifier); !ok {
1004                 c.Fatal("notifyingResponseRecorder is broken")
1005         }
1006         // If anyone asks, the client has disconnected.
1007         resp.closer <- true
1008
1009         ok := make(chan struct{})
1010         go func() {
1011                 req, _ := http.NewRequest("GET", fmt.Sprintf("/%s+%d", TestHash, len(TestBlock)), nil)
1012                 s.handler.ServeHTTP(resp, req)
1013                 ok <- struct{}{}
1014         }()
1015
1016         select {
1017         case <-time.After(20 * time.Second):
1018                 c.Fatal("request took >20s, close notifier must be broken")
1019         case <-ok:
1020         }
1021
1022         ExpectStatusCode(c, "client disconnect", http.StatusServiceUnavailable, resp.ResponseRecorder)
1023         for i, v := range s.handler.volmgr.AllWritable() {
1024                 if calls := v.Volume.(*MockVolume).called["GET"]; calls != 0 {
1025                         c.Errorf("volume %d got %d calls, expected 0", i, calls)
1026                 }
1027         }
1028 }
1029
1030 // Invoke the GetBlockHandler a bunch of times to test for bufferpool resource
1031 // leak.
1032 func (s *HandlerSuite) TestGetHandlerNoBufferLeak(c *check.C) {
1033         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
1034
1035         vols := s.handler.volmgr.AllWritable()
1036         if err := vols[0].Put(context.Background(), TestHash, TestBlock); err != nil {
1037                 c.Error(err)
1038         }
1039
1040         ok := make(chan bool)
1041         go func() {
1042                 for i := 0; i < s.cluster.API.MaxKeepBlockBuffers+1; i++ {
1043                         // Unauthenticated request, unsigned locator
1044                         // => OK
1045                         unsignedLocator := "/" + TestHash
1046                         response := IssueRequest(s.handler,
1047                                 &RequestTester{
1048                                         method: "GET",
1049                                         uri:    unsignedLocator,
1050                                 })
1051                         ExpectStatusCode(c,
1052                                 "Unauthenticated request, unsigned locator", http.StatusOK, response)
1053                         ExpectBody(c,
1054                                 "Unauthenticated request, unsigned locator",
1055                                 string(TestBlock),
1056                                 response)
1057                 }
1058                 ok <- true
1059         }()
1060         select {
1061         case <-time.After(20 * time.Second):
1062                 // If the buffer pool leaks, the test goroutine hangs.
1063                 c.Fatal("test did not finish, assuming pool leaked")
1064         case <-ok:
1065         }
1066 }
1067
1068 func (s *HandlerSuite) TestPutReplicationHeader(c *check.C) {
1069         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
1070
1071         resp := IssueRequest(s.handler, &RequestTester{
1072                 method:      "PUT",
1073                 uri:         "/" + TestHash,
1074                 requestBody: TestBlock,
1075         })
1076         if r := resp.Header().Get("X-Keep-Replicas-Stored"); r != "1" {
1077                 c.Logf("%#v", resp)
1078                 c.Errorf("Got X-Keep-Replicas-Stored: %q, expected %q", r, "1")
1079         }
1080 }
1081
1082 func (s *HandlerSuite) TestUntrashHandler(c *check.C) {
1083         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
1084
1085         // Set up Keep volumes
1086         vols := s.handler.volmgr.AllWritable()
1087         vols[0].Put(context.Background(), TestHash, TestBlock)
1088
1089         s.cluster.SystemRootToken = "DATA MANAGER TOKEN"
1090
1091         // unauthenticatedReq => UnauthorizedError
1092         unauthenticatedReq := &RequestTester{
1093                 method: "PUT",
1094                 uri:    "/untrash/" + TestHash,
1095         }
1096         response := IssueRequest(s.handler, unauthenticatedReq)
1097         ExpectStatusCode(c,
1098                 "Unauthenticated request",
1099                 UnauthorizedError.HTTPCode,
1100                 response)
1101
1102         // notDataManagerReq => UnauthorizedError
1103         notDataManagerReq := &RequestTester{
1104                 method:   "PUT",
1105                 uri:      "/untrash/" + TestHash,
1106                 apiToken: knownToken,
1107         }
1108
1109         response = IssueRequest(s.handler, notDataManagerReq)
1110         ExpectStatusCode(c,
1111                 "Non-datamanager token",
1112                 UnauthorizedError.HTTPCode,
1113                 response)
1114
1115         // datamanagerWithBadHashReq => StatusBadRequest
1116         datamanagerWithBadHashReq := &RequestTester{
1117                 method:   "PUT",
1118                 uri:      "/untrash/thisisnotalocator",
1119                 apiToken: s.cluster.SystemRootToken,
1120         }
1121         response = IssueRequest(s.handler, datamanagerWithBadHashReq)
1122         ExpectStatusCode(c,
1123                 "Bad locator in untrash request",
1124                 http.StatusBadRequest,
1125                 response)
1126
1127         // datamanagerWrongMethodReq => StatusBadRequest
1128         datamanagerWrongMethodReq := &RequestTester{
1129                 method:   "GET",
1130                 uri:      "/untrash/" + TestHash,
1131                 apiToken: s.cluster.SystemRootToken,
1132         }
1133         response = IssueRequest(s.handler, datamanagerWrongMethodReq)
1134         ExpectStatusCode(c,
1135                 "Only PUT method is supported for untrash",
1136                 http.StatusMethodNotAllowed,
1137                 response)
1138
1139         // datamanagerReq => StatusOK
1140         datamanagerReq := &RequestTester{
1141                 method:   "PUT",
1142                 uri:      "/untrash/" + TestHash,
1143                 apiToken: s.cluster.SystemRootToken,
1144         }
1145         response = IssueRequest(s.handler, datamanagerReq)
1146         ExpectStatusCode(c,
1147                 "",
1148                 http.StatusOK,
1149                 response)
1150         expected := "Successfully untrashed on: [MockVolume],[MockVolume]"
1151         if response.Body.String() != expected {
1152                 c.Errorf(
1153                         "Untrash response mismatched: expected %s, got:\n%s",
1154                         expected, response.Body.String())
1155         }
1156 }
1157
1158 func (s *HandlerSuite) TestUntrashHandlerWithNoWritableVolumes(c *check.C) {
1159         // Change all volumes to read-only
1160         for uuid, v := range s.cluster.Volumes {
1161                 v.ReadOnly = true
1162                 s.cluster.Volumes[uuid] = v
1163         }
1164         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
1165
1166         // datamanagerReq => StatusOK
1167         datamanagerReq := &RequestTester{
1168                 method:   "PUT",
1169                 uri:      "/untrash/" + TestHash,
1170                 apiToken: s.cluster.SystemRootToken,
1171         }
1172         response := IssueRequest(s.handler, datamanagerReq)
1173         ExpectStatusCode(c,
1174                 "No writable volumes",
1175                 http.StatusNotFound,
1176                 response)
1177 }
1178
1179 func (s *HandlerSuite) TestHealthCheckPing(c *check.C) {
1180         s.cluster.ManagementToken = arvadostest.ManagementToken
1181         c.Assert(s.handler.setup(context.Background(), s.cluster, "", prometheus.NewRegistry(), testServiceURL), check.IsNil)
1182         pingReq := &RequestTester{
1183                 method:   "GET",
1184                 uri:      "/_health/ping",
1185                 apiToken: arvadostest.ManagementToken,
1186         }
1187         response := IssueHealthCheckRequest(s.handler, pingReq)
1188         ExpectStatusCode(c,
1189                 "",
1190                 http.StatusOK,
1191                 response)
1192         want := `{"health":"OK"}`
1193         if !strings.Contains(response.Body.String(), want) {
1194                 c.Errorf("expected response to include %s: got %s", want, response.Body.String())
1195         }
1196 }