fix(admin): create-admin-wallet fully bootstraps a fresh instance (latch + compose)

create-admin-wallet registered a pending admin key but never latched setup, so on
a fresh instance it left the bootstrap half-open and a later `setup` collided with
it (setup's guard only checked for an ACTIVE admin; the cli key was pending).

- New atomic Database::create_admin_key_bootstrapping: under the admin-keys
  advisory lock, insert the pending generated key AND latch `locked` (unless
  already validly locked), append audit, commit — one transaction. Fails CLOSED on
  a tampered latch (defers to reset-setup) instead of silently healing it.
- bootstrap_admin (setup) now refuses on ANY live admin key (active OR pending),
  not just active, so setup composes with create-admin-wallet. The adopt-existing-
  deployment path (users but no admin keys) is unaffected: no live key -> proceeds.
- reset_setup now takes the same advisory lock (all latch writers serialize) and
  documents the live-key recovery path.
- CLI prints bootstrapped-vs-added and a terminating lost-secret recovery hint.
- Docs (README + OPERATOR_SETUP section 5): both mutually-exclusive bootstrap paths.

Adversarially reviewed (atomicity / lockout / regression — all ship). L1 test
extended: fresh-latch, no-relatch-when-locked, pending semantics, composition
guard, and tamper-refuse (fails closed + rolls back the key insert).
This commit is contained in:
jwinterm
2026-07-02 03:19:32 -04:00
parent a919ea7b52
commit d3dd3fc113
6 changed files with 316 additions and 26 deletions
+24 -9
View File
@@ -12,7 +12,10 @@
//! revoke-key --id <uuid> (may revoke the LAST key)
//! replace-key --old <uuid> --pubkey <64hex> [--revoke-all]
//! create-admin-wallet --out <path> (generates a key; writes the
//! secret 0600; registers the pubkey)
//! secret 0600; registers the pubkey
//! as pending; latches the bootstrap
//! on a fresh instance — a complete
//! first-run, no `setup` needed)
//! doctor
//!
//! Deferred (documented): remac-keys (integrity-secret rotation must also
@@ -43,7 +46,7 @@ COMMANDS:
add-key --pubkey <64hex> [--label <s>]
revoke-key --id <uuid>
replace-key --old <uuid> --pubkey <64hex> [--revoke-all]
create-admin-wallet --out <path>
create-admin-wallet --out <path> (generate a key + bootstrap a fresh instance)
mint-invite [--count <n>] [--label <s>] (registration invite codes)
doctor
";
@@ -343,9 +346,11 @@ async fn create_admin_wallet(db: &Database, secret: &str, args: &[String]) -> Re
secret_hex.zeroize();
write_res.map_err(|e| format!("write secret to {out}: {e}"))?;
// Register the PUBLIC key only.
match db
.create_admin_key_audited(
// Register the PUBLIC key only (pending), and latch the bootstrap if this is a
// fresh instance — so `create-admin-wallet` alone fully bootstraps, atomically,
// rather than leaving setup half-open for a later `setup` to collide with.
let latched = match db
.create_admin_key_bootstrapping(
NewAdminKey {
pubkey: pubkey.clone(),
label: Some("cli-generated".into()),
@@ -360,13 +365,23 @@ async fn create_admin_wallet(db: &Database, secret: &str, args: &[String]) -> Re
.await
.map_err(|e| e.to_string())?
{
AddKeyOutcome::Created(_) => {}
AddKeyOutcome::CapReached => return Err("unexpected: cap reached".into()),
}
(AddKeyOutcome::Created(_), latched) => latched,
(AddKeyOutcome::CapReached, _) => return Err("unexpected: cap reached".into()),
};
println!("generated admin key; secret written to {out} (mode 0600)");
println!("pubkey: {pubkey}");
println!("import the secret into your NIP-98 signer; it activates on first login");
if latched {
println!("fresh instance bootstrapped: setup latched (locked) — no `setup` needed");
println!("import the secret into your NIP-98 signer and log in to activate.");
println!(
"if you lose this secret before that first login, recover by re-running \
`create-admin-wallet` (adds a fresh key), then `revoke-key` the stale one"
);
} else {
println!("instance already bootstrapped: added a pending admin key");
println!("import the secret into your NIP-98 signer; it activates on first login");
}
Ok(())
}