media: incarnation on a bucket tag, no minted shortcut (spec A3)
Fix round 2 of media conversion plan 2 Task 3, implementing spec amendment §11 A3 (Paul, under stopping rule 2). - Minted is gone: CreateNewBucket, BucketCreator, PreExisting, the walk's minting logic and round 1's clear-on-replacement and clear-before-teardown are reverted. walk.go, aws.go, provisioner.go and memprovisioner.go are back to abfa911c; teardown no longer touches the conversion block, so uploads hashes survive a failed teardown. - The incarnation lives on the bucket as tag carlos-media-incarnation. storecfg.Buckets gains BucketTags and PutBucketTags; WriteIncarnation keeps every other tag and fails closed on unreadable tags or a full tag set. NewIncarnation moves to storecfg. - mediaStoreCheck, shared by the door (mediaStoreEligible) and dispatch (mediaStoreFacts, for Task 5's StoreGuard.Current), reads the tag on every call. A missing or different tag re-verifies, then tags the bucket and records a fresh incarnation; any failed step leaves the store ineligible.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
13 files changed,
+780
−919
internal/console/mediastore.go+86 −32internal/console/mediastore_test.go+284 −130internal/objstore/aws.go+4 −27internal/objstore/aws_test.go+0 −124internal/objstore/conversion_test.go+149 −0internal/objstore/memprovisioner.go+2 −22internal/objstore/objstore.go+13 −27internal/objstore/provisioner.go+0 −17internal/objstore/walk.go+3 −66internal/objstore/walk_test.go+0 −473internal/storecfg/fake.go+44 −1internal/storecfg/storecfg.go+86 −0internal/storecfg/storecfg_test.go+109 −0
CI failed — run details
diff --git a/internal/console/mediastore.go b/internal/console/mediastore.go| index 91d765e74..9d6e1632c 100644 |
| --- a/internal/console/mediastore.go |
| +++ b/internal/console/mediastore.go |
| @@ -5,6 +5,7 @@ import ( |
| "errors" |
| "fmt" |
| + "github.com/carlosframework/platform/internal/mediajobs" |
| "github.com/carlosframework/platform/internal/objstore" |
| "github.com/carlosframework/platform/internal/storecfg" |
| ) |
| @@ -18,52 +19,72 @@ const ( |
| mediaCodeStoreUnverified = "store_unverified" |
| ) |
| -// errStoreBucketsUnset: a store that needs verifying, on a console with no |
| -// way to read bucket settings. A deployment fault, not the member's. |
| +// errStoreBucketsUnset: a console with no way to read bucket settings or |
| +// tags. A deployment fault, not the member's. |
| var errStoreBucketsUnset = errors.New("console: store verification is not configured (no StoreBuckets)") |
| -// mediaStoreEligible says whether the app's own store may be used for media |
| -// conversion and browser uploads, and answers its record when it may. The |
| -// caller holds the app's store walk lock (storeWalks.lock(acct, app, "")), |
| -// so a teardown cannot run between this check and what the caller does |
| -// next; no SQLite transaction may be open, because Inspect is a network |
| -// call. |
| +// mediaStoreEligible is mediaStoreCheck for the door: the app's own store, |
| +// for an account in hand. |
| +func (s *Server) mediaStoreEligible(ctx context.Context, acct *Account, app string) (rec *objstore.Record, code, check string, err error) { |
| + return s.mediaStoreCheck(ctx, acct.Sqid, app) |
| +} |
| + |
| +// mediaStoreCheck says whether the app's own store may be used for media |
| +// conversion and browser uploads (spec §5.2, amendment A3), and answers its |
| +// record when it may. The submission door (through mediaStoreEligible) and |
| +// dispatch (through mediaStoreFacts) both call it, so the two can never |
| +// disagree about a store. |
| +// |
| +// The caller holds the app's store walk lock (storeWalks.lock(acct, app, |
| +// "")), so a teardown cannot run between this check and what the caller |
| +// does next. No SQLite transaction may be open: every step below but the |
| +// first is a network call. |
| // |
| // - no record, or no Provisioned block: code no_store. |
| -// - a Conversion block that is Minted (the grant walk created the bucket) |
| -// or already verified: eligible, with nothing read from the bucket. |
| -// - otherwise the bucket's settings are read and checked once |
| -// (storecfg.Verify). A pass records Conversion{a new incarnation, |
| -// VerifiedAt now}, keeping any Uploads block, in a write conditional on |
| -// the record as read; a lost race is an error, and the next call checks |
| -// again. A failure is code store_unverified with the failing check, and |
| -// writes nothing, so the next call checks again too. |
| +// - the bucket's incarnation tag is read on every call. A Conversion block |
| +// that is verified and whose incarnation equals the tag: eligible, with |
| +// nothing else read from the bucket. |
| +// - otherwise (no block, no tag, a tag that differs: a recreated bucket |
| +// carries none) the bucket's settings are checked (storecfg.Verify). A |
| +// failure is code store_unverified with the failing check, and writes |
| +// nothing. A pass mints a fresh incarnation, tags the bucket with it |
| +// (keeping the bucket's other tags), then records Conversion{that |
| +// incarnation, VerifiedAt now} keeping any Uploads block, in a write |
| +// conditional on the record as read. |
| // |
| -// code is "" when eligible. err is for a failure to find out (a record that |
| -// cannot be read, a bucket that cannot be inspected, a lost write); the |
| -// caller answers it as an internal error, never as a refusal. |
| -func (s *Server) mediaStoreEligible(ctx context.Context, acct *Account, app string) (rec *objstore.Record, code, check string, err error) { |
| - rec, etag, err := objstore.GetIn(ctx, s.cfg.Releases, acct.Sqid, app, "") |
| +// Either write failing leaves the tag and the record disagreeing, or the |
| +// record unchanged, so the store stays ineligible and the next call checks |
| +// it again. code is "" when eligible. err is a failure to find out (a |
| +// record or tags that cannot be read, a bucket that cannot be inspected, a |
| +// lost write); the caller answers it as an internal error, never as a |
| +// refusal, and never as eligible. |
| +func (s *Server) mediaStoreCheck(ctx context.Context, sqid, app string) (rec *objstore.Record, code, check string, err error) { |
| + rec, etag, err := objstore.GetIn(ctx, s.cfg.Releases, sqid, app, "") |
| if err != nil { |
| - return nil, "", "", fmt.Errorf("console: media store %s/%s: read record: %w", acct.Sqid, app, err) |
| + return nil, "", "", fmt.Errorf("console: media store %s/%s: read record: %w", sqid, app, err) |
| } |
| if rec == nil || rec.Provisioned == nil { |
| return rec, mediaCodeNoStore, "", nil |
| } |
| - if c := rec.Conversion; c != nil && (c.Minted || c.VerifiedAt != nil) { |
| - return rec, "", "", nil |
| - } |
| - if s.cfg.StoreBuckets == nil { |
| + b := s.cfg.StoreBuckets |
| + if b == nil { |
| return nil, "", "", errStoreBucketsUnset |
| } |
| bucket := rec.Provisioned.Bucket |
| - facts, err := s.cfg.StoreBuckets.Inspect(ctx, bucket) |
| + tag, err := storecfg.ReadIncarnation(ctx, b, bucket) |
| if err != nil { |
| - return nil, "", "", fmt.Errorf("console: media store %s/%s: inspect bucket %s: %w", acct.Sqid, app, bucket, err) |
| + return nil, "", "", fmt.Errorf("console: media store %s/%s: %w", sqid, app, err) |
| + } |
| + if c := rec.Conversion; c != nil && c.VerifiedAt != nil && storecfg.ValidIncarnation(c.Incarnation) && tag == c.Incarnation { |
| + return rec, "", "", nil |
| + } |
| + facts, err := b.Inspect(ctx, bucket) |
| + if err != nil { |
| + return nil, "", "", fmt.Errorf("console: media store %s/%s: inspect bucket %s: %w", sqid, app, bucket, err) |
| } |
| want := storecfg.Want{ |
| Bucket: bucket, |
| - MintedName: objstore.MintedBucketIn(s.cfg.StoreDeployment, acct.Sqid, app, ""), |
| + MintedName: objstore.MintedBucketIn(s.cfg.StoreDeployment, sqid, app, ""), |
| Account: s.cfg.StoreAccountID, |
| Region: rec.Provisioned.Region, |
| } |
| @@ -77,14 +98,47 @@ func (s *Server) mediaStoreEligible(ctx context.Context, acct *Account, app stri |
| if failed := storecfg.Verify(facts, want); failed != "" { |
| return rec, mediaCodeStoreUnverified, failed, nil |
| } |
| - inc, err := objstore.NewIncarnation() |
| + inc, err := storecfg.NewIncarnation() |
| if err != nil { |
| return nil, "", "", err |
| } |
| + // The tag first: a record naming an incarnation the bucket never carried |
| + // would be as untrusted as none, but a bucket tagged and never recorded |
| + // is simply verified again next time. Either order fails closed; this one |
| + // never leaves a record that looks verified on its own. |
| + if err := storecfg.WriteIncarnation(ctx, b, bucket, inc); err != nil { |
| + return nil, "", "", fmt.Errorf("console: media store %s/%s: %w", sqid, app, err) |
| + } |
| now := s.now().UTC() |
| rec.Conversion = &objstore.Conversion{Incarnation: inc, VerifiedAt: &now, Uploads: uploads} |
| - if _, err := objstore.PutIn(ctx, s.cfg.Releases, acct.Sqid, app, "", rec, etag); err != nil { |
| - return nil, "", "", fmt.Errorf("console: media store %s/%s: record verification: %w", acct.Sqid, app, err) |
| + if _, err := objstore.PutIn(ctx, s.cfg.Releases, sqid, app, "", rec, etag); err != nil { |
| + return nil, "", "", fmt.Errorf("console: media store %s/%s: record verification: %w", sqid, app, err) |
| } |
| return rec, "", "", nil |
| } |
| + |
| +// mediaStoreFacts is mediaStoreCheck in the shape dispatch re-checks |
| +// (mediajobs.StoreGuard.Current): the console's StoreGuard answers Current |
| +// with this, under the same walk lock, outside any transaction. A store |
| +// that is not eligible answers no incarnation, so every queued row of it |
| +// fails store_changed; a store verified afresh answers its new one, which |
| +// no queued row carries. An error leaves the row queued. |
| +func (s *Server) mediaStoreFacts(ctx context.Context, sqid, app string) (mediajobs.StoreFacts, error) { |
| + rec, code, _, err := s.mediaStoreCheck(ctx, sqid, app) |
| + if err != nil { |
| + return mediajobs.StoreFacts{}, err |
| + } |
| + if code == mediaCodeNoStore { |
| + return mediajobs.StoreFacts{}, nil |
| + } |
| + f := mediajobs.StoreFacts{ |
| + Exists: true, |
| + Bucket: rec.Provisioned.Bucket, |
| + Region: rec.Provisioned.Region, |
| + WritesDenied: rec.WritesDenied != nil, |
| + } |
| + if code == "" { |
| + f.Incarnation = rec.Conversion.Incarnation |
| + } |
| + return f, nil |
| +} |
diff --git a/internal/console/mediastore_test.go b/internal/console/mediastore_test.go| index 05b998d0d..43cb12a23 100644 |
| --- a/internal/console/mediastore_test.go |
| +++ b/internal/console/mediastore_test.go |
| @@ -5,7 +5,6 @@ import ( |
| "context" |
| "errors" |
| "net/http" |
| - "regexp" |
| "testing" |
| "time" |
| @@ -16,21 +15,25 @@ import ( |
| const mediaTestAccountID = "123456789012" |
| -var mediaHex32 = regexp.MustCompile(`^[0-9a-f]{32}$`) |
| - |
| // mediaStoreFixture is storeArmed plus a storecfg.Fake and the deployment's |
| -// account id. preExisting puts a bucket under the minted name before the |
| -// grant, so the walk does not mint and the store has to be verified. |
| -func mediaStoreFixture(t *testing.T, code string, preExisting bool) (*consoleFixture, *objstore.MemProvisioner, *storecfg.Fake, string) { |
| +// account id. |
| +func mediaStoreFixture(t *testing.T, code string) (*consoleFixture, *objstore.MemProvisioner, *storecfg.Fake, string) { |
| t.Helper() |
| fx, prov, token, _ := storeArmed(t, code) |
| buckets := storecfg.NewFake() |
| fx.srv.cfg.StoreConversion = true |
| fx.srv.cfg.StoreBuckets = buckets |
| fx.srv.cfg.StoreAccountID = mediaTestAccountID |
| - if preExisting { |
| - prov.PreExisting = map[string]bool{mediaMintedName(fx): true} |
| - } |
| + return fx, prov, buckets, token |
| +} |
| + |
| +// grantedMediaStore is mediaStoreFixture with the app's store granted and a |
| +// bucket that passes every check. |
| +func grantedMediaStore(t *testing.T, code string) (*consoleFixture, *objstore.MemProvisioner, *storecfg.Fake, string) { |
| + t.Helper() |
| + fx, prov, buckets, token := mediaStoreFixture(t, code) |
| + grantStore(t, fx, token) |
| + buckets.Facts[mediaMintedName(fx)] = goodBucket() |
| return fx, prov, buckets, token |
| } |
| @@ -62,8 +65,12 @@ func eligible(t *testing.T, fx *consoleFixture) (*objstore.Record, string, strin |
| return fx.srv.mediaStoreEligible(context.Background(), mediaAcct(fx), "titogo") |
| } |
| +func bucketTag(fx *consoleFixture, buckets *storecfg.Fake) string { |
| + return buckets.Tags[mediaMintedName(fx)][storecfg.IncarnationTag] |
| +} |
| + |
| func TestEligibleNoStore(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-nostore", false) |
| + fx, _, buckets, token := mediaStoreFixture(t, "code-media-nostore") |
| if _, code, _, err := eligible(t, fx); err != nil || code != "no_store" { |
| t.Fatalf("no record: code %q, %v; want no_store", code, err) |
| } |
| @@ -71,37 +78,20 @@ func TestEligibleNoStore(t *testing.T) { |
| if _, code, _, err := eligible(t, fx); err != nil || code != "no_store" { |
| t.Fatalf("declared only: code %q, %v; want no_store", code, err) |
| } |
| - if n := buckets.CallCount("Inspect"); n != 0 { |
| - t.Fatalf("no_store inspected the bucket %d times", n) |
| + if len(buckets.Calls) != 0 { |
| + t.Fatalf("no_store touched the bucket: %v", buckets.Calls) |
| } |
| } |
| -// A store the walk minted is eligible with nothing read from the bucket. |
| -func TestEligibleMintedNeedsNoCheck(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-minted", false) |
| - grantStore(t, fx, token) |
| - before := storedRecord(t, fx, "titogo") |
| - if before.Conversion == nil || !before.Conversion.Minted { |
| - t.Fatalf("fixture: grant did not mint: %+v", before.Conversion) |
| - } |
| - rec, code, check, err := eligible(t, fx) |
| - if err != nil || code != "" || check != "" { |
| - t.Fatalf("minted store: %q %q %v", code, check, err) |
| - } |
| - if rec.Conversion.Incarnation != before.Conversion.Incarnation { |
| - t.Fatalf("incarnation changed: %q -> %q", before.Conversion.Incarnation, rec.Conversion.Incarnation) |
| - } |
| - if n := buckets.CallCount("Inspect"); n != 0 { |
| - t.Fatalf("minted store inspected %d times", n) |
| - } |
| -} |
| - |
| -// An unminted store is checked once; the pass is recorded, and the next |
| -// call reads nothing from the bucket and keeps the incarnation. |
| +// A3: a store whose bucket the platform itself just created gets no |
| +// shortcut. Its first media use verifies it, tags the bucket and records |
| +// the incarnation; the next call reads the tag, finds it matches, and reads |
| +// nothing else. |
| func TestEligibleVerifiesOnceAndRecords(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-verify", true) |
| - grantStore(t, fx, token) |
| - buckets.Facts[mediaMintedName(fx)] = goodBucket() |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-verify") |
| + if rec := storedRecord(t, fx, "titogo"); rec.Conversion != nil { |
| + t.Fatalf("a grant recorded conversion provenance: %+v", rec.Conversion) |
| + } |
| at := time.Date(2026, 11, 17, 10, 0, 0, 0, time.UTC) |
| fx.srv.opNow = func() time.Time { return at } |
| @@ -110,17 +100,20 @@ func TestEligibleVerifiesOnceAndRecords(t *testing.T) { |
| t.Fatalf("first check: %q %q %v", code, check, err) |
| } |
| c := rec.Conversion |
| - if c == nil || c.Minted || c.VerifiedAt == nil || !c.VerifiedAt.Equal(at) || !mediaHex32.MatchString(c.Incarnation) { |
| + if c == nil || c.VerifiedAt == nil || !c.VerifiedAt.Equal(at) || !storecfg.ValidIncarnation(c.Incarnation) { |
| t.Fatalf("conversion = %+v", c) |
| } |
| + if got := bucketTag(fx, buckets); got != c.Incarnation { |
| + t.Fatalf("bucket tag = %q, record %q", got, c.Incarnation) |
| + } |
| stored := storedRecord(t, fx, "titogo") |
| if stored.Conversion == nil || stored.Conversion.Incarnation != c.Incarnation || stored.Provisioned == nil || |
| stored.Provisioned.KeyID != rec.Provisioned.KeyID { |
| t.Fatalf("stored = %+v / %+v", stored.Conversion, stored.Provisioned) |
| } |
| - // The next call trusts the record: no Inspect, same incarnation, even |
| - // though the bucket has since changed. |
| + // The next call trusts the record because the tag agrees: no Inspect, |
| + // same incarnation, even though the bucket's settings have since moved. |
| buckets.Facts[mediaMintedName(fx)] = storecfg.Facts{Encryption: "aws:kms"} |
| rec2, code, _, err := eligible(t, fx) |
| if err != nil || code != "" { |
| @@ -132,13 +125,133 @@ func TestEligibleVerifiesOnceAndRecords(t *testing.T) { |
| if n := buckets.CallCount("Inspect"); n != 1 { |
| t.Fatalf("Inspect calls = %d, want 1", n) |
| } |
| + if n := buckets.CallCount("BucketTags"); n < 2 { |
| + t.Fatalf("BucketTags calls = %d: every call must read the tag", n) |
| + } |
| +} |
| + |
| +// A bucket made again under the same name carries no tag. The record still |
| +// holds the old block (no teardown, nothing cleared), and the store is |
| +// verified again with a new incarnation all the same. |
| +func TestRecreatedBucketIsVerifiedAgain(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-recreated") |
| + first, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("first: %q %v", code, err) |
| + } |
| + name := mediaMintedName(fx) |
| + buckets.DropBucket(name) |
| + buckets.Facts[name] = goodBucket() |
| + if storedRecord(t, fx, "titogo").Conversion.Incarnation != first.Conversion.Incarnation { |
| + t.Fatal("fixture: the record's block changed") |
| + } |
| + |
| + second, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("recreated: %q %v", code, err) |
| + } |
| + if second.Conversion.Incarnation == first.Conversion.Incarnation { |
| + t.Fatalf("the recreated bucket inherited incarnation %q", first.Conversion.Incarnation) |
| + } |
| + if n := buckets.CallCount("Inspect"); n != 2 { |
| + t.Fatalf("Inspect calls = %d, want 2 (the recreated bucket is checked)", n) |
| + } |
| + if got := bucketTag(fx, buckets); got != second.Conversion.Incarnation { |
| + t.Fatalf("tag = %q, record %q", got, second.Conversion.Incarnation) |
| + } |
| +} |
| + |
| +// A tag that differs from the record is not trusted: the store is verified |
| +// again and gets a fresh incarnation, neither the record's nor the tag's. |
| +func TestTagMismatchVerifiesAgain(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-mismatch") |
| + first, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("first: %q %v", code, err) |
| + } |
| + const other = "fedcba9876543210fedcba9876543210" |
| + buckets.Tags[mediaMintedName(fx)][storecfg.IncarnationTag] = other |
| + |
| + second, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("mismatch: %q %v", code, err) |
| + } |
| + if inc := second.Conversion.Incarnation; inc == first.Conversion.Incarnation || inc == other { |
| + t.Fatalf("incarnation %q after a mismatch (record had %q, tag %q)", inc, first.Conversion.Incarnation, other) |
| + } |
| + if n := buckets.CallCount("Inspect"); n != 2 { |
| + t.Fatalf("Inspect calls = %d, want 2", n) |
| + } |
| + |
| + // A mismatching bucket that no longer passes is refused, and the old |
| + // record is not accepted in its place. |
| + buckets.Tags[mediaMintedName(fx)][storecfg.IncarnationTag] = other |
| + versioned := goodBucket() |
| + versioned.Versioning = "Enabled" |
| + buckets.Facts[mediaMintedName(fx)] = versioned |
| + if _, code, check, err := eligible(t, fx); err != nil || code != "store_unverified" || check != "versioning" { |
| + t.Fatalf("mismatch on a versioned bucket: %q %q %v", code, check, err) |
| + } |
| +} |
| + |
| +// The tag write keeps the bucket's other tags. |
| +func TestEligibleKeepsTheBucketsOtherTags(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-tags") |
| + name := mediaMintedName(fx) |
| + buckets.Tags[name] = map[string]string{"cost-centre": "shop", "team": "web"} |
| + rec, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("eligible: %q %v", code, err) |
| + } |
| + got := buckets.Tags[name] |
| + if len(got) != 3 || got["cost-centre"] != "shop" || got["team"] != "web" || got[storecfg.IncarnationTag] != rec.Conversion.Incarnation { |
| + t.Fatalf("tags after verification = %v", got) |
| + } |
| +} |
| + |
| +// A failed tag write, or tags that cannot be read, leave the store |
| +// ineligible with the record unchanged; the next call checks again. |
| +func TestFailedTagWriteLeavesStoreIneligible(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-tagfail") |
| + before, beforeTag := rawStoreRecord(t, fx) |
| + buckets.Fail["PutBucketTags"] = errors.New("injected: tagging denied") |
| + if _, code, _, err := eligible(t, fx); err == nil || code != "" { |
| + t.Fatalf("failed tag write: code %q, err %v; want an error and no code", code, err) |
| + } |
| + if after, afterTag := rawStoreRecord(t, fx); afterTag != beforeTag || !bytes.Equal(after, before) { |
| + t.Fatalf("a failed tag write changed the record:\n%s\n->\n%s", before, after) |
| + } |
| + if got := bucketTag(fx, buckets); got != "" { |
| + t.Fatalf("tag = %q after a failed write", got) |
| + } |
| + |
| + rec, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("retry: %q %v", code, err) |
| + } |
| + if n := buckets.CallCount("Inspect"); n != 2 { |
| + t.Fatalf("Inspect calls = %d, want 2", n) |
| + } |
| + |
| + // Verified now; tags that cannot be read are not an untagged bucket and |
| + // not a match either. The store is not eligible. |
| + buckets.Fail["BucketTags"] = errors.New("injected: throttled") |
| + if _, code, _, err := eligible(t, fx); err == nil || code != "" { |
| + t.Fatalf("unreadable tags: code %q, err %v", code, err) |
| + } |
| + buckets.Fail["BucketTags"] = storecfg.ErrTagsUnsupported |
| + if _, _, _, err := eligible(t, fx); !errors.Is(err, storecfg.ErrTagsUnsupported) { |
| + t.Fatalf("backend without tags: %v", err) |
| + } |
| + if storedRecord(t, fx, "titogo").Conversion.Incarnation != rec.Conversion.Incarnation { |
| + t.Fatal("a tag read failure changed the record") |
| + } |
| } |
| // A store that already carries the platform's own uploads rules (recorded |
| // in an Uploads block) passes, and the pass keeps that block. |
| func TestEligibleKeepsUploads(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-keep-uploads", true) |
| - grantStore(t, fx, token) |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-keep-uploads") |
| origins := []string{"https://shop.example.com"} |
| f := goodBucket() |
| f.CORS, f.Lifecycle = storecfg.UploadsCORS(origins), storecfg.UploadsLifecycle() |
| @@ -149,7 +262,9 @@ func TestEligibleKeepsUploads(t *testing.T) { |
| if err != nil { |
| t.Fatal(err) |
| } |
| - rec.Conversion = &objstore.Conversion{Incarnation: "stale", Uploads: up} |
| + stale := "0123456789abcdef0123456789abcdef" |
| + at := time.Now().UTC() |
| + rec.Conversion = &objstore.Conversion{Incarnation: stale, VerifiedAt: &at, Uploads: up} |
| if _, err := objstore.Put(context.Background(), fx.srv.cfg.Releases, fx.sqid, "titogo", rec, etag); err != nil { |
| t.Fatal(err) |
| } |
| @@ -163,16 +278,15 @@ func TestEligibleKeepsUploads(t *testing.T) { |
| c.Uploads.LifecycleHash != up.LifecycleHash || !c.Uploads.At.Equal(up.At) || c.Uploads.By != up.By { |
| t.Fatalf("stored conversion = %+v", c) |
| } |
| - if c.Incarnation == "stale" || c.Incarnation != got.Conversion.Incarnation { |
| - t.Fatalf("incarnation = %q, want a new one", c.Incarnation) |
| + if c.Incarnation == stale || c.Incarnation != got.Conversion.Incarnation { |
| + t.Fatalf("incarnation = %q, want a new one (the bucket carried no tag)", c.Incarnation) |
| } |
| } |
| // Every refusal and every failure to find out leaves the record byte for |
| -// byte as it was, and the next call checks again. |
| +// byte as it was, tags nothing, and the next call checks again. |
| func TestEligibleRefusalWritesNothing(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-refuse", true) |
| - grantStore(t, fx, token) |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-refuse") |
| name := mediaMintedName(fx) |
| before, beforeTag := rawStoreRecord(t, fx) |
| unchanged := func(t *testing.T) { |
| @@ -181,6 +295,9 @@ func TestEligibleRefusalWritesNothing(t *testing.T) { |
| if afterTag != beforeTag || !bytes.Equal(after, before) { |
| t.Fatalf("the record changed:\n%s\n->\n%s", before, after) |
| } |
| + if n := buckets.CallCount("PutBucketTags"); n != 0 { |
| + t.Fatalf("a refusal tagged the bucket (%d writes)", n) |
| + } |
| } |
| versioned := goodBucket() |
| @@ -228,7 +345,7 @@ func TestEligibleRefusalWritesNothing(t *testing.T) { |
| unchanged(t) |
| }) |
| - t.Run("lost-write", func(t *testing.T) { |
| + t.Run("lost-record-write", func(t *testing.T) { |
| buckets.Facts[name] = goodBucket() |
| fx.srv.cfg.StoreBuckets = racingBuckets{Buckets: buckets, race: func() { |
| // Another door writes the record while the bucket is inspected. |
| @@ -248,6 +365,11 @@ func TestEligibleRefusalWritesNothing(t *testing.T) { |
| if c := storedRecord(t, fx, "titogo"); c.Conversion != nil || c.Declared.By != "someone-else@example.com" { |
| t.Fatalf("after a lost write: %+v, declared by %q", c.Conversion, c.Declared.By) |
| } |
| + // The bucket was tagged before the record write lost; the next call |
| + // finds a tag the record does not hold, and checks again. |
| + if _, code, _, err := eligible(t, fx); err != nil || code != "" { |
| + t.Fatalf("after a lost write, retry: %q %v", code, err) |
| + } |
| }) |
| } |
| @@ -261,21 +383,11 @@ func (r racingBuckets) Inspect(ctx context.Context, bucket string) (storecfg.Fac |
| return r.Buckets.Inspect(ctx, bucket) |
| } |
| -// F3: a bucket already there under the minted name, granted with no adopt. |
| -// The walk does not mint, and the console's check refuses it for what it |
| -// holds: SSE-KMS first, then, once that is fixed, a foreign policy. |
| -func TestPreExistingMintedNameIsNotMinted(t *testing.T) { |
| - fx, prov, buckets, token := mediaStoreFixture(t, "code-media-preexisting", true) |
| - grantStore(t, fx, token) |
| +// The checks run in Verify's order through the console: SSE-KMS first, then, |
| +// once that is fixed, a foreign policy, then eligible. |
| +func TestEligibleRefusesKMSThenPolicy(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-kms-policy") |
| name := mediaMintedName(fx) |
| - rec := storedRecord(t, fx, "titogo") |
| - if rec.Provisioned == nil || rec.Provisioned.Bucket != name || rec.Conversion != nil { |
| - t.Fatalf("pre-existing minted name: provisioned %+v, conversion %+v", rec.Provisioned, rec.Conversion) |
| - } |
| - if _, ok := prov.Buckets[name]; !ok { |
| - t.Fatalf("fixture: no bucket %q", name) |
| - } |
| - |
| kms := goodBucket() |
| kms.Encryption, kms.HasPolicy = "aws:kms", true |
| buckets.Facts[name] = kms |
| @@ -289,7 +401,7 @@ func TestPreExistingMintedNameIsNotMinted(t *testing.T) { |
| t.Fatalf("foreign policy: %q %q %v", code, check, err) |
| } |
| if storedRecord(t, fx, "titogo").Conversion != nil { |
| - t.Fatal("a refused store recorded provenance") |
| + t.Fatal("a refused store recorded a verification") |
| } |
| buckets.Facts[name] = goodBucket() |
| if _, code, check, err := eligible(t, fx); err != nil || code != "" { |
| @@ -297,52 +409,11 @@ func TestPreExistingMintedNameIsNotMinted(t *testing.T) { |
| } |
| } |
| -// Teardown deletes the record, so the store made again under the same app is |
| -// a new incarnation, minted or verified. |
| -func TestRecreatedStoreHasANewIncarnation(t *testing.T) { |
| - for name, preExisting := range map[string]bool{"verified": true, "minted": false} { |
| - t.Run(name, func(t *testing.T) { |
| - fx, prov, buckets, token := mediaStoreFixture(t, "code-media-recreate-"+name, preExisting) |
| - grantStore(t, fx, token) |
| - buckets.Facts[mediaMintedName(fx)] = goodBucket() |
| - first, code, _, err := eligible(t, fx) |
| - if err != nil || code != "" { |
| - t.Fatalf("first store: %q %v", code, err) |
| - } |
| - if first.Conversion.Minted == preExisting { |
| - t.Fatalf("fixture: minted = %v with preExisting %v", first.Conversion.Minted, preExisting) |
| - } |
| - |
| - func() { |
| - defer fx.srv.storeWalks.lock(fx.sqid, "titogo", "")() |
| - if err := objstore.Teardown(context.Background(), fx.srv.cfg.Releases, prov, nil, fx.sqid, "titogo"); err != nil { |
| - t.Fatalf("teardown: %v", err) |
| - } |
| - }() |
| - if storedRecord(t, fx, "titogo") != nil { |
| - t.Fatal("teardown left the record") |
| - } |
| - if _, code, _, err := eligible(t, fx); err != nil || code != "no_store" { |
| - t.Fatalf("after teardown: %q %v", code, err) |
| - } |
| - |
| - grantStore(t, fx, token) |
| - second, code, _, err := eligible(t, fx) |
| - if err != nil || code != "" { |
| - t.Fatalf("second store: %q %v", code, err) |
| - } |
| - if second.Conversion.Incarnation == first.Conversion.Incarnation || !mediaHex32.MatchString(second.Conversion.Incarnation) { |
| - t.Fatalf("incarnation %q, first was %q", second.Conversion.Incarnation, first.Conversion.Incarnation) |
| - } |
| - }) |
| - } |
| -} |
| - |
| -// Fable fix round 1: the name check reaches the console. An operator adopts |
| -// a bucket that is not the name this deployment mints; every other fact is |
| -// good, and the store is refused for its name with nothing written. |
| +// The name check reaches the console: an operator adopts a bucket that is |
| +// not the name this deployment mints; every other fact is good, and the |
| +// store is refused for its name with nothing written. |
| func TestEligibleRefusesAnAdoptedName(t *testing.T) { |
| - fx, _, buckets, token := mediaStoreFixture(t, "code-media-adopted", false) |
| + fx, _, buckets, token := mediaStoreFixture(t, "code-media-adopted") |
| declareStore(t, fx, token) |
| const adopted = "shop-uploads" |
| if status, out := postJSON(t, storeURL(fx, "titogo")+"/grant", map[string]any{"bucket": adopted}, token); status != http.StatusOK { |
| @@ -362,6 +433,35 @@ func TestEligibleRefusesAnAdoptedName(t *testing.T) { |
| } |
| } |
| +// Teardown deletes the record, so the store made again under the same app is |
| +// a new incarnation. |
| +func TestRecreatedStoreHasANewIncarnation(t *testing.T) { |
| + fx, prov, buckets, token := grantedMediaStore(t, "code-media-recreate") |
| + first, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("first store: %q %v", code, err) |
| + } |
| + func() { |
| + defer fx.srv.storeWalks.lock(fx.sqid, "titogo", "")() |
| + if err := objstore.Teardown(context.Background(), fx.srv.cfg.Releases, prov, nil, fx.sqid, "titogo"); err != nil { |
| + t.Fatalf("teardown: %v", err) |
| + } |
| + }() |
| + buckets.DropBucket(mediaMintedName(fx)) |
| + if _, code, _, err := eligible(t, fx); err != nil || code != "no_store" { |
| + t.Fatalf("after teardown: %q %v", code, err) |
| + } |
| + grantStore(t, fx, token) |
| + buckets.Facts[mediaMintedName(fx)] = goodBucket() |
| + second, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("second store: %q %v", code, err) |
| + } |
| + if second.Conversion.Incarnation == first.Conversion.Incarnation { |
| + t.Fatalf("incarnation %q survived a teardown", first.Conversion.Incarnation) |
| + } |
| +} |
| + |
| // failDeleteOnce fails the first Delete of key: a teardown that dies after |
| // its destructive steps (bucket, user and delivered variables gone). |
| type failDeleteOnce struct { |
| @@ -377,16 +477,32 @@ func (f *failDeleteOnce) Delete(ctx context.Context, key string) error { |
| return f.Store.Delete(ctx, key) |
| } |
| -// Astra fix round 1, P1, end to end: a minted store's teardown fails at the |
| -// scan sidecar, the record stands, the grant that follows makes a |
| -// replacement bucket. The console must check the replacement afresh and give |
| -// it a new incarnation, never accept the old Minted claim. |
| -func TestInterruptedTeardownStoreIsVerifiedAfresh(t *testing.T) { |
| - fx, prov, buckets, token := mediaStoreFixture(t, "code-media-interrupted", false) |
| - grantStore(t, fx, token) |
| +// Astra's P1 and P2 together. A store with uploads enabled is torn down; the |
| +// teardown fails at the scan sidecar, after the bucket is gone, and the |
| +// record stands with its whole conversion block: the uploads hashes are |
| +// kept. The grant that follows makes a replacement bucket, which carries no |
| +// tag, so the console verifies it afresh with a new incarnation and still |
| +// carries the hashes. |
| +func TestFailedTeardownKeepsUploadsAndTheReplacementIsVerified(t *testing.T) { |
| + fx, prov, buckets, token := grantedMediaStore(t, "code-media-interrupted") |
| + origins := []string{"https://shop.example.com"} |
| + name := mediaMintedName(fx) |
| + f := goodBucket() |
| + f.CORS, f.Lifecycle = storecfg.UploadsCORS(origins), storecfg.UploadsLifecycle() |
| + buckets.Facts[name] = f |
| + rec, etag, err := objstore.Get(context.Background(), fx.srv.cfg.Releases, fx.sqid, "titogo") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + up := &objstore.Uploads{Origins: origins, CORSHash: storecfg.Hash(f.CORS), LifecycleHash: storecfg.Hash(f.Lifecycle), |
| + At: time.Date(2026, 11, 17, 8, 0, 0, 0, time.UTC), By: "alice@keymail.dev"} |
| + rec.Conversion = &objstore.Conversion{Incarnation: "0123456789abcdef0123456789abcdef", Uploads: up} |
| + if _, err := objstore.Put(context.Background(), fx.srv.cfg.Releases, fx.sqid, "titogo", rec, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| first, code, _, err := eligible(t, fx) |
| - if err != nil || code != "" || !first.Conversion.Minted { |
| - t.Fatalf("fixture: %q %v %+v", code, err, first.Conversion) |
| + if err != nil || code != "" { |
| + t.Fatalf("fixture: %q %v", code, err) |
| } |
| func() { |
| @@ -396,29 +512,67 @@ func TestInterruptedTeardownStoreIsVerifiedAfresh(t *testing.T) { |
| t.Fatal("teardown: no error from the injected failure") |
| } |
| }() |
| - if rec := storedRecord(t, fx, "titogo"); rec == nil || rec.Provisioned == nil || rec.Conversion != nil { |
| - t.Fatalf("after the failed teardown: %+v", rec) |
| - } |
| - // Between the teardown and the regrant there is no bucket to inspect: an |
| - // error, not an eligible store. |
| - if _, code, _, err := eligible(t, fx); err == nil || code != "" { |
| - t.Fatalf("store with no bucket: code %q, err %v; want an error", code, err) |
| + buckets.DropBucket(name) // the teardown deleted it |
| + half := storedRecord(t, fx, "titogo") |
| + if half == nil || half.Provisioned == nil || half.Conversion == nil || half.Conversion.Uploads == nil || |
| + half.Conversion.Uploads.CORSHash != up.CORSHash || half.Conversion.Uploads.LifecycleHash != up.LifecycleHash { |
| + t.Fatalf("after the failed teardown: %+v", half) |
| } |
| if status, out := postJSON(t, storeURL(fx, "titogo")+"/grant", map[string]any{}, token); status != http.StatusOK { |
| t.Fatalf("regrant = %d %v", status, out) |
| } |
| - buckets.Facts[mediaMintedName(fx)] = goodBucket() |
| - inspects := buckets.CallCount("Inspect") |
| + buckets.Facts[name] = goodBucket() |
| second, code, _, err := eligible(t, fx) |
| if err != nil || code != "" { |
| t.Fatalf("replacement store: %q %v", code, err) |
| } |
| c := second.Conversion |
| - if c.Minted || c.VerifiedAt == nil || c.Incarnation == first.Conversion.Incarnation || !mediaHex32.MatchString(c.Incarnation) { |
| + if c.Incarnation == first.Conversion.Incarnation || c.VerifiedAt == nil || c.Uploads == nil || c.Uploads.CORSHash != up.CORSHash { |
| t.Fatalf("replacement conversion = %+v, first incarnation %q", c, first.Conversion.Incarnation) |
| } |
| - if buckets.CallCount("Inspect") != inspects+1 { |
| - t.Fatal("the replacement was not inspected") |
| + if got := bucketTag(fx, buckets); got != c.Incarnation { |
| + t.Fatalf("replacement tag = %q, record %q", got, c.Incarnation) |
| + } |
| +} |
| + |
| +// Dispatch's view (mediajobs.StoreGuard.Current, wired by Task 5) is the |
| +// same check: it reads the tag, verifies a recreated bucket afresh, and |
| +// answers no incarnation for a store that is not eligible. |
| +func TestMediaStoreFactsIsTheSameCheck(t *testing.T) { |
| + fx, _, buckets, _ := grantedMediaStore(t, "code-media-facts") |
| + ctx := context.Background() |
| + facts := func() (string, bool, error) { |
| + defer fx.srv.storeWalks.lock(fx.sqid, "titogo", "")() |
| + f, err := fx.srv.mediaStoreFacts(ctx, fx.sqid, "titogo") |
| + return f.Incarnation, f.Exists, err |
| + } |
| + first, code, _, err := eligible(t, fx) |
| + if err != nil || code != "" { |
| + t.Fatalf("door: %q %v", code, err) |
| + } |
| + if inc, exists, err := facts(); err != nil || !exists || inc != first.Conversion.Incarnation { |
| + t.Fatalf("dispatch on a verified store: %q %v %v", inc, exists, err) |
| + } |
| + |
| + name := mediaMintedName(fx) |
| + buckets.DropBucket(name) |
| + buckets.Facts[name] = goodBucket() |
| + inc, exists, err := facts() |
| + if err != nil || !exists || inc == "" || inc == first.Conversion.Incarnation { |
| + t.Fatalf("dispatch on a recreated bucket: %q %v %v; want a fresh incarnation", inc, exists, err) |
| + } |
| + |
| + buckets.DropBucket(name) |
| + versioned := goodBucket() |
| + versioned.Versioning = "Enabled" |
| + buckets.Facts[name] = versioned |
| + if inc, exists, err := facts(); err != nil || !exists || inc != "" { |
| + t.Fatalf("dispatch on an unverifiable store: %q %v %v; want no incarnation", inc, exists, err) |
| + } |
| + |
| + buckets.Fail["BucketTags"] = errors.New("injected") |
| + if _, _, err := facts(); err == nil { |
| + t.Fatal("dispatch on unreadable tags answered no error") |
| } |
| } |
diff --git a/internal/objstore/aws.go b/internal/objstore/aws.go| index 2e879a025..341b0295f 100644 |
| --- a/internal/objstore/aws.go |
| +++ b/internal/objstore/aws.go |
| @@ -56,11 +56,6 @@ type awsProvisioner struct { |
| // user this provisioner creates. See EnsureUser for why it is not |
| // optional in any deployment whose console role can reach IAM. |
| boundary string |
| - // endpoint is the S3-compatible endpoint the S3 client was built for, "" |
| - // for AWS itself. CreateNewBucket never answers created through one: an |
| - // S3-compatible backend's answer to re-creating a bucket its caller |
| - // already owns is not AWS's, so a success there proves nothing. |
| - endpoint string |
| } |
| var _ Provisioner = (*awsProvisioner)(nil) |
| @@ -105,7 +100,7 @@ func NewAWSProvisionerWith(ctx context.Context, region, endpoint, boundary strin |
| return nil, fmt.Errorf("objstore: load aws config: %w", err) |
| } |
| iamClient := iam.NewFromConfig(cfg) |
| - return &awsProvisioner{s3: s3Client, iam: iamClient, region: region, boundary: boundary, endpoint: endpoint}, nil |
| + return &awsProvisioner{s3: s3Client, iam: iamClient, region: region, boundary: boundary}, nil |
| } |
| // S3ClientFor builds a bare *s3.Client the same way NewAWSProvisioner builds |
| @@ -296,25 +291,8 @@ func isNoSuchBucket(err error) bool { |
| // from "someone else got the name first", and only the former is safe to |
| // swallow. |
| func (p *awsProvisioner) EnsureBucket(ctx context.Context, name string) (string, error) { |
| - region, _, err := p.CreateNewBucket(ctx, name) |
| - return region, err |
| -} |
| - |
| -var _ BucketCreator = (*awsProvisioner)(nil) |
| - |
| -// CreateNewBucket is EnsureBucket that also says whether this call created |
| -// the bucket. Only a success from AWS's own S3 outside us-east-1 counts: |
| -// there, an existing bucket of ours answers BucketAlreadyOwnedByYou. In |
| -// us-east-1 (and with no region, which lands there) AWS answers a bare |
| -// success to an owner creating its own bucket again, so a success proves |
| -// nothing and is created == false. Through a custom endpoint (an |
| -// S3-compatible backend) it is false too: nothing says that backend |
| -// refuses a re-create the way AWS does. |
| -func (p *awsProvisioner) CreateNewBucket(ctx context.Context, name string) (string, bool, error) { |
| in := &awss3.CreateBucketInput{Bucket: aws.String(name)} |
| - located := p.region != "" && p.region != "us-east-1" |
| - provable := located && p.endpoint == "" |
| - if located { |
| + if p.region != "" && p.region != "us-east-1" { |
| in.CreateBucketConfiguration = &s3types.CreateBucketConfiguration{ |
| LocationConstraint: s3types.BucketLocationConstraint(p.region), |
| } |
| @@ -322,11 +300,10 @@ func (p *awsProvisioner) CreateNewBucket(ctx context.Context, name string) (stri |
| if _, err := p.s3.CreateBucket(ctx, in); err != nil { |
| var owned *s3types.BucketAlreadyOwnedByYou |
| if !errors.As(err, &owned) { |
| - return "", false, fmt.Errorf("objstore: create bucket %q: %w", name, err) |
| + return "", fmt.Errorf("objstore: create bucket %q: %w", name, err) |
| } |
| - return p.region, false, nil |
| } |
| - return p.region, provable, nil |
| + return p.region, nil |
| } |
| // EmptyBucket deletes every object in name, paginating ListObjectsV2 and |
diff --git a/internal/objstore/aws_test.go b/internal/objstore/aws_test.go| index 3d819ca9b..073c835f9 100644 |
| --- a/internal/objstore/aws_test.go |
| +++ b/internal/objstore/aws_test.go |
| @@ -4,7 +4,6 @@ import ( |
| "context" |
| "encoding/json" |
| "testing" |
| - "time" |
| "github.com/aws/aws-sdk-go-v2/aws" |
| "github.com/aws/aws-sdk-go-v2/service/iam" |
| @@ -12,7 +11,6 @@ import ( |
| awss3 "github.com/aws/aws-sdk-go-v2/service/s3" |
| s3types "github.com/aws/aws-sdk-go-v2/service/s3/types" |
| "github.com/aws/smithy-go" |
| - "github.com/carlosframework/platform/internal/release" |
| ) |
| // TestNewAWSProvisioner is construction-only: it must succeed offline (the |
| @@ -643,125 +641,3 @@ func TestEnsureUserOmitsAnEmptyBoundary(t *testing.T) { |
| t.Fatal("PermissionsBoundary was set from an empty boundary; want the field omitted") |
| } |
| } |
| - |
| -// CreateNewBucket answers created only for a success that proves this call |
| -// made the bucket. us-east-1 (and no region, which lands there) answers a |
| -// bare success to an owner re-creating its own bucket, so that proves |
| -// nothing; BucketAlreadyOwnedByYou is the owned bucket, not a new one. |
| -func TestAWSCreateNewBucketTable(t *testing.T) { |
| - owned := func(*awss3.CreateBucketInput) (*awss3.CreateBucketOutput, error) { |
| - return nil, &s3types.BucketAlreadyOwnedByYou{Message: aws.String("already yours")} |
| - } |
| - ok := func(*awss3.CreateBucketInput) (*awss3.CreateBucketOutput, error) { |
| - return &awss3.CreateBucketOutput{}, nil |
| - } |
| - taken := func(*awss3.CreateBucketInput) (*awss3.CreateBucketOutput, error) { |
| - return nil, &s3types.BucketAlreadyExists{Message: aws.String("someone else's")} |
| - } |
| - for _, tc := range []struct { |
| - name, region, endpoint string |
| - create func(*awss3.CreateBucketInput) (*awss3.CreateBucketOutput, error) |
| - created bool |
| - wantErr bool |
| - }{ |
| - {"success", "eu-west-1", "", ok, true, false}, |
| - {"already-owned", "eu-west-1", "", owned, false, false}, |
| - {"us-east-1-success", "us-east-1", "", ok, false, false}, |
| - {"no-region-success", "", "", ok, false, false}, |
| - {"us-east-1-already-owned", "us-east-1", "", owned, false, false}, |
| - {"taken", "eu-west-1", "", taken, false, true}, |
| - // An S3-compatible backend may answer 200 to an owner re-creating |
| - // its own bucket, so its success proves nothing. |
| - {"custom-endpoint-success", "eu-west-1", "https://s3.example.net", ok, false, false}, |
| - {"custom-endpoint-already-owned", "eu-west-1", "https://s3.example.net", owned, false, false}, |
| - } { |
| - t.Run(tc.name, func(t *testing.T) { |
| - p := &awsProvisioner{region: tc.region, endpoint: tc.endpoint, s3: &stubS3{t: t, createBucket: tc.create}} |
| - region, created, err := p.CreateNewBucket(context.Background(), "b1") |
| - if tc.wantErr { |
| - if err == nil || created { |
| - t.Fatalf("CreateNewBucket = %v, %v; want an error and not created", created, err) |
| - } |
| - return |
| - } |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if created != tc.created || region != tc.region { |
| - t.Fatalf("CreateNewBucket = (%q, %v), want (%q, %v)", region, created, tc.region, tc.created) |
| - } |
| - // EnsureBucket answers the same, without the flag. |
| - if r, err := p.EnsureBucket(context.Background(), "b1"); err != nil || r != tc.region { |
| - t.Fatalf("EnsureBucket = %q, %v", r, err) |
| - } |
| - }) |
| - } |
| -} |
| - |
| -// The constructor records the endpoint it built the S3 client for, which is |
| -// what CreateNewBucket reads. |
| -func TestNewAWSProvisionerRecordsEndpoint(t *testing.T) { |
| - t.Setenv("AWS_ACCESS_KEY_ID", "AKIDEXAMPLE") |
| - t.Setenv("AWS_SECRET_ACCESS_KEY", "secret") |
| - for _, endpoint := range []string{"", "https://s3.example.net"} { |
| - p, err := NewAWSProvisioner(context.Background(), "eu-west-1", endpoint, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if got := p.(*awsProvisioner).endpoint; got != endpoint { |
| - t.Fatalf("endpoint = %q, want %q", got, endpoint) |
| - } |
| - } |
| -} |
| - |
| -// A whole first grant through the AWS provisioner: minted against AWS's own |
| -// S3 outside us-east-1, and never through a custom endpoint, even though the |
| -// create succeeds the same way. |
| -func TestAWSGrantMintsOnlyWithoutCustomEndpoint(t *testing.T) { |
| - for _, tc := range []struct { |
| - name, endpoint string |
| - minted bool |
| - }{ |
| - {"aws", "", true}, |
| - {"custom-endpoint", "https://s3.example.net", false}, |
| - } { |
| - t.Run(tc.name, func(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - p := &awsProvisioner{region: "eu-west-1", endpoint: tc.endpoint, |
| - s3: &stubS3{t: t, createBucket: func(*awss3.CreateBucketInput) (*awss3.CreateBucketOutput, error) { |
| - return &awss3.CreateBucketOutput{}, nil |
| - }}, |
| - iam: &stubIAM{t: t, |
| - createUser: func(in *iam.CreateUserInput) (*iam.CreateUserOutput, error) { |
| - return &iam.CreateUserOutput{User: &iamtypes.User{Arn: aws.String("arn:aws:iam::123456789012:user/carlos-store/" + aws.ToString(in.UserName))}}, nil |
| - }, |
| - putUserPolicy: func(*iam.PutUserPolicyInput) (*iam.PutUserPolicyOutput, error) { |
| - return &iam.PutUserPolicyOutput{}, nil |
| - }, |
| - listAccessKeys: func(*iam.ListAccessKeysInput) (*iam.ListAccessKeysOutput, error) { |
| - return &iam.ListAccessKeysOutput{}, nil |
| - }, |
| - createAccessKey: func(*iam.CreateAccessKeyInput) (*iam.CreateAccessKeyOutput, error) { |
| - return &iam.CreateAccessKeyOutput{AccessKey: &iamtypes.AccessKey{ |
| - AccessKeyId: aws.String("AKIAEXAMPLE"), SecretAccessKey: aws.String("secretexample")}}, nil |
| - }, |
| - }} |
| - d := testDeployment() |
| - d.Endpoint = tc.endpoint |
| - rec, err := Grant(context.Background(), s, p, d, storeTestSealer(pub, "bab", "titogo"), |
| - "bab", "titogo", "op", GrantSourceOperator, "", time.Now().UTC()) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - got := rec.Conversion != nil && rec.Conversion.Minted |
| - if got != tc.minted { |
| - t.Fatalf("minted = %v (%+v), want %v", got, rec.Conversion, tc.minted) |
| - } |
| - if !tc.minted && rec.Conversion != nil { |
| - t.Fatalf("conversion = %+v through a custom endpoint", rec.Conversion) |
| - } |
| - }) |
| - } |
| -} |
diff --git a/internal/objstore/conversion_test.go b/internal/objstore/conversion_test.go| new file mode 100644 |
| index 000000000..6ed2d9702 |
| --- /dev/null |
| +++ b/internal/objstore/conversion_test.go |
| @@ -0,0 +1,149 @@ |
| +package objstore |
| + |
| +import ( |
| + "context" |
| + "crypto/ed25519" |
| + "crypto/rand" |
| + "encoding/json" |
| + "errors" |
| + "os" |
| + "reflect" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +// The conversion block (media conversion spec §5.2, amendment A3). |
| + |
| +func testConversionBlock() *Conversion { |
| + at := time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) |
| + return &Conversion{Incarnation: "0123456789abcdef0123456789abcdef", VerifiedAt: &at, |
| + Uploads: &Uploads{Origins: []string{"https://shop.example.com"}, CORSHash: "c0ffee", LifecycleHash: "beef", |
| + At: at, By: "member@example.com"}} |
| +} |
| + |
| +// The block's wire names are the brief's, and a record written before it |
| +// existed decodes with none. |
| +func TestConversionBlockWireNames(t *testing.T) { |
| + b, err := json.Marshal(testConversionBlock()) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want := `{"incarnation":"0123456789abcdef0123456789abcdef","verified_at":"2026-11-17T09:00:00Z","uploads":{"origins":["https://shop.example.com"],"cors_hash":"c0ffee","lifecycle_hash":"beef","at":"2026-11-17T09:00:00Z","by":"member@example.com"}}` |
| + if string(b) != want { |
| + t.Fatalf("wire = %s\nwant %s", b, want) |
| + } |
| + var rec Record |
| + if err := json.Unmarshal([]byte(`{"schema":1,"app":"titogo"}`), &rec); err != nil || rec.Conversion != nil { |
| + t.Fatalf("old record: %+v, %v", rec.Conversion, err) |
| + } |
| +} |
| + |
| +// The conversion block is additive: release.PurgeApp's local struct ignores |
| +// it in both directions, exactly as TestPurgeGuardIgnoresScanningBlock asserts |
| +// for the scanning block. |
| +func TestPurgeGuardIgnoresConversionBlock(t *testing.T) { |
| + ctx := context.Background() |
| + _, pub := testSealKeypair(t) |
| + relPub, _, err := ed25519.GenerateKey(rand.Reader) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + backdated := time.Now().Add(-2 * release.TrashCooloff) |
| + |
| + t.Run("provisioned-plus-conversion-still-blocks", func(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + p := NewMemProvisioner("eu-west-1") |
| + rotateFixture(t, s, pub, p) |
| + stored, etag, err := Get(ctx, s, "bab", "titogo") |
| + if err != nil || stored == nil { |
| + t.Fatalf("re-read: %+v, %v", stored, err) |
| + } |
| + stored.Conversion = testConversionBlock() |
| + if _, err := Put(ctx, s, "bab", "titogo", stored, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := release.TrashApp(ctx, s, "bab", "titogo", "paul@keymail.dev", backdated); err != nil { |
| + t.Fatal(err) |
| + } |
| + err = release.PurgeApp(ctx, s, relPub, "bab", "titogo", time.Now()) |
| + if !errors.Is(err, release.ErrStoreProvisioned) { |
| + t.Fatalf("PurgeApp over provisioned+conversion = %v, want release.ErrStoreProvisioned", err) |
| + } |
| + }) |
| + |
| + t.Run("conversion-alone-names-no-estate", func(t *testing.T) { |
| + s := release.NewDirStore(t.TempDir()) |
| + declare(t, s, "bab", "titogo", "TITO_S3") |
| + stored, etag, err := Get(ctx, s, "bab", "titogo") |
| + if err != nil || stored == nil { |
| + t.Fatalf("re-read: %+v, %v", stored, err) |
| + } |
| + stored.Conversion = testConversionBlock() |
| + if _, err := Put(ctx, s, "bab", "titogo", stored, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := release.TrashApp(ctx, s, "bab", "titogo", "paul@keymail.dev", backdated); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := release.PurgeApp(ctx, s, relPub, "bab", "titogo", time.Now()); err != nil { |
| + t.Fatalf("PurgeApp over a declared+conversion record: %v", err) |
| + } |
| + if _, _, err := s.Get(ctx, Key("bab", "titogo")); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("store.json survived the purge: %v", err) |
| + } |
| + }) |
| +} |
| + |
| +// failingDeleteStore fails the FIRST Delete of failKeyOnce, then behaves |
| +// normally: a teardown that dies after its destructive steps. |
| +type failingDeleteStore struct { |
| + release.Store |
| + failKeyOnce string |
| +} |
| + |
| +func (f *failingDeleteStore) Delete(ctx context.Context, key string) error { |
| + if key != "" && key == f.failKeyOnce { |
| + f.failKeyOnce = "" |
| + return errors.New("injected: delete failed") |
| + } |
| + return f.Store.Delete(ctx, key) |
| +} |
| + |
| +// Fix round 2 (A3, Astra's P2): teardown never touches the conversion block. |
| +// A teardown that destroys the bucket, the user and the delivered variables |
| +// and then fails leaves the record standing; the uploads hashes in it are |
| +// the only account of what the platform wrote, and they survive byte for |
| +// byte. (The incarnation surviving is harmless under A3: the replacement |
| +// bucket carries no tag, so the console verifies it afresh.) |
| +func TestFailedTeardownKeepsTheConversionBlock(t *testing.T) { |
| + ctx := context.Background() |
| + _, pub := testSealKeypair(t) |
| + s := &failingDeleteStore{Store: release.NewDirStore(t.TempDir())} |
| + p := NewMemProvisioner("eu-west-1") |
| + rec := rotateFixture(t, s, pub, p) |
| + stored, etag, err := Get(ctx, s, "bab", "titogo") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + stored.Conversion = testConversionBlock() |
| + if _, err := Put(ctx, s, "bab", "titogo", stored, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + |
| + s.failKeyOnce = ScanKey("bab", "titogo") |
| + if err := Teardown(ctx, s, p, nil, "bab", "titogo"); err == nil { |
| + t.Fatal("teardown: no error from the injected sidecar failure") |
| + } |
| + if _, ok := p.Buckets[rec.Provisioned.Bucket]; ok { |
| + t.Fatal("fixture: the bucket survived the teardown") |
| + } |
| + after, _, err := Get(ctx, s, "bab", "titogo") |
| + if err != nil || after == nil { |
| + t.Fatalf("record after the failed teardown: %+v, %v", after, err) |
| + } |
| + if !reflect.DeepEqual(after.Conversion, testConversionBlock()) { |
| + t.Fatalf("the failed teardown changed the conversion block:\n%+v\nwant\n%+v", after.Conversion, testConversionBlock()) |
| + } |
| +} |
diff --git a/internal/objstore/memprovisioner.go b/internal/objstore/memprovisioner.go| index b040e2147..233e3b18a 100644 |
| --- a/internal/objstore/memprovisioner.go |
| +++ b/internal/objstore/memprovisioner.go |
| @@ -40,10 +40,6 @@ type MemProvisioner struct { |
| // with a secret it cannot deliver (a real backend picks the bytes, so |
| // a walk must not assume they are renderable). |
| MintSecret string |
| - // PreExisting names buckets that exist before any walk runs, as a |
| - // bucket made by hand under the name the deployment would mint does. |
| - // CreateNewBucket answers created == false for them. |
| - PreExisting map[string]bool |
| } |
| // NewMemProvisioner returns an empty fake that answers region for every |
| @@ -69,31 +65,15 @@ func (p *MemProvisioner) takeFailure(method string) error { |
| } |
| func (p *MemProvisioner) EnsureBucket(ctx context.Context, name string) (string, error) { |
| - region, _, err := p.createBucket(name) |
| - return region, err |
| -} |
| - |
| -var _ BucketCreator = (*MemProvisioner)(nil) |
| - |
| -// CreateNewBucket is EnsureBucket answering whether this call made the |
| -// bucket: false when it already existed, in Buckets or in PreExisting. |
| -// It is the same bucket step, so FailNext["EnsureBucket"] fails it too. |
| -func (p *MemProvisioner) CreateNewBucket(ctx context.Context, name string) (string, bool, error) { |
| - return p.createBucket(name) |
| -} |
| - |
| -func (p *MemProvisioner) createBucket(name string) (string, bool, error) { |
| p.mu.Lock() |
| defer p.mu.Unlock() |
| if err := p.takeFailure("EnsureBucket"); err != nil { |
| - return "", false, err |
| + return "", err |
| } |
| - created := false |
| if p.Buckets[name] == nil { |
| p.Buckets[name] = map[string][]byte{} |
| - created = !p.PreExisting[name] |
| } |
| - return p.region, created, nil |
| + return p.region, nil |
| } |
| func (p *MemProvisioner) EnsureUser(ctx context.Context, name, path, bucketARN string, deny *DenyTag, denyWrites bool) (string, error) { |
diff --git a/internal/objstore/objstore.go b/internal/objstore/objstore.go| index 7a00b9d32..8e1a3c56a 100644 |
| --- a/internal/objstore/objstore.go |
| +++ b/internal/objstore/objstore.go |
| @@ -7,8 +7,6 @@ package objstore |
| import ( |
| "context" |
| - "crypto/rand" |
| - "encoding/hex" |
| "encoding/json" |
| "errors" |
| "fmt" |
| @@ -170,28 +168,25 @@ type Record struct { |
| // lets a regrant that failed after the record changed be retried |
| // (Astra plans-4b finding 1). |
| WritesDenyApplied *bool `json:"writes_deny_applied,omitempty"` |
| - // Conversion is the store's provenance for media conversion and browser |
| - // uploads (media conversion spec §5.2). Absent until the grant walk |
| - // records a bucket it created itself, or the console verifies one. |
| - // Additive, like Scanning: release.PurgeApp's local struct ignores it |
| - // (TestPurgeGuardIgnoresConversionBlock). |
| + // Conversion records that the store's bucket passed the media |
| + // conversion check (spec §5.2, amendment A3) and which incarnation of |
| + // the store that made it. Absent until the console first verifies the |
| + // bucket. Additive, like Scanning: release.PurgeApp's local struct |
| + // ignores it (TestPurgeGuardIgnoresConversionBlock). |
| Conversion *Conversion `json:"conversion,omitempty"` |
| } |
| -// Conversion says why a store's bucket may be used for conversion, and |
| -// which incarnation of the store this is. |
| +// Conversion is the console's record of a store's verification for media |
| +// conversion and browser uploads. |
| // |
| -// Incarnation is random and minted with the block. Teardown deletes the |
| -// record, so a store made again under the same app gets a new incarnation |
| -// and never inherits the old store's media jobs. |
| -// |
| -// Minted is true only when this deployment's grant walk itself created the |
| -// bucket (CreateNewBucket answered created on the first grant of a minted |
| -// name). VerifiedAt is set when the console checked an existing bucket's |
| -// settings instead. A block with neither is not yet eligible. |
| +// Incarnation is random, minted at a verification, and also written onto |
| +// the bucket itself as a tag (storecfg.IncarnationTag). The record alone |
| +// proves nothing: the console trusts it only while the bucket's tag reads |
| +// back the same value, so a bucket made again under the same name, which |
| +// carries no tag, is verified afresh and never inherits an old store's |
| +// media jobs, whatever state this record was left in. |
| type Conversion struct { |
| Incarnation string `json:"incarnation"` |
| - Minted bool `json:"minted,omitempty"` |
| VerifiedAt *time.Time `json:"verified_at,omitempty"` |
| Uploads *Uploads `json:"uploads,omitempty"` |
| } |
| @@ -207,15 +202,6 @@ type Uploads struct { |
| By string `json:"by"` |
| } |
| -// NewIncarnation is 32 hex characters from 16 bytes of crypto/rand. |
| -func NewIncarnation() (string, error) { |
| - var b [16]byte |
| - if _, err := rand.Read(b[:]); err != nil { |
| - return "", fmt.Errorf("objstore: incarnation: %w", err) |
| - } |
| - return hex.EncodeToString(b[:]), nil |
| -} |
| - |
| // WritesDenied says why a store refuses uploads, and since when. |
| type WritesDenied struct { |
| Reason string `json:"reason"` |
diff --git a/internal/objstore/provisioner.go b/internal/objstore/provisioner.go| index 867bbb46c..d5b0b8da5 100644 |
| --- a/internal/objstore/provisioner.go |
| +++ b/internal/objstore/provisioner.go |
| @@ -33,20 +33,3 @@ type Provisioner interface { |
| // DeleteUser removes the user's keys and inline policy first. |
| DeleteUser(ctx context.Context, user string) error |
| } |
| - |
| -// BucketCreator is an optional Provisioner capability: create a bucket and |
| -// say whether THIS call created it. The grant walk uses it to record that |
| -// a store's bucket is one this deployment made (Conversion.Minted, media |
| -// conversion spec §5.2), which no other evidence can establish. |
| -// |
| -// created must be true only when the backend answered that this very call |
| -// made the bucket. "Already owned by you", or any success the backend also |
| -// gives to an owner creating an existing bucket again, is created == false: |
| -// the bucket may predate the walk, and its settings are then unknown. When |
| -// unsure, answer false; the cost is a verification, never a refusal. |
| -// |
| -// A Provisioner that does not implement it is treated as never creating |
| -// one, so every store it backs is verified before conversion uses it. |
| -type BucketCreator interface { |
| - CreateNewBucket(ctx context.Context, name string) (region string, created bool, err error) |
| -} |
diff --git a/internal/objstore/walk.go b/internal/objstore/walk.go| index 939683f6c..f0aa74a42 100644 |
| --- a/internal/objstore/walk.go |
| +++ b/internal/objstore/walk.go |
| @@ -282,11 +282,6 @@ func claimRawGrantIn(ctx context.Context, s release.Store, p Provisioner, d Depl |
| if clearMark { |
| rec.Declared.Seed = "" |
| } |
| - // Whether this run is the one choosing the bucket, for step 3's |
| - // provenance: only a first grant of a minted name can be a walk that |
| - // made its own bucket. A re-run cannot tell its earlier self's create |
| - // from a bucket that was already there, so it never claims one. |
| - minting := rec.Grant == nil && adoptBucket == "" && bucket == MintedBucketIn(d, acct, app, env) |
| if rec.Grant == nil || clearMark { |
| if rec.Grant == nil { |
| rec.Grant = &GrantBlock{Backend: d.Backend, Bucket: bucket, Source: source, At: now, By: actor} |
| @@ -332,53 +327,11 @@ func claimRawGrantIn(ctx context.Context, s release.Store, p Provisioner, d Depl |
| } else if healthy { |
| return rec, nil |
| } |
| - // 3. the bucket. A provisioner that can say so reports whether this call |
| - // created it; any other counts as not having created it. |
| - var region string |
| - created := false |
| - if bc, ok := p.(BucketCreator); ok { |
| - region, created, err = bc.CreateNewBucket(ctx, bucket) |
| - } else { |
| - region, err = p.EnsureBucket(ctx, bucket) |
| - } |
| + // 3. the bucket |
| + region, err := p.EnsureBucket(ctx, bucket) |
| if err != nil { |
| return nil, fmt.Errorf("objstore: ensure bucket %s: %w", bucket, err) |
| } |
| - // Provenance (media conversion spec §5.2): a bucket this walk created, |
| - // under the name it minted, on the first grant, holds the settings a new |
| - // bucket gets and nothing else, so it is eligible for conversion without |
| - // a check. Everything else (an adopted bucket, a bucket already there |
| - // under the minted name, a re-run after the create) stays unminted and |
| - // is verified by the console before first use. The block rides the next |
| - // record write, step 6; a walk that fails before then loses it, and the |
| - // re-run that follows does not mint, which is the safe direction. |
| - var minted *Conversion |
| - if created && minting && rec.Conversion == nil { |
| - inc, err := NewIncarnation() |
| - if err != nil { |
| - return nil, err |
| - } |
| - minted = &Conversion{Incarnation: inc, Minted: true} |
| - rec.Conversion = minted |
| - } |
| - // Provenance describes one bucket at one place. A walk that found the |
| - // store's bucket gone and made another (a teardown that died before its |
| - // record went, a bucket deleted by hand), or that moves the store to |
| - // another endpoint or region, invalidates whatever the record held: an |
| - // old incarnation must never name the replacement, and a Minted claim |
| - // must never outlive the bucket it was about. Persisted here, before the |
| - // key and delivery steps, so a walk that fails after this point cannot |
| - // leave the old block standing over the new estate. The console then |
| - // verifies the store afresh, with a new incarnation. |
| - if minted == nil && rec.Conversion != nil { |
| - moved := rec.Provisioned != nil && (rec.Provisioned.Endpoint != d.Endpoint || rec.Provisioned.Region != region) |
| - if created || moved { |
| - rec.Conversion = nil |
| - if etag, err = PutIn(ctx, s, acct, app, env, rec, etag); err != nil { |
| - return nil, fmt.Errorf("objstore: clear conversion provenance for %s/%s: %w", acct, app, err) |
| - } |
| - } |
| - } |
| // 4. key. Reap first: reaching here means delivery is missing, so any |
| // existing CURRENT key is either undelivered (its secret died with a |
| // failed config write — unrecoverable) or stranded by a crash between mint |
| @@ -443,11 +396,6 @@ func claimRawGrantIn(ctx context.Context, s release.Store, p Provisioner, d Depl |
| return nil, err |
| } |
| fresh.Provisioned = rec.Provisioned |
| - // The provenance step 3 minted is this walk's too. A block the |
| - // competing writer recorded meanwhile is theirs and stays. |
| - if minted != nil && fresh.Conversion == nil { |
| - fresh.Conversion = minted |
| - } |
| if _, perr := PutIn(ctx, s, acct, app, env, fresh, freshEtag); perr != nil { |
| return nil, perr |
| } |
| @@ -645,7 +593,7 @@ func claimRawTeardown(ctx context.Context, s release.Store, p Provisioner, sc Sc |
| // the bucket, the principal and the bundle the vars come out of are all named |
| // by this record alone. |
| func claimRawTeardownIn(ctx context.Context, s release.Store, p Provisioner, sc Scanner, acct, app, env string) error { |
| - rec, etag, err := GetIn(ctx, s, acct, app, env) |
| + rec, _, err := GetIn(ctx, s, acct, app, env) |
| if err != nil { |
| return err |
| } |
| @@ -710,17 +658,6 @@ func claimRawTeardownIn(ctx context.Context, s release.Store, p Provisioner, sc |
| if bucketShared { |
| bucket = "" |
| } |
| - // Provenance goes first, before anything is destroyed (media conversion |
| - // spec §5.2). A teardown that fails partway leaves the record standing, |
| - // and a later grant may make a replacement bucket under the same name; |
| - // the old incarnation and any Minted claim must not survive to describe |
| - // it. A lost write here stops the walk with nothing destroyed. |
| - if rec.Conversion != nil { |
| - rec.Conversion = nil |
| - if _, err := PutIn(ctx, s, acct, app, env, rec, etag); err != nil { |
| - return fmt.Errorf("objstore: clear conversion provenance for %s/%s: %w", acct, app, err) |
| - } |
| - } |
| // The scanner goes BEFORE the bucket, while the record that names it still |
| // exists. A Malware Protection plan (and its managed EventBridge rule) |
| // outlives the bucket it protects, counting against a non-adjustable |
diff --git a/internal/objstore/walk_test.go b/internal/objstore/walk_test.go| index 6cc0a4761..2bf057e83 100644 |
| --- a/internal/objstore/walk_test.go |
| +++ b/internal/objstore/walk_test.go |
| @@ -1,15 +1,12 @@ |
| package objstore |
| import ( |
| - "bytes" |
| "context" |
| "crypto/ecdh" |
| "crypto/ed25519" |
| "crypto/rand" |
| - "encoding/json" |
| "errors" |
| "os" |
| - "regexp" |
| "strings" |
| "testing" |
| "time" |
| @@ -1582,473 +1579,3 @@ func TestGrantRefusesRetryAgainstAnotherProvision(t *testing.T) { |
| t.Fatalf("the competing grant was clobbered: %+v, %v", stored.Provisioned, err) |
| } |
| } |
| - |
| -// ---- conversion provenance (media conversion spec §5.2) ------------------- |
| - |
| -var hex32 = regexp.MustCompile(`^[0-9a-f]{32}$`) |
| - |
| -func grantOnce(t *testing.T, s release.Store, p Provisioner, pub *ecdh.PublicKey, adopt string) (*Record, error) { |
| - t.Helper() |
| - return Grant(context.Background(), s, p, testDeployment(), storeTestSealer(pub, "bab", "titogo"), |
| - "bab", "titogo", "op", GrantSourceOperator, adopt, time.Now().UTC()) |
| -} |
| - |
| -func storedConversion(t *testing.T, s release.Store) *Conversion { |
| - t.Helper() |
| - rec, _, err := Get(context.Background(), s, "bab", "titogo") |
| - if err != nil || rec == nil { |
| - t.Fatalf("re-read: %+v, %v", rec, err) |
| - } |
| - return rec.Conversion |
| -} |
| - |
| -// The first grant of a minted name, whose bucket the walk itself created, |
| -// records that in the walk's next write: Minted, a fresh incarnation, no |
| -// verification time. |
| -func TestFirstGrantRecordsMinted(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - c := rec.Conversion |
| - if c == nil || !c.Minted || c.VerifiedAt != nil || c.Uploads != nil || !hex32.MatchString(c.Incarnation) { |
| - t.Fatalf("conversion = %+v, want minted with a 32-hex incarnation", c) |
| - } |
| - stored := storedConversion(t, s) |
| - if stored == nil || *stored != *c { |
| - t.Fatalf("stored conversion = %+v, want %+v", stored, c) |
| - } |
| - |
| - // A healthy re-run is a no-op, and a re-run that walks the whole |
| - // sequence again keeps the block it found: same incarnation. |
| - if _, err := grantOnce(t, s, p, pub, ""); err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, err := appconfig.Apply(context.Background(), s, "bab", "titogo", nil, []string{"TITO_S3_BUCKET"}); err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, err := grantOnce(t, s, p, pub, ""); err != nil { |
| - t.Fatal(err) |
| - } |
| - if again := storedConversion(t, s); again == nil || *again != *c { |
| - t.Fatalf("re-run changed the conversion block: %+v, want %+v", again, c) |
| - } |
| -} |
| - |
| -// An adopted bucket never earns Minted, even when it carries the name the |
| -// deployment would mint and the walk happened to create it. |
| -func TestAdoptedUnderMintedNameIsNotMinted(t *testing.T) { |
| - for name, adopt := range map[string]string{ |
| - "minted-name": MintedBucket(testDeployment(), "bab", "titogo"), |
| - "other-name": "titogo-uploads", |
| - } { |
| - t.Run(name, func(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - rec, err := grantOnce(t, s, p, pub, adopt) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, ok := p.Buckets[adopt]; !ok { |
| - t.Fatalf("fixture: bucket %q not made", adopt) |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("adopted bucket recorded provenance: %+v", rec.Conversion) |
| - } |
| - }) |
| - } |
| -} |
| - |
| -// A bucket that was already there under the minted name (made by hand, say) |
| -// is not one this walk created, so it stays unminted and is verified. |
| -func TestPreExistingMintedNameIsNotMinted(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - minted := MintedBucket(testDeployment(), "bab", "titogo") |
| - p.PreExisting = map[string]bool{minted: true} |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if rec.Provisioned == nil || rec.Provisioned.Bucket != minted { |
| - t.Fatalf("provisioned = %+v, want the minted name", rec.Provisioned) |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("pre-existing bucket recorded provenance: %+v", rec.Conversion) |
| - } |
| -} |
| - |
| -// A walk that created the bucket and then failed loses its claim: the re-run |
| -// finds the bucket already there and cannot tell that its earlier self made |
| -// it, so it does not mint. |
| -func TestRerunAfterCreateIsNotMinted(t *testing.T) { |
| - for _, method := range []string{"EnsureUser", "MintKey"} { |
| - t.Run(method, func(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - if method == "EnsureUser" { |
| - // EnsureUser runs before the bucket step, so fail it on the |
| - // SECOND grant's walk instead: first grant reaches the bucket |
| - // and dies at MintKey, the re-run fails EnsureUser, the third |
| - // run completes. |
| - p.FailNext["MintKey"] = errors.New("injected") |
| - if _, err := grantOnce(t, s, p, pub, ""); err == nil { |
| - t.Fatal("first grant: no error") |
| - } |
| - p.FailNext["EnsureUser"] = errors.New("injected") |
| - if _, err := grantOnce(t, s, p, pub, ""); err == nil { |
| - t.Fatal("second grant: no error") |
| - } |
| - } else { |
| - p.FailNext[method] = errors.New("injected") |
| - if _, err := grantOnce(t, s, p, pub, ""); err == nil { |
| - t.Fatal("first grant: no error") |
| - } |
| - } |
| - if _, ok := p.Buckets[MintedBucket(testDeployment(), "bab", "titogo")]; !ok { |
| - t.Fatal("fixture: the failed walk never created the bucket") |
| - } |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if rec.Provisioned == nil { |
| - t.Fatal("re-run did not provision") |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("re-run after the create recorded provenance: %+v", rec.Conversion) |
| - } |
| - }) |
| - } |
| - |
| - t.Run("delivery", func(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := &failingStore{Store: release.NewDirStore(t.TempDir()), failKeyOnce: appconfig.Key("bab", "titogo")} |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - if _, err := grantOnce(t, s, p, pub, ""); err == nil { |
| - t.Fatal("first grant: no error") |
| - } |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("re-run after a failed delivery recorded provenance: %+v", rec.Conversion) |
| - } |
| - }) |
| -} |
| - |
| -// A provisioner without BucketCreator (the console's claim wrappers) never |
| -// mints: every store it backs is verified instead. |
| -func TestGrantWithoutBucketCreatorIsNotMinted(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := ensureOnly{NewMemProvisioner("eu-west-1")} |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if rec.Conversion != nil { |
| - t.Fatalf("conversion = %+v through a plain Provisioner", rec.Conversion) |
| - } |
| -} |
| - |
| -// ensureOnly hides MemProvisioner's CreateNewBucket: embedding the interface |
| -// promotes only Provisioner's methods. |
| -type ensureOnly struct{ Provisioner } |
| - |
| -// racesProvisionedPut lands race on the store record just before the walk's |
| -// step-6 write (the first Put of the record that carries a provisioned block), |
| -// so a first grant's lost CAS can be exercised: the step-1 write goes through. |
| -type racesProvisionedPut struct { |
| - release.Store |
| - race func(inner release.Store) |
| -} |
| - |
| -func (r *racesProvisionedPut) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| - if r.race != nil && key == Key("bab", "titogo") && bytes.Contains(body, []byte(`"provisioned"`)) { |
| - race := r.race |
| - r.race = nil |
| - race(r.Store) |
| - } |
| - return r.Store.Put(ctx, key, body, ifMatch) |
| -} |
| - |
| -// The step-6 retry carries the block this walk minted, as it carries the |
| -// provisioned block; a first grant that lost its CAS must not lose its |
| -// provenance silently while keeping the store. |
| -func TestFirstGrantMintedSurvivesLostCAS(t *testing.T) { |
| - ctx := context.Background() |
| - _, pub := testSealKeypair(t) |
| - s := &racesProvisionedPut{Store: release.NewDirStore(t.TempDir())} |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - s.race = func(in release.Store) { |
| - other, etag, err := Get(ctx, in, "bab", "titogo") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - other.Declared.By = "someone-else@example.com" |
| - if _, err := Put(ctx, in, "bab", "titogo", other, etag); err != nil { |
| - t.Fatal(err) |
| - } |
| - } |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - stored, _, err := Get(ctx, s, "bab", "titogo") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if stored.Declared.By != "someone-else@example.com" { |
| - t.Fatal("fixture: the race never landed") |
| - } |
| - if stored.Conversion == nil || !stored.Conversion.Minted || rec.Conversion == nil || |
| - stored.Conversion.Incarnation != rec.Conversion.Incarnation { |
| - t.Fatalf("stored conversion = %+v, returned %+v", stored.Conversion, rec.Conversion) |
| - } |
| -} |
| - |
| -// The conversion block is additive: release.PurgeApp's local struct ignores |
| -// it in both directions, exactly as TestPurgeGuardIgnoresScanningBlock asserts |
| -// for the scanning block. |
| -func TestPurgeGuardIgnoresConversionBlock(t *testing.T) { |
| - ctx := context.Background() |
| - _, pub := testSealKeypair(t) |
| - relPub, _, err := ed25519.GenerateKey(rand.Reader) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - backdated := time.Now().Add(-2 * release.TrashCooloff) |
| - verified := time.Now().UTC() |
| - block := &Conversion{Incarnation: "0123456789abcdef0123456789abcdef", VerifiedAt: &verified, |
| - Uploads: &Uploads{Origins: []string{"https://shop.example.com"}, CORSHash: "c", LifecycleHash: "l", At: verified, By: "m@example.com"}} |
| - |
| - t.Run("provisioned-plus-conversion-still-blocks", func(t *testing.T) { |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - rotateFixture(t, s, pub, p) |
| - stored, etag, err := Get(ctx, s, "bab", "titogo") |
| - if err != nil || stored == nil { |
| - t.Fatalf("re-read: %+v, %v", stored, err) |
| - } |
| - stored.Conversion = block |
| - if _, err := Put(ctx, s, "bab", "titogo", stored, etag); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := release.TrashApp(ctx, s, "bab", "titogo", "paul@keymail.dev", backdated); err != nil { |
| - t.Fatal(err) |
| - } |
| - err = release.PurgeApp(ctx, s, relPub, "bab", "titogo", time.Now()) |
| - if !errors.Is(err, release.ErrStoreProvisioned) { |
| - t.Fatalf("PurgeApp over provisioned+conversion = %v, want release.ErrStoreProvisioned", err) |
| - } |
| - }) |
| - |
| - t.Run("conversion-alone-names-no-estate", func(t *testing.T) { |
| - s := release.NewDirStore(t.TempDir()) |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - stored, etag, err := Get(ctx, s, "bab", "titogo") |
| - if err != nil || stored == nil { |
| - t.Fatalf("re-read: %+v, %v", stored, err) |
| - } |
| - stored.Conversion = block |
| - if _, err := Put(ctx, s, "bab", "titogo", stored, etag); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := release.TrashApp(ctx, s, "bab", "titogo", "paul@keymail.dev", backdated); err != nil { |
| - t.Fatal(err) |
| - } |
| - if err := release.PurgeApp(ctx, s, relPub, "bab", "titogo", time.Now()); err != nil { |
| - t.Fatalf("PurgeApp over a declared+conversion record: %v", err) |
| - } |
| - if _, _, err := s.Get(ctx, Key("bab", "titogo")); !errors.Is(err, os.ErrNotExist) { |
| - t.Fatalf("store.json survived the purge: %v", err) |
| - } |
| - }) |
| -} |
| - |
| -// The block's wire names are the brief's, and a record written before it |
| -// existed decodes with none. |
| -func TestConversionBlockWireNames(t *testing.T) { |
| - at := time.Date(2026, 11, 17, 9, 0, 0, 0, time.UTC) |
| - b, err := json.Marshal(Conversion{Incarnation: "i", VerifiedAt: &at, |
| - Uploads: &Uploads{Origins: []string{"https://shop.example.com"}, CORSHash: "c", LifecycleHash: "l", At: at, By: "m"}}) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - want := `{"incarnation":"i","verified_at":"2026-11-17T09:00:00Z","uploads":{"origins":["https://shop.example.com"],"cors_hash":"c","lifecycle_hash":"l","at":"2026-11-17T09:00:00Z","by":"m"}}` |
| - if string(b) != want { |
| - t.Fatalf("wire = %s\nwant %s", b, want) |
| - } |
| - var rec Record |
| - if err := json.Unmarshal([]byte(`{"schema":1,"app":"titogo"}`), &rec); err != nil || rec.Conversion != nil { |
| - t.Fatalf("old record: %+v, %v", rec.Conversion, err) |
| - } |
| -} |
| - |
| -// failingDeleteStore fails the FIRST Delete of failKeyOnce, then behaves |
| -// normally: a teardown that dies after its destructive steps. |
| -type failingDeleteStore struct { |
| - release.Store |
| - failKeyOnce string |
| -} |
| - |
| -func (f *failingDeleteStore) Delete(ctx context.Context, key string) error { |
| - if key != "" && key == f.failKeyOnce { |
| - f.failKeyOnce = "" |
| - return errors.New("injected: delete failed") |
| - } |
| - return f.Store.Delete(ctx, key) |
| -} |
| - |
| -// Astra fix round 1, P1: a teardown that destroys the bucket, the user and |
| -// the delivered variables and then fails (here, at the scan sidecar) leaves |
| -// the record standing. The grant that follows makes a replacement bucket |
| -// under the same name; the old incarnation and its Minted claim must not |
| -// describe it. |
| -func TestInterruptedTeardownThenRegrantDropsProvenance(t *testing.T) { |
| - ctx := context.Background() |
| - _, pub := testSealKeypair(t) |
| - inner := release.NewDirStore(t.TempDir()) |
| - s := &failingDeleteStore{Store: inner} |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - first, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if first.Conversion == nil || !first.Conversion.Minted { |
| - t.Fatalf("fixture: first grant not minted: %+v", first.Conversion) |
| - } |
| - bucket := first.Provisioned.Bucket |
| - |
| - s.failKeyOnce = ScanKey("bab", "titogo") |
| - if err := Teardown(ctx, s, p, nil, "bab", "titogo"); err == nil { |
| - t.Fatal("teardown: no error from the injected sidecar failure") |
| - } |
| - if _, ok := p.Buckets[bucket]; ok { |
| - t.Fatal("fixture: the bucket survived the teardown") |
| - } |
| - half, _, err := Get(ctx, s, "bab", "titogo") |
| - if err != nil || half == nil || half.Provisioned == nil { |
| - t.Fatalf("fixture: record after the failed teardown = %+v, %v", half, err) |
| - } |
| - if half.Conversion != nil { |
| - t.Fatalf("the failed teardown left provenance standing: %+v", half.Conversion) |
| - } |
| - |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, ok := p.Buckets[bucket]; !ok { |
| - t.Fatal("fixture: the regrant made no replacement bucket") |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("regrant over the replacement: %+v, want no provenance (verified afresh)", rec.Conversion) |
| - } |
| -} |
| - |
| -// The grant side, on its own: a bucket gone from under a standing record |
| -// (deleted by hand, or by a teardown from before provenance was cleared |
| -// first) is made again, and the walk drops the old block before delivery. |
| -func TestGrantReplacingTheBucketDropsProvenance(t *testing.T) { |
| - ctx := context.Background() |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - first, err := grantOnce(t, s, p, pub, "") |
| - if err != nil || first.Conversion == nil { |
| - t.Fatalf("first grant: %+v, %v", first, err) |
| - } |
| - delete(p.Buckets, first.Provisioned.Bucket) |
| - if _, err := appconfig.Apply(ctx, s, "bab", "titogo", nil, []string{"TITO_S3_BUCKET"}); err != nil { |
| - t.Fatal(err) |
| - } |
| - // Fail delivery, so only the step-3 write can have dropped the block. |
| - p.FailNext["MintKey"] = errors.New("injected") |
| - if _, err := grantOnce(t, s, p, pub, ""); err == nil { |
| - t.Fatal("regrant: no error from the injected failure") |
| - } |
| - if c := storedConversion(t, s); c != nil { |
| - t.Fatalf("a regrant that made a replacement bucket kept %+v", c) |
| - } |
| - if _, err := grantOnce(t, s, p, pub, ""); err != nil { |
| - t.Fatal(err) |
| - } |
| - if c := storedConversion(t, s); c != nil { |
| - t.Fatalf("after the regrant: %+v", c) |
| - } |
| -} |
| - |
| -// Moving a store to another endpoint invalidates its provenance: the block |
| -// described a bucket on the old backend. |
| -func TestGrantMovingTheEndpointDropsProvenance(t *testing.T) { |
| - ctx := context.Background() |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - first, err := grantOnce(t, s, p, pub, "") |
| - if err != nil || first.Conversion == nil { |
| - t.Fatalf("first grant: %+v, %v", first, err) |
| - } |
| - d := testDeployment() |
| - d.Endpoint = "https://s3.example.net" |
| - rec, err := Grant(ctx, s, p, d, storeTestSealer(pub, "bab", "titogo"), "bab", "titogo", "op", GrantSourceOperator, "", time.Now().UTC()) |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if rec.Provisioned.Endpoint != d.Endpoint { |
| - t.Fatalf("fixture: endpoint = %q", rec.Provisioned.Endpoint) |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("endpoint move kept %+v", rec.Conversion) |
| - } |
| -} |
| - |
| -// Fable fix round 1: the first-grant conjunct of `minting`. A first grant |
| -// adopts the minted name and dies at EnsureUser, before the bucket step, so |
| -// its grant block pins the name; the re-run, with no adopt, creates the |
| -// bucket. It is still an adopted bucket and must not mint. |
| -func TestAdoptedFirstGrantRerunIsNotMinted(t *testing.T) { |
| - _, pub := testSealKeypair(t) |
| - s := release.NewDirStore(t.TempDir()) |
| - p := NewMemProvisioner("eu-west-1") |
| - declare(t, s, "bab", "titogo", "TITO_S3") |
| - minted := MintedBucket(testDeployment(), "bab", "titogo") |
| - p.FailNext["EnsureUser"] = errors.New("injected") |
| - if _, err := grantOnce(t, s, p, pub, minted); err == nil { |
| - t.Fatal("first grant: no error") |
| - } |
| - if _, ok := p.Buckets[minted]; ok { |
| - t.Fatal("fixture: the first grant reached the bucket step") |
| - } |
| - rec, err := grantOnce(t, s, p, pub, "") |
| - if err != nil { |
| - t.Fatal(err) |
| - } |
| - if _, ok := p.Buckets[minted]; !ok || rec.Provisioned.Bucket != minted { |
| - t.Fatalf("fixture: re-run did not create %q", minted) |
| - } |
| - if rec.Conversion != nil || storedConversion(t, s) != nil { |
| - t.Fatalf("an adopted first grant's re-run minted: %+v", rec.Conversion) |
| - } |
| -} |
diff --git a/internal/storecfg/fake.go b/internal/storecfg/fake.go| index ec54b84c3..6ee277676 100644 |
| --- a/internal/storecfg/fake.go |
| +++ b/internal/storecfg/fake.go |
| @@ -11,9 +11,13 @@ import ( |
| // PutUploads and DeleteUploads change the bucket's CORS and Lifecycle the |
| // way a real backend would. Fail, keyed by method name, fails the next call |
| // to that method once. Calls records every call as "Method bucket". |
| +// |
| +// Tags maps a bucket name to its tag set; a bucket in Facts with no entry has |
| +// none. PutBucketTags replaces the set, as S3 does. |
| type Fake struct { |
| mu sync.Mutex |
| Facts map[string]Facts |
| + Tags map[string]map[string]string |
| Fail map[string]error |
| Calls []string |
| } |
| @@ -22,7 +26,7 @@ var _ Buckets = (*Fake)(nil) |
| // NewFake answers with no buckets. |
| func NewFake() *Fake { |
| - return &Fake{Facts: map[string]Facts{}, Fail: map[string]error{}} |
| + return &Fake{Facts: map[string]Facts{}, Tags: map[string]map[string]string{}, Fail: map[string]error{}} |
| } |
| // CallCount is how many calls to method the fake has seen. |
| @@ -80,3 +84,42 @@ func (f *Fake) DeleteUploads(ctx context.Context, bucket string) error { |
| f.Facts[bucket] = facts |
| return nil |
| } |
| + |
| +func (f *Fake) BucketTags(ctx context.Context, bucket string) (map[string]string, error) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if _, err := f.begin("BucketTags", bucket); err != nil { |
| + return nil, err |
| + } |
| + out := map[string]string{} |
| + for k, v := range f.Tags[bucket] { |
| + out[k] = v |
| + } |
| + return out, nil |
| +} |
| + |
| +func (f *Fake) PutBucketTags(ctx context.Context, bucket string, tags map[string]string) error { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + if _, err := f.begin("PutBucketTags", bucket); err != nil { |
| + return err |
| + } |
| + set := make(map[string]string, len(tags)) |
| + for k, v := range tags { |
| + set[k] = v |
| + } |
| + if f.Tags == nil { |
| + f.Tags = map[string]map[string]string{} |
| + } |
| + f.Tags[bucket] = set |
| + return nil |
| +} |
| + |
| +// DropBucket forgets the bucket, its facts and its tags: a bucket deleted |
| +// out from under a standing store record. |
| +func (f *Fake) DropBucket(bucket string) { |
| + f.mu.Lock() |
| + defer f.mu.Unlock() |
| + delete(f.Facts, bucket) |
| + delete(f.Tags, bucket) |
| +} |
diff --git a/internal/storecfg/storecfg.go b/internal/storecfg/storecfg.go| index 98824124b..97096a137 100644 |
| --- a/internal/storecfg/storecfg.go |
| +++ b/internal/storecfg/storecfg.go |
| @@ -12,10 +12,14 @@ package storecfg |
| import ( |
| "context" |
| + "crypto/rand" |
| "crypto/sha256" |
| "encoding/hex" |
| "encoding/json" |
| + "errors" |
| + "fmt" |
| "slices" |
| + "strings" |
| ) |
| // Facts is what a bucket holds, as Buckets.Inspect reads it. |
| @@ -43,6 +47,88 @@ type Buckets interface { |
| PutUploads(ctx context.Context, bucket string, origins []string) error |
| // DeleteUploads removes the bucket's CORS and lifecycle configuration. |
| DeleteUploads(ctx context.Context, bucket string) error |
| + // BucketTags answers every tag on the bucket: an empty map when it has |
| + // none (S3's NoSuchTagSet is not an error). Any error means the tags |
| + // are unknown, and every caller fails closed on it. A backend that |
| + // cannot hold bucket tags answers ErrTagsUnsupported, so a store there |
| + // can never be verified. |
| + BucketTags(ctx context.Context, bucket string) (map[string]string, error) |
| + // PutBucketTags REPLACES every tag on the bucket with tags, as S3's |
| + // PutBucketTagging does. Call it through WriteIncarnation, which keeps |
| + // the tags the platform does not own. |
| + PutBucketTags(ctx context.Context, bucket string, tags map[string]string) error |
| +} |
| + |
| +// IncarnationTag is the bucket tag carrying a store's incarnation (media |
| +// conversion spec amendment A3). It shares no name with any tag the |
| +// platform already writes: managed fleet buckets carry carlos:deployment and |
| +// carlos:fleet, the scanner's carlos-scan, carlos-scan-version and |
| +// carlos-scan-at are OBJECT tags, and the aws: prefix is AWS's own. |
| +// Tenants cannot write bucket tags at all; the store user's policy grants |
| +// object actions only. |
| +const IncarnationTag = "carlos-media-incarnation" |
| + |
| +// MaxBucketTags is S3's limit on tags per bucket. |
| +const MaxBucketTags = 50 |
| + |
| +// ErrTagsUnsupported is a storage backend with no bucket tags. Conversion |
| +// needs the incarnation on the bucket, so a store there is never eligible. |
| +var ErrTagsUnsupported = errors.New("storecfg: this storage backend cannot hold bucket tags, which media conversion needs") |
| + |
| +// NewIncarnation is 32 hex characters from 16 bytes of crypto/rand. |
| +func NewIncarnation() (string, error) { |
| + var b [16]byte |
| + if _, err := rand.Read(b[:]); err != nil { |
| + return "", fmt.Errorf("storecfg: incarnation: %w", err) |
| + } |
| + return hex.EncodeToString(b[:]), nil |
| +} |
| + |
| +// ValidIncarnation is the shape NewIncarnation makes. |
| +func ValidIncarnation(s string) bool { |
| + if len(s) != 32 { |
| + return false |
| + } |
| + _, err := hex.DecodeString(s) |
| + return err == nil && strings.ToLower(s) == s |
| +} |
| + |
| +// ReadIncarnation is the incarnation tag on the bucket, "" when it has |
| +// none. An error means it is unknown: the caller must treat the store as |
| +// not eligible, never as untagged. |
| +func ReadIncarnation(ctx context.Context, b Buckets, bucket string) (string, error) { |
| + tags, err := b.BucketTags(ctx, bucket) |
| + if err != nil { |
| + return "", fmt.Errorf("storecfg: read tags of %s: %w", bucket, err) |
| + } |
| + return tags[IncarnationTag], nil |
| +} |
| + |
| +// WriteIncarnation tags the bucket with inc, keeping every other tag it |
| +// carries: PutBucketTagging replaces the whole set, so the set is read |
| +// first, and a bucket whose tags cannot be read is not written. A bucket |
| +// already at S3's tag limit without the platform's tag is refused rather |
| +// than stripped of one of its own. |
| +func WriteIncarnation(ctx context.Context, b Buckets, bucket, inc string) error { |
| + if !ValidIncarnation(inc) { |
| + return fmt.Errorf("storecfg: incarnation %q is not 32 lowercase hex characters", inc) |
| + } |
| + tags, err := b.BucketTags(ctx, bucket) |
| + if err != nil { |
| + return fmt.Errorf("storecfg: read tags of %s before tagging it: %w", bucket, err) |
| + } |
| + merged := make(map[string]string, len(tags)+1) |
| + for k, v := range tags { |
| + merged[k] = v |
| + } |
| + merged[IncarnationTag] = inc |
| + if len(merged) > MaxBucketTags { |
| + return fmt.Errorf("storecfg: %s already carries %d tags, S3's limit; it has no room for %s", bucket, len(tags), IncarnationTag) |
| + } |
| + if err := b.PutBucketTags(ctx, bucket, merged); err != nil { |
| + return fmt.Errorf("storecfg: tag %s: %w", bucket, err) |
| + } |
| + return nil |
| } |
| // CORSRule is one CORS rule in canonical form. It carries every field an S3 |
diff --git a/internal/storecfg/storecfg_test.go b/internal/storecfg/storecfg_test.go| index 05a101a5e..f48154a64 100644 |
| --- a/internal/storecfg/storecfg_test.go |
| +++ b/internal/storecfg/storecfg_test.go |
| @@ -4,6 +4,7 @@ import ( |
| "bytes" |
| "context" |
| "errors" |
| + "fmt" |
| "os" |
| "os/exec" |
| "path/filepath" |
| @@ -219,3 +220,111 @@ func TestStorecfgLinksNoCloudSDK(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// ---- the incarnation tag (amendment A3) ------------------------------------ |
| + |
| +const incA = "0123456789abcdef0123456789abcdef" |
| +const incB = "fedcba9876543210fedcba9876543210" |
| + |
| +func TestNewIncarnationShape(t *testing.T) { |
| + a, err := NewIncarnation() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + b, _ := NewIncarnation() |
| + if !ValidIncarnation(a) || !ValidIncarnation(b) || a == b { |
| + t.Fatalf("NewIncarnation = %q, %q", a, b) |
| + } |
| + for _, bad := range []string{"", "0123", incA + "0", "0123456789ABCDEF0123456789ABCDEF", "0123456789abcdef0123456789abcdeg"} { |
| + if ValidIncarnation(bad) { |
| + t.Errorf("ValidIncarnation(%q) = true", bad) |
| + } |
| + } |
| +} |
| + |
| +// PutBucketTagging replaces the whole set, so the write keeps every tag the |
| +// platform does not own and replaces only its own. |
| +func TestWriteIncarnationKeepsOtherTags(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.Facts[testBucket] = goodFacts() |
| + f.Tags[testBucket] = map[string]string{"cost-centre": "shop", "team": "web", IncarnationTag: incA} |
| + if err := WriteIncarnation(ctx, f, testBucket, incB); err != nil { |
| + t.Fatal(err) |
| + } |
| + want := map[string]string{"cost-centre": "shop", "team": "web", IncarnationTag: incB} |
| + if got := f.Tags[testBucket]; len(got) != len(want) || got["cost-centre"] != "shop" || got["team"] != "web" || got[IncarnationTag] != incB { |
| + t.Fatalf("tags = %v, want %v", got, want) |
| + } |
| + if got, err := ReadIncarnation(ctx, f, testBucket); err != nil || got != incB { |
| + t.Fatalf("ReadIncarnation = %q, %v", got, err) |
| + } |
| +} |
| + |
| +// A bucket whose tags cannot be read is never written: writing blind would |
| +// strip whatever it carries. |
| +func TestWriteIncarnationFailsClosedOnUnreadableTags(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.Facts[testBucket] = goodFacts() |
| + f.Tags[testBucket] = map[string]string{"cost-centre": "shop"} |
| + f.Fail["BucketTags"] = errors.New("injected: access denied") |
| + if err := WriteIncarnation(ctx, f, testBucket, incA); err == nil { |
| + t.Fatal("WriteIncarnation over unreadable tags answered nil") |
| + } |
| + if n := f.CallCount("PutBucketTags"); n != 0 { |
| + t.Fatalf("PutBucketTags called %d times over unreadable tags", n) |
| + } |
| + if f.Tags[testBucket]["cost-centre"] != "shop" { |
| + t.Fatalf("tags = %v", f.Tags[testBucket]) |
| + } |
| + // The same for a backend that has no bucket tags at all. |
| + f.Fail["BucketTags"] = ErrTagsUnsupported |
| + if err := WriteIncarnation(ctx, f, testBucket, incA); !errors.Is(err, ErrTagsUnsupported) { |
| + t.Fatalf("unsupported backend: %v", err) |
| + } |
| + f.Fail["BucketTags"] = ErrTagsUnsupported |
| + if _, err := ReadIncarnation(ctx, f, testBucket); !errors.Is(err, ErrTagsUnsupported) { |
| + t.Fatalf("ReadIncarnation on an unsupported backend: %v", err) |
| + } |
| +} |
| + |
| +// S3 holds at most 50 tags. A bucket with 50 of its own has no room, and |
| +// the write refuses rather than dropping one; replacing our own tag on a |
| +// full bucket is fine. |
| +func TestWriteIncarnationRespectsTheTagLimit(t *testing.T) { |
| + ctx := context.Background() |
| + f := NewFake() |
| + f.Facts[testBucket] = goodFacts() |
| + full := map[string]string{} |
| + for i := 0; i < MaxBucketTags; i++ { |
| + full[fmt.Sprintf("k%02d", i)] = "v" |
| + } |
| + f.Tags[testBucket] = full |
| + if err := WriteIncarnation(ctx, f, testBucket, incA); err == nil { |
| + t.Fatal("a full bucket took another tag") |
| + } |
| + if n := f.CallCount("PutBucketTags"); n != 0 { |
| + t.Fatalf("PutBucketTags called %d times on a full bucket", n) |
| + } |
| + delete(full, "k00") |
| + full[IncarnationTag] = incA |
| + f.Tags[testBucket] = full |
| + if err := WriteIncarnation(ctx, f, testBucket, incB); err != nil { |
| + t.Fatalf("replacing our own tag on a full bucket: %v", err) |
| + } |
| + if err := WriteIncarnation(ctx, f, testBucket, "not-hex"); err == nil { |
| + t.Fatal("a malformed incarnation was written") |
| + } |
| +} |
| + |
| +func TestReadIncarnationUntagged(t *testing.T) { |
| + f := NewFake() |
| + f.Facts[testBucket] = goodFacts() |
| + if got, err := ReadIncarnation(context.Background(), f, testBucket); err != nil || got != "" { |
| + t.Fatalf("untagged bucket: %q, %v", got, err) |
| + } |
| + if _, err := ReadIncarnation(context.Background(), f, "absent"); err == nil { |
| + t.Fatal("an absent bucket read as untagged") |
| + } |
| +} |