store uploads: disable removes unrecorded rules; status inspects
Task 9 last fix (Astra round 3 P2). A first enable that wrote the bucket and failed before its record left rules no record names: disable answered off without removing them and status assumed a match. Under A4, disable now checks eligibility and removes both rules whether or not hashes are recorded, then clears the record; status inspects a provisioned store's bucket and reports rules changed for a rule with no recorded hash, in the CLI too.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+164
−23
internal/console/apistoreuploads.go+39 −15internal/console/apistoreuploads_test.go+110 −8store_uploads.go+10 −0store_uploads_test.go+5 −0
CI failed — run details
diff --git a/internal/console/apistoreuploads.go b/internal/console/apistoreuploads.go| index 3dfd34125..c9962336f 100644 |
| --- a/internal/console/apistoreuploads.go |
| +++ b/internal/console/apistoreuploads.go |
| @@ -233,10 +233,11 @@ func (s *Server) uploadsEligible(ctx context.Context, w http.ResponseWriter, acc |
| } |
| // handleAPIStoreUploadsGet is GET /api/cli/apps/{account}/{app}/store/uploads: |
| -// `carlos store uploads status`. It reads the record and, when uploads are |
| -// on, Inspects the bucket and compares it with the rules the record says |
| -// the platform wrote, so an operator sees a hand edit (`rules changed`). It |
| -// writes nothing and takes no lock. |
| +// `carlos store uploads status`. It reads the record and, for a provisioned |
| +// store, Inspects the bucket and compares it with the rules the record says |
| +// the platform wrote (none, when uploads are off), so an operator sees a |
| +// hand edit or an unrecorded write (`rules changed`). It writes nothing and |
| +// takes no lock. |
| func (s *Server) handleAPIStoreUploadsGet(w http.ResponseWriter, r *http.Request) { |
| _, acct, app, ok := s.apiAccountApp(w, r) |
| if !ok { |
| @@ -261,8 +262,7 @@ func (s *Server) handleAPIStoreUploadsGet(w http.ResponseWriter, r *http.Request |
| uploadsInternal(w, "status "+acct.Sqid+"/"+app, err) |
| return |
| } |
| - up := recordedUploads(rec) |
| - if up == nil { |
| + if rec == nil || rec.Provisioned == nil { |
| writeJSON(w, http.StatusOK, uploadsOff()) |
| return |
| } |
| @@ -271,9 +271,30 @@ func (s *Server) handleAPIStoreUploadsGet(w http.ResponseWriter, r *http.Request |
| uploadsInternal(w, "status inspect "+acct.Sqid+"/"+app, err) |
| return |
| } |
| + up := recordedUploads(rec) |
| + if up == nil { |
| + writeJSON(w, http.StatusOK, uploadsOffAgainst(facts)) |
| + return |
| + } |
| writeJSON(w, http.StatusOK, uploadsOn(up, uploadsMismatch(facts, up))) |
| } |
| +// uploadsOffAgainst is the answer for uploads that are off on a bucket as it |
| +// is: a rule the bucket holds with no hash recorded (a first enable that |
| +// wrote the bucket and failed before its record, or a hand edit) is a |
| +// mismatch, in storecfg.Verify's order, so status says the rules changed |
| +// while the platform's own CORS rule may still be letting browsers in. |
| +func uploadsOffAgainst(f storecfg.Facts) uploadsAnswer { |
| + a := uploadsOff() |
| + switch { |
| + case len(f.Lifecycle) > 0: |
| + a.BucketMatches, a.Mismatch = false, storecfg.CheckLifecycle |
| + case len(f.CORS) > 0: |
| + a.BucketMatches, a.Mismatch = false, storecfg.CheckCORS |
| + } |
| + return a |
| +} |
| + |
| // handleAPIStoreUploadsSet is POST /api/cli/apps/{account}/{app}/store/uploads: |
| // `carlos store uploads enable` ({"enabled": true}) and `disable` |
| // ({"enabled": false}). After apiAccountApp (a stranger meets its 404 |
| @@ -398,14 +419,17 @@ func (s *Server) handleAPIStoreUploadsSet(w http.ResponseWriter, r *http.Request |
| } |
| // handleUploadsDisable is `carlos store uploads disable`, under the account |
| -// lock and then the app's store walk lock. With nothing recorded it writes |
| -// nothing and reads no bucket. Otherwise the store must still be eligible |
| -// (mediaStoreEligible: a recreated bucket is verified afresh, and one |
| -// carrying rules other than the recorded ones is refused store_unverified, |
| -// since they are not this store's platform rules). Then DeleteUploads |
| -// removes both rules, an operator's hand edit included (spec §11 A4), and |
| -// the uploads block is cleared. A failure between the two leaves the record |
| -// naming rules that are gone; the next disable clears it. |
| +// lock and then the app's store walk lock. With no provisioned store it |
| +// writes nothing and reads no bucket. Otherwise the store must be eligible |
| +// (mediaStoreEligible: a store never verified, or a recreated bucket, is |
| +// verified now, and one carrying rules other than the recorded ones is |
| +// refused store_unverified, since they are not this store's platform |
| +// rules). Then DeleteUploads removes both rules whether or not any hashes |
| +// are recorded (spec §11 A4: a first enable that wrote the bucket and failed |
| +// before its record leaves rules no record names, and an operator's hand |
| +// edit goes too), and the uploads block is cleared. A failure between the |
| +// two leaves the record naming rules that are gone; the next disable clears |
| +// it. |
| func (s *Server) handleUploadsDisable(w http.ResponseWriter, r *http.Request, addr string, acct *Account, app string) { |
| ctx, cancel := context.WithTimeout(r.Context(), storeGrantTimeout) |
| defer cancel() |
| @@ -416,7 +440,7 @@ func (s *Server) handleUploadsDisable(w http.ResponseWriter, r *http.Request, ad |
| uploadsInternal(w, "disable "+acct.Sqid+"/"+app, err) |
| return |
| } |
| - if recordedUploads(rec) == nil { |
| + if rec == nil || rec.Provisioned == nil { |
| writeJSON(w, http.StatusOK, uploadsOff()) |
| return |
| } |
diff --git a/internal/console/apistoreuploads_test.go b/internal/console/apistoreuploads_test.go| index e85419999..f2fee1078 100644 |
| --- a/internal/console/apistoreuploads_test.go |
| +++ b/internal/console/apistoreuploads_test.go |
| @@ -16,6 +16,7 @@ import ( |
| "github.com/carlosframework/platform/internal/appconfig" |
| "github.com/carlosframework/platform/internal/objstore" |
| "github.com/carlosframework/platform/internal/registry" |
| + "github.com/carlosframework/platform/internal/release" |
| "github.com/carlosframework/platform/internal/storecfg" |
| ) |
| @@ -356,17 +357,25 @@ func TestUploadsDisableRemovesBothRules(t *testing.T) { |
| t.Fatalf("record kept uploads: %+v", up) |
| } |
| }) |
| - t.Run("nothing recorded: nothing read, nothing written", func(t *testing.T) { |
| - u := newUploads(t, "code-uploads-disable-none") |
| - u.setFacts(func(f *storecfg.Facts) { f.CORS = foreignCORS() }) |
| - before, _ := rawStoreRecord(t, u.consoleFixture) |
| + t.Run("no store: nothing read, nothing written", func(t *testing.T) { |
| + fx, _, buckets, token := mediaStoreFixture(t, "code-uploads-disable-nostore") |
| + u := &uploadsFixture{consoleFixture: fx, t: t, buckets: buckets, token: token, bucket: mediaMintedName(fx)} |
| r := u.disable() |
| u.wantOK(r) |
| if r.ans.Enabled || len(u.buckets.Calls) != 0 { |
| t.Fatalf("answer %s, calls %v", r.raw, u.buckets.Calls) |
| } |
| - if after, _ := rawStoreRecord(t, u.consoleFixture); !bytes.Equal(before, after) { |
| - t.Fatal("disable wrote the record") |
| + }) |
| + t.Run("a store never verified, with a foreign rule: refused, nothing removed", func(t *testing.T) { |
| + u := newUploads(t, "code-uploads-disable-unverified") |
| + u.setFacts(func(f *storecfg.Facts) { f.CORS = foreignCORS() }) |
| + before, _ := rawStoreRecord(t, u.consoleFixture) |
| + u.wantRefused(u.disable(), http.StatusConflict, "store_unverified", uploadsUnverified("cors")) |
| + if after, _ := rawStoreRecord(t, u.consoleFixture); !bytes.Equal(before, after) || u.buckets.CallCount("DeleteUploads") != 0 { |
| + t.Fatal("a refused disable wrote something") |
| + } |
| + if !bytes.Equal(u.facts().CORS, foreignCORS()) { |
| + t.Fatal("a rule on a store the platform never verified was removed") |
| } |
| }) |
| } |
| @@ -438,9 +447,15 @@ func TestUploadsStatusComparesWithTheRecord(t *testing.T) { |
| if s := u.status(); s.status != http.StatusOK || s.ans.Enabled || !s.ans.BucketMatches || s.ans.Origins == nil { |
| t.Fatalf("off = %s", s.raw) |
| } |
| - if len(u.buckets.Calls) != 0 { |
| - t.Fatalf("status of uploads that are off read the bucket: %v", u.buckets.Calls) |
| + if n := u.buckets.CallCount("PutUploads") + u.buckets.CallCount("DeleteUploads") + u.buckets.CallCount("PutBucketTags"); n != 0 { |
| + t.Fatalf("status wrote the bucket %d times", n) |
| } |
| + // Off, with a rule no record names: the rules changed. |
| + u.setFacts(func(f *storecfg.Facts) { f.CORS = foreignCORS() }) |
| + if s := u.status(); s.ans.Enabled || s.ans.BucketMatches || s.ans.Mismatch != "cors" { |
| + t.Fatalf("off with a CORS rule = %s", s.raw) |
| + } |
| + u.setFacts(func(f *storecfg.Facts) { f.CORS = nil }) |
| u.wantOK(u.enable("https://shop.example.com")) |
| u.setFacts(func(f *storecfg.Facts) { f.CORS = foreignCORS() }) |
| if s := u.status(); !s.ans.Enabled || s.ans.BucketMatches || s.ans.Mismatch != "cors" { |
| @@ -683,3 +698,90 @@ func uploadsPolicyDeniesWrites(t *testing.T, doc string) bool { |
| } |
| return false |
| } |
| + |
| +// uploadsHookBuckets is the fake with PutUploads replaceable. |
| +type uploadsHookBuckets struct { |
| + *storecfg.Fake |
| + put func(ctx context.Context, bucket string, origins []string) error |
| +} |
| + |
| +func (b *uploadsHookBuckets) PutUploads(ctx context.Context, bucket string, origins []string) error { |
| + if b.put != nil { |
| + return b.put(ctx, bucket, origins) |
| + } |
| + return b.Fake.PutUploads(ctx, bucket, origins) |
| +} |
| + |
| +// uploadsRecordFailStore fails the store record's writes while armed. |
| +type uploadsRecordFailStore struct { |
| + release.Store |
| + key string |
| + armed atomic.Bool |
| +} |
| + |
| +func (s *uploadsRecordFailStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + if key == s.key && s.armed.Load() { |
| + return "", errors.New("bucket write failed") |
| + } |
| + return s.Store.Put(ctx, key, body, ifMatch) |
| +} |
| + |
| +// Astra round 3 P2: a first enable that wrote the bucket and failed before |
| +// its record leaves rules no record names. Status shows them, and disable |
| +// (spec §11 A4) removes them. |
| +func TestUploadsUnrecordedFirstWrite(t *testing.T) { |
| + t.Run("the CORS rule lands, the lifecycle rule fails", func(t *testing.T) { |
| + u := newUploads(t, "code-uploads-unrecorded-partial") |
| + hb := &uploadsHookBuckets{Fake: u.buckets} |
| + hb.put = func(_ context.Context, _ string, origins []string) error { |
| + u.setFacts(func(f *storecfg.Facts) { f.CORS = storecfg.UploadsCORS(origins) }) |
| + return errors.New("lifecycle write failed") |
| + } |
| + u.srv.cfg.StoreBuckets = hb |
| + if r := u.enable("https://shop.example.com"); r.status != http.StatusInternalServerError { |
| + t.Fatalf("status = %d %s", r.status, r.raw) |
| + } |
| + if up := storedRecord(t, u.consoleFixture, "titogo").Conversion.Uploads; up != nil || u.facts().CORS == nil { |
| + t.Fatalf("record %+v, CORS %s", up, u.facts().CORS) |
| + } |
| + if s := u.status(); s.ans.Enabled || s.ans.BucketMatches || s.ans.Mismatch != "cors" { |
| + t.Fatalf("status of the half write = %s", s.raw) |
| + } |
| + u.wantOK(u.disable()) |
| + if f := u.facts(); f.CORS != nil || f.Lifecycle != nil { |
| + t.Fatalf("rules left: %s %s", f.CORS, f.Lifecycle) |
| + } |
| + if s := u.status(); s.ans.Enabled || !s.ans.BucketMatches { |
| + t.Fatalf("status after disable = %s", s.raw) |
| + } |
| + }) |
| + t.Run("both rules land, the record write fails", func(t *testing.T) { |
| + u := newUploads(t, "code-uploads-unrecorded-record") |
| + if _, code, _, err := eligible(t, u.consoleFixture); err != nil || code != "" { |
| + t.Fatalf("verify: %q %v", code, err) |
| + } |
| + fs := &uploadsRecordFailStore{Store: u.srv.cfg.Releases, key: objstore.Key(u.sqid, "titogo")} |
| + u.srv.cfg.Releases = fs |
| + hb := &uploadsHookBuckets{Fake: u.buckets} |
| + hb.put = func(ctx context.Context, bucket string, origins []string) error { |
| + err := hb.Fake.PutUploads(ctx, bucket, origins) |
| + fs.armed.Store(true) |
| + return err |
| + } |
| + u.srv.cfg.StoreBuckets = hb |
| + if r := u.enable("https://shop.example.com"); r.status != http.StatusInternalServerError { |
| + t.Fatalf("status = %d %s", r.status, r.raw) |
| + } |
| + fs.armed.Store(false) |
| + if up := storedRecord(t, u.consoleFixture, "titogo").Conversion.Uploads; up != nil { |
| + t.Fatalf("record = %+v", up) |
| + } |
| + if s := u.status(); s.ans.Enabled || s.ans.BucketMatches || s.ans.Mismatch != "lifecycle" { |
| + t.Fatalf("status of the unrecorded write = %s", s.raw) |
| + } |
| + u.wantOK(u.disable()) |
| + if f := u.facts(); f.CORS != nil || f.Lifecycle != nil { |
| + t.Fatalf("rules left: %s %s", f.CORS, f.Lifecycle) |
| + } |
| + }) |
| +} |
diff --git a/store_uploads.go b/store_uploads.go| index 50ac9f247..2da5f1d69 100644 |
| --- a/store_uploads.go |
| +++ b/store_uploads.go |
| @@ -144,6 +144,11 @@ func runStoreUploadsWith(ctx context.Context, s *apiSession, a apiAcct, app, ver |
| func printStoreUploads(w io.Writer, res apiUploadsReply, note bool) { |
| if !res.Enabled { |
| fmt.Fprintln(w, cliUploadsOff) |
| + // Off, but the bucket still holds rules no record names (a first |
| + // enable that failed partway, or a hand edit): say so. |
| + if !res.BucketMatches { |
| + printUploadsRules(w, res) |
| + } |
| return |
| } |
| fmt.Fprintln(w, cliUploadsOn) |
| @@ -151,6 +156,11 @@ func printStoreUploads(w io.Writer, res apiUploadsReply, note bool) { |
| if note { |
| fmt.Fprintln(w, cliUploadsDefaultNote) |
| } |
| + printUploadsRules(w, res) |
| +} |
| + |
| +// printUploadsRules is the deck's rules line for res. |
| +func printUploadsRules(w io.Writer, res apiUploadsReply) { |
| switch { |
| case res.BucketMatches: |
| fmt.Fprintln(w, cliUploadsRulesOK) |
diff --git a/store_uploads_test.go b/store_uploads_test.go| index 2d7bc7987..52760b175 100644 |
| --- a/store_uploads_test.go |
| +++ b/store_uploads_test.go |
| @@ -60,6 +60,10 @@ func TestStoreUploadsVerbs(t *testing.T) { |
| if got := run("status"); got != "uploads on\norigins https://shop.example.com\nrules changed: the multipart cleanup rule is not the one this platform wrote\n" { |
| t.Fatalf("status lifecycle:\n%s", got) |
| } |
| + answer = `{"enabled":false,"origins":[],"bucket_matches":false,"mismatch":"cors"}` |
| + if got := run("status"); got != "uploads off\nrules changed: the CORS rule is not the one this platform wrote\n" { |
| + t.Fatalf("status off with a rule left:\n%s", got) |
| + } |
| answer = `{"enabled":false,"origins":[],"bucket_matches":true,"mismatch":""}` |
| if got := run("status"); got != "uploads off\n" { |
| t.Fatalf("status off:\n%s", got) |
| @@ -74,6 +78,7 @@ func TestStoreUploadsVerbs(t *testing.T) { |
| `GET /api/cli/apps/bab/shop/store/uploads `, |
| `GET /api/cli/apps/bab/shop/store/uploads `, |
| `GET /api/cli/apps/bab/shop/store/uploads `, |
| + `GET /api/cli/apps/bab/shop/store/uploads `, |
| `POST /api/cli/apps/bab/shop/store/uploads {"enabled":false}`, |
| } |
| if strings.Join(calls, "\n") != strings.Join(wantCalls, "\n") { |