console: demo holding-account removal revokes the member's credentials
removeDemoHoldingMemberWrite deleted the membership without the revocation every other removal makes. It now calls revokeMemberCredentialsTx in the same transaction (queueing app rows when the claim gate is on) and removes the member's rows through removeMembershipRowsTx. The last-owner token refusal is unchanged. (Fable P2-5, review round 2.)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+31
−15
cmd/console/member_demo_test.go+16 −2internal/console/demo_members.go+12 −10internal/console/member_demo_test.go+3 −3
diff --git a/cmd/console/member_demo_test.go b/cmd/console/member_demo_test.go| index 361bc6951..fc15f49c6 100644 |
| --- a/cmd/console/member_demo_test.go |
| +++ b/cmd/console/member_demo_test.go |
| @@ -15,7 +15,7 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) { |
| bin := buildConsole(t) |
| for _, tc := range []struct { |
| name, enabled, domain, fleetOwner, target, address, want string |
| - extraMember, extraOwner, liveTokens bool |
| + extraMember, extraOwner, liveTokens, ownToken bool |
| }{ |
| {name: "prepare before admission", fleetOwner: "shop", want: "has no members"}, |
| {name: "admission enabled", enabled: "1", fleetOwner: "shop", want: "has no members"}, |
| @@ -30,6 +30,9 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) { |
| {name: "live tokens", fleetOwner: "shop", liveTokens: true, want: "console: cannot remove the last owner while CLI or service tokens remain unrevoked. revoke them first through the account's service pages (including expired tokens):"}, |
| {name: "live tokens with member remaining", fleetOwner: "shop", extraMember: true, liveTokens: true, want: "console: cannot remove the last owner while CLI or service tokens remain unrevoked. revoke them first through the account's service pages (including expired tokens):"}, |
| {name: "another owner can revoke", fleetOwner: "shop", extraOwner: true, liveTokens: true, want: "removed"}, |
| + // The removed member's own credentials end with the membership |
| + // (security hunt H4-M1, review round 2), as on any other account. |
| + {name: "removed owner's credentials end", fleetOwner: "shop", extraOwner: true, ownToken: true, want: "removed"}, |
| } { |
| t.Run(tc.name, func(t *testing.T) { |
| dir := t.TempDir() |
| @@ -57,7 +60,7 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) { |
| } |
| // Tokens minted by someone else must be reported too. No implicit revocation. |
| // A mint re-reads its minter's membership (security hunt H4-M1), so the |
| - // minter is a member for the mint only; the demo path revokes nothing. |
| + // minter is a member for the mint only. |
| if err := st.AddMember(42, "another@keymail.dev", console.RoleMember, now); err != nil { |
| t.Fatal(err) |
| } |
| @@ -84,6 +87,12 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) { |
| t.Fatal(err) |
| } |
| } |
| + var own string |
| + if tc.ownToken { |
| + if own, err = st.NewAppToken(42, "example", "owner@keymail.dev", "owner's credential", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| reg, err := registry.Open(regPath) |
| if err != nil { |
| t.Fatal(err) |
| @@ -138,6 +147,11 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) { |
| if (tc.extraMember || tc.extraOwner || !success) && strings.Contains(out, "has no members") { |
| t.Fatalf("false empty report: %s", out) |
| } |
| + if tc.ownToken { |
| + if _, ok := st.CLIPrincipal(own, time.Now()); ok { |
| + t.Fatal("the removed owner's instance token still resolves") |
| + } |
| + } |
| after, err := st.CLITokens("another@keymail.dev") |
| if err != nil || (tc.liveTokens && (len(after) != 1 || after[0].ID != toks[0].ID)) || (!tc.liveTokens && len(after) != 0) { |
| t.Fatalf("tokens changed: %v %v", after, err) |
diff --git a/internal/console/demo_members.go b/internal/console/demo_members.go| index eb8f6ac8e..c6b7b1ddf 100644 |
| --- a/internal/console/demo_members.go |
| +++ b/internal/console/demo_members.go |
| @@ -15,7 +15,10 @@ import ( |
| // RemoveDemoHoldingMember is only for the box-side member tool. The registry |
| // is attached read-only so fleet ownership and membership are read in the same |
| // transaction. Use a dedicated Store: the attachment lasts for this call. |
| -// HTTP removal continues to use removeMemberTx and its last-owner guard. |
| +// HTTP removal continues to use removeMemberRevokingTx and its last-owner |
| +// guard. Like every removal, this one revokes what the member minted for |
| +// the account (revokeMemberCredentialsTx); the console's link sync loop |
| +// publishes the door-key records it queues. |
| func (s *Store) RemoveDemoHoldingMember(accountID int64, address string, cfg demo.Config, registryPath string, _ *plans.Catalog, _ time.Time) (err error) { |
| // Preparation works before admission is enabled, but needs the complete |
| // admission tuple. Validate a copy without changing the operator's switch. |
| @@ -40,13 +43,16 @@ func (s *Store) RemoveDemoHoldingMember(accountID int64, address string, cfg dem |
| err = fmt.Errorf("console: removal committed but registry detach failed: %w", detachErr) |
| } |
| }() |
| - err = s.MembershipTransition(removeDemoHoldingMemberWrite(accountID, address, cfg)) |
| + err = s.MembershipTransition(removeDemoHoldingMemberWrite(accountID, address, cfg, s.gate != nil)) |
| return err |
| } |
| // Recheck both identities here; an earlier command-side lookup cannot grant |
| -// the exception. No token is revoked by this write. |
| -func removeDemoHoldingMemberWrite(accountID int64, address string, cfg demo.Config) func(*sql.Tx) error { |
| +// the exception. The last-owner refusal below still stands for anyone's |
| +// live tokens; past it, the removed member's own credentials are revoked |
| +// in this transaction, as every removal does (security hunt H4-M1, review |
| +// round 2). gated is the host claim's SQL gate being on (Store.gate). |
| +func removeDemoHoldingMemberWrite(accountID int64, address string, cfg demo.Config, gated bool) func(*sql.Tx) error { |
| return func(tx *sql.Tx) error { |
| cfg.Enabled = true |
| if err := cfg.Validate(); err != nil { |
| @@ -101,14 +107,10 @@ func removeDemoHoldingMemberWrite(accountID int64, address string, cfg demo.Conf |
| } |
| } |
| } |
| - if _, err := tx.Exec(`DELETE FROM app_stars WHERE account_id = ? AND address = ?`, accountID, address); err != nil { |
| + if _, err := revokeMemberCredentialsTx(tx, accountID, address, time.Now(), gated); err != nil { |
| return err |
| } |
| - if _, err := tx.Exec(`DELETE FROM getting_started_marks WHERE account_id = ? AND address = ?`, accountID, address); err != nil { |
| - return err |
| - } |
| - _, err = tx.Exec(`DELETE FROM memberships WHERE account_id = ? AND address = ?`, accountID, address) |
| - return err |
| + return removeMembershipRowsTx(tx, accountID, address) |
| } |
| } |
diff --git a/internal/console/member_demo_test.go b/internal/console/member_demo_test.go| index fc4fdd3a4..49502e817 100644 |
| --- a/internal/console/member_demo_test.go |
| +++ b/internal/console/member_demo_test.go |
| @@ -82,7 +82,7 @@ func TestDemoHoldingMemberWriteRechecksAndRollsBack(t *testing.T) { |
| if tc.edit != nil { |
| tc.edit(&cfg) |
| } |
| - write := removeDemoHoldingMemberWrite(42, "owner@keymail.dev", cfg) |
| + write := removeDemoHoldingMemberWrite(42, "owner@keymail.dev", cfg, false) |
| if tc.moveFleet { |
| fleet.Account = "other" |
| if err := reg.UpsertFleet(fleet); err != nil { |
| @@ -195,7 +195,7 @@ func TestDemoHoldingOpsAccountRefused(t *testing.T) { |
| if _, err := st.db.Exec(`ATTACH DATABASE ? AS demo_holding_registry`, path); err != nil { |
| t.Fatal(err) |
| } |
| - err = st.MembershipTransition(removeDemoHoldingMemberWrite(OpsAccountID, "owner@keymail.dev", cfg)) |
| + err = st.MembershipTransition(removeDemoHoldingMemberWrite(OpsAccountID, "owner@keymail.dev", cfg, false)) |
| want = "not the configured demo holding account" |
| } |
| if err == nil || !strings.Contains(err.Error(), want) { |
| @@ -249,7 +249,7 @@ func TestDemoHoldingLastOwnerTokens(t *testing.T) { |
| } |
| // Construct the write before minting credentials to catch a check |
| // that moves outside the membership transaction. |
| - write := removeDemoHoldingMemberWrite(42, "owner@keymail.dev", cfg) |
| + write := removeDemoHoldingMemberWrite(42, "owner@keymail.dev", cfg, false) |
| for _, id := range []string{"first-token-id", "second-token-id"} { |
| _, err := st.db.Exec(`INSERT INTO cli_tokens(token_hash,address,label,name,created_at,expires_at,account_id,kind,revoked_at) |
| VALUES(?, 'another@keymail.dev', 'deploy label', ?, ?, ?, ?, ?, ?)`, id, "deploy name "+id, now.Unix(), expires, tc.account, tc.kind, revoked) |