carlos / platform Public

fix: holding account tokens are revoked before the last owner

codex agent, operated by paul@keymail.dev pushed by claude agent, operated by paul@keymail.dev 861cb10c2ee13d377224a184bfd947dc14c9d415 parent 42cb3ce
5 files changed, +182 −20
  • README.md +1 −1
  • cmd/console/member.go +0 −12
  • cmd/console/member_demo_test.go +17 −4
  • internal/console/demo_members.go +32 −3
  • internal/console/member_demo_test.go +132 −0

Intent

Human

<task-notification> <task-id>b1lcr9gew</task-id> <tool-use-id>toolu_012bdmpuGoodeshcFtpAM3i7</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/b1lcr9gew.output</output-file> <status>completed</status> <summary>Background command "Run the three witnesses on the demo box" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>b639w848m</task-id> <tool-use-id>toolu_01QiGsTM534XLoSsN3bmjRJC</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/b639w848m.output</output-file> <status>completed</status> <summary>Background command "Wait for witness results" completed (exit code 0)</summary> </task-notification>

Human

<task-notification> <task-id>a3663cb3b351278e2</task-id> <tool-use-id>toolu_01X6gw8Cpf77RoCvBN8DeEbT</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/cd1b6de2-ac77-426f-a77d-fc5436969c88/tasks/a3663cb3b351278e2.output</output-file> <status>completed</status> <summary>Agent "Security review of holding-account change" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>**READY AFTER FIXES** (no P1s) - **P2-1:** The README (README.md:622) and the tool's output say to revoke the remaining tokens after removing the last owner. But every token the tool lists is an account-bound credential, and those can only be revoked by a member through the account's service pages (svcpage.go:572-580). There is no box-side command to revoke them. An operator who follows the README empties the account and then can't clear the tokens, so admission refuses until someone adds an owner back. The README and the output should say to revoke them before the final removal. - **P2-2:** The ops-account refusal (demo_members.go:25 and :64) has no test. Deleting both checks leaves every test passing. - **P2-3:** Copy review hasn't run yet. The README paragraph and the new output lines ("has no members", "Live CLI tokens remain…") still need it. Do it together with the P2-1 rewording. Review: /home/paulca/.cache/tmp/claude-1001/agent-start-demo-1/holding-review-r1.md</result> <usage><subagent_tokens>84402</subagent_tokens><tool_uses>25</tool_uses><duration_ms>264184</duration_ms></usage> </task-notification>

