1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 "git.curoverse.com/arvados.git/lib/config"
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
24 "git.curoverse.com/arvados.git/sdk/go/arvadostest"
25 "git.curoverse.com/arvados.git/sdk/go/keepclient"
26 log "github.com/sirupsen/logrus"
31 // Gocheck boilerplate
32 func Test(t *testing.T) {
36 // Gocheck boilerplate
37 var _ = Suite(&ServerRequiredSuite{})
39 // Tests that require the Keep server running
40 type ServerRequiredSuite struct{}
42 // Gocheck boilerplate
43 var _ = Suite(&NoKeepServerSuite{})
45 // Test with no keepserver to simulate errors
46 type NoKeepServerSuite struct{}
48 var TestProxyUUID = "zzzzz-bi6l4-lrixqc4fxofbmzz"
50 // Wait (up to 1 second) for keepproxy to listen on a port. This
51 // avoids a race condition where we hit a "connection refused" error
52 // because we start testing the proxy too soon.
53 func waitForListener() {
57 for i := 0; listener == nil && i < 10000; i += ms {
58 time.Sleep(ms * time.Millisecond)
61 panic("Timed out waiting for listener to start")
65 func closeListener() {
71 func (s *ServerRequiredSuite) SetUpSuite(c *C) {
72 arvadostest.StartAPI()
73 arvadostest.StartKeep(2, false)
76 func (s *ServerRequiredSuite) SetUpTest(c *C) {
77 arvadostest.ResetEnv()
80 func (s *ServerRequiredSuite) TearDownSuite(c *C) {
81 arvadostest.StopKeep(2)
85 func (s *NoKeepServerSuite) SetUpSuite(c *C) {
86 arvadostest.StartAPI()
87 // We need API to have some keep services listed, but the
88 // services themselves should be unresponsive.
89 arvadostest.StartKeep(2, false)
90 arvadostest.StopKeep(2)
93 func (s *NoKeepServerSuite) SetUpTest(c *C) {
94 arvadostest.ResetEnv()
97 func (s *NoKeepServerSuite) TearDownSuite(c *C) {
101 func runProxy(c *C, bogusClientToken bool) *keepclient.KeepClient {
102 cfg, err := config.NewLoader(nil, nil).Load()
103 c.Assert(err, Equals, nil)
104 cluster, err := cfg.GetCluster("")
105 c.Assert(err, Equals, nil)
107 cluster.Services.Keepproxy.InternalURLs = map[arvados.URL]arvados.ServiceInstance{arvados.URL{Host: ":0"}: arvados.ServiceInstance{}}
110 go run(log.New(), cluster)
113 client := arvados.NewClientFromEnv()
114 arv, err := arvadosclient.New(client)
115 c.Assert(err, Equals, nil)
116 if bogusClientToken {
117 arv.ApiToken = "bogus-token"
119 kc := keepclient.New(arv)
120 sr := map[string]string{
121 TestProxyUUID: "http://" + listener.Addr().String(),
123 kc.SetServiceRoots(sr, sr, sr)
124 kc.Arvados.External = true
129 func (s *ServerRequiredSuite) TestResponseViaHeader(c *C) {
131 defer closeListener()
133 req, err := http.NewRequest("POST",
134 "http://"+listener.Addr().String()+"/",
135 strings.NewReader("TestViaHeader"))
136 c.Assert(err, Equals, nil)
137 req.Header.Add("Authorization", "OAuth2 "+arvadostest.ActiveToken)
138 resp, err := (&http.Client{}).Do(req)
139 c.Assert(err, Equals, nil)
140 c.Check(resp.Header.Get("Via"), Equals, "HTTP/1.1 keepproxy")
141 locator, err := ioutil.ReadAll(resp.Body)
142 c.Assert(err, Equals, nil)
145 req, err = http.NewRequest("GET",
146 "http://"+listener.Addr().String()+"/"+string(locator),
148 c.Assert(err, Equals, nil)
149 resp, err = (&http.Client{}).Do(req)
150 c.Assert(err, Equals, nil)
151 c.Check(resp.Header.Get("Via"), Equals, "HTTP/1.1 keepproxy")
155 func (s *ServerRequiredSuite) TestLoopDetection(c *C) {
156 kc := runProxy(c, false)
157 defer closeListener()
159 sr := map[string]string{
160 TestProxyUUID: "http://" + listener.Addr().String(),
162 router.(*proxyHandler).KeepClient.SetServiceRoots(sr, sr, sr)
164 content := []byte("TestLoopDetection")
165 _, _, err := kc.PutB(content)
166 c.Check(err, ErrorMatches, `.*loop detected.*`)
168 hash := fmt.Sprintf("%x", md5.Sum(content))
169 _, _, _, err = kc.Get(hash)
170 c.Check(err, ErrorMatches, `.*loop detected.*`)
173 func (s *ServerRequiredSuite) TestStorageClassesHeader(c *C) {
174 kc := runProxy(c, false)
175 defer closeListener()
177 // Set up fake keepstore to record request headers
179 ts := httptest.NewServer(http.HandlerFunc(
180 func(w http.ResponseWriter, r *http.Request) {
182 http.Error(w, "Error", http.StatusInternalServerError)
186 // Point keepproxy router's keepclient to the fake keepstore
187 sr := map[string]string{
188 TestProxyUUID: ts.URL,
190 router.(*proxyHandler).KeepClient.SetServiceRoots(sr, sr, sr)
192 // Set up client to ask for storage classes to keepproxy
193 kc.StorageClasses = []string{"secure"}
194 content := []byte("Very important data")
195 _, _, err := kc.PutB(content)
197 c.Check(hdr.Get("X-Keep-Storage-Classes"), Equals, "secure")
200 func (s *ServerRequiredSuite) TestDesiredReplicas(c *C) {
201 kc := runProxy(c, false)
202 defer closeListener()
204 content := []byte("TestDesiredReplicas")
205 hash := fmt.Sprintf("%x", md5.Sum(content))
207 for _, kc.Want_replicas = range []int{0, 1, 2} {
208 locator, rep, err := kc.PutB(content)
209 c.Check(err, Equals, nil)
210 c.Check(rep, Equals, kc.Want_replicas)
212 c.Check(locator, Matches, fmt.Sprintf(`^%s\+%d(\+.+)?$`, hash, len(content)))
217 func (s *ServerRequiredSuite) TestPutWrongContentLength(c *C) {
218 kc := runProxy(c, false)
219 defer closeListener()
221 content := []byte("TestPutWrongContentLength")
222 hash := fmt.Sprintf("%x", md5.Sum(content))
224 // If we use http.Client to send these requests to the network
225 // server we just started, the Go http library automatically
226 // fixes the invalid Content-Length header. In order to test
227 // our server behavior, we have to call the handler directly
228 // using an httptest.ResponseRecorder.
229 rtr := MakeRESTRouter(kc, 10*time.Second, "")
231 type testcase struct {
236 for _, t := range []testcase{
237 {"1", http.StatusBadRequest},
238 {"", http.StatusLengthRequired},
239 {"-1", http.StatusLengthRequired},
240 {"abcdef", http.StatusLengthRequired},
242 req, err := http.NewRequest("PUT",
243 fmt.Sprintf("http://%s/%s+%d", listener.Addr().String(), hash, len(content)),
244 bytes.NewReader(content))
246 req.Header.Set("Content-Length", t.sendLength)
247 req.Header.Set("Authorization", "OAuth2 "+arvadostest.ActiveToken)
248 req.Header.Set("Content-Type", "application/octet-stream")
250 resp := httptest.NewRecorder()
251 rtr.ServeHTTP(resp, req)
252 c.Check(resp.Code, Equals, t.expectStatus)
256 func (s *ServerRequiredSuite) TestManyFailedPuts(c *C) {
257 kc := runProxy(c, false)
258 defer closeListener()
259 router.(*proxyHandler).timeout = time.Nanosecond
261 buf := make([]byte, 1<<20)
263 var wg sync.WaitGroup
264 for i := 0; i < 128; i++ {
271 done := make(chan bool)
278 case <-time.After(10 * time.Second):
283 func (s *ServerRequiredSuite) TestPutAskGet(c *C) {
284 kc := runProxy(c, false)
285 defer closeListener()
287 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
291 _, _, err := kc.Ask(hash)
292 c.Check(err, Equals, keepclient.BlockNotFound)
293 c.Log("Finished Ask (expected BlockNotFound)")
297 reader, _, _, err := kc.Get(hash)
298 c.Check(reader, Equals, nil)
299 c.Check(err, Equals, keepclient.BlockNotFound)
300 c.Log("Finished Get (expected BlockNotFound)")
303 // Note in bug #5309 among other errors keepproxy would set
304 // Content-Length incorrectly on the 404 BlockNotFound response, this
305 // would result in a protocol violation that would prevent reuse of the
306 // connection, which would manifest by the next attempt to use the
307 // connection (in this case the PutB below) failing. So to test for
308 // that bug it's necessary to trigger an error response (such as
309 // BlockNotFound) and then do something else with the same httpClient
315 hash2, rep, err = kc.PutB([]byte("foo"))
316 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+3(\+.+)?$`, hash))
317 c.Check(rep, Equals, 2)
318 c.Check(err, Equals, nil)
319 c.Log("Finished PutB (expected success)")
323 blocklen, _, err := kc.Ask(hash2)
324 c.Assert(err, Equals, nil)
325 c.Check(blocklen, Equals, int64(3))
326 c.Log("Finished Ask (expected success)")
330 reader, blocklen, _, err := kc.Get(hash2)
331 c.Assert(err, Equals, nil)
332 all, err := ioutil.ReadAll(reader)
334 c.Check(all, DeepEquals, []byte("foo"))
335 c.Check(blocklen, Equals, int64(3))
336 c.Log("Finished Get (expected success)")
342 hash2, rep, err = kc.PutB([]byte(""))
343 c.Check(hash2, Matches, `^d41d8cd98f00b204e9800998ecf8427e\+0(\+.+)?$`)
344 c.Check(rep, Equals, 2)
345 c.Check(err, Equals, nil)
346 c.Log("Finished PutB zero block")
350 reader, blocklen, _, err := kc.Get("d41d8cd98f00b204e9800998ecf8427e")
351 c.Assert(err, Equals, nil)
352 all, err := ioutil.ReadAll(reader)
354 c.Check(all, DeepEquals, []byte(""))
355 c.Check(blocklen, Equals, int64(0))
356 c.Log("Finished Get zero block")
360 func (s *ServerRequiredSuite) TestPutAskGetForbidden(c *C) {
361 kc := runProxy(c, true)
362 defer closeListener()
364 hash := fmt.Sprintf("%x+3", md5.Sum([]byte("bar")))
366 _, _, err := kc.Ask(hash)
367 c.Check(err, FitsTypeOf, &keepclient.ErrNotFound{})
369 hash2, rep, err := kc.PutB([]byte("bar"))
370 c.Check(hash2, Equals, "")
371 c.Check(rep, Equals, 0)
372 c.Check(err, FitsTypeOf, keepclient.InsufficientReplicasError(errors.New("")))
374 blocklen, _, err := kc.Ask(hash)
375 c.Check(err, FitsTypeOf, &keepclient.ErrNotFound{})
376 c.Check(err, ErrorMatches, ".*not found.*")
377 c.Check(blocklen, Equals, int64(0))
379 _, blocklen, _, err = kc.Get(hash)
380 c.Check(err, FitsTypeOf, &keepclient.ErrNotFound{})
381 c.Check(err, ErrorMatches, ".*not found.*")
382 c.Check(blocklen, Equals, int64(0))
386 func (s *ServerRequiredSuite) TestCorsHeaders(c *C) {
388 defer closeListener()
391 client := http.Client{}
392 req, err := http.NewRequest("OPTIONS",
393 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))),
396 req.Header.Add("Access-Control-Request-Method", "PUT")
397 req.Header.Add("Access-Control-Request-Headers", "Authorization, X-Keep-Desired-Replicas")
398 resp, err := client.Do(req)
399 c.Check(err, Equals, nil)
400 c.Check(resp.StatusCode, Equals, 200)
401 body, err := ioutil.ReadAll(resp.Body)
403 c.Check(string(body), Equals, "")
404 c.Check(resp.Header.Get("Access-Control-Allow-Methods"), Equals, "GET, HEAD, POST, PUT, OPTIONS")
405 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
409 resp, err := http.Get(
410 fmt.Sprintf("http://%s/%x+3", listener.Addr().String(), md5.Sum([]byte("foo"))))
411 c.Check(err, Equals, nil)
412 c.Check(resp.Header.Get("Access-Control-Allow-Headers"), Equals, "Authorization, Content-Length, Content-Type, X-Keep-Desired-Replicas")
413 c.Check(resp.Header.Get("Access-Control-Allow-Origin"), Equals, "*")
417 func (s *ServerRequiredSuite) TestPostWithoutHash(c *C) {
419 defer closeListener()
422 client := http.Client{}
423 req, err := http.NewRequest("POST",
424 "http://"+listener.Addr().String()+"/",
425 strings.NewReader("qux"))
427 req.Header.Add("Authorization", "OAuth2 "+arvadostest.ActiveToken)
428 req.Header.Add("Content-Type", "application/octet-stream")
429 resp, err := client.Do(req)
430 c.Check(err, Equals, nil)
431 body, err := ioutil.ReadAll(resp.Body)
432 c.Check(err, Equals, nil)
433 c.Check(string(body), Matches,
434 fmt.Sprintf(`^%x\+3(\+.+)?$`, md5.Sum([]byte("qux"))))
438 func (s *ServerRequiredSuite) TestStripHint(c *C) {
439 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz", "$1"),
441 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
442 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
444 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
445 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz", "$1"),
447 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz")
448 c.Check(removeHint.ReplaceAllString("http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73", "$1"),
450 "http://keep.zzzzz.arvadosapi.com:25107/2228819a18d3727630fa30c81853d23f+67108864+K@zzzzz-zzzzz-zzzzzzzzzzzzzzz+A37b6ab198qqqq28d903b975266b23ee711e1852c@55635f73")
455 // Put one block, with 2 replicas
456 // With no prefix (expect the block locator, twice)
457 // With an existing prefix (expect the block locator, twice)
458 // With a valid but non-existing prefix (expect "\n")
459 // With an invalid prefix (expect error)
460 func (s *ServerRequiredSuite) TestGetIndex(c *C) {
461 kc := runProxy(c, false)
462 defer closeListener()
464 // Put "index-data" blocks
465 data := []byte("index-data")
466 hash := fmt.Sprintf("%x", md5.Sum(data))
468 hash2, rep, err := kc.PutB(data)
469 c.Check(hash2, Matches, fmt.Sprintf(`^%s\+10(\+.+)?$`, hash))
470 c.Check(rep, Equals, 2)
471 c.Check(err, Equals, nil)
473 reader, blocklen, _, err := kc.Get(hash)
475 c.Check(blocklen, Equals, int64(10))
476 all, err := ioutil.ReadAll(reader)
478 c.Check(all, DeepEquals, data)
480 // Put some more blocks
481 _, _, err = kc.PutB([]byte("some-more-index-data"))
484 kc.Arvados.ApiToken = arvadostest.DataManagerToken
487 for _, spec := range []struct {
492 {"", true, true}, // with no prefix
493 {hash[:3], true, false}, // with matching prefix
494 {"abcdef", false, false}, // with no such prefix
496 indexReader, err := kc.GetIndex(TestProxyUUID, spec.prefix)
497 c.Assert(err, Equals, nil)
498 indexResp, err := ioutil.ReadAll(indexReader)
499 c.Assert(err, Equals, nil)
500 locators := strings.Split(string(indexResp), "\n")
503 for _, locator := range locators {
507 c.Check(locator[:len(spec.prefix)], Equals, spec.prefix)
508 if locator[:32] == hash {
514 c.Check(gotTestHash == 2, Equals, spec.expectTestHash)
515 c.Check(gotOther > 0, Equals, spec.expectOther)
518 // GetIndex with invalid prefix
519 _, err = kc.GetIndex(TestProxyUUID, "xyz")
520 c.Assert((err != nil), Equals, true)
523 func (s *ServerRequiredSuite) TestCollectionSharingToken(c *C) {
524 kc := runProxy(c, false)
525 defer closeListener()
526 hash, _, err := kc.PutB([]byte("shareddata"))
528 kc.Arvados.ApiToken = arvadostest.FooCollectionSharingToken
529 rdr, _, _, err := kc.Get(hash)
531 data, err := ioutil.ReadAll(rdr)
533 c.Check(data, DeepEquals, []byte("shareddata"))
536 func (s *ServerRequiredSuite) TestPutAskGetInvalidToken(c *C) {
537 kc := runProxy(c, false)
538 defer closeListener()
541 hash, rep, err := kc.PutB([]byte("foo"))
543 c.Check(rep, Equals, 2)
545 for _, badToken := range []string{
547 "2ym314ysp27sk7h943q6vtc378srb06se3pq6ghurylyf3pdmx", // expired
549 kc.Arvados.ApiToken = badToken
551 // Ask and Get will fail only if the upstream
552 // keepstore server checks for valid signatures.
553 // Without knowing the blob signing key, there is no
554 // way for keepproxy to know whether a given token is
555 // permitted to read a block. So these tests fail:
557 _, _, err = kc.Ask(hash)
558 c.Assert(err, FitsTypeOf, &keepclient.ErrNotFound{})
559 c.Check(err.(*keepclient.ErrNotFound).Temporary(), Equals, false)
560 c.Check(err, ErrorMatches, ".*HTTP 403.*")
562 _, _, _, err = kc.Get(hash)
563 c.Assert(err, FitsTypeOf, &keepclient.ErrNotFound{})
564 c.Check(err.(*keepclient.ErrNotFound).Temporary(), Equals, false)
565 c.Check(err, ErrorMatches, ".*HTTP 403 \"Missing or invalid Authorization header\".*")
568 _, _, err = kc.PutB([]byte("foo"))
569 c.Check(err, ErrorMatches, ".*403.*Missing or invalid Authorization header")
573 func (s *ServerRequiredSuite) TestAskGetKeepProxyConnectionError(c *C) {
574 kc := runProxy(c, false)
575 defer closeListener()
577 // Point keepproxy at a non-existent keepstore
578 locals := map[string]string{
579 TestProxyUUID: "http://localhost:12345",
581 router.(*proxyHandler).KeepClient.SetServiceRoots(locals, nil, nil)
583 // Ask should result in temporary bad gateway error
584 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
585 _, _, err := kc.Ask(hash)
587 errNotFound, _ := err.(*keepclient.ErrNotFound)
588 c.Check(errNotFound.Temporary(), Equals, true)
589 c.Assert(err, ErrorMatches, ".*HTTP 502.*")
591 // Get should result in temporary bad gateway error
592 _, _, _, err = kc.Get(hash)
594 errNotFound, _ = err.(*keepclient.ErrNotFound)
595 c.Check(errNotFound.Temporary(), Equals, true)
596 c.Assert(err, ErrorMatches, ".*HTTP 502.*")
599 func (s *NoKeepServerSuite) TestAskGetNoKeepServerError(c *C) {
600 kc := runProxy(c, false)
601 defer closeListener()
603 hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
604 for _, f := range []func() error{
606 _, _, err := kc.Ask(hash)
610 _, _, _, err := kc.Get(hash)
615 c.Assert(err, NotNil)
616 errNotFound, _ := err.(*keepclient.ErrNotFound)
617 c.Check(errNotFound.Temporary(), Equals, true)
618 c.Check(err, ErrorMatches, `.*HTTP 502.*`)
622 func (s *ServerRequiredSuite) TestPing(c *C) {
623 kc := runProxy(c, false)
624 defer closeListener()
626 rtr := MakeRESTRouter(kc, 10*time.Second, arvadostest.ManagementToken)
628 req, err := http.NewRequest("GET",
629 "http://"+listener.Addr().String()+"/_health/ping",
632 req.Header.Set("Authorization", "Bearer "+arvadostest.ManagementToken)
634 resp := httptest.NewRecorder()
635 rtr.ServeHTTP(resp, req)
636 c.Check(resp.Code, Equals, 200)
637 c.Assert(resp.Body.String(), Matches, `{"health":"OK"}\n?`)