1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
20 "git.arvados.org/arvados.git/sdk/go/arvados"
21 "git.arvados.org/arvados.git/sdk/go/ctxlog"
23 "github.com/aws/aws-sdk-go-v2/aws"
24 "github.com/aws/aws-sdk-go-v2/service/s3"
25 "github.com/aws/aws-sdk-go-v2/service/s3/s3manager"
27 "github.com/johannesboyne/gofakes3"
28 "github.com/johannesboyne/gofakes3/backend/s3mem"
29 "github.com/prometheus/client_golang/prometheus"
30 "github.com/sirupsen/logrus"
31 check "gopkg.in/check.v1"
35 S3AWSTestBucketName = "testbucket"
38 type s3AWSFakeClock struct {
42 func (c *s3AWSFakeClock) Now() time.Time {
44 return time.Now().UTC()
49 func (c *s3AWSFakeClock) Since(t time.Time) time.Duration {
53 var _ = check.Suite(&StubbedS3AWSSuite{})
55 var srv httptest.Server
57 type StubbedS3AWSSuite struct {
58 s3server *httptest.Server
59 metadata *httptest.Server
60 cluster *arvados.Cluster
62 volumes []*TestableS3AWSVolume
65 func (s *StubbedS3AWSSuite) SetUpTest(c *check.C) {
68 s.cluster = testCluster(c)
69 s.cluster.Volumes = map[string]arvados.Volume{
70 "zzzzz-nyw5e-000000000000000": {Driver: "S3"},
71 "zzzzz-nyw5e-111111111111111": {Driver: "S3"},
73 s.handler = &handler{}
76 func (s *StubbedS3AWSSuite) TestGeneric(c *check.C) {
77 DoGenericVolumeTests(c, false, func(t TB, cluster *arvados.Cluster, volume arvados.Volume, logger logrus.FieldLogger, metrics *volumeMetricsVecs) TestableVolume {
78 // Use a negative raceWindow so s3test's 1-second
79 // timestamp precision doesn't confuse fixRace.
80 return s.newTestableVolume(c, cluster, volume, metrics, -2*time.Second)
84 func (s *StubbedS3AWSSuite) TestGenericReadOnly(c *check.C) {
85 DoGenericVolumeTests(c, true, func(t TB, cluster *arvados.Cluster, volume arvados.Volume, logger logrus.FieldLogger, metrics *volumeMetricsVecs) TestableVolume {
86 return s.newTestableVolume(c, cluster, volume, metrics, -2*time.Second)
90 func (s *StubbedS3AWSSuite) TestGenericWithPrefix(c *check.C) {
91 DoGenericVolumeTests(c, false, func(t TB, cluster *arvados.Cluster, volume arvados.Volume, logger logrus.FieldLogger, metrics *volumeMetricsVecs) TestableVolume {
92 v := s.newTestableVolume(c, cluster, volume, metrics, -2*time.Second)
98 func (s *StubbedS3AWSSuite) TestIndex(c *check.C) {
99 v := s.newTestableVolume(c, s.cluster, arvados.Volume{Replication: 2}, newVolumeMetricsVecs(prometheus.NewRegistry()), 0)
101 for i := 0; i < 256; i++ {
102 v.PutRaw(fmt.Sprintf("%02x%030x", i, i), []byte{102, 111, 111})
104 for _, spec := range []struct {
113 buf := new(bytes.Buffer)
114 err := v.IndexTo(spec.prefix, buf)
115 c.Check(err, check.IsNil)
117 idx := bytes.SplitAfter(buf.Bytes(), []byte{10})
118 c.Check(len(idx), check.Equals, spec.expectMatch+1)
119 c.Check(len(idx[len(idx)-1]), check.Equals, 0)
123 func (s *StubbedS3AWSSuite) TestSignature(c *check.C) {
124 var header http.Header
125 stub := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
130 // The aws-sdk-go-v2 driver only supports S3 V4 signatures. S3 v2 signatures are being phased out
131 // as of June 24, 2020. Cf. https://forums.aws.amazon.com/ann.jspa?annID=5816
133 S3VolumeDriverParameters: arvados.S3VolumeDriverParameters{
135 SecretAccessKey: "xxx",
137 Region: "test-region-1",
138 Bucket: "test-bucket-name",
141 logger: ctxlog.TestLogger(c),
142 metrics: newVolumeMetricsVecs(prometheus.NewRegistry()),
145 // Our test S3 server uses the older 'Path Style'
146 vol.bucket.svc.ForcePathStyle = true
148 c.Check(err, check.IsNil)
149 err = vol.Put(context.Background(), "acbd18db4cc2f85cedef654fccc4a4d8", []byte("foo"))
150 c.Check(err, check.IsNil)
151 c.Check(header.Get("Authorization"), check.Matches, `AWS4-HMAC-SHA256 .*`)
154 func (s *StubbedS3AWSSuite) TestIAMRoleCredentials(c *check.C) {
155 s.metadata = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
156 upd := time.Now().UTC().Add(-time.Hour).Format(time.RFC3339)
157 exp := time.Now().UTC().Add(time.Hour).Format(time.RFC3339)
158 // Literal example from
159 // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#instance-metadata-security-credentials
160 // but with updated timestamps
161 io.WriteString(w, `{"Code":"Success","LastUpdated":"`+upd+`","Type":"AWS-HMAC","AccessKeyId":"ASIAIOSFODNN7EXAMPLE","SecretAccessKey":"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY","Token":"token","Expiration":"`+exp+`"}`)
163 defer s.metadata.Close()
166 S3VolumeDriverParameters: arvados.S3VolumeDriverParameters{
167 IAMRole: s.metadata.URL + "/latest/api/token",
168 Endpoint: "http://localhost:12345",
169 Region: "test-region-1",
170 Bucket: "test-bucket-name",
173 logger: ctxlog.TestLogger(c),
174 metrics: newVolumeMetricsVecs(prometheus.NewRegistry()),
176 err := v.check(s.metadata.URL + "/latest")
177 c.Check(err, check.IsNil)
178 creds, err := v.bucket.svc.Client.Config.Credentials.Retrieve(context.Background())
179 c.Check(err, check.IsNil)
180 c.Check(creds.AccessKeyID, check.Equals, "ASIAIOSFODNN7EXAMPLE")
181 c.Check(creds.SecretAccessKey, check.Equals, "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY")
183 s.metadata = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
184 w.WriteHeader(http.StatusNotFound)
186 deadv := &S3AWSVolume{
187 S3VolumeDriverParameters: arvados.S3VolumeDriverParameters{
188 IAMRole: s.metadata.URL + "/fake-metadata/test-role",
189 Endpoint: "http://localhost:12345",
190 Region: "test-region-1",
191 Bucket: "test-bucket-name",
194 logger: ctxlog.TestLogger(c),
195 metrics: newVolumeMetricsVecs(prometheus.NewRegistry()),
197 err = deadv.check(s.metadata.URL + "/latest")
198 c.Check(err, check.IsNil)
199 _, err = deadv.bucket.svc.Client.Config.Credentials.Retrieve(context.Background())
200 c.Check(err, check.ErrorMatches, `(?s).*EC2RoleRequestError: no EC2 instance role found.*`)
201 c.Check(err, check.ErrorMatches, `(?s).*404.*`)
204 func (s *StubbedS3AWSSuite) TestStats(c *check.C) {
205 v := s.newTestableVolume(c, s.cluster, arvados.Volume{Replication: 2}, newVolumeMetricsVecs(prometheus.NewRegistry()), 5*time.Minute)
206 stats := func() string {
207 buf, err := json.Marshal(v.InternalStats())
208 c.Check(err, check.IsNil)
212 c.Check(stats(), check.Matches, `.*"Ops":0,.*`)
214 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
215 _, err := v.Get(context.Background(), loc, make([]byte, 3))
216 c.Check(err, check.NotNil)
217 c.Check(stats(), check.Matches, `.*"Ops":[^0],.*`)
218 c.Check(stats(), check.Matches, `.*"s3.requestFailure 404 NoSuchKey[^"]*":[^0].*`)
219 c.Check(stats(), check.Matches, `.*"InBytes":0,.*`)
221 err = v.Put(context.Background(), loc, []byte("foo"))
222 c.Check(err, check.IsNil)
223 c.Check(stats(), check.Matches, `.*"OutBytes":3,.*`)
224 c.Check(stats(), check.Matches, `.*"PutOps":2,.*`)
226 _, err = v.Get(context.Background(), loc, make([]byte, 3))
227 c.Check(err, check.IsNil)
228 _, err = v.Get(context.Background(), loc, make([]byte, 3))
229 c.Check(err, check.IsNil)
230 c.Check(stats(), check.Matches, `.*"InBytes":6,.*`)
233 type s3AWSBlockingHandler struct {
234 requested chan *http.Request
235 unblock chan struct{}
238 func (h *s3AWSBlockingHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
239 if r.Method == "PUT" && !strings.Contains(strings.Trim(r.URL.Path, "/"), "/") {
240 // Accept PutBucket ("PUT /bucketname/"), called by
244 if h.requested != nil {
247 if h.unblock != nil {
250 http.Error(w, "nothing here", http.StatusNotFound)
253 func (s *StubbedS3AWSSuite) TestGetContextCancel(c *check.C) {
254 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
255 buf := make([]byte, 3)
257 s.testContextCancel(c, func(ctx context.Context, v *TestableS3AWSVolume) error {
258 _, err := v.Get(ctx, loc, buf)
263 func (s *StubbedS3AWSSuite) TestCompareContextCancel(c *check.C) {
264 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
267 s.testContextCancel(c, func(ctx context.Context, v *TestableS3AWSVolume) error {
268 return v.Compare(ctx, loc, buf)
272 func (s *StubbedS3AWSSuite) TestPutContextCancel(c *check.C) {
273 loc := "acbd18db4cc2f85cedef654fccc4a4d8"
276 s.testContextCancel(c, func(ctx context.Context, v *TestableS3AWSVolume) error {
277 return v.Put(ctx, loc, buf)
281 func (s *StubbedS3AWSSuite) testContextCancel(c *check.C, testFunc func(context.Context, *TestableS3AWSVolume) error) {
282 handler := &s3AWSBlockingHandler{}
283 s.s3server = httptest.NewServer(handler)
284 defer s.s3server.Close()
286 v := s.newTestableVolume(c, s.cluster, arvados.Volume{Replication: 2}, newVolumeMetricsVecs(prometheus.NewRegistry()), 5*time.Minute)
288 ctx, cancel := context.WithCancel(context.Background())
290 handler.requested = make(chan *http.Request)
291 handler.unblock = make(chan struct{})
292 defer close(handler.unblock)
294 doneFunc := make(chan struct{})
296 err := testFunc(ctx, v)
297 c.Check(err, check.Equals, context.Canceled)
301 timeout := time.After(10 * time.Second)
303 // Wait for the stub server to receive a request, meaning
304 // Get() is waiting for an s3 operation.
307 c.Fatal("timed out waiting for test func to call our handler")
309 c.Fatal("test func finished without even calling our handler!")
310 case <-handler.requested:
322 func (s *StubbedS3AWSSuite) TestBackendStates(c *check.C) {
323 s.cluster.Collections.BlobTrashLifetime.Set("1h")
324 s.cluster.Collections.BlobSigningTTL.Set("1h")
326 v := s.newTestableVolume(c, s.cluster, arvados.Volume{Replication: 2}, newVolumeMetricsVecs(prometheus.NewRegistry()), 5*time.Minute)
329 putS3Obj := func(t time.Time, key string, data []byte) {
333 v.serverClock.now = &t
334 uploader := s3manager.NewUploaderWithClient(v.bucket.svc)
335 _, err := uploader.UploadWithContext(context.Background(), &s3manager.UploadInput{
336 Bucket: aws.String(v.bucket.bucket),
337 Key: aws.String(key),
338 Body: bytes.NewReader(data),
343 v.serverClock.now = nil
352 for _, scenario := range []struct {
359 canGetAfterTrash bool
361 haveTrashAfterEmpty bool
365 "No related objects",
367 false, false, false, false, false, false,
370 // Stored by older version, or there was a
371 // race between EmptyTrash and Put: Trash is a
372 // no-op even though the data object is very
375 t0.Add(-48 * time.Hour), none, none,
376 true, true, true, false, false, false,
379 "Not trash, but old enough to be eligible for trash",
380 t0.Add(-24 * time.Hour), t0.Add(-2 * time.Hour), none,
381 true, true, false, false, false, false,
384 "Not trash, and not old enough to be eligible for trash",
385 t0.Add(-24 * time.Hour), t0.Add(-30 * time.Minute), none,
386 true, true, true, false, false, false,
389 "Trashed + untrashed copies exist, due to recent race between Trash and Put",
390 t0.Add(-24 * time.Hour), t0.Add(-3 * time.Minute), t0.Add(-2 * time.Minute),
391 true, true, true, true, true, false,
394 "Trashed + untrashed copies exist, trash nearly eligible for deletion: prone to Trash race",
395 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-59 * time.Minute),
396 true, false, true, true, true, false,
399 "Trashed + untrashed copies exist, trash is eligible for deletion: prone to Trash race",
400 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-61 * time.Minute),
401 true, false, true, true, false, false,
404 "Trashed + untrashed copies exist, due to old race between Put and unfinished Trash: emptying trash is unsafe",
405 t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour), t0.Add(-12 * time.Hour),
406 true, false, true, true, true, true,
409 "Trashed + untrashed copies exist, used to be unsafe to empty, but since made safe by fixRace+Touch",
410 t0.Add(-time.Second), t0.Add(-time.Second), t0.Add(-12 * time.Hour),
411 true, true, true, true, false, false,
414 "Trashed + untrashed copies exist because Trash operation was interrupted (no race)",
415 t0.Add(-24 * time.Hour), t0.Add(-24 * time.Hour), t0.Add(-12 * time.Hour),
416 true, false, true, true, false, false,
419 "Trash, not yet eligible for deletion",
420 none, t0.Add(-12 * time.Hour), t0.Add(-time.Minute),
421 false, false, false, true, true, false,
424 "Trash, not yet eligible for deletion, prone to races",
425 none, t0.Add(-12 * time.Hour), t0.Add(-59 * time.Minute),
426 false, false, false, true, true, false,
429 "Trash, eligible for deletion",
430 none, t0.Add(-12 * time.Hour), t0.Add(-2 * time.Hour),
431 false, false, false, true, false, false,
434 "Erroneously trashed during a race, detected before BlobTrashLifetime",
435 none, t0.Add(-30 * time.Minute), t0.Add(-29 * time.Minute),
436 true, false, true, true, true, false,
439 "Erroneously trashed during a race, rescue during EmptyTrash despite reaching BlobTrashLifetime",
440 none, t0.Add(-90 * time.Minute), t0.Add(-89 * time.Minute),
441 true, false, true, true, true, false,
444 "Trashed copy exists with no recent/* marker (cause unknown); repair by untrashing",
445 none, none, t0.Add(-time.Minute),
446 false, false, false, true, true, true,
449 for _, prefixLength := range []int{0, 3} {
450 v.PrefixLength = prefixLength
451 c.Logf("Scenario: %q (prefixLength=%d)", scenario.label, prefixLength)
453 // We have a few tests to run for each scenario, and
454 // the tests are expected to change state. By calling
455 // this setup func between tests, we (re)create the
456 // scenario as specified, using a new unique block
457 // locator to prevent interference from previous
460 setupScenario := func() (string, []byte) {
462 blk := []byte(fmt.Sprintf("%d", nextKey))
463 loc := fmt.Sprintf("%x", md5.Sum(blk))
465 if prefixLength > 0 {
466 key = loc[:prefixLength] + "/" + loc
468 c.Log("\t", loc, "\t", key)
469 putS3Obj(scenario.dataT, key, blk)
470 putS3Obj(scenario.recentT, "recent/"+key, nil)
471 putS3Obj(scenario.trashT, "trash/"+key, blk)
472 v.serverClock.now = &t0
477 loc, blk := setupScenario()
478 buf := make([]byte, len(blk))
479 _, err := v.Get(context.Background(), loc, buf)
480 c.Check(err == nil, check.Equals, scenario.canGet)
482 c.Check(os.IsNotExist(err), check.Equals, true)
485 // Call Trash, then check canTrash and canGetAfterTrash
486 loc, _ = setupScenario()
488 c.Check(err == nil, check.Equals, scenario.canTrash)
489 _, err = v.Get(context.Background(), loc, buf)
490 c.Check(err == nil, check.Equals, scenario.canGetAfterTrash)
492 c.Check(os.IsNotExist(err), check.Equals, true)
495 // Call Untrash, then check canUntrash
496 loc, _ = setupScenario()
498 c.Check(err == nil, check.Equals, scenario.canUntrash)
499 if scenario.dataT != none || scenario.trashT != none {
500 // In all scenarios where the data exists, we
501 // should be able to Get after Untrash --
502 // regardless of timestamps, errors, race
504 _, err = v.Get(context.Background(), loc, buf)
505 c.Check(err, check.IsNil)
508 // Call EmptyTrash, then check haveTrashAfterEmpty and
510 loc, _ = setupScenario()
512 _, err = v.head("trash/" + v.key(loc))
513 c.Check(err == nil, check.Equals, scenario.haveTrashAfterEmpty)
514 if scenario.freshAfterEmpty {
515 t, err := v.Mtime(loc)
516 c.Check(err, check.IsNil)
517 // new mtime must be current (with an
518 // allowance for 1s timestamp precision)
519 c.Check(t.After(t0.Add(-time.Second)), check.Equals, true)
522 // Check for current Mtime after Put (applies to all
524 loc, blk = setupScenario()
525 err = v.Put(context.Background(), loc, blk)
526 c.Check(err, check.IsNil)
527 t, err := v.Mtime(loc)
528 c.Check(err, check.IsNil)
529 c.Check(t.After(t0.Add(-time.Second)), check.Equals, true)
534 type TestableS3AWSVolume struct {
536 server *httptest.Server
538 serverClock *s3AWSFakeClock
541 type LogrusLog struct {
542 log *logrus.FieldLogger
545 func (l LogrusLog) Print(level gofakes3.LogLevel, v ...interface{}) {
547 case gofakes3.LogErr:
548 (*l.log).Errorln(v...)
549 case gofakes3.LogWarn:
550 (*l.log).Warnln(v...)
551 case gofakes3.LogInfo:
552 (*l.log).Infoln(v...)
554 panic("unknown level")
558 func (s *StubbedS3AWSSuite) newTestableVolume(c *check.C, cluster *arvados.Cluster, volume arvados.Volume, metrics *volumeMetricsVecs, raceWindow time.Duration) *TestableS3AWSVolume {
560 clock := &s3AWSFakeClock{}
562 backend := s3mem.New(s3mem.WithTimeSource(clock))
564 // To enable GoFakeS3 debug logging, pass logger to gofakes3.WithLogger()
565 /* logger := new(LogrusLog)
566 ctxLogger := ctxlog.FromContext(context.Background())
567 logger.log = &ctxLogger */
568 faker := gofakes3.New(backend, gofakes3.WithTimeSource(clock), gofakes3.WithLogger(nil), gofakes3.WithTimeSkewLimit(0))
569 srv := httptest.NewServer(faker.Server())
572 if s.s3server != nil {
573 endpoint = s.s3server.URL
576 iamRole, accessKey, secretKey := "", "xxx", "xxx"
577 if s.metadata != nil {
578 iamRole, accessKey, secretKey = s.metadata.URL+"/fake-metadata/test-role", "", ""
581 v := &TestableS3AWSVolume{
582 S3AWSVolume: &S3AWSVolume{
583 S3VolumeDriverParameters: arvados.S3VolumeDriverParameters{
585 AccessKeyID: accessKey,
586 SecretAccessKey: secretKey,
587 Bucket: S3AWSTestBucketName,
589 Region: "test-region-1",
590 LocationConstraint: true,
596 logger: ctxlog.TestLogger(c),
603 c.Assert(v.S3AWSVolume.check(""), check.IsNil)
604 // Our test S3 server uses the older 'Path Style'
605 v.S3AWSVolume.bucket.svc.ForcePathStyle = true
606 // Create the testbucket
607 input := &s3.CreateBucketInput{
608 Bucket: aws.String(S3AWSTestBucketName),
610 req := v.S3AWSVolume.bucket.svc.CreateBucketRequest(input)
611 _, err := req.Send(context.Background())
612 c.Assert(err, check.IsNil)
613 // We couldn't set RaceWindow until now because check()
614 // rejects negative values.
615 v.S3AWSVolume.RaceWindow = arvados.Duration(raceWindow)
619 // PutRaw skips the ContentMD5 test
620 func (v *TestableS3AWSVolume) PutRaw(loc string, block []byte) {
622 r := NewCountingReader(bytes.NewReader(block), v.bucket.stats.TickOutBytes)
624 uploader := s3manager.NewUploaderWithClient(v.bucket.svc, func(u *s3manager.Uploader) {
625 u.PartSize = 5 * 1024 * 1024
629 _, err := uploader.Upload(&s3manager.UploadInput{
630 Bucket: aws.String(v.bucket.bucket),
631 Key: aws.String(key),
635 v.logger.Printf("PutRaw: %s: %+v", key, err)
638 empty := bytes.NewReader([]byte{})
639 _, err = uploader.Upload(&s3manager.UploadInput{
640 Bucket: aws.String(v.bucket.bucket),
641 Key: aws.String("recent/" + key),
645 v.logger.Printf("PutRaw: recent/%s: %+v", key, err)
649 // TouchWithDate turns back the clock while doing a Touch(). We assume
650 // there are no other operations happening on the same s3test server
652 func (v *TestableS3AWSVolume) TouchWithDate(loc string, lastPut time.Time) {
653 v.serverClock.now = &lastPut
655 uploader := s3manager.NewUploaderWithClient(v.bucket.svc)
656 empty := bytes.NewReader([]byte{})
657 _, err := uploader.UploadWithContext(context.Background(), &s3manager.UploadInput{
658 Bucket: aws.String(v.bucket.bucket),
659 Key: aws.String("recent/" + v.key(loc)),
666 v.serverClock.now = nil
669 func (v *TestableS3AWSVolume) Teardown() {
673 func (v *TestableS3AWSVolume) ReadWriteOperationLabelValues() (r, w string) {