console: a refused customer update writes nothing, plan before rename
Task 9 fix round 1. The update door changes the plan before the name, so an operator assigning a plan after the door's pre-check is refused by the transition's re-check under the lapse lock before anything is written: the 409 leaves the plan row, the name and the audit untouched. A seam, updateBeforePlan, lets a test assign the operator's plan mid-request. Ruling B extends to update: the gate splits into platformAuthorize and platformFinishPending, and the update door runs every refusal (budget, customer, body, name, plan, operator row) before pending mints are finished. lockAccount's comment no longer names only two holders, and a customer gone at read-back is the stdlib 404.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+176
−36
internal/console/apiplatformcustomers.go+64 −31internal/console/apiplatformcustomers_test.go+101 −0internal/console/applocks.go+7 −5internal/console/platform_seams.go+4 −0
diff --git a/internal/console/apiplatformcustomers.go b/internal/console/apiplatformcustomers.go| index 1fa5792b3..5cae46504 100644 |
| --- a/internal/console/apiplatformcustomers.go |
| +++ b/internal/console/apiplatformcustomers.go |
| @@ -181,8 +181,8 @@ func (s *Server) platformAuthorize(w http.ResponseWriter, r *http.Request, failR |
| return c, true |
| } |
| -// platformGateFor is the gate every platform door but create runs: |
| -// platformAuthorize, then, when the platform has pending mints, they are |
| +// platformGateFor is the gate of every platform door that has nothing to |
| +// refuse before it may write (the read doors): platformAuthorize, then, when the platform has pending mints, they are |
| // finished under the platform lifecycle lock, released before the handler |
| // runs (the lock is not re-entrant). A held promise is passed over and the |
| // door goes on (fix round 2). failRow is the caller's door's own failure |
| @@ -192,12 +192,23 @@ func (s *Server) platformAuthorize(w http.ResponseWriter, r *http.Request, failR |
| // |
| // Create runs platformAuthorize alone and finishes under its own hold of |
| // the lock, after it has validated its body, so a refused create writes |
| -// nothing (Astra P2-4). |
| +// nothing (Astra P2-4). Update runs platformAuthorize, its refusals, then |
| +// platformFinishPending (ruling B, Task 9 fix round 1). |
| func (s *Server) platformGateFor(w http.ResponseWriter, r *http.Request, failRow string) (platformCaller, bool) { |
| c, ok := s.platformAuthorize(w, r, failRow) |
| - if !ok { |
| + if !ok || !s.platformFinishPending(w, r, c) { |
| return platformCaller{}, false |
| } |
| + return c, true |
| +} |
| + |
| +// platformFinishPending is the gate's second half: when the platform has |
| +// pending mints, they are finished under the platform lifecycle lock, |
| +// released before it returns. It answers whether the door goes on (on a |
| +// write door, a failed finish has been answered with the door's row). A |
| +// write door that validates its request first (create, update; ruling B) |
| +// runs platformAuthorize, its refusals, then this. |
| +func (s *Server) platformFinishPending(w http.ResponseWriter, r *http.Request, c platformCaller) bool { |
| ctx := r.Context() |
| acct := c.Platform |
| pending, err := s.store.PendingPlatformMints(ctx, acct.Sqid) |
| @@ -208,12 +219,12 @@ func (s *Server) platformGateFor(w http.ResponseWriter, r *http.Request, failRow |
| } |
| if err != nil { |
| log.Printf("console: platform %s: finish pending customer creates: %v", acct.Sqid, err) |
| - if failRow != "" { |
| + if c.failRow != "" { |
| s.platformDoorFailed(w, c) |
| - return platformCaller{}, false |
| + return false |
| } |
| } |
| - return c, true |
| + return true |
| } |
| // platformDoorFailed answers a store or bucket failure: 502 with the |
| @@ -761,20 +772,32 @@ type updatePlatformCustomerRequest struct { |
| // handlePlatformCustomerUpdate answers PATCH .../customers/{customer}: a |
| // new name, a new plan within the grant, or both, and the customer's view. |
| -// Every refusal comes before the first write, so a refused update writes |
| -// nothing (Astra r1-4): the write budget (door.rate), the customer (the |
| -// stdlib 404), an empty change (update.empty), the name (create.name), the |
| -// plan (create.plan, or create.no_catalog_plan with no catalog), and an |
| -// operator's plan row (update.operator_plan). Then the rename, bucket |
| -// first, and the plan change through the plan transition. A store or |
| -// bucket failure is 502 update.failed (pre-flight C10). A name or plan the |
| -// customer already has is no change: nothing is written or audited for it. |
| // |
| -// The gate finishes the platform's pending mints first, as on every door |
| -// but create; a held one is passed over (Task 8, ruling C). The update |
| -// takes no platform lifecycle lock. |
| +// A refused update writes nothing (Astra r1-4, and ruling B as for create): |
| +// every refusal comes before the platform's pending mints are finished and |
| +// before the update's own first write. In order: authorization |
| +// (platformAuthorize), the write budget (door.rate), the customer (the |
| +// stdlib 404), the body's shape, an empty change (update.empty), the name |
| +// (create.name), the plan (create.plan, or create.no_catalog_plan with no |
| +// catalog), and an operator's plan row (update.operator_plan). Then the |
| +// gate's second half finishes pending mints (a held one is passed over, |
| +// Task 8 ruling C), and the writes run, the plan before the name: |
| +// |
| +// - the plan change runs through the plan transition, under the lapse lock |
| +// an operator's assignment also takes (transitionAccountPlan), and its |
| +// closure re-checks the operator row there. An operator who assigned a |
| +// plan after the pre-check is caught before anything is written, so the |
| +// 409 leaves the plan, the name and the audit untouched (Task 9 fix |
| +// round 1); |
| +// - then the rename, bucket first (renamePlatformCustomer). |
| +// |
| +// A store or bucket failure is 502 update.failed (pre-flight C10); a rename |
| +// failing after the plan changed leaves the plan changed, and a retry |
| +// converges (the same plan is no change). A name or plan the customer |
| +// already has is no change: nothing is written or audited for it. The |
| +// update takes no platform lifecycle lock. |
| func (s *Server) handlePlatformCustomerUpdate(w http.ResponseWriter, r *http.Request) { |
| - c, ok := s.platformGateFor(w, r, "update.failed") |
| + c, ok := s.platformAuthorize(w, r, "update.failed") |
| if !ok { |
| return |
| } |
| @@ -823,18 +846,15 @@ func (s *Server) handlePlatformCustomerUpdate(w http.ResponseWriter, r *http.Req |
| s.platformDoorFailed(w, c) |
| return |
| } |
| - |
| - if rename { |
| - if err := s.renamePlatformCustomer(ctx, cust, req.Name); err != nil { |
| - s.platformUpdateFailed(w, r, c, cust, "rename", err) |
| - return |
| - } |
| - detail := fmt.Sprintf(platformCopy["audit.customer_rename"], lc.Name, req.Name) |
| - s.audit(c.Platform, accountAuditSubject, c.Actor, auditPlatformCustomerRename, detail) |
| - s.audit(cust, accountAuditSubject, c.Actor, auditPlatformCustomerRename, detail) |
| - cust.Name = req.Name |
| + // The gate's second half, now that nothing below refuses up front. |
| + if !s.platformFinishPending(w, r, c) { |
| + return |
| } |
| + |
| if replan { |
| + if s.updateBeforePlan != nil { |
| + s.updateBeforePlan() |
| + } |
| reason := strings.TrimSpace(req.Reason) |
| if reason == "" { |
| reason = fmt.Sprintf("platform %s", c.Platform.Sqid) |
| @@ -843,17 +863,30 @@ func (s *Server) handlePlatformCustomerUpdate(w http.ResponseWriter, r *http.Req |
| s.platformUpdateFailed(w, r, c, cust, "plan", err) |
| return |
| } |
| - detail := fmt.Sprintf(platformCopy["audit.customer_plan"], cust.Name, req.Plan) |
| + detail := fmt.Sprintf(platformCopy["audit.customer_plan"], lc.Name, req.Plan) |
| s.audit(c.Platform, accountAuditSubject, c.Actor, auditPlatformCustomerPlan, detail) |
| s.audit(cust, accountAuditSubject, c.Actor, auditPlatformCustomerPlan, detail) |
| } |
| + if rename { |
| + if err := s.renamePlatformCustomer(ctx, cust, req.Name); err != nil { |
| + s.platformUpdateFailed(w, r, c, cust, "rename", err) |
| + return |
| + } |
| + detail := fmt.Sprintf(platformCopy["audit.customer_rename"], lc.Name, req.Name) |
| + s.audit(c.Platform, accountAuditSubject, c.Actor, auditPlatformCustomerRename, detail) |
| + s.audit(cust, accountAuditSubject, c.Actor, auditPlatformCustomerRename, detail) |
| + } |
| updated, err := s.store.LinkedCustomer(ctx, c.Platform.Sqid, cust.Sqid) |
| - if err != nil || updated == nil { |
| + if err != nil { |
| log.Printf("console: platform %s: updated customer %s but could not read it back: %v", c.Platform.Sqid, cust.Sqid, err) |
| s.platformDoorFailed(w, c) |
| return |
| } |
| + if updated == nil { |
| + http.NotFound(w, r) // gone meanwhile, whatever the reason |
| + return |
| + } |
| v, err := s.customerView(ctx, *updated, time.Now()) |
| if err != nil { |
| log.Printf("console: platform %s: customer %s: %v", c.Platform.Sqid, cust.Sqid, err) |
diff --git a/internal/console/apiplatformcustomers_test.go b/internal/console/apiplatformcustomers_test.go| index a35b86501..389ac94fd 100644 |
| --- a/internal/console/apiplatformcustomers_test.go |
| +++ b/internal/console/apiplatformcustomers_test.go |
| @@ -1645,3 +1645,104 @@ func TestCustomerUpdateGoesOnPastAHeldPromise(t *testing.T) { |
| t.Fatalf("pending mints %+v, want the held 4900 promise kept", pm) |
| } |
| } |
| + |
| +// TestCustomerUpdateRefusedWhenAnOperatorAssignsMidway is Task 9 fix round |
| +// 1 (Astra): an operator assigns a plan after the door's pre-check and |
| +// before its plan change. The transition's re-check under the lapse lock |
| +// refuses it, and because the plan runs before the rename, the 409 leaves |
| +// the name, the plan row and the audit untouched. |
| +func TestCustomerUpdateRefusedWhenAnOperatorAssignsMidway(t *testing.T) { |
| + fx, p, tok, cust := customerWorld(t, "free") |
| + var operatorRow AccountPlanRow |
| + fired := false |
| + fx.srv.updateBeforePlan = func() { |
| + fired = true |
| + if err := fx.srv.applyAccountPlan(context.Background(), cust, "free", nil, PlanSourceOperator, nil, "comp", |
| + "ops@keymail.dev", time.Now()); err != nil { |
| + t.Errorf("operator assignment: %v", err) |
| + return |
| + } |
| + row, err := fx.st.AccountPlan(cust.ID) |
| + if err != nil || row == nil { |
| + t.Errorf("operator row: %+v %v", row, err) |
| + return |
| + } |
| + operatorRow = *row |
| + } |
| + status, out, _ := updateCustomer(t, fx.ts.URL, p.Sqid, cust.Sqid, tok, map[string]any{"name": "shop-2", "plan": "personal"}) |
| + if !fired { |
| + t.Fatal("the update never reached its plan change") |
| + } |
| + wantPlatformError(t, "update across an operator's assignment", status, out, http.StatusConflict, "update.operator_plan") |
| + if after := customerPlanRow(t, fx, cust.ID); after.Plan != "free" || after.Source != PlanSourceOperator || |
| + after.Revision != operatorRow.Revision { |
| + t.Fatalf("plan row %+v, want the operator's row %+v untouched", after, operatorRow) |
| + } |
| + wantCustomerNamed(t, fx, cust.Sqid, "shop") |
| + for _, id := range []int64{p.ID, cust.ID} { |
| + for _, action := range []string{auditPlatformCustomerRename, auditPlatformCustomerPlan} { |
| + if rows := platformAudit(t, fx, id, action); len(rows) != 0 { |
| + t.Fatalf("%s audit on %d %+v after the refusal, want none", action, id, rows) |
| + } |
| + } |
| + } |
| +} |
| + |
| +// TestCustomerUpdateRefusedWritesNothingWithAPendingMint is ruling B for |
| +// update (Task 9 fix round 1): a refused update does not finish the |
| +// platform's pending mints. Once a valid update arrives, the gate finishes |
| +// them as before. |
| +func TestCustomerUpdateRefusedWritesNothingWithAPendingMint(t *testing.T) { |
| + fx, p, tok, cust := customerWorld(t, "free") |
| + ctx := context.Background() |
| + if err := fx.st.ReservePlatformMint(ctx, PendingMint{MintID: strings.Repeat("ab", 16), Platform: p.Sqid, Ref: "4900", |
| + Name: "cafe", Plan: "free", PlanReason: "platform " + p.Sqid, By: "service:" + p.Sqid + "/cms", |
| + CreatedAt: time.Now()}); err != nil { |
| + t.Fatal(err) |
| + } |
| + wantUnfinished := func(what string) { |
| + t.Helper() |
| + if pm := pendingMints(t, fx.st, p.Sqid); len(pm) != 1 || pm[0].Ref != "4900" { |
| + t.Fatalf("%s: pending mints %+v, want the 4900 promise unfinished", what, pm) |
| + } |
| + if ms := platformManifests(t, fx.rel, p.Sqid); len(ms) != 1 || ms[0].Sqid != cust.Sqid { |
| + t.Fatalf("%s: manifests naming the platform %+v, want shop's alone", what, ms) |
| + } |
| + if cs, err := fx.st.LinkedCustomers(ctx, p.Sqid); err != nil || len(cs) != 1 { |
| + t.Fatalf("%s: customers %+v %v, want shop alone", what, cs, err) |
| + } |
| + if rows := platformAudit(t, fx, p.ID, auditPlatformCustomerCreate); len(rows) != 1 { |
| + t.Fatalf("%s: create audit %+v, want shop's alone", what, rows) |
| + } |
| + wantCustomerNamed(t, fx, cust.Sqid, "shop") |
| + } |
| + for _, c := range []struct { |
| + what string |
| + customer string |
| + body map[string]any |
| + status int |
| + }{ |
| + {"an empty body", cust.Sqid, map[string]any{}, http.StatusBadRequest}, |
| + {"an unknown field", cust.Sqid, map[string]any{"nmae": "shop-2"}, http.StatusBadRequest}, |
| + {"a bad name", cust.Sqid, map[string]any{"name": "Shop Two"}, http.StatusBadRequest}, |
| + {"a plan outside the grant", cust.Sqid, map[string]any{"plan": "team"}, http.StatusBadRequest}, |
| + {"an unknown customer", "zzzz", map[string]any{"name": "shop-2"}, http.StatusNotFound}, |
| + } { |
| + status, _, raw := updateCustomer(t, fx.ts.URL, p.Sqid, c.customer, tok, c.body) |
| + if status != c.status { |
| + t.Fatalf("%s = %d %s, want %d", c.what, status, raw, c.status) |
| + } |
| + wantUnfinished(c.what) |
| + } |
| + |
| + // A valid update finishes the promise on its way. |
| + if status, _, raw := updateCustomer(t, fx.ts.URL, p.Sqid, cust.Sqid, tok, map[string]any{"name": "shop-2"}); status != http.StatusOK { |
| + t.Fatalf("valid update = %d %s", status, raw) |
| + } |
| + if pm := pendingMints(t, fx.st, p.Sqid); len(pm) != 0 { |
| + t.Fatalf("pending mints %+v after a valid update, want the promise finished", pm) |
| + } |
| + if cs, err := fx.st.LinkedCustomers(ctx, p.Sqid); err != nil || len(cs) != 2 { |
| + t.Fatalf("customers %+v %v, want shop-2 and cafe", cs, err) |
| + } |
| +} |
diff --git a/internal/console/applocks.go b/internal/console/applocks.go| index 976f06f41..e773db7d6 100644 |
| --- a/internal/console/applocks.go |
| +++ b/internal/console/applocks.go |
| @@ -53,12 +53,14 @@ func (l *keyedLocks) lock(key string) func() { |
| } |
| } |
| -// lockAccount serialises counting temporary apps against creating or |
| -// restoring one, which is what makes the cap exact (spec §3, §6). |
| +// lockAccount is the per-account lock: a count and the write it bounds run |
| +// under it, so a cap is exact. It began with the temporary-app cap |
| +// (/new-temporary and restoreApp, spec §3, §6); planLock's capped doors, |
| +// store and mail doors and a platform's customer rename take it too. |
| // |
| -// LOCK ORDER: account before app, never the other way round. Only |
| -// /new-temporary and restoreApp take this lock. It sits between the |
| -// platform lifecycle lock and the billing hold (lockPlatformLifecycle). |
| +// LOCK ORDER: account before app, never the other way round. It sits |
| +// between the platform lifecycle lock and the billing hold |
| +// (lockPlatformLifecycle). |
| // |
| // It emits lockEvent("account", sqid, ...) like lockApp, so a test can |
| // watch the order (platform accounts plan, Task 9). |
diff --git a/internal/console/platform_seams.go b/internal/console/platform_seams.go| index 9226bbcc9..9571d1755 100644 |
| --- a/internal/console/platform_seams.go |
| +++ b/internal/console/platform_seams.go |
| @@ -28,6 +28,10 @@ type platformSeams struct { |
| // deleteAccountNow's re-run of deletionRefused passed, before the |
| // bucket's tombstone, under the same locks. |
| deleteBeforeTombstone func() |
| + // updateBeforePlan, when set (tests only), is called by the customer |
| + // update door after its operator-row pre-check and before the plan |
| + // change, with no lock held: a test may assign an operator's plan there. |
| + updateBeforePlan func() |
| } |
| // platformStep runs the platformMintStep seam, if one is set. |