From c09ef6c35839d560650df318b84648a45b5e035c Mon Sep 17 00:00:00 2001 From: atlas Date: Fri, 27 Nov 2026 05:25:02 +0100 Subject: [PATCH] swarmctl, vm tests: wait for the store's active node after an unseal An unsealed store has no active node for a few seconds and refuses writes with "local node not active". swarmctl bao grant-bootstrap now repeats bao policy list and bao policy write every second, up to a minute, while the store answers that; setup.md says so. The full-setup and semi-spread tests wait for bao policy list with the root token before the bootstrap. full-setup.py: when create_agent gives up it also prints the controller pod's logs, the pod list and the swarm builder's journal. A run that failed with no archetype "reviewer" for 13 minutes left no controller or builder output to read. --- docs/1-getting-started/setup.md | 2 +- nix/vm-tests/full-setup.py | 13 +++++++ nix/vm-tests/semi-spread.nix | 3 ++ swarmctl/src/bao.rs | 66 ++++++++++++++++++++++++++++++--- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/docs/1-getting-started/setup.md b/docs/1-getting-started/setup.md index 37cf37e0e..87bbc286b 100644 --- a/docs/1-getting-started/setup.md +++ b/docs/1-getting-started/setup.md @@ -125,7 +125,7 @@ swarmctl bao grant-bootstrap # prompts for the root token, input hidden ``` It writes the `bao-bootstrap` policy, places a 24h grant token for -`swarm-bao-granter-role` and restarts that unit. The token comes from the +`swarm-bao-granter-role` and restarts that unit. Right after an unseal the store has no active node for a few seconds and refuses writes with `local node not active`; the verb asks again every second, for up to a minute, before it reports that. The token comes from the hidden prompt, or from stdin when stdin isn't a terminal; never from an argument or environment variable. If the granter exists already, it runs the same steps again: this is also how a change to the granter's own diff --git a/nix/vm-tests/full-setup.py b/nix/vm-tests/full-setup.py index 656094265..56ea148e8 100644 --- a/nix/vm-tests/full-setup.py +++ b/nix/vm-tests/full-setup.py @@ -87,6 +87,14 @@ def create_agent(body): return machine.sleep(10) diagnose("swarm-controller") + # The controller runs as a pod and the builder answers its archetype question: their own + # logs say why the list is empty. + for command in [ + "kubectl -n hyperhive logs deploy/swarm-controller --tail=80", + "kubectl -n hyperhive get pods -o wide", + "journalctl --machine=swarm-builder --no-pager -n 60 | tail -c 6000", + ]: + print(machine.execute(command)[1]) # The queue refuses every client when its responder cannot answer. nats and # the responder run in the container; the units that fetch its keys and # certificate run on the host. @@ -142,6 +150,11 @@ def unseal(): with subtest("step 4: bao operator unseal, with the shamir seal, from the saved keys"): unseal() machine.succeed("bao status") + # Unsealed is not active: the node takes a few seconds to elect itself, and a write before + # that is refused with "local node not active". + machine.wait_until_succeeds( + f'BAO_TOKEN="$(jq -r .root_token {INIT})" bao policy list', timeout=120 + ) with subtest("step 1: the granter bootstrap"): machine.succeed( diff --git a/nix/vm-tests/semi-spread.nix b/nix/vm-tests/semi-spread.nix index 93458915a..43ec028b1 100644 --- a/nix/vm-tests/semi-spread.nix +++ b/nix/vm-tests/semi-spread.nix @@ -464,6 +464,9 @@ let with subtest("step 4: bao operator unseal, with the shamir seal"): swarm.succeed(f"bao operator unseal \"$(jq -r '.unseal_keys_b64[0]' {INIT})\"") swarm.succeed("bao status") + # Unsealed is not active: the node takes a few seconds to elect itself, and a write before + # that is refused with "local node not active". + swarm.wait_until_succeeds(f"BAO_TOKEN=\"$(jq -r .root_token {INIT})\" bao policy list", timeout=120) with subtest("step 1: the granter bootstrap"): swarm.succeed( diff --git a/swarmctl/src/bao.rs b/swarmctl/src/bao.rs index 32d1f75e1..54b5d0ea6 100644 --- a/swarmctl/src/bao.rs +++ b/swarmctl/src/bao.rs @@ -37,7 +37,8 @@ const GRANTER_POLICY: &str = "bao-granter"; const GRANTER_UNIT: &str = "swarm-bao-granter-role"; /// How long `--and-grant` waits for a freshly initialised store to unseal -/// itself. A `pkcs11` seal unseals within seconds; a `shamir` seal never does. +/// itself, and how long a call waits for an unsealed store to elect its active +/// node. A `pkcs11` seal unseals within seconds; a `shamir` seal never does. const UNSEAL_WAIT: Duration = Duration::from_mins(1); #[derive(Args)] @@ -529,7 +530,11 @@ impl Granted { /// `bao-bootstrap` policy, mint the 24h grant token, place it for the granter /// unit and restart that unit. fn grant_bootstrap_with(tools: &Tools, paths: &GrantPaths, root: &str) -> Result { - let policies = run(tools.bao_cmd(&["policy", "list"]).env("BAO_TOKEN", root))?; + let policies = run_when_active(tools, || { + let mut cmd = tools.bao_cmd(&["policy", "list"]); + cmd.env("BAO_TOKEN", root); + cmd + })?; ensure(&policies, "bao policy list", &[root])?; // A granter that exists is not a reason to stop: the deployed policy text // may be newer than the one in the store, and the granter unit is what @@ -538,10 +543,11 @@ fn grant_bootstrap_with(tools: &Tools, paths: &GrantPaths, root: &str) -> Result .lines() .any(|line| line.trim() == GRANTER_POLICY); - let write = run(tools - .bao_cmd(&["policy", "write", BOOTSTRAP_POLICY]) - .arg(&paths.policy_file) - .env("BAO_TOKEN", root))?; + let write = run_when_active(tools, || { + let mut cmd = tools.bao_cmd(&["policy", "write", BOOTSTRAP_POLICY]); + cmd.arg(&paths.policy_file).env("BAO_TOKEN", root); + cmd + })?; ensure(&write, "bao policy write", &[root])?; let minted = run(tools @@ -643,6 +649,26 @@ fn run(cmd: &mut Command) -> Result { .with_context(|| format!("running {program}")) } +/// What `bao` prints while an unsealed store has no active node yet, which is +/// a few seconds after an unseal; `bao status` already exits 0 then. +const NOT_ACTIVE: &str = "not active"; + +/// [`run`] of the command `make` builds, again every second for up to +/// `tools.unseal_wait` while the store answers that it has no active node. +/// Any other outcome is returned as it is. +fn run_when_active(tools: &Tools, make: impl Fn() -> Command) -> Result { + let deadline = Instant::now() + tools.unseal_wait; + loop { + let out = run(&mut make())?; + let standby = + !out.status.success() && String::from_utf8_lossy(&out.stderr).contains(NOT_ACTIVE); + if !standby || Instant::now() >= deadline { + return Ok(out); + } + std::thread::sleep(Duration::from_secs(1)); + } +} + /// Fails with the child's stderr, secrets replaced, when it did not succeed. fn ensure(out: &Output, what: &str, secrets: &[&str]) -> Result<()> { if out.status.success() { @@ -737,6 +763,10 @@ case "$(basename "$0") $1 $2 $3" in cat "$D/init.json"; exit 0 ;; "bao status "*) exit "$(cat "$D/seal_status")" ;; "bao policy list"*) + if [ -s "$D/standby_left" ] && [ "$(cat "$D/standby_left")" -gt 0 ]; then + echo $(($(cat "$D/standby_left") - 1)) > "$D/standby_left" + echo "* local node not active but active cluster node not found" >&2; exit 2 + fi if [ -e "$D/list_fails" ]; then echo "permission denied for token $BAO_TOKEN" >&2; exit 2; fi cat "$D/policies"; exit 0 ;; "bao policy write"*) exit 0 ;; @@ -1292,6 +1322,30 @@ exit 9 assert!(format!("{err:#}").contains("seal status"), "{err:#}"); } + #[test] + fn a_store_without_an_active_node_is_asked_again_until_it_has_one() { + let mut f = Fixture::new("standby-then-active"); + f.tools.unseal_wait = Duration::from_secs(30); + f.set("standby_left", "2"); + grant_bootstrap_with(&f.tools, &f.paths, ROOT).expect("succeeds once the node is active"); + let lists = f.calls().iter().filter(|c| *c == "bao policy list").count(); + assert_eq!(lists, 3, "{:?}", f.calls()); + } + + #[test] + fn a_store_that_never_gets_an_active_node_fails_with_what_it_said() { + let f = Fixture::new("standby-forever"); + f.set("standby_left", "99"); + let err = grant_bootstrap_with(&f.tools, &f.paths, ROOT).expect_err("no active node"); + let text = format!("{err:#}"); + assert!(text.contains("local node not active"), "{text}"); + assert!( + !f.calls().iter().any(|c| c.starts_with("bao policy write")), + "{:?}", + f.calls() + ); + } + #[test] fn a_missing_bao_binary_names_the_binary() { let f = Fixture::new("no-bao");