carlos / platform Public

console: refuse new uploads past storage grace (plans 4b)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev a089ae0ea601345e3fd80b4b4d105303b6f59ecb parent c5b0081
4 files changed, +234 −1
  • internal/console/claim_app_mutation_test.go +1 −1
  • internal/console/plangracepass.go +5 −0
  • internal/console/planstorage.go +83 −0
  • internal/console/planstorage_test.go +145 −0
diff --git a/internal/console/claim_app_mutation_test.go b/internal/console/claim_app_mutation_test.go
index 4e1d34cc5..bce2b01af 100644
--- a/internal/console/claim_app_mutation_test.go
+++ b/internal/console/claim_app_mutation_test.go
@@ -42,7 +42,7 @@ func TestClaimAppMutationGate(t *testing.T) {
base := func() hostops.AppAdmission {
return hostops.AppAdmission{V: 1, AppID: "app", Revision: 1, Epoch: 1, State: "open", Registrations: map[string]hostops.Registration{}, Effects: map[string]hostops.Effect{}}
}
- for _, door := range []string{"restore", "protection", "store-grant", "store-provision", "store-rotate", "store-rotate-finish", "store-destroy", "scan-regrant", "store-declare"} {
+ for _, door := range []string{"restore", "protection", "store-grant", "store-provision", "store-rotate", "store-rotate-finish", "store-destroy", "scan-regrant", "store-declare", "plan-storage-regrant"} {
t.Run(door, func(t *testing.T) {
ctx := context.Background()
begin := func(a hostops.AppAdmission, id string) (*hostops.OperationHandle, error, *hostops.ControlWriter) {
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go
index 09704b34e..8f40f3571 100644
--- a/internal/console/plangracepass.go
+++ b/internal/console/plangracepass.go
@@ -83,6 +83,11 @@ func (s *Server) planGraceAccount(ctx context.Context, acct *Account, now time.T
return err
}
states = append(states, st)
+ if c == capStorage {
+ if err := s.convergeStorageDeny(ctx, acct, st, true, now); err != nil {
+ log.Printf("console: plan grace %s: storage stage: %v", acct.Sqid, err)
+ }
+ }
case errs[c] != nil:
log.Printf("console: plan grace %s: %s: %v (row kept)", acct.Sqid, c, errs[c])
if p := byCap[c]; p != nil {
diff --git a/internal/console/planstorage.go b/internal/console/planstorage.go
new file mode 100644
index 000000000..ef4ea52c8
--- /dev/null
+++ b/internal/console/planstorage.go
@@ -0,0 +1,83 @@
+package console
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "time"
+
+ "github.com/carlosframework/platform/internal/appconfig"
+ "github.com/carlosframework/platform/internal/objstore"
+)
+
+// storageDenyDue is whether the account's stores should refuse new uploads
+// (plans 4b, spec §5.5): past grace with notice received, or at twice the
+// cap, the table's hard ceiling, which does not wait for notice (Paul,
+// 2026-10-25).
+func storageDenyDue(st capState, now time.Time) bool {
+ return afterGraceDue(st, now) || (st.Level.over() && st.Limit > 0 && st.Used >= 2*st.Limit)
+}
+
+// convergeStorageDeny brings every provisioned store of the account in line
+// with storageDenyDue: it marks or clears each record's WritesDenied and
+// re-runs the grant, which rewrites the policy (the regrantForScanning
+// precedent). An unmeasured storage cap changes nothing: unknown is not
+// over, and not under either.
+func (s *Server) convergeStorageDeny(ctx context.Context, acct *Account, st capState, measured bool, now time.Time) error {
+ if s.cfg.PlanCatalog == nil || !measured || s.cfg.StoreProvisioner == nil {
+ return nil
+ }
+ want := storageDenyDue(st, now)
+ apps, err := s.store.AppsIn(acct.ID)
+ if err != nil {
+ return err
+ }
+ for _, app := range apps {
+ envs, err := objstore.StoreEnvironments(ctx, s.cfg.Releases, acct.Sqid, app)
+ if err != nil {
+ return err
+ }
+ for _, env := range append([]string{""}, envs...) {
+ if err := s.convergeStoreDeny(ctx, acct, app, env, st, want, now); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
+
+func (s *Server) convergeStoreDeny(ctx context.Context, acct *Account, app, env string, st capState, want bool, now time.Time) error {
+ rec, _, err := objstore.GetIn(ctx, s.cfg.Releases, acct.Sqid, app, env)
+ if err != nil {
+ return err
+ }
+ if rec == nil || rec.Provisioned == nil || (rec.WritesDenied != nil) == want {
+ return nil
+ }
+ pub, err := appconfig.ParsePublicKey(s.cfg.SecretsPub, "CARLOS_SECRETS_PUBKEY")
+ if err != nil {
+ log.Printf("console: plan storage %s: no secrets key, store policies left as they are: %v", acct.Sqid, err)
+ return nil
+ }
+ var wd *objstore.WritesDenied
+ action, detail := "store.writes_allowed", fmt.Sprintf("plan storage: %d of %d bytes; new uploads allowed again", st.Used, st.Limit)
+ if want {
+ wd = &objstore.WritesDenied{Reason: fmt.Sprintf("plan storage: %d of %d bytes", st.Used, st.Limit), At: now}
+ action, detail = "store.writes_denied", wd.Reason+"; new uploads refused, downloads and deletes still work"
+ }
+ if _, err := objstore.SetWritesDenied(ctx, s.cfg.Releases, acct.Sqid, app, env, wd); err != nil {
+ return err
+ }
+ source := objstore.GrantSourceOperator
+ if rec.Grant != nil && rec.Grant.Source != "" {
+ source = rec.Grant.Source
+ }
+ if _, err := claimAppCall(s, ctx, acct, app, "plan-storage-regrant", func(ctx context.Context) (*objstore.Record, error) {
+ return objstore.GrantIn(ctx, s.cfg.Releases, s.cfg.StoreProvisioner, s.cfg.StoreDeployment,
+ pub, acct.Sqid, app, env, "plan", source, "", now)
+ }); err != nil {
+ return err
+ }
+ s.audit(acct, app, "plan", action, detail)
+ return nil
+}
diff --git a/internal/console/planstorage_test.go b/internal/console/planstorage_test.go
new file mode 100644
index 000000000..8e38ee2e1
--- /dev/null
+++ b/internal/console/planstorage_test.go
@@ -0,0 +1,145 @@
+package console
+
+import (
+ "context"
+ "encoding/json"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/objstore"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// deniedStoreFixture: plans on, titogo's own store granted on a fake
+// provisioner.
+func deniedStoreFixture(t *testing.T, code string) (*consoleFixture, *objstore.MemProvisioner, *Account) {
+ t.Helper()
+ fx, fake, token, _ := storeArmed(t, code)
+ grantStore(t, fx, token)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ return fx, fake, acct
+}
+
+func storageRow(acct *Account, used, limit int64, firstOver time.Time) capState {
+ st := evaluateCap(nil, capStorage, used, limit, firstOver)
+ st.AccountID = acct.ID
+ return st
+}
+
+func titogoStore(t *testing.T, fx *consoleFixture) *objstore.Record {
+ t.Helper()
+ rec, _, err := objstore.Get(context.Background(), fx.rel, release.OpsAccount, "titogo")
+ if err != nil || rec == nil {
+ t.Fatalf("titogo store: %+v, %v", rec, err)
+ }
+ return rec
+}
+
+func hasWriteDeny(t *testing.T, policy string) bool {
+ t.Helper()
+ var doc struct {
+ Statement []struct {
+ Effect string
+ Action []string
+ Condition map[string]any
+ }
+ }
+ if err := json.Unmarshal([]byte(policy), &doc); err != nil {
+ t.Fatalf("policy: %v\n%s", err, policy)
+ }
+ for _, st := range doc.Statement {
+ if st.Effect == "Deny" && len(st.Action) == 1 && st.Action[0] == "s3:PutObject" && st.Condition == nil {
+ return true
+ }
+ }
+ return false
+}
+
+func TestStorageDenyDue(t *testing.T) {
+ acct := &Account{ID: 1}
+ st := storageRow(acct, 150, 100, planNow)
+ if storageDenyDue(st, planNow.Add(time.Hour)) {
+ t.Error("due in grace")
+ }
+ st.NoticeAt = planNow
+ if !storageDenyDue(st, planNow.Add(7*24*time.Hour)) {
+ t.Error("not due with grace over and notice 7 days old")
+ }
+ ceiling := storageRow(acct, 200, 100, planNow)
+ if !storageDenyDue(ceiling, planNow.Add(time.Minute)) {
+ t.Error("not due at twice the cap")
+ }
+}
+
+func TestStorageDenySetAfterGrace(t *testing.T) {
+ fx, fake, acct := deniedStoreFixture(t, "code-plan-store-deny")
+ st := storageRow(acct, 2_000_000_000, 1_500_000_000, planNow.Add(-10*24*time.Hour))
+ st.NoticeAt = planNow.Add(-9 * 24 * time.Hour)
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, st, true, planNow); err != nil {
+ t.Fatal(err)
+ }
+ rec := titogoStore(t, fx)
+ if rec.WritesDenied == nil {
+ t.Fatal("record not marked")
+ }
+ if !hasWriteDeny(t, fake.PolicyFor(rec.Provisioned.IAMUser)) {
+ t.Fatalf("policy has no write deny:\n%s", fake.PolicyFor(rec.Provisioned.IAMUser))
+ }
+ events, _ := fx.st.Events(acct.ID, "titogo", 10)
+ found := false
+ for _, e := range events {
+ found = found || e.Action == "store.writes_denied"
+ }
+ if !found {
+ t.Errorf("no store.writes_denied audit: %+v", events)
+ }
+}
+
+func TestStorageDenyClearsWhenBackUnder(t *testing.T) {
+ fx, fake, acct := deniedStoreFixture(t, "code-plan-store-clear")
+ st := storageRow(acct, 3_000_000_000, 1_000_000_000, planNow)
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, st, true, planNow); err != nil {
+ t.Fatal(err)
+ }
+ under := storageRow(acct, 500_000_000, 1_000_000_000, planNow)
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, under, true, planNow.Add(time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ rec := titogoStore(t, fx)
+ if rec.WritesDenied != nil || hasWriteDeny(t, fake.PolicyFor(rec.Provisioned.IAMUser)) {
+ t.Fatal("deny not lifted when usage fell under the cap")
+ }
+ events, _ := fx.st.Events(acct.ID, "titogo", 10)
+ found := false
+ for _, e := range events {
+ found = found || e.Action == "store.writes_allowed"
+ }
+ if !found {
+ t.Errorf("no store.writes_allowed audit")
+ }
+}
+
+func TestStorageDenyUnchangedWhenUnmeasured(t *testing.T) {
+ fx, _, acct := deniedStoreFixture(t, "code-plan-store-unknown")
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, storageRow(acct, 3_000_000_000, 1_000_000_000, planNow), true, planNow); err != nil {
+ t.Fatal(err)
+ }
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, capState{}, false, planNow.Add(time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if titogoStore(t, fx).WritesDenied == nil {
+ t.Fatal("an unmeasured pass cleared the deny")
+ }
+}
+
+func TestStorageDenyWithoutACatalogUnchanged(t *testing.T) {
+ fx, _, acct := deniedStoreFixture(t, "code-plan-store-off")
+ fx.srv.cfg.PlanCatalog = nil
+ if err := fx.srv.convergeStorageDeny(context.Background(), acct, storageRow(acct, 3_000_000_000, 1_000_000_000, planNow), true, planNow); err != nil {
+ t.Fatal(err)
+ }
+ if titogoStore(t, fx).WritesDenied != nil {
+ t.Fatal("denied with plans off")
+ }
+}