fix: inspect fleet buckets before create; keep delivered-key precedence
Task 4 review round 1. P1-1: preparation no longer calls CreateBucket to discover a bucket. In us-east-1, re-creating an owned bucket succeeds and resets its ACLs, so an untagged or mis-tagged bucket could be changed before refusal. The location and tags are now read first with ExpectedBucketOwner. Only NoSuchBucket allows one CreateBucket, sent with its tags and no SDK retry. A lost reply is recovered by the next pass's identity reads, through exact tags only. P2-1: rotation scans the whole key inventory for live delivered keys before judging keep, so an inactive or missing keep can no longer hide ErrDeliveredKeyStillLive (the console's needs-operator signal). P2-2: the fleet-data policy drops ListBucketMultipartUploads. S3 has no s3:prefix key for it and nothing lists uploads. Part listing and abort on the data prefixes stay. The fixture and structural tests changed with it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+209
−69
internal/awsmanaged/credentials.go+11 −9internal/awsmanaged/credentials_test.go+31 −0internal/awsmanaged/policy.go+4 −3internal/awsmanaged/policy_test.go+20 −10internal/awsmanaged/storage.go+50 −21internal/awsmanaged/storage_test.go+93 −15internal/awsmanaged/testdata/fleet-data-policy.json+0 −11
diff --git a/internal/awsmanaged/credentials.go b/internal/awsmanaged/credentials.go| index 76742985d..ed3d5fb61 100644 |
| --- a/internal/awsmanaged/credentials.go |
| +++ b/internal/awsmanaged/credentials.go |
| @@ -5,6 +5,7 @@ import ( |
| "fmt" |
| "net/url" |
| "regexp" |
| + "slices" |
| "time" |
| "github.com/aws/aws-sdk-go-v2/aws" |
| @@ -283,20 +284,21 @@ func (s *Storage) RotateFleetCredentials(ctx context.Context, fleet, keep string |
| if err != nil { |
| return fleetstore.Credentials{}, err |
| } |
| - keptActive := false |
| + // The whole inventory is scanned for protected keys first, so no other |
| + // refusal can hide the one the console turns into needs-operator. |
| for _, k := range keys { |
| if protected[k.id] { |
| return fleetstore.Credentials{}, fmt.Errorf("awsmanaged: %w", fleetstore.ErrDeliveredKeyStillLive) |
| } |
| - if k.id == keep { |
| - keptActive = k.active |
| - if !k.active { |
| - return fleetstore.Credentials{}, drift("key to keep is not active") |
| - } |
| - } |
| } |
| - if keep != "" && !keptActive { |
| - return fleetstore.Credentials{}, drift("key to keep does not exist") |
| + if keep != "" { |
| + i := slices.IndexFunc(keys, func(k accessKey) bool { return k.id == keep }) |
| + if i < 0 { |
| + return fleetstore.Credentials{}, drift("key to keep does not exist") |
| + } |
| + if !keys[i].active { |
| + return fleetstore.Credentials{}, drift("key to keep is not active") |
| + } |
| } |
| for _, k := range keys { |
| if k.id == keep { |
diff --git a/internal/awsmanaged/credentials_test.go b/internal/awsmanaged/credentials_test.go| index fba8127f4..4c103ccc3 100644 |
| --- a/internal/awsmanaged/credentials_test.go |
| +++ b/internal/awsmanaged/credentials_test.go |
| @@ -484,6 +484,37 @@ func TestAWSFleetCredentialsDoubleLostRotation(t *testing.T) { |
| t.Fatal("mutated keys") |
| } |
| }) |
| + // An unusable keep must not hide a live delivered key: only the |
| + // sentinel makes the console record needs-operator. Pages hold one key, |
| + // so the first order puts the delivered key on page 2. |
| + for _, order := range []string{"inactive keep first", "delivered first"} { |
| + t.Run(order+": protected key wins over an inactive keep", func(t *testing.T) { |
| + s, user := preparedStorage(t) |
| + var keep, other string |
| + if order == "inactive keep first" { |
| + keep = s.iam.addKey(user, iamtypes.StatusTypeInactive) |
| + other = s.iam.addKey(user, iamtypes.StatusTypeActive) |
| + } else { |
| + other = s.iam.addKey(user, iamtypes.StatusTypeActive) |
| + keep = s.iam.addKey(user, iamtypes.StatusTypeInactive) |
| + } |
| + _, err := s.RotateFleetCredentials(ctx, testFleet, keep, []string{other, keep}) |
| + if !errors.Is(err, fleetstore.ErrDeliveredKeyStillLive) { |
| + t.Fatalf("got %v", err) |
| + } |
| + if s.iam.keyMutations() != 0 { |
| + t.Fatal("mutated keys") |
| + } |
| + }) |
| + } |
| + t.Run("protected key wins over a missing keep", func(t *testing.T) { |
| + s, user := preparedStorage(t) |
| + s.iam.addKey(user, iamtypes.StatusTypeActive) // undelivered |
| + other := s.iam.addKey(user, iamtypes.StatusTypeActive) |
| + if _, err := s.RotateFleetCredentials(ctx, testFleet, "AKIA9999999999999999", []string{other, "AKIA9999999999999999"}); !errors.Is(err, fleetstore.ErrDeliveredKeyStillLive) { |
| + t.Fatalf("got %v", err) |
| + } |
| + }) |
| t.Run("delivered key gone is no obstacle", func(t *testing.T) { |
| s, user := preparedStorage(t) |
| k1 := rotate(t, s, "").ID |
diff --git a/internal/awsmanaged/policy.go b/internal/awsmanaged/policy.go| index 7b894bf26..eb846f05f 100644 |
| --- a/internal/awsmanaged/policy.go |
| +++ b/internal/awsmanaged/policy.go |
| @@ -72,7 +72,10 @@ func (s *stringSet) UnmarshalJSON(b []byte) error { |
| // a missing key answers 404 rather than 403. The secrets-key object may be |
| // created only by a request carrying If-None-Match: *, so the first writer |
| // wins and nothing overwrites it. No DeleteObjectVersion: lease release |
| -// deletes a bare key, and destructive version paths stay refused. The final |
| +// deletes a bare key, and destructive version paths stay refused. No |
| +// ListBucketMultipartUploads either: S3 has no prefix condition key for it, |
| +// so it could only be granted bucket-wide, and nothing in the platform lists |
| +// uploads (a box aborts and lists the parts of its own uploads). The final |
| // Deny keeps every other bucket out of reach even if some resource policy |
| // elsewhere would allow it. |
| func fleetDataPolicy(bucketARN string) (string, error) { |
| @@ -91,8 +94,6 @@ func fleetDataPolicy(bucketARN string) (string, error) { |
| {Sid: "Bucket", Effect: "Allow", Action: stringSet{"s3:GetBucketLocation", "s3:ListBucket"}, Resource: stringSet{bucketARN}}, |
| {Sid: "Versions", Effect: "Allow", Action: stringSet{"s3:ListBucketVersions"}, Resource: stringSet{bucketARN}, |
| Condition: map[string]map[string]stringSet{"StringLike": {"s3:prefix": versionPrefixes}}}, |
| - {Sid: "Uploads", Effect: "Allow", Action: stringSet{"s3:ListBucketMultipartUploads"}, Resource: stringSet{bucketARN}, |
| - Condition: map[string]map[string]stringSet{"StringLike": {"s3:prefix": append(stringSet{}, fleetDataPrefixes...)}}}, |
| {Sid: "Data", Effect: "Allow", Action: stringSet{ |
| "s3:GetObject", "s3:PutObject", "s3:DeleteObject", "s3:GetObjectVersion", |
| "s3:AbortMultipartUpload", "s3:ListMultipartUploadParts", |
diff --git a/internal/awsmanaged/policy_test.go b/internal/awsmanaged/policy_test.go| index c27df3f65..6992ddd9b 100644 |
| --- a/internal/awsmanaged/policy_test.go |
| +++ b/internal/awsmanaged/policy_test.go |
| @@ -152,14 +152,14 @@ func TestAWSFleetDataPolicyIsolation(t *testing.T) { |
| if l := byAction("s3:ListBucket"); len(l) != 1 || l[0].Condition != nil { |
| t.Errorf("ListBucket must be unconditioned on the exact bucket: %+v", l) |
| } |
| - for a, want := range map[string]stringSet{ |
| - "s3:ListBucketVersions": {"_carlos/probes/*", "instances/*", "move-snapshots/*", "moves/*", "system/fleet-secrets-key.json"}, |
| - "s3:ListBucketMultipartUploads": {"instances/*", "move-snapshots/*", "moves/*"}, |
| - } { |
| - l := byAction(a) |
| - if len(l) != 1 || len(l[0].Condition) != 1 || !reflectEqual(l[0].Condition["StringLike"]["s3:prefix"], want) { |
| - t.Errorf("%s prefix condition %+v", a, l) |
| - } |
| + if l := byAction("s3:ListBucketVersions"); len(l) != 1 || len(l[0].Condition) != 1 || |
| + !reflectEqual(l[0].Condition["StringLike"]["s3:prefix"], stringSet{"_carlos/probes/*", "instances/*", "move-snapshots/*", "moves/*", "system/fleet-secrets-key.json"}) { |
| + t.Errorf("ListBucketVersions prefix condition %+v", l) |
| + } |
| + // S3 has no s3:prefix key for this action, so any grant would be |
| + // bucket-wide; nothing lists uploads, so there is none. |
| + if l := byAction("s3:ListBucketMultipartUploads"); len(l) != 0 { |
| + t.Errorf("multipart upload listing granted: %+v", l) |
| } |
| key := objects + fleetSecretsKeyObject |
| puts := 0 |
| @@ -195,7 +195,9 @@ func TestAWSFleetDataPolicyIsolation(t *testing.T) { |
| {"s3:ListBucket", bucket, nil, true}, |
| {"s3:ListBucketVersions", bucket, map[string]string{"s3:prefix": "instances/h1/"}, true}, |
| {"s3:ListBucketVersions", bucket, map[string]string{"s3:prefix": ""}, false}, |
| - {"s3:ListBucketMultipartUploads", bucket, map[string]string{"s3:prefix": "_carlos/probes/"}, false}, |
| + {"s3:ListBucketMultipartUploads", bucket, nil, false}, |
| + {"s3:ListBucketMultipartUploads", bucket, map[string]string{"s3:prefix": "instances/h1/"}, false}, |
| + {"s3:ListMultipartUploadParts", objects + "instances/h1/big", nil, true}, |
| {"s3:GetObject", objects + "_carlos/probes/p1", nil, true}, |
| {"s3:DeleteObject", objects + "_carlos/probes/p1", nil, true}, |
| // The secrets key: read, first write only, never delete. |
| @@ -377,7 +379,8 @@ func TestAWSFleetStorageConfig(t *testing.T) { |
| // TestAWSFleetTransport is the serialization witness: real SDK clients |
| // loaded through rootaws, every byte through a mock transport, metadata off |
| -// in process. It checks the us-east-1 create carries Tags and no location, |
| +// in process. It checks the us-east-1 create follows a NoSuchBucket read, |
| +// is sent once and carries Tags and no location, |
| // ExpectedBucketOwner reaches the wire, and CreateAccessKey is sent once. |
| func TestAWSFleetTransport(t *testing.T) { |
| none := filepath.Join(t.TempDir(), "none") |
| @@ -393,6 +396,7 @@ func TestAWSFleetTransport(t *testing.T) { |
| var mu sync.Mutex |
| counts := map[string]int{} |
| var createBody string |
| + created := false |
| ok := func(body string) *http.Response { |
| return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": []string{"text/xml"}}, Body: io.NopCloser(strings.NewReader(body))} |
| } |
| @@ -425,7 +429,10 @@ func TestAWSFleetTransport(t *testing.T) { |
| switch { |
| case r.Method == http.MethodPut && r.URL.RawQuery == "": |
| createBody = string(body) |
| + created = true |
| return ok(""), nil |
| + case q.Has("location") && !created: |
| + return s3XMLError(404, "NoSuchBucket"), nil |
| case q.Has("location"): |
| return ok(`<LocationConstraint xmlns="http://s3.amazonaws.com/doc/2006-03-01/"></LocationConstraint>`), nil |
| case q.Has("tagging"): |
| @@ -480,6 +487,9 @@ func TestAWSFleetTransport(t *testing.T) { |
| if err != nil || got.Bucket != bucket { |
| t.Fatalf("%+v %v", got, err) |
| } |
| + if counts["PUT "] != 1 || counts["GET location="] != 2 { |
| + t.Errorf("create sent %d times after %d location reads; want one create between two reads", counts["PUT "], counts["GET location="]) |
| + } |
| if strings.Contains(createBody, "LocationConstraint") || !strings.Contains(createBody, "<Key>carlos:deployment</Key><Value>"+testDeployment+"</Value>") || |
| !strings.Contains(createBody, "<Key>carlos:fleet</Key><Value>"+testFleet+"</Value>") { |
| t.Errorf("us-east-1 create body %s", createBody) |
diff --git a/internal/awsmanaged/storage.go b/internal/awsmanaged/storage.go| index 8cf4cc756..fee899674 100644 |
| --- a/internal/awsmanaged/storage.go |
| +++ b/internal/awsmanaged/storage.go |
| @@ -211,29 +211,27 @@ func (s *Storage) EnsureFleetStorage(ctx context.Context, fleet, region string) |
| return fleetstore.FleetStorage{Bucket: bucket, Region: region}, nil |
| } |
| -// ensureBucket creates the bucket with its identity tags in the one |
| -// request, then proves it is this account's, in region, carrying exactly |
| -// this deployment's and fleet's tags before it configures anything. A lost |
| -// creation reply is recovered through those tags; an untagged or foreign |
| -// bucket is never relabelled or adopted. |
| +// ensureBucket proves the bucket is this account's, in region, carrying |
| +// exactly this deployment's and fleet's tags, before anything can change it. |
| +// CreateBucket is sent only after S3 has answered NoSuchBucket, and only |
| +// once: in us-east-1 a create of a bucket the account already owns succeeds |
| +// and resets its ACLs, so it is never a way to find out whether a bucket |
| +// exists. A lost creation reply is recovered by the next attempt's identity |
| +// reads, through the tags the create carried. An untagged or foreign bucket |
| +// is never relabelled or adopted. |
| func (s *Storage) ensureBucket(ctx context.Context, api s3API, bucket, fleet, region string) error { |
| - conf := &s3types.CreateBucketConfiguration{Tags: bucketTags(s.cfg.Deployment, fleet)} |
| - if region != "us-east-1" { |
| - conf.LocationConstraint = s3types.BucketLocationConstraint(region) |
| - } |
| - if _, err := api.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String(bucket), CreateBucketConfiguration: conf}); err != nil { |
| - switch apiCode(err) { |
| - case "BucketAlreadyOwnedByYou": |
| - // Ours by name only: the checks below decide. |
| - case "BucketAlreadyExists": |
| - return drift("bucket name belongs to another account") |
| - case "AccessDenied": |
| - return drift("bucket creation was refused") |
| - default: |
| - return uncertain(ctx, "create bucket") |
| + err := s.checkBucketIdentity(ctx, api, bucket, fleet, region) |
| + if errors.Is(err, errBucketAbsent) { |
| + err = s.createBucket(ctx, api, bucket, fleet, region) |
| + if err == nil { |
| + err = s.checkBucketIdentity(ctx, api, bucket, fleet, region) |
| + } |
| + if errors.Is(err, errBucketAbsent) { |
| + // Just created and not yet visible: the next pass reads again. |
| + return uncertain(ctx, "new bucket not yet visible") |
| } |
| } |
| - if err := s.checkBucketIdentity(ctx, api, bucket, fleet, region); err != nil { |
| + if err != nil { |
| return err |
| } |
| for _, step := range []func(context.Context, s3API, string) error{s.ensureVersioning, s.ensureEncryption, s.ensurePublicBlock, s.ensureLifecycle} { |
| @@ -244,12 +242,43 @@ func (s *Storage) ensureBucket(ctx context.Context, api s3API, bucket, fleet, re |
| return nil |
| } |
| +// errBucketAbsent is S3's NoSuchBucket for the bucket's location: the only |
| +// answer that lets preparation create it. |
| +var errBucketAbsent = errors.New("bucket does not exist") |
| + |
| +// createBucket sends one CreateBucket carrying the identity tags (kept in |
| +// us-east-1, where the location is omitted). The SDK does not retry it. |
| +func (s *Storage) createBucket(ctx context.Context, api s3API, bucket, fleet, region string) error { |
| + conf := &s3types.CreateBucketConfiguration{Tags: bucketTags(s.cfg.Deployment, fleet)} |
| + if region != "us-east-1" { |
| + conf.LocationConstraint = s3types.BucketLocationConstraint(region) |
| + } |
| + _, err := api.CreateBucket(ctx, &awss3.CreateBucketInput{Bucket: aws.String(bucket), CreateBucketConfiguration: conf}, |
| + func(o *awss3.Options) { o.RetryMaxAttempts = 1 }) |
| + switch code := apiCode(err); { |
| + case err == nil, code == "BucketAlreadyOwnedByYou": |
| + // Created now, or by a racing pass of ours: the identity reads decide. |
| + return nil |
| + case code == "BucketAlreadyExists": |
| + return drift("bucket name belongs to another account") |
| + case code == "AccessDenied": |
| + return drift("bucket creation was refused") |
| + } |
| + return uncertain(ctx, "create bucket") |
| +} |
| + |
| func (s *Storage) owner() *string { return aws.String(s.cfg.AccountID) } |
| +// checkBucketIdentity reads the bucket's location and tags with |
| +// ExpectedBucketOwner. Only NoSuchBucket is absence; another account's |
| +// bucket is AccessDenied, and every other failure proves nothing. |
| func (s *Storage) checkBucketIdentity(ctx context.Context, api s3API, bucket, fleet, region string) error { |
| loc, err := api.GetBucketLocation(ctx, &awss3.GetBucketLocationInput{Bucket: aws.String(bucket), ExpectedBucketOwner: s.owner()}) |
| if err != nil { |
| - if apiCode(err) == "AccessDenied" { |
| + switch apiCode(err) { |
| + case "NoSuchBucket": |
| + return errBucketAbsent |
| + case "AccessDenied": |
| return drift("bucket is not this account's") |
| } |
| return uncertain(ctx, "bucket location") |
diff --git a/internal/awsmanaged/storage_test.go b/internal/awsmanaged/storage_test.go| index 88eb5ec05..85d9c1108 100644 |
| --- a/internal/awsmanaged/storage_test.go |
| +++ b/internal/awsmanaged/storage_test.go |
| @@ -40,6 +40,9 @@ type fakeBucket struct { |
| enc *s3types.ServerSideEncryptionConfiguration |
| pab *s3types.PublicAccessBlockConfiguration |
| rules []s3types.LifecycleRule |
| + // aclResets counts CreateBucket calls that hit this existing bucket in |
| + // us-east-1, where S3 answers success and resets the bucket's ACLs. |
| + aclResets int |
| } |
| // fakeS3World is S3's global bucket namespace; fakeS3 is one region's |
| @@ -56,6 +59,8 @@ type fakeS3World struct { |
| legacyEU bool |
| creates []*awss3.CreateBucketInput |
| regions []string // region of each CreateBucket call |
| + attempts []int // RetryMaxAttempts of each CreateBucket call |
| + dropTags bool // a create takes effect without its tags |
| } |
| type fakeS3 struct { |
| @@ -124,11 +129,16 @@ func (w *fakeS3World) owned(name, owner *string) (*fakeBucket, error) { |
| return b, nil |
| } |
| -func (f *fakeS3) CreateBucket(ctx context.Context, in *awss3.CreateBucketInput, _ ...func(*awss3.Options)) (*awss3.CreateBucketOutput, error) { |
| +func (f *fakeS3) CreateBucket(ctx context.Context, in *awss3.CreateBucketInput, optFns ...func(*awss3.Options)) (*awss3.CreateBucketOutput, error) { |
| w := f.w |
| + var o awss3.Options |
| + for _, fn := range optFns { |
| + fn(&o) |
| + } |
| err := w.call("CreateBucket", func() error { |
| w.creates = append(w.creates, in) |
| w.regions = append(w.regions, f.region) |
| + w.attempts = append(w.attempts, o.RetryMaxAttempts) |
| var lc string |
| var tags []s3types.Tag |
| if c := in.CreateBucketConfiguration; c != nil { |
| @@ -146,12 +156,13 @@ func (f *fakeS3) CreateBucket(ctx context.Context, in *awss3.CreateBucketInput, |
| return apiErr("BucketAlreadyExists") |
| } |
| if b.region == "us-east-1" && f.region == "us-east-1" { |
| + b.aclResets++ |
| return nil |
| } |
| return apiErr("BucketAlreadyOwnedByYou") |
| } |
| w.buckets[name] = &fakeBucket{region: f.region, owner: testAccount, tags: append([]s3types.Tag(nil), tags...)} |
| - if len(tags) == 0 { |
| + if len(tags) == 0 || w.dropTags { |
| w.buckets[name].tags = nil |
| } |
| return nil |
| @@ -428,10 +439,13 @@ func TestAWSFleetBucketContract(t *testing.T) { |
| if s.iam.count("CreateAccessKey") != 0 { |
| t.Fatal("preparing storage minted a key") |
| } |
| - // A retry converges with no writes. |
| + if s.s3.count("CreateBucket") != 1 || s.s3.attempts[0] != 1 || s.s3.count("GetBucketLocation") != 2 { |
| + t.Fatalf("absent bucket: %d creates (attempts %v), %d location reads", s.s3.count("CreateBucket"), s.s3.attempts, s.s3.count("GetBucketLocation")) |
| + } |
| + // A retry converges with no writes and no create. |
| puts, policyPuts := s.s3.puts(), s.iam.count("PutUserPolicy") |
| again, err := s.EnsureFleetStorage(ctx, testFleet, testRegion) |
| - if err != nil || again != got || s.s3.puts() != puts || s.iam.count("PutUserPolicy") != policyPuts { |
| + if err != nil || again != got || s.s3.puts() != puts || s.iam.count("PutUserPolicy") != policyPuts || s.s3.count("CreateBucket") != 1 { |
| t.Fatalf("retry %+v %v, puts %d->%d", again, err, puts, s.s3.puts()) |
| } |
| }) |
| @@ -445,10 +459,15 @@ func TestAWSFleetBucketContract(t *testing.T) { |
| if c == nil || c.LocationConstraint != "" || len(c.Tags) != 2 { |
| t.Fatalf("us-east-1 create request %+v", c) |
| } |
| - // us-east-1 answers a repeated create with success: tags decide. |
| + // A second pass reads the bucket and never re-creates it: in |
| + // us-east-1 that would succeed and reset its ACLs. |
| + bucket, _ := testNames(t, testFleet) |
| if _, err := s.EnsureFleetStorage(ctx, testFleet, "us-east-1"); err != nil { |
| t.Fatal(err) |
| } |
| + if s.s3.count("CreateBucket") != 1 || s.s3.bucket(bucket).aclResets != 0 { |
| + t.Fatal("re-created an existing us-east-1 bucket") |
| + } |
| }) |
| t.Run("legacy EU location", func(t *testing.T) { |
| s := newTestStorage(t) |
| @@ -494,14 +513,17 @@ func TestAWSFleetBucketContract(t *testing.T) { |
| "foreign owner": {testRegion, func(w *fakeS3World, b string) { |
| w.buckets[b] = &fakeBucket{region: testRegion, owner: foreignAcct, tags: ownTags(testFleet)} |
| }}, |
| - "foreign owner answered as ours": {testRegion, func(w *fakeS3World, b string) { |
| - w.buckets[b] = &fakeBucket{region: testRegion, owner: foreignAcct, tags: ownTags(testFleet)} |
| - w.before = func(op string, _ int) error { |
| - if op == "CreateBucket" { |
| - return apiErr("BucketAlreadyOwnedByYou") |
| - } |
| - return nil |
| - } |
| + "foreign owner in us-east-1": {"us-east-1", func(w *fakeS3World, b string) { |
| + w.buckets[b] = &fakeBucket{region: "us-east-1", owner: foreignAcct, tags: ownTags(testFleet)} |
| + }}, |
| + "untagged in us-east-1": {"us-east-1", func(w *fakeS3World, b string) { |
| + w.buckets[b] = &fakeBucket{region: "us-east-1", owner: testAccount} |
| + }}, |
| + "other fleet tag in us-east-1": {"us-east-1", func(w *fakeS3World, b string) { |
| + w.buckets[b] = &fakeBucket{region: "us-east-1", owner: testAccount, tags: ownTags("shop-2")} |
| + }}, |
| + "untagged in another region": {testRegion, func(w *fakeS3World, b string) { |
| + w.buckets[b] = &fakeBucket{region: testRegion, owner: testAccount} |
| }}, |
| "other deployment tag": {testRegion, func(w *fakeS3World, b string) { |
| w.buckets[b] = &fakeBucket{region: testRegion, owner: testAccount, tags: []s3types.Tag{ |
| @@ -557,8 +579,41 @@ func TestAWSFleetBucketContract(t *testing.T) { |
| if s.iam.total() != 0 { |
| t.Fatal("touched IAM before the bucket was proven") |
| } |
| + if s.s3.count("CreateBucket") != 0 || s.s3.buckets[bucket].aclResets != 0 { |
| + t.Fatal("sent CreateBucket for an existing bucket") |
| + } |
| }) |
| } |
| + t.Run("absent, then taken by another account", func(t *testing.T) { |
| + s := newTestStorage(t) |
| + s.s3.before = func(op string, _ int) error { |
| + if op == "CreateBucket" { |
| + return apiErr("BucketAlreadyExists") |
| + } |
| + return nil |
| + } |
| + _, err := s.EnsureFleetStorage(ctx, testFleet, testRegion) |
| + drifted(t, err) |
| + if s.s3.count("CreateBucket") != 1 || s.s3.puts() != 0 || s.iam.total() != 0 { |
| + t.Fatal("went on after losing the name") |
| + } |
| + }) |
| + t.Run("ambiguous location read creates nothing", func(t *testing.T) { |
| + s := newTestStorage(t) |
| + bucket, _ := testNames(t, testFleet) |
| + s.s3.buckets[bucket] = &fakeBucket{region: "us-east-1", owner: testAccount} |
| + s.s3.before = func(op string, _ int) error { |
| + if op == "GetBucketLocation" { |
| + return apiErr("InternalError") |
| + } |
| + return nil |
| + } |
| + _, err := s.EnsureFleetStorage(ctx, testFleet, "us-east-1") |
| + ambiguous(t, err) |
| + if s.s3.count("CreateBucket") != 0 || s.s3.buckets[bucket].aclResets != 0 { |
| + t.Fatal("an unproven read led to CreateBucket") |
| + } |
| + }) |
| t.Run("region not configured", func(t *testing.T) { |
| s := newTestStorage(t) |
| if _, err := s.EnsureFleetStorage(ctx, testFleet, "ap-south-1"); err == nil || s.s3.count("CreateBucket") != 0 { |
| @@ -649,8 +704,8 @@ func TestAWSFleetBucketPartialFailure(t *testing.T) { |
| _, err := s.EnsureFleetStorage(ctx, testFleet, testRegion) |
| drifted(t, err) |
| } |
| - if s.s3.puts() != 0 || s.iam.total() != 0 || s.s3.buckets[bucket].tags != nil { |
| - t.Fatal("adopted or relabelled an untagged bucket") |
| + if s.s3.puts() != 0 || s.iam.total() != 0 || s.s3.buckets[bucket].tags != nil || s.s3.count("CreateBucket") != 0 { |
| + t.Fatal("adopted, re-created or relabelled an untagged bucket") |
| } |
| }) |
| t.Run("lost create reply", func(t *testing.T) { |
| @@ -668,5 +723,28 @@ func TestAWSFleetBucketPartialFailure(t *testing.T) { |
| if _, err := s.EnsureFleetStorage(ctx, testFleet, testRegion); err != nil { |
| t.Fatalf("did not recover the bucket through its tags: %v", err) |
| } |
| + if s.s3.count("CreateBucket") != 1 { |
| + t.Fatalf("CreateBucket sent %d times", s.s3.count("CreateBucket")) |
| + } |
| + }) |
| + t.Run("lost create reply without tags is not adopted", func(t *testing.T) { |
| + s := newTestStorage(t) |
| + s.s3.dropTags = true |
| + s.s3.after = func(op string, n int) error { |
| + if op == "CreateBucket" { |
| + return apiErr("RequestTimeout") |
| + } |
| + return nil |
| + } |
| + if _, err := s.EnsureFleetStorage(ctx, testFleet, "us-east-1"); err == nil { |
| + t.Fatal("accepted a lost reply") |
| + } |
| + s.s3.after = nil |
| + _, err := s.EnsureFleetStorage(ctx, testFleet, "us-east-1") |
| + drifted(t, err) |
| + bucket, _ := testNames(t, testFleet) |
| + if s.s3.count("CreateBucket") != 1 || s.s3.puts() != 0 || s.s3.bucket(bucket).aclResets != 0 { |
| + t.Fatal("adopted or re-created a bucket without its tags") |
| + } |
| }) |
| } |
diff --git a/internal/awsmanaged/testdata/fleet-data-policy.json b/internal/awsmanaged/testdata/fleet-data-policy.json| index c3a6c3d92..1472da3fb 100644 |
| --- a/internal/awsmanaged/testdata/fleet-data-policy.json |
| +++ b/internal/awsmanaged/testdata/fleet-data-policy.json |
| @@ -18,17 +18,6 @@ |
| } |
| } |
| }, |
| - { |
| - "Sid": "Uploads", |
| - "Effect": "Allow", |
| - "Action": ["s3:ListBucketMultipartUploads"], |
| - "Resource": ["arn:aws:s3:::carlos-fleet-0123456789abcdef01234567"], |
| - "Condition": { |
| - "StringLike": { |
| - "s3:prefix": ["instances/*", "moves/*", "move-snapshots/*"] |
| - } |
| - } |
| - }, |
| { |
| "Sid": "Data", |
| "Effect": "Allow", |