diff --git a/README.md b/README.md
index cecef6f73..ad043ed04 100644
--- a/README.md
+++ b/README.md
@@ -619,7 +619,7 @@ Set these variables on the console:
| `CARLOS_DEMO_CREATE_GLOBAL_DAY` | Positive integer. Default `100`. |
| `CARLOS_DEMO_MAX_LIVE` | Positive integer. Default `20`. Failed and retiring demos keep their place until cleanup completes. |
-After creating the holding account and its demo fleet, run `console member -remove <address> "$CARLOS_DEMO_ACCOUNT"` on the console box for each member, leaving the owner until last. Use the console's environment with valid demo settings; you can leave `CARLOS_DEMO_APPS` unset while preparing the account. Only the configured holding account can lose its last owner, and it must own `CARLOS_DEMO_FLEET`. The command reports when no members remain and lists any live CLI tokens. Revoke those tokens separately before enabling admission.
+Create the holding account and its demo fleet, then revoke all of the account's CLI and service tokens through its service pages, including expired tokens. Only a member can revoke them, so do this before removing the last owner. Then run `console member -remove <address> "$CARLOS_DEMO_ACCOUNT"` on the console box for each member, leaving the owner until last. Use the console's environment with valid demo settings; you can leave `CARLOS_DEMO_APPS` unset while preparing the account. Only the configured holding account can lose its last owner, and it must own `CARLOS_DEMO_FLEET`. The command refuses to remove the last owner while any tokens remain unrevoked and lists them. It reports when no members remain.
Put each demo box's settings in its root-owned `/etc/carlos/host.env`. Set `CARLOS_DEMO_BOX=1` and the same `CARLOS_DEMO_ACCOUNT`, `CARLOS_DEMO_FLEET` and `CARLOS_DEMO_DOMAIN` tuple as the console. Keep the box's existing `CARLOS_FLEET`, box identity, tunnel credentials and release public key consistent with that fleet. These are per-box authority settings. Preserve them through host.env rewrites, using the same per-box mechanism as `CARLOS_FLEET`; do not deliver them through shared or regional parameters.
diff --git a/cmd/console/member.go b/cmd/console/member.go
index 17eccdcbb..967e52f95 100644
--- a/cmd/console/member.go
+++ b/cmd/console/member.go
@@ -243,18 +243,6 @@ func memberRemoveDemoHolding(st *console.Store, acct *console.Account, address s
return
}
fmt.Printf("%s (%s) has no members\n", acct.Sqid, acct.Name)
- tokens, err := st.AccountCLITokens(acct.ID)
- if err != nil {
- log.Fatalf("cannot list remaining CLI tokens; check and revoke them before enabling demo admission: %v", err)
- }
- if len(tokens) == 0 {
- fmt.Println("No live CLI tokens remain on this account.")
- return
- }
- fmt.Println("Live CLI tokens remain on this account; revoke them before enabling demo admission:")
- for _, tok := range tokens {
- fmt.Printf("%s\taddress=%q\tname=%q\tlabel=%q\tkind=%q\tapp=%q\n", tok.ID, tok.Address, tok.Name, tok.Label, tok.Kind, tok.App)
- }
}
// parseInterspersed parses flags that may appear AFTER positional arguments.
diff --git a/cmd/console/member_demo_test.go b/cmd/console/member_demo_test.go
index 52e8cde6a..4b581f004 100644
--- a/cmd/console/member_demo_test.go
+++ b/cmd/console/member_demo_test.go
@@ -14,7 +14,7 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
bin := buildConsole(t)
for _, tc := range []struct {
name, enabled, domain, fleetOwner, target, address, want string
- extraMember bool
+ extraMember, extraOwner, liveTokens bool
}{
{name: "prepare before admission", fleetOwner: "shop", want: "has no members"},
{name: "admission enabled", enabled: "1", fleetOwner: "shop", want: "has no members"},
@@ -26,6 +26,9 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
{name: "different account invalid config", enabled: "bad", target: "other", want: "last owner"},
{name: "not a member", fleetOwner: "shop", address: "absent@keymail.dev", want: "not a member"},
{name: "members remain", fleetOwner: "shop", extraMember: true, want: "removed"},
+ {name: "live tokens", fleetOwner: "shop", liveTokens: true, want: "revoke them first through the account's service pages"},
+ {name: "live tokens with member remaining", fleetOwner: "shop", extraMember: true, liveTokens: true, want: "revoke them first through the account's service pages"},
+ {name: "another owner can revoke", fleetOwner: "shop", extraOwner: true, liveTokens: true, want: "removed"},
} {
t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
@@ -46,6 +49,11 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
t.Fatal(err)
}
}
+ if tc.extraOwner {
+ if err := st.AddMember(42, "second@keymail.dev", console.RoleOwner, now); err != nil {
+ t.Fatal(err)
+ }
+ }
// Tokens minted by someone else must be reported too. No implicit revocation.
secret, err := st.NewAppToken(42, "example", "another@keymail.dev", "demo credential", now)
if err != nil {
@@ -55,6 +63,11 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
if err != nil || len(toks) != 1 {
t.Fatalf("tokens: %v %v", toks, err)
}
+ if !tc.liveTokens {
+ if err := st.RevokeAccountToken(42, toks[0].ID, now); err != nil {
+ t.Fatal(err)
+ }
+ }
reg, err := registry.Open(regPath)
if err != nil {
t.Fatal(err)
@@ -96,7 +109,7 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
if err != nil || (role == "") != success {
t.Fatalf("owner role=%q err=%v", role, err)
}
- if success && !tc.extraMember {
+ if !success && tc.liveTokens {
for _, want := range []string{toks[0].ID, "demo credential", "another@keymail.dev", "revoke"} {
if !strings.Contains(out, want) {
t.Errorf("output missing %q: %s", want, out)
@@ -106,11 +119,11 @@ func TestMemberDemoHoldingLastOwner(t *testing.T) {
t.Fatal("printed token secret")
}
}
- if tc.extraMember && strings.Contains(out, "has no members") {
+ if (tc.extraMember || tc.extraOwner || !success) && strings.Contains(out, "has no members") {
t.Fatalf("false empty report: %s", out)
}
after, err := st.CLITokens("another@keymail.dev")
- if err != nil || len(after) != 1 || after[0].ID != toks[0].ID {
+ 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 ec2a32b00..72719f04a 100644
--- a/internal/console/demo_members.go
+++ b/internal/console/demo_members.go
@@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"path/filepath"
+ "strings"
"time"
"github.com/carlosframework/platform/internal/demo"
@@ -80,6 +81,32 @@ func removeDemoHoldingMemberWrite(accountID int64, address string, cfg demo.Conf
if err != nil {
return err
}
+ if role == RoleOwner {
+ var owners int
+ if err := tx.QueryRow(`SELECT COUNT(*) FROM memberships WHERE account_id = ? AND role = ?`, accountID, RoleOwner).Scan(&owners); err != nil {
+ return err
+ }
+ if owners == 1 {
+ // Keep a member who can revoke credentials through the service
+ // pages. Count expired but unrevoked tokens, as admission does.
+ rows, err := tx.Query(accountCLITokensSQL, accountID)
+ if err != nil {
+ return err
+ }
+ tokens, err := scanCLITokenRows(rows)
+ if err != nil {
+ return err
+ }
+ if len(tokens) != 0 {
+ var message strings.Builder
+ message.WriteString("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):")
+ for _, tok := range tokens {
+ fmt.Fprintf(&message, "\n%s\taddress=%q\tname=%q\tlabel=%q\tkind=%q\tapp=%q", tok.ID, tok.Address, tok.Name, tok.Label, tok.Kind, tok.App)
+ }
+ return fmt.Errorf("%s", message.String())
+ }
+ }
+ }
if _, err := tx.Exec(`DELETE FROM app_stars WHERE account_id = ? AND address = ?`, accountID, address); err != nil {
return err
}
@@ -92,11 +119,13 @@ func removeDemoHoldingMemberWrite(accountID int64, address string, cfg demo.Conf
// but unrevoked rows and credentials minted by other people. IDs are hashes,
// never bearer secrets.
func (s *Store) AccountCLITokens(accountID int64) ([]CLIToken, error) {
- rows, err := s.db.Query(`SELECT token_hash, address, label, remote_addr, kind, account_id, app,
-role, name, created_at, expires_at, last_used, revoked_at, rotated_at, rungs, commands, scope
-FROM cli_tokens WHERE account_id = ? AND revoked_at = 0 ORDER BY created_at DESC, token_hash`, accountID)
+ rows, err := s.db.Query(accountCLITokensSQL, accountID)
if err != nil {
return nil, err
}
return scanCLITokenRows(rows)
}
+
+const accountCLITokensSQL = `SELECT token_hash, address, label, remote_addr, kind, account_id, app,
+role, name, created_at, expires_at, last_used, revoked_at, rotated_at, rungs, commands, scope
+FROM cli_tokens WHERE account_id = ? AND revoked_at = 0 ORDER BY created_at DESC, token_hash`
diff --git a/internal/console/member_demo_test.go b/internal/console/member_demo_test.go
index e933a4d14..3ec800400 100644
--- a/internal/console/member_demo_test.go
+++ b/internal/console/member_demo_test.go
@@ -166,3 +166,135 @@ func TestDemoHoldingAccountCLITokensMatchAdmission(t *testing.T) {
t.Fatalf("tokens=%v err=%v", tokens, err)
}
}
+
+func TestDemoHoldingOpsAccountRefused(t *testing.T) {
+ for _, entry := range []string{"public method", "transaction callback"} {
+ t.Run(entry, func(t *testing.T) {
+ st, _ := demoTestStore(t)
+ now := time.Now()
+ if err := st.CreateAccount(OpsAccountID, "ops", "Operators", "owner@keymail.dev", now); err != nil {
+ t.Fatal(err)
+ }
+ cfg := demoTestConfig(t)
+ cfg.Account = "ops"
+ path := filepath.Join(t.TempDir(), "registry.db")
+ reg, err := registry.Open(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.UpsertFleet(registry.Fleet{Name: cfg.Fleet, Account: cfg.Account, Bucket: "local"}); err != nil {
+ t.Fatal(err)
+ }
+ reg.Close()
+ want := "the ops account cannot be a demo holding account"
+ if entry == "public method" {
+ // An unreadable registry distinguishes the early check from the
+ // callback check. Removing the early check must fail this case.
+ err = st.RemoveDemoHoldingMember(OpsAccountID, "owner@keymail.dev", cfg, filepath.Join(path, "missing.db"), nil, now)
+ } else {
+ if _, err := st.db.Exec(`ATTACH DATABASE ? AS demo_holding_registry`, path); err != nil {
+ t.Fatal(err)
+ }
+ _, err = st.MembershipTransition(OpsAccountID, nil, nil, removeDemoHoldingMemberWrite(OpsAccountID, "owner@keymail.dev", cfg), now)
+ want = "not the configured demo holding account"
+ }
+ if err == nil || !strings.Contains(err.Error(), want) {
+ t.Fatalf("err=%v want %q", err, want)
+ }
+ if role, err := st.Role(OpsAccountID, "owner@keymail.dev"); err != nil || role != RoleOwner {
+ t.Fatalf("role=%q err=%v", role, err)
+ }
+ })
+ }
+}
+
+func TestDemoHoldingLastOwnerTokens(t *testing.T) {
+ for _, tc := range []struct {
+ name, kind string
+ account int64
+ expired bool
+ revoked bool
+ wantRefuse bool
+ }{
+ {name: "app token", kind: "app", account: 42, wantRefuse: true},
+ {name: "service token", kind: "service", account: 42, wantRefuse: true},
+ {name: "expired token", kind: "service", account: 42, expired: true, wantRefuse: true},
+ {name: "unknown kind", kind: "future-kind", account: 42, wantRefuse: true},
+ {name: "revoked token", kind: "service", account: 42, revoked: true},
+ {name: "another account", kind: "service", account: 43},
+ {name: "personal token", kind: "terminal", account: 0},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ st, _ := demoTestStore(t)
+ now := time.Now()
+ if err := st.AddMember(42, "owner@keymail.dev", RoleOwner, now); err != nil {
+ t.Fatal(err)
+ }
+ cfg := demoTestConfig(t)
+ path := filepath.Join(t.TempDir(), "registry.db")
+ reg, err := registry.Open(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.UpsertFleet(registry.Fleet{Name: cfg.Fleet, Account: cfg.Account, Bucket: "local"}); err != nil {
+ t.Fatal(err)
+ }
+ reg.Close()
+ expires, revoked := now.Add(time.Hour).Unix(), int64(0)
+ if tc.expired {
+ expires = now.Add(-time.Hour).Unix()
+ }
+ if tc.revoked {
+ revoked = now.Unix()
+ }
+ // Construct the write before minting credentials to catch a check
+ // that moves outside the membership transaction.
+ write := removeDemoHoldingMemberWrite(42, "owner@keymail.dev", cfg)
+ 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)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+ if _, err := st.db.Exec(`ATTACH DATABASE ? AS demo_holding_registry`, path); err != nil {
+ t.Fatal(err)
+ }
+ _, err = st.MembershipTransition(42, nil, nil, write, now)
+ if (err != nil) != tc.wantRefuse {
+ t.Fatalf("err=%v want refusal=%v", err, tc.wantRefuse)
+ }
+ if tc.wantRefuse {
+ for _, want := range []string{"first-token-id", "second-token-id", "deploy label", "deploy name", "another@keymail.dev", "revoke them first through the account's service pages"} {
+ if !strings.Contains(err.Error(), want) {
+ t.Errorf("refusal missing %q: %v", want, err)
+ }
+ }
+ }
+ role, err := st.Role(42, "owner@keymail.dev")
+ if err != nil || (role == RoleOwner) != tc.wantRefuse {
+ t.Fatalf("owner role=%q err=%v", role, err)
+ }
+ var count int
+ if err := st.db.QueryRow(`SELECT COUNT(*) FROM cli_tokens WHERE revoked_at = ?`, revoked).Scan(&count); err != nil || count != 2 {
+ t.Fatalf("tokens changed: count=%d err=%v", count, err)
+ }
+ if _, err := st.db.Exec(`DETACH DATABASE demo_holding_registry`); err != nil {
+ t.Fatal(err)
+ }
+ if tc.wantRefuse {
+ for _, id := range []string{"first-token-id", "second-token-id"} {
+ if err := st.RevokeAccountToken(42, id, now); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if err := st.RemoveDemoHoldingMember(42, "owner@keymail.dev", cfg, path, nil, now); err != nil {
+ t.Fatalf("remove after revocation: %v", err)
+ }
+ if role, err := st.Role(42, "owner@keymail.dev"); err != nil || role != "" {
+ t.Fatalf("owner remains after revocation: role=%q err=%v", role, err)
+ }
+ }
+ })
+ }
+}