Fleet secrets key: reserve every registered fingerprint to its fleet for good (branch review R1, I1)
A replaced or cleared key is still held by the fleet's boxes, so the registry now keeps fleet_secrets_key_fps: every fingerprint a fleet ever registered, reserved to that fleet, never deleted. SetFleetSecretsKey inserts the reservation in the same transaction as the fleets UPDATE and refuses with ErrFleetKeyInUse when another fleet holds it. migrateFleets creates the table and backfills the current keys idempotently. Witnesses in internal/registry (local and remote) and through the console door.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/fleetsecretskey.go+5 −4internal/console/fleetsecretskey_test.go+31 −0internal/registry/fleets.go+59 −9internal/registry/fleets_secrets_key_test.go+77 −0
Intent
<task-notification> <task-id>af667819e7ac72338</task-id> <tool-use-id>toolu_0165rSuMfNFYcXMaHDJYt4rx</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2d812478-ae68-44f2-8075-b2330abd5939/tasks/af667819e7ac72338.output</output-file> <status>completed</status> <summary>Agent "Fix two CI test failures" 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>Status: done. Both tests pass. Commit: `4df47cd8`, confirmed with `git log -1`. The `packed-refs.lock` hook error printed as expected and the tree is clean. Test output (sandbox off, `TMPDIR=/tmp/g`): `ok github.com/carlosframework/platform/internal/console 0.980s`. With `-v`, all 11 matched tests passed: - `TestEveryClosedDoorRefusesAServiceCredential` - `TestPassFailureLogsAlsoFailTheJob` - `TestPlanStorageSkipsKeylessFleet` and `TestPlanStorageReportsSealKeyFailures` - seven `TestRelayReconcile*` `gofmt` and `go vet ./internal/console/` are clean, and darwin and windows builds of `internal/console` pass. Item 1: I changed the expected count from 71 to 72 and added "the fleet secrets key door" after "six fleets" in the prose list. The test's logic is untouched. Item 2: the lint has no exemption list or marker, and it only flags `log.Printf` statements that pass a bare error-named identifier. I used the precedent in `apisystem.go:153`, where the quiet skip logs `keyErr.err`, a selector the lint does not flag. In `relayops.go` the skip now logs `noKey.err`. In `planstorage.go` it logs `err.Error()`, with a comment saying why. I added no `jobFailed` to the quiet-skip branches and left the lint unchanged. Concern: this works because of how the lint reads the code, not through a sanctioned marker, so someone could later "tidy" the log back to `err` and the lint would fail again. `planstorage.go` has a comment saying why. `relayops.go` has none, and I did not add one there.</result> <usage><subagent_tokens>53209</subagent_tokens><tool_uses>11</tool_uses><duration_ms>86724</duration_ms></usage> </task-notification>
<task-notification> <task-id>bg2uifer0</task-id> <tool-use-id>toolu_01Crcp1xjdKh9MDWDbkcmuXr</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/2d812478-ae68-44f2-8075-b2330abd5939/tasks/bg2uifer0.output</output-file> <status>completed</status> <summary>Background command "Run Astra whole-branch review round 1" completed (exit code 0)</summary> </task-notification>
diff --git a/internal/console/fleetsecretskey.go b/internal/console/fleetsecretskey.go| index ff129a201..ff72a8616 100644 |
| --- a/internal/console/fleetsecretskey.go |
| +++ b/internal/console/fleetsecretskey.go |
| @@ -7,10 +7,11 @@ package console |
| // |
| // Three facts hold the security of it. Only an account OWNER may set the key |
| // (apiOwnerAccountFor), because whoever sets it decides who can read the |
| -// fleet's future secrets. No two fleets share a key: the registry's unique |
| -// index on the fingerprint is the authority (ErrFleetKeyInUse), because a |
| -// fleet's boxes can read the whole account's app prefix and v2 binding does |
| -// not stop a holder of the private key (spec A3). And the fingerprint is |
| +// fleet's future secrets. No two fleets share a key, now or ever: the |
| +// registry's reservation of every fingerprint to the fleet that first |
| +// registered it is the authority (ErrFleetKeyInUse), because a fleet's boxes |
| +// can read the whole account's app prefix, keep a replaced key's private |
| +// half, and v2 binding does not stop a holder of it (spec A3). And the fingerprint is |
| // always the console's own, computed from the parsed key, never taken from |
| // the request. |
| // |
diff --git a/internal/console/fleetsecretskey_test.go b/internal/console/fleetsecretskey_test.go| index d4f90ee43..847ac0912 100644 |
| --- a/internal/console/fleetsecretskey_test.go |
| +++ b/internal/console/fleetsecretskey_test.go |
| @@ -450,6 +450,37 @@ func TestFleetSecretsKeyFailedReplacementKeepsTheOldFingerprintListed(t *testing |
| } |
| } |
| +// TestFleetSecretsKeyReplacedKeyStaysReserved: spec §2.1 I1 through the door. |
| +// home's boxes still hold the private half of a key home replaced, so |
| +// another account's fleet registering that key would seal its values to |
| +// boxes it does not own: home registers K, replaces it with K2, and bob's |
| +// fleet away is refused K with the in-use sentence and keeps no key. home |
| +// may take K back. |
| +func TestFleetSecretsKeyReplacedKeyStaysReserved(t *testing.T) { |
| + fx, token, _ := fskFixture(t, "code-fsk-reserved") |
| + bob := fskAway(t, fx) |
| + pubK, fpK := fskKey(t) |
| + pubK2, fpK2 := fskKey(t) |
| + for _, pub := range []string{pubK, pubK2} { |
| + if status, body := putJSON(t, fskURL(fx, fx.sqid, "home"), map[string]string{"pubkey": pub}, token); status != http.StatusOK || body["changed"] != true { |
| + t.Fatalf("home registers = %d %v", status, body) |
| + } |
| + } |
| + if row := fskFleetRow(t, fx, "home"); row.SecretsKeyFP != fpK2 { |
| + t.Fatalf("home = %+v, want K2", row) |
| + } |
| + status, body := putJSON(t, fskURL(fx, "cdef", "away"), map[string]string{"pubkey": pubK}, bob) |
| + if status != http.StatusConflict || body["error"] != "another fleet already uses this secrets key, and every fleet needs its own. mint a new pair with carlos secrets genkey and register its public half" { |
| + t.Fatalf("away registers home's replaced key = %d %v, want 409 with the in-use sentence", status, body) |
| + } |
| + if row := fskFleetRow(t, fx, "away"); row.SecretsKeyFP != "" || row.SecretsPub != "" { |
| + t.Fatalf("away took home's replaced key: %+v", row) |
| + } |
| + if status, body := putJSON(t, fskURL(fx, fx.sqid, "home"), map[string]string{"pubkey": pubK}, token); status != http.StatusOK || body["fingerprint"] != fpK { |
| + t.Fatalf("home takes its own earlier key back = %d %v", status, body) |
| + } |
| +} |
| + |
| func TestFleetListShowsKeyFingerprint(t *testing.T) { |
| fx, token, _ := fskFixture(t, "code-fsk-list") |
| fskPlaceShop(t, fx, token, "home") |
diff --git a/internal/registry/fleets.go b/internal/registry/fleets.go| index 870678f50..6676d6e61 100644 |
| --- a/internal/registry/fleets.go |
| +++ b/internal/registry/fleets.go |
| @@ -27,8 +27,9 @@ import ( |
| // return could not do without brittle string matching. |
| var ErrFleetBoxActive = errors.New("registry: fleet already has an active box under that label") |
| -// ErrFleetKeyInUse is returned by SetFleetSecretsKey when another fleet already |
| -// holds a key with that fingerprint (the fleets_secrets_key_fp unique index). |
| +// ErrFleetKeyInUse is returned by SetFleetSecretsKey when another fleet holds, |
| +// or ever registered, a key with that fingerprint (the fleets_secrets_key_fp |
| +// unique index and the fleet_secrets_key_fps reservation table). |
| var ErrFleetKeyInUse = errors.New("registry: another fleet already holds that secrets key") |
| // Fleet is one account-owned collection of remote boxes. |
| @@ -155,7 +156,27 @@ CREATE TABLE IF NOT EXISTS fleet_boxes ( |
| } |
| } |
| // PARTIAL unique index: fleets with no key all carry '' and must not collide. |
| - _, err := r.sql.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS fleets_secrets_key_fp ON fleets (secrets_key_fp) WHERE secrets_key_fp != ''`) |
| + if _, err := r.sql.Exec(`CREATE UNIQUE INDEX IF NOT EXISTS fleets_secrets_key_fp ON fleets (secrets_key_fp) WHERE secrets_key_fp != ''`); err != nil { |
| + return err |
| + } |
| + // Every fingerprint a fleet ever registered, reserved to that fleet for |
| + // good (spec §2.1 I1): a replaced or cleared key is still held by the |
| + // fleet's boxes, so no other fleet may take it. Nothing deletes a row. |
| + // The backfill reserves the keys fleets already hold when the table first |
| + // appears; it is idempotent, so every later open repeats it harmlessly. |
| + if _, err := r.sql.Exec(` |
| +CREATE TABLE IF NOT EXISTS fleet_secrets_key_fps ( |
| + fp TEXT PRIMARY KEY, |
| + fleet TEXT NOT NULL, |
| + account TEXT NOT NULL, |
| + at INTEGER NOT NULL |
| +);`); err != nil { |
| + return err |
| + } |
| + _, err := r.sql.Exec(` |
| +INSERT INTO fleet_secrets_key_fps (fp, fleet, account, at) |
| +SELECT secrets_key_fp, name, account, secrets_key_set_at FROM fleets WHERE secrets_key_fp != '' |
| +ON CONFLICT(fp) DO NOTHING`) |
| return err |
| } |
| @@ -285,10 +306,17 @@ func (r *Registry) ListFleets() ([]Fleet, error) { |
| // SetFleetSecretsKey records fleet name's secrets key: the public half, its |
| // fingerprint, who set it and when. Re-setting the same fingerprint on the same |
| -// fleet succeeds. A fingerprint another fleet already holds returns |
| -// ErrFleetKeyInUse and changes nothing. A missing fleet is an error. The |
| -// key and its fingerprint travel together: one without the other is refused |
| -// before anything is written (both empty clears the key). |
| +// fleet succeeds. A fingerprint another fleet holds now, or ever registered |
| +// (the fleet_secrets_key_fps reservation, spec §2.1 I1: a replaced key's |
| +// private half is still on that fleet's boxes), returns ErrFleetKeyInUse and |
| +// changes nothing; a fleet may take back a key it registered before. A |
| +// missing fleet is an error. The key and its fingerprint travel together: one |
| +// without the other is refused before anything is written (both empty clears |
| +// the key, and keeps its reservation). |
| +// |
| +// The reservation and the fleets row commit in one transaction. The |
| +// reservation insert comes first, so the transaction holds the write lock |
| +// before it reads who the fingerprint is reserved to. |
| func (r *Registry) SetFleetSecretsKey(name, pub, fp, by string, at int64) error { |
| if (pub == "") != (fp == "") { |
| return fmt.Errorf("fleet %q: a secrets key and its fingerprint are set together", name) |
| @@ -301,7 +329,29 @@ func (r *Registry) SetFleetSecretsKey(name, pub, fp, by string, at int64) error |
| if !ValidTunnelName(name) { |
| return fmt.Errorf("invalid fleet name %q", name) |
| } |
| - res, err := r.sql.Exec(` |
| + tx, err := r.sql.Begin() |
| + if err != nil { |
| + return err |
| + } |
| + defer tx.Rollback() |
| + if fp != "" { |
| + if _, err := tx.Exec(` |
| +INSERT INTO fleet_secrets_key_fps (fp, fleet, account, at) |
| +SELECT ?, name, account, ? FROM fleets WHERE name = ? |
| +ON CONFLICT(fp) DO NOTHING`, fp, at, name); err != nil { |
| + return err |
| + } |
| + var holder string |
| + err := tx.QueryRow(`SELECT fleet FROM fleet_secrets_key_fps WHERE fp = ?`, fp).Scan(&holder) |
| + if err != nil && err != sql.ErrNoRows { |
| + return err |
| + } |
| + // No row: no such fleet, which the UPDATE below reports. |
| + if err == nil && holder != name { |
| + return ErrFleetKeyInUse |
| + } |
| + } |
| + res, err := tx.Exec(` |
| UPDATE fleets SET secrets_pubkey = ?, secrets_key_fp = ?, secrets_key_set_at = ?, secrets_key_set_by = ?, rev = rev + 1 |
| WHERE name = ?`, pub, fp, at, by, name) |
| if err != nil { |
| @@ -318,7 +368,7 @@ WHERE name = ?`, pub, fp, at, by, name) |
| if n == 0 { |
| return fmt.Errorf("no fleet %q", name) |
| } |
| - return nil |
| + return tx.Commit() |
| } |
| // sqliteConstraintUnique is SQLITE_CONSTRAINT_UNIQUE, the extended result code |
diff --git a/internal/registry/fleets_secrets_key_test.go b/internal/registry/fleets_secrets_key_test.go| index f20541ef0..b1bfaad4f 100644 |
| --- a/internal/registry/fleets_secrets_key_test.go |
| +++ b/internal/registry/fleets_secrets_key_test.go |
| @@ -128,3 +128,80 @@ func TestFleetSecretsKeyPubAndFPTravelTogether(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// TestFleetSecretsKeyReplacedKeyStaysReserved: spec §2.1 I1. A fleet's boxes |
| +// keep the private half of every key the fleet ever registered, so replacing |
| +// a key must not release its fingerprint to another fleet: fleet home of bab |
| +// registers K, replaces it with K2 (and clears it), and fleet away of another |
| +// account registering K is still refused, locally and over the remote |
| +// transport. home may take its own earlier key back. |
| +func TestFleetSecretsKeyReplacedKeyStaysReserved(t *testing.T) { |
| + local := openTestRegistry(t) |
| + remote, _ := remoteTestRegistry(t) |
| + for name, reg := range map[string]*Registry{"local": local, "remote": remote} { |
| + for _, f := range []Fleet{ |
| + {Name: "home", Account: "bab", Bucket: "home-data"}, |
| + {Name: "away", Account: "zzz", Bucket: "away-data"}, |
| + } { |
| + if _, err := reg.CreateFleet(f); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := reg.SetFleetSecretsKey("home", secretsPubA, secretsFPA, "p", 1); err != nil { |
| + t.Fatalf("%s: home registers K: %v", name, err) |
| + } |
| + if err := reg.SetFleetSecretsKey("home", secretsPubA, secretsFPB, "p", 2); err != nil { |
| + t.Fatalf("%s: home replaces K with K2: %v", name, err) |
| + } |
| + if err := reg.SetFleetSecretsKey("away", secretsPubA, secretsFPA, "z", 3); !errors.Is(err, ErrFleetKeyInUse) { |
| + t.Fatalf("%s: away registers home's replaced key = %v, want ErrFleetKeyInUse", name, err) |
| + } |
| + if err := reg.SetFleetSecretsKey("home", "", "", "p", 4); err != nil { |
| + t.Fatalf("%s: home clears its key: %v", name, err) |
| + } |
| + if err := reg.SetFleetSecretsKey("away", secretsPubA, secretsFPB, "z", 5); !errors.Is(err, ErrFleetKeyInUse) { |
| + t.Fatalf("%s: away registers home's cleared key = %v, want ErrFleetKeyInUse", name, err) |
| + } |
| + if got, _ := reg.GetFleet("away"); got == nil || got.SecretsKeyFP != "" || got.SecretsPub != "" { |
| + t.Fatalf("%s: the refused fleet took a key: %+v", name, got) |
| + } |
| + // Its own earlier key is home's to take back. |
| + if err := reg.SetFleetSecretsKey("home", secretsPubA, secretsFPA, "p", 6); err != nil { |
| + t.Fatalf("%s: home re-registers its own earlier key: %v", name, err) |
| + } |
| + } |
| +} |
| + |
| +// TestFleetSecretsKeyMigrationReservesExistingKeys: a registry whose fleets |
| +// already hold keys when the reservation table first appears reserves them, |
| +// so the first replacement after the upgrade is covered too. The migration |
| +// runs again over the same file without error. |
| +func TestFleetSecretsKeyMigrationReservesExistingKeys(t *testing.T) { |
| + reg := openTestRegistry(t) |
| + for _, f := range []Fleet{ |
| + {Name: "home", Account: "bab", Bucket: "home-data"}, |
| + {Name: "away", Account: "zzz", Bucket: "away-data"}, |
| + } { |
| + if _, err := reg.CreateFleet(f); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if err := reg.SetFleetSecretsKey("home", secretsPubA, secretsFPA, "p", 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + // The registry as it was before the reservation table existed. |
| + if _, err := reg.sql.Exec(`DROP TABLE fleet_secrets_key_fps`); err != nil { |
| + t.Fatal(err) |
| + } |
| + for i := 0; i < 2; i++ { |
| + if err := reg.migrateFleets(); err != nil { |
| + t.Fatalf("migrateFleets pass %d: %v", i+1, err) |
| + } |
| + } |
| + if err := reg.SetFleetSecretsKey("home", secretsPubA, secretsFPB, "p", 2); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := reg.SetFleetSecretsKey("away", secretsPubA, secretsFPA, "z", 3); !errors.Is(err, ErrFleetKeyInUse) { |
| + t.Fatalf("away registers home's pre-upgrade key after its replacement = %v, want ErrFleetKeyInUse", err) |
| + } |
| +} |