console: platform revoke passes the claim gate, mints always end
Fix round 1 of Task 4 (Astra and Fable). - The claim gate gets a revoke-only allowance (RevokeFree, RevokeScope): an UPDATE that only raises cli_tokens.revoked_at from 0, on a row with platform_account_id != 0, needs no permit. RevokePlatformGrant's sweep of app-bound per-customer credentials no longer aborts, so the grant and its generation end in one transaction under an armed gate. - DeleteAccount retires the pending mint a customer was materialized from, and CreateLinkedAccount over that customer's tombstone drops the mint and returns ErrAccountGone without resurrecting the account. - ReservePlatformMint: a ref lost to the unique index is ErrPlatformRefTaken; a mint id already reserved or carried by an account is errPlatformMintUsed. - Tests for each, plus PutPlatformGrant's ErrAccountGone and a tighter duplicate-ref assertion.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+274
−23
internal/console/ownership.go+13 −3internal/console/platform_store.go+45 −16internal/console/platform_store_test.go+192 −2internal/console/store_gate.go+24 −2
diff --git a/internal/console/ownership.go b/internal/console/ownership.go| index 9f71832d8..614dadff9 100644 |
| --- a/internal/console/ownership.go |
| +++ b/internal/console/ownership.go |
| @@ -786,7 +786,8 @@ func (s *Store) TransferOwnership(accountID int64, from, to string, now time.Tim |
| // rule stays safe. An account id is spent once; there is no un-deleting one. |
| // |
| // A platform account with a live customer account or a pending mint is |
| -// refused with ErrPlatformHasCustomers (platform_store.go). |
| +// refused with ErrPlatformHasCustomers (platform_store.go). A customer |
| +// account takes its own unfinished pending mint with it. |
| // |
| // Audit rows are deliberately left behind: they are the record of what was |
| // done, and a deletion is not a reason to lose it. |
| @@ -808,9 +809,10 @@ func (s *Store) DeleteAccount(accountID int64, now time.Time) error { |
| } |
| // A platform account is never deleted while it has a live customer or a |
| // promised one (a pending mint): either would be left linked to nothing. |
| - // DeleteAccount takes no ctx; the new query uses the Context form anyway. |
| + // DeleteAccount takes no ctx; the new queries use the Context form anyway. |
| + ctx := context.Background() |
| var customers int |
| - if err := tx.QueryRowContext(context.Background(), `SELECT |
| + if err := tx.QueryRowContext(ctx, `SELECT |
| (SELECT COUNT(*) FROM accounts c JOIN accounts p ON c.platform = p.sqid |
| WHERE p.id = ? AND p.sqid != '' AND c.deleted_at = 0) |
| + (SELECT COUNT(*) FROM platform_pending_mints m JOIN accounts p ON m.platform = p.sqid |
| @@ -820,6 +822,14 @@ func (s *Store) DeleteAccount(accountID int64, now time.Time) error { |
| if customers > 0 { |
| return ErrPlatformHasCustomers |
| } |
| + // A customer materialized from a pending mint that was never finished |
| + // takes the mint with it, so the promise cannot outlive the account and |
| + // hold its platform's capacity and ref for ever. |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM platform_pending_mints WHERE EXISTS |
| + (SELECT 1 FROM accounts a WHERE a.id = ? AND a.platform != '' AND a.platform_mint != '' |
| + AND a.platform = platform_pending_mints.platform AND a.platform_mint = platform_pending_mints.mint_id)`, accountID); err != nil { |
| + return err |
| + } |
| for _, stmt := range []string{ |
| `DELETE FROM app_stars WHERE account_id = ?`, |
| `DELETE FROM getting_started_marks WHERE account_id = ?`, |
diff --git a/internal/console/platform_store.go b/internal/console/platform_store.go| index 94c6d91bd..e4f692500 100644 |
| --- a/internal/console/platform_store.go |
| +++ b/internal/console/platform_store.go |
| @@ -95,6 +95,10 @@ var ( |
| ErrNotPlatform = errors.New("console: this account is not a platform account") |
| ErrPlatformHasCustomers = errors.New("console: this platform account still has customer accounts") |
| ErrPlatformRefTaken = errors.New("console: that reference already names a customer account") |
| + // errPlatformMintUsed is a mint id already reserved, or already carried |
| + // by an account. Mint ids are 128 random bits minted fresh for each |
| + // create, so this is a caller's bug, never a customer's mistake. |
| + errPlatformMintUsed = errors.New("console: that mint id is already in use") |
| ) |
| // dbQuerier is the one method both *sql.DB (and the Store's sqlHandle) and |
| @@ -389,8 +393,11 @@ func (s *Store) LinkedCustomerCount(ctx context.Context, platformSqid string) (i |
| // ReservePlatformMint records a promised customer create before the bucket |
| // is touched. A non-empty ref already held by a live customer of the |
| -// platform, or by another of its pending mints, is ErrPlatformRefTaken; the |
| -// partial unique index on pending refs backs the check. |
| +// platform, or by another of its pending mints, is ErrPlatformRefTaken: the |
| +// first is checked here, the second is the partial unique index's to refuse |
| +// (platform_pending_mints_ref), so a reservation that loses that race gets |
| +// the same answer. A mint id already reserved, or carried by an account, is |
| +// errPlatformMintUsed: a mint id names one promise. |
| func (s *Store) ReservePlatformMint(ctx context.Context, m PendingMint) error { |
| if m.MintID == "" || m.Platform == "" { |
| return errors.New("console: a pending mint needs a mint id and a platform") |
| @@ -400,21 +407,30 @@ func (s *Store) ReservePlatformMint(ctx context.Context, m PendingMint) error { |
| return err |
| } |
| defer tx.Rollback() |
| - if m.Ref != "" { |
| - var held int |
| - if err := tx.QueryRowContext(ctx, `SELECT |
| - (SELECT COUNT(*) FROM accounts WHERE platform = ? AND platform_ref = ? AND deleted_at = 0) |
| - + (SELECT COUNT(*) FROM platform_pending_mints WHERE platform = ? AND ref = ?)`, |
| - m.Platform, m.Ref, m.Platform, m.Ref).Scan(&held); err != nil { |
| - return err |
| - } |
| - if held > 0 { |
| - return ErrPlatformRefTaken |
| - } |
| + var used, held int |
| + if err := tx.QueryRowContext(ctx, `SELECT |
| + (SELECT COUNT(*) FROM platform_pending_mints WHERE mint_id = ?) + (SELECT COUNT(*) FROM accounts WHERE platform_mint = ?), |
| + (SELECT COUNT(*) FROM accounts WHERE platform = ? AND platform_ref = ? AND platform_ref != '' AND deleted_at = 0)`, |
| + m.MintID, m.MintID, m.Platform, m.Ref).Scan(&used, &held); err != nil { |
| + return err |
| + } |
| + if used > 0 { |
| + return errPlatformMintUsed |
| + } |
| + if held > 0 { |
| + return ErrPlatformRefTaken |
| } |
| if _, err := tx.ExecContext(ctx, `INSERT INTO platform_pending_mints |
| (mint_id, platform, ref, name, plan, plan_reason, by, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, |
| m.MintID, m.Platform, m.Ref, m.Name, m.Plan, m.PlanReason, m.By, m.CreatedAt.Unix()); err != nil { |
| + // SQLite names the index's columns in its message; there is no |
| + // other way through database/sql to tell which constraint failed. |
| + switch msg := err.Error(); { |
| + case strings.Contains(msg, "UNIQUE constraint failed: platform_pending_mints.platform, platform_pending_mints.ref"): |
| + return ErrPlatformRefTaken |
| + case strings.Contains(msg, "UNIQUE constraint failed: platform_pending_mints.mint_id"): |
| + return errPlatformMintUsed |
| + } |
| return err |
| } |
| return tx.Commit() |
| @@ -448,8 +464,10 @@ func (s *Store) PendingPlatformMints(ctx context.Context, platformSqid string) ( |
| // replaces only a missing or default-source plan row; otherwise the |
| // accounts row is inserted with its link, and the plan row written with |
| // source platform. Then the pending mint is deleted. Finishing twice is a |
| -// no-op. An id held by any other account is an error, and the pending |
| -// mint is kept. |
| +// no-op. A finish over this customer's tombstone (materialized, then |
| +// deleted) drops the pending mint and is ErrAccountGone: a pending mint |
| +// always reaches an end state, and a deleted account is never brought back. |
| +// An id held by any other account is an error, and the pending mint is kept. |
| // |
| // No membership: a customer account has no owner until one claims it. No |
| // launch sponsorship either: a customer's plan is its platform's (decision |
| @@ -475,8 +493,19 @@ func (s *Store) CreateLinkedAccount(ctx context.Context, a NewLinkedAccount, now |
| } |
| case err != nil: |
| return err |
| - case sqid != a.Sqid || platform != a.Platform || deleted != 0: |
| + case sqid != a.Sqid || platform != a.Platform: |
| return errors.New("console: that account id belongs to another account") |
| + case deleted != 0: |
| + // This customer was materialized and then deleted before the |
| + // finish. The finish ends here: the promise is dropped (the delete |
| + // normally did that already) and the account stays deleted. |
| + if _, err := tx.ExecContext(ctx, `DELETE FROM platform_pending_mints WHERE mint_id = ?`, a.MintID); err != nil { |
| + return err |
| + } |
| + if err := tx.Commit(); err != nil { |
| + return err |
| + } |
| + return ErrAccountGone |
| default: |
| // Materialized first (or finished before): only a default plan |
| // gives way to the platform's. |
diff --git a/internal/console/platform_store_test.go b/internal/console/platform_store_test.go| index 93f640fb8..0633d8d80 100644 |
| --- a/internal/console/platform_store_test.go |
| +++ b/internal/console/platform_store_test.go |
| @@ -3,6 +3,7 @@ package console |
| import ( |
| "context" |
| "errors" |
| + "maps" |
| "path/filepath" |
| "reflect" |
| "testing" |
| @@ -455,8 +456,8 @@ func TestLinkedCustomerRefIsUniquePerPlatform(t *testing.T) { |
| if err := s.CreateLinkedAccount(ctx, first, platformNow); err != nil { |
| t.Fatal(err) |
| } |
| - if err := s.CreateLinkedAccount(ctx, customerOf("bcd", "bdg", "4812", ""), platformNow); err == nil { |
| - t.Fatal("a second customer with ref 4812 on bcd was created") |
| + if err := s.CreateLinkedAccount(ctx, customerOf("bcd", "bdg", "4812", ""), platformNow); !errors.Is(err, ErrPlatformRefTaken) { |
| + t.Fatalf("a second customer with ref 4812 on bcd = %v, want ErrPlatformRefTaken", err) |
| } |
| if err := s.CreateLinkedAccount(ctx, customerOf("bcf", "bdh", "4812", ""), platformNow); err != nil { |
| t.Fatalf("ref 4812 on another platform: %v", err) |
| @@ -637,3 +638,192 @@ func TestPlatformTablesUnderTheGate(t *testing.T) { |
| t.Fatal(err) |
| } |
| } |
| + |
| +// A per-customer Deploy credential is bound to an app, so under the armed |
| +// gate its row is app-scoped. The revoke still ends the grant, moves the |
| +// generation and marks that row revoked: a refused sweep must never roll |
| +// the generation back and leave the grant active. |
| +func TestRevokePlatformGrantUnderTheGateWithAnAppBoundCredential(t *testing.T) { |
| + s := openGatedStore(t) |
| + ctx := context.Background() |
| + p := newPlatform(t, s, "bcd") |
| + grantPlatform(t, s, p) |
| + cust := testAccountID() |
| + scope := claimSQLScope{AccountID: cust, App: "web", Mode: claimModeEffect} |
| + if err := s.withClaimSQL(ctx, []claimSQLScope{scope}, allowAllClaimSQL, func(v *Store) error { |
| + _, err := v.db.ExecContext(ctx, `INSERT INTO cli_tokens |
| + (token_hash, address, created_at, expires_at, kind, account_id, app, role, rungs, name, platform_account_id, platform_generation) |
| + VALUES ('deploy-web', 'svc@example.com', ?, ?, 'platform', ?, 'web', 'deploy', 'prod', 'deploy-web', ?, 1), |
| + ('service-web', 'svc@example.com', ?, 0, 'service', ?, 'web', 'deploy', 'prod', 'service-web', 0, 0)`, |
| + platformNow.Unix(), platformNow.Add(2*time.Hour).Unix(), cust, p, platformNow.Unix(), cust) |
| + return err |
| + }); err != nil { |
| + t.Fatal(err) |
| + } |
| + // The allowance is the platform rows' alone: an ordinary app-bound |
| + // service credential still needs a permit to be revoked. |
| + if _, err := s.db.ExecContext(ctx, `UPDATE cli_tokens SET revoked_at = 5 WHERE token_hash = 'service-web'`); err == nil { |
| + t.Fatal("an unpermitted revoke of an app-bound service credential passed the gate") |
| + } |
| + // The gate is armed: an ordinary write to that row is still refused. |
| + if _, err := s.db.ExecContext(ctx, `UPDATE cli_tokens SET name = 'renamed' WHERE token_hash = 'deploy-web'`); err == nil { |
| + t.Fatal("an unpermitted rename of an app-bound credential passed the gate") |
| + } |
| + revokeAt := platformNow.Add(time.Hour) |
| + n, err := s.RevokePlatformGrant(ctx, p, "operator@example.com", "ended", revokeAt) |
| + if err != nil || n != 1 { |
| + t.Fatalf("RevokePlatformGrant under the gate = %d, %v; want 1, nil", n, err) |
| + } |
| + g, err := s.PlatformGrant(ctx, p) |
| + if err != nil || g.State != PlatformGrantRevoked || g.Generation != 2 { |
| + t.Fatalf("grant after the gated revoke = %+v, %v; want revoked at generation 2", g, err) |
| + } |
| + if at := tokenRevokedAt(t, s, "deploy-web"); at != revokeAt.Unix() { |
| + t.Fatalf("app-bound credential revoked_at = %d, want %d", at, revokeAt.Unix()) |
| + } |
| + // Revoked once is revoked for good: lowering it is not a revoke. |
| + if _, err := s.db.ExecContext(ctx, `UPDATE cli_tokens SET revoked_at = 0 WHERE token_hash = 'deploy-web'`); err == nil { |
| + t.Fatal("un-revoking an app-bound credential passed the gate") |
| + } |
| +} |
| + |
| +// A customer materialized from a pending mint and deleted before the mint |
| +// was finished takes the mint with it: the capacity and the ref come back, |
| +// a retried finish does not bring the account back, and the platform can be |
| +// deleted. |
| +func TestDeletingAMaterializedCustomerRetiresItsPendingMint(t *testing.T) { |
| + s := openTestStore(t) |
| + ctx := context.Background() |
| + p := newPlatform(t, s, "bcd") |
| + reserveMint(t, s, "bcd", "aa11", "4812") |
| + c := customerOf("bcd", "bdf", "4812", "aa11") |
| + if _, err := s.EnsureLinkedAccount(c.ID, c.Sqid, c.Name, "bcd", "4812", "aa11", platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + // Another platform's pending mint is not this customer's. |
| + reserveMint(t, s, "bcf", "bb22", "") |
| + if err := s.DeleteAccount(c.ID, platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n, m := linkedCount(t, s, "bcd"), pendingMints(t, s, "bcd"); n != 0 || len(m) != 0 { |
| + t.Fatalf("after the delete: count %d, pending %+v; want 0, none", n, m) |
| + } |
| + if m := pendingMints(t, s, "bcf"); len(m) != 1 { |
| + t.Fatalf("the other platform's pending mints = %+v, want its one kept", m) |
| + } |
| + // The retried finish ends there: the account stays deleted. |
| + if err := s.CreateLinkedAccount(ctx, c, platformNow); !errors.Is(err, ErrAccountGone) { |
| + t.Fatalf("retried finish of a deleted customer = %v, want ErrAccountGone", err) |
| + } |
| + if a, err := s.AccountByID(c.ID); err != nil || a != nil { |
| + t.Fatalf("the deleted customer after the retry = %+v, %v; want still deleted", a, err) |
| + } |
| + if n := linkedCount(t, s, "bcd"); n != 0 { |
| + t.Fatalf("count after the retry = %d, want 0", n) |
| + } |
| + // The ref is free: a new customer takes it. |
| + reserveMint(t, s, "bcd", "cc33", "4812") |
| + d := customerOf("bcd", "bdg", "4812", "cc33") |
| + if err := s.CreateLinkedAccount(ctx, d, platformNow); err != nil { |
| + t.Fatalf("a new customer with the freed ref: %v", err) |
| + } |
| + if err := s.DeleteAccount(d.ID, platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.DeleteAccount(p, platformNow); err != nil { |
| + t.Fatalf("deleting the platform afterwards = %v, want nil", err) |
| + } |
| +} |
| + |
| +// A finish whose pending mint a delete already retired, with the mint |
| +// still recorded (an interrupted older path), drops the mint too. |
| +func TestFinishingOverATombstoneDropsTheMint(t *testing.T) { |
| + s := openTestStore(t) |
| + ctx := context.Background() |
| + c := customerOf("bcd", "bdf", "4812", "aa11") |
| + if _, err := s.EnsureLinkedAccount(c.ID, c.Sqid, c.Name, "bcd", "4812", "", platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.DeleteAccount(c.ID, platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + reserveMint(t, s, "bcd", "aa11", "4812") |
| + if err := s.CreateLinkedAccount(ctx, c, platformNow); !errors.Is(err, ErrAccountGone) { |
| + t.Fatalf("finish over a tombstone = %v, want ErrAccountGone", err) |
| + } |
| + if m := pendingMints(t, s, "bcd"); len(m) != 0 { |
| + t.Fatalf("pending mints = %+v, want the finished one gone", m) |
| + } |
| +} |
| + |
| +func TestReservePlatformMintRefusesADuplicate(t *testing.T) { |
| + s := openTestStore(t) |
| + ctx := context.Background() |
| + reserveMint(t, s, "bcd", "aa11", "4812") |
| + // A ref that only the unique index catches (the live-account check sees |
| + // no account) is still ErrPlatformRefTaken. |
| + if err := s.ReservePlatformMint(ctx, PendingMint{MintID: "bb22", Platform: "bcd", Ref: "4812", Name: "Again", |
| + By: "service:bcd/cms", CreatedAt: platformNow}); !errors.Is(err, ErrPlatformRefTaken) { |
| + t.Fatalf("a pending ref twice = %v, want ErrPlatformRefTaken", err) |
| + } |
| + // A mint id is one promise: reserving it again, with any ref, is |
| + // errPlatformMintUsed, and the first reservation is unchanged. |
| + for _, ref := range []string{"4812", "4813", ""} { |
| + if err := s.ReservePlatformMint(ctx, PendingMint{MintID: "aa11", Platform: "bcd", Ref: ref, Name: "Again", |
| + By: "service:bcd/cms", CreatedAt: platformNow}); !errors.Is(err, errPlatformMintUsed) { |
| + t.Fatalf("mint id aa11 again with ref %q = %v, want errPlatformMintUsed", ref, err) |
| + } |
| + } |
| + // So is an id a finished customer already carries. |
| + c := customerOf("bcd", "bdf", "4812", "aa11") |
| + if err := s.CreateLinkedAccount(ctx, c, platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.ReservePlatformMint(ctx, PendingMint{MintID: "aa11", Platform: "bcd", Ref: "4999", Name: "Again", |
| + By: "service:bcd/cms", CreatedAt: platformNow}); !errors.Is(err, errPlatformMintUsed) { |
| + t.Fatalf("a finished mint id again = %v, want errPlatformMintUsed", err) |
| + } |
| + if m := pendingMints(t, s, "bcd"); len(m) != 0 { |
| + t.Fatalf("pending mints = %+v, want none", m) |
| + } |
| +} |
| + |
| +func TestPutPlatformGrantRefusesADeadAccount(t *testing.T) { |
| + s := openTestStore(t) |
| + ctx := context.Background() |
| + if err := s.PutPlatformGrant(ctx, PlatformGrant{AccountID: 999_999, MaxCustomers: 1, Reason: "r", |
| + GrantedBy: "operator@example.com"}, platformNow); !errors.Is(err, ErrAccountGone) { |
| + t.Fatalf("grant to an unknown account = %v, want ErrAccountGone", err) |
| + } |
| + p := newPlatform(t, s, "bcd") |
| + if err := s.DeleteAccount(p, platformNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.PutPlatformGrant(ctx, PlatformGrant{AccountID: p, MaxCustomers: 1, Reason: "r", |
| + GrantedBy: "operator@example.com"}, platformNow); !errors.Is(err, ErrAccountGone) { |
| + t.Fatalf("grant to a deleted account = %v, want ErrAccountGone", err) |
| + } |
| + if g, err := s.PlatformGrant(ctx, p); err != nil || g != nil { |
| + t.Fatalf("grant row after the refusals = %+v, %v; want none", g, err) |
| + } |
| +} |
| + |
| +// The revoke allowance names real, non-scope columns, or the gate does not arm. |
| +func TestClaimSQLCensusChecksTheRevokeAllowance(t *testing.T) { |
| + s := openGatedStore(t) |
| + for _, bad := range []claimSQLTable{ |
| + {RevokeFree: "revoked_at"}, |
| + {RevokeScope: "platform_account_id"}, |
| + {RevokeFree: "revoked_at", RevokeScope: "no_such_column"}, |
| + {RevokeFree: "revoked_at", RevokeScope: "account_id"}, |
| + {RevokeFree: "app", RevokeScope: "platform_account_id"}, |
| + } { |
| + tables := maps.Clone(claimSQLTables) |
| + spec := tables["cli_tokens"] |
| + spec.RevokeFree, spec.RevokeScope = bad.RevokeFree, bad.RevokeScope |
| + tables["cli_tokens"] = spec |
| + if _, err := claimSQLCensus(s.bootDB(), tables); !errors.Is(err, ErrClaimSQLCensus) { |
| + t.Errorf("census with RevokeFree %q, RevokeScope %q = %v, want ErrClaimSQLCensus", bad.RevokeFree, bad.RevokeScope, err) |
| + } |
| + } |
| +} |
diff --git a/internal/console/store_gate.go b/internal/console/store_gate.go| index 4ce46c6b3..855d242d4 100644 |
| --- a/internal/console/store_gate.go |
| +++ b/internal/console/store_gate.go |
| @@ -161,6 +161,17 @@ type claimSQLTable struct { |
| // in an UPDATE of the retired incarnation's rows; "" means none. |
| PurgeMonotone string |
| + // RevokeFree is the one column an UPDATE may raise from 0 to a positive |
| + // value with no permit, every other column unchanged, on a row whose |
| + // RevokeScope column is not 0. A revoke only takes authority away, and |
| + // the rows it covers are revoked by a sweep that holds no app |
| + // incarnation to bind a permit to (cli_tokens: a platform grant's |
| + // revoke, which must end every per-customer credential, app-bound ones |
| + // included, in the transaction that ends the grant's generation). |
| + // Lowering it, or changing anything else, still needs a permit. |
| + RevokeFree string |
| + RevokeScope string |
| + |
| // Labels lists columns whose names look like app references but are not |
| // one: a label the table's own rows carry, never a scope. The census skips |
| // them for this table only, and honours the field on a gated table only |
| @@ -222,7 +233,7 @@ var claimSQLTables = map[string]claimSQLTable{ |
| // are refused outright; enabled access follows account membership. |
| "owners": {App: "app", Refuse: "legacy per-app grants name no account"}, |
| "audit": {Account: "account_id", App: "app"}, |
| - "cli_tokens": {Account: "account_id", App: "app", FreeWhenNoApp: true}, // user tokens carry app '' |
| + "cli_tokens": {Account: "account_id", App: "app", FreeWhenNoApp: true, RevokeFree: "revoked_at", RevokeScope: "platform_account_id"}, // user tokens carry app ''; a per-customer platform credential is revoked with no permit |
| "door_keys": {Account: "account_id", App: "app", PurgeMonotone: "revoked_at"}, |
| "door_key_revisions": {Account: "account_id", App: "app", PurgeMonotone: "revision"}, |
| "app_policy_revisions": {Account: "account_id", App: "app"}, // apppolicy_store.go; the purge never writes it (revisions only rise) |
| @@ -551,6 +562,12 @@ func claimSQLCensus(db *sql.DB, tables map[string]claimSQLTable) (map[string]cla |
| if c := spec.PurgeMonotone; c != "" && (!cols[name][c] || c == spec.Account || c == spec.Sqid || c == spec.App || c == spec.AppID || spec.Via != nil) { |
| bad("table %q: PurgeMonotone column %q is missing or a scope column", name, c) |
| } |
| + if c, sc := spec.RevokeFree, spec.RevokeScope; c != "" || sc != "" { |
| + scopeCol := func(x string) bool { return x == spec.Account || x == spec.Sqid || x == spec.App || x == spec.AppID } |
| + if c == "" || sc == "" || c == sc || !cols[name][c] || !cols[name][sc] || scopeCol(c) || scopeCol(sc) || spec.Via != nil || spec.Refuse != "" { |
| + bad("table %q: RevokeFree %q and RevokeScope %q must both be present, distinct, not scope columns, on a direct unrefused table", name, c, sc) |
| + } |
| + } |
| } |
| if len(problems) > 0 { |
| return nil, fmt.Errorf("%w: %s", ErrClaimSQLCensus, strings.Join(problems, "; ")) |
| @@ -651,7 +668,8 @@ const claimDenyMessage = "claim: app write not permitted" |
| // INSERT an effect permit covers NEW |
| // UPDATE effect permits cover OLD and NEW; or one terminal permit covers |
| // OLD and NEW with the same app_id and only its columns change; or |
| -// one purge permit covers both and only PurgeMonotone rises |
| +// one purge permit covers both and only PurgeMonotone rises; or, |
| +// with no permit, a RevokeFree revoke of a RevokeScope row |
| // DELETE an effect permit covers OLD, or a purge permit's retired rows do |
| func claimTriggerSQL(name string, t claimSQLTable, hasApps bool) []string { |
| on := " ON " + claimQ(name) + " BEGIN SELECT RAISE(ABORT, '" + claimDenyMessage + "') WHERE " |
| @@ -682,6 +700,10 @@ func claimTriggerSQL(name string, t claimSQLTable, hasApps bool) []string { |
| update = append(update, "("+both+" AND "+rises+" AND "+t.unchanged(c)+")") |
| } |
| } |
| + if c := t.RevokeFree; c != "" { |
| + revokes := claimTrue("OLD." + claimQ(t.RevokeScope) + " != 0 AND OLD." + claimQ(c) + " = 0 AND NEW." + claimQ(c) + " > 0") |
| + update = append(update, "("+revokes+" AND "+t.unchanged(c)+")") |
| + } |
| return []string{ |
| mk("insert", "NOT "+t.effect("NEW")), |
| mk("update", "NOT ("+strings.Join(update, " OR ")+")"), |