Watch
0
0
Fork
You've already forked hyperhive
0

swarmctl setup status: show the pending bootstrap re-run for the pods' login

`swarm-bao-k8s-jwt-policy` exits 0 when the bootstrap token lacks
`sys/auth/jwt-k8s`, so a store set up before the node looked fine in
`swarmctl setup status`: the unit is active and nothing marks the root-only
`swarmctl bao grant-bootstrap` re-run as pending.

The unit now keeps /run/swarm-bao-k8s-jwt-pending while it stays off and
removes it before it writes the mount's config. `setup status` reads the file
and shows step 3, "granter bootstrapped", as not done, naming the verb.
`module-eval-k8s` pins the order of the touch, the exit and the removal.
This commit is contained in:
maia 2026-11-25 14:56:39 +01:00 • committed by mara
commit 6acf1012cb
6 changed files with 80 additions and 0 deletions

View file

@ -162,6 +162,11 @@ The grant adds `sys/auth/jwt-k8s` to the bootstrap policy, and the restart lets
`swarm-bao-granter-role` enable the mount and `swarm-bao-k8s-jwt-policy` write
the pods' login. Until both have run, the pods can't log in.
Until the grant runs, `swarmctl setup status` shows step 3, "granter
bootstrapped", as not done. `swarm-bao-k8s-jwt-policy` exits 0 and keeps
`/run/swarm-bao-k8s-jwt-pending` while the bootstrap token lacks the grant, and
the status reads that file.
## Validation
`module-eval-k8s` compares an all-local host with the option on against the

View file

@ -15,4 +15,9 @@
tokenDir = "/var/run/secrets/hyperhive/bao";
tokenFile = "/var/run/secrets/hyperhive/bao/token";
# Exists while `swarm-bao-k8s-jwt-policy` waits for the root-only re-run of
# `swarmctl bao grant-bootstrap`. `PENDING_MARKER` in swarmctl/src/setup.rs
# is the same path.
pendingMarker = "/run/swarm-bao-k8s-jwt-pending";
}

View file

@ -526,6 +526,9 @@ in
case "$caps" in
*update*) ;;
*)
# `swarmctl setup status` reads this file: a unit that stays off
# exits 0, so it looks active.
touch ${podAuth.pendingMarker}
echo "the granter may not configure auth/${podAuth.mount} yet (capabilities: $caps), so the pods' login stays off." >&2
echo "one-time step, as root on this host (docs/1-getting-started/setup.md):" >&2
${lib.concatMapStringsSep "\n" (l: "echo ${lib.escapeShellArg " ${l}"} >&2") granterSetupSteps}
@ -533,6 +536,7 @@ in
exit 0
;;
esac
rm -f ${podAuth.pendingMarker}
# One public key per private key in the files, which may each hold
# several. A file k3s has not written is skipped, and no key at all

View file

@ -1402,6 +1402,28 @@ let
&& lib.hasInfix "bound_subject=system:serviceaccount:hyperhive:swarm-review-classifier" jwt
&& lib.hasInfix "token_policies=swarm-claude-login " login;
}
{
# `swarmctl setup status` reads this file to tell that the root-only
# grant-bootstrap re-run is pending (`PENDING_MARKER` in
# swarmctl/src/setup.rs). The unit must create it on the stay-off path and
# remove it before it writes the mount's config, or the check goes quiet
# or never clears.
name = "the pods' login unit keeps the marker file `swarmctl setup status` reads while it waits for the root step";
ok =
let
jwt = withNode.systemd.services.swarm-bao-k8s-jwt-policy.script;
marker = "/run/swarm-bao-k8s-jwt-pending";
touched = lib.splitString "touch ${marker}" jwt;
cleared = lib.splitString "rm -f ${marker}" jwt;
# What follows the one `touch`, up to and after the one `rm`.
between = lib.head (lib.splitString "rm -f ${marker}" (lib.elemAt touched 1));
in
lib.length touched == 2
&& lib.length cleared == 2
&& lib.hasInfix "rm -f ${marker}" (lib.elemAt touched 1)
&& lib.hasInfix "exit 0" between
&& lib.hasInfix "bao write auth/jwt-k8s/config" (lib.elemAt cleared 1);
}
{
name = "the four daemons' and the poller's runtime \"k8s\" needs the node";
ok =

View file

@ -849,6 +849,7 @@ mod tests {
}
.to_owned(),
denied: false,
waits_for_root: false,
}]),
admin_user: Ok(self.user),
agents: Ok(Agents {

View file

@ -32,6 +32,13 @@ const GRANTER_MARKER: &str = "Uploaded policy: bao-granter";
/// Waits for authelia, which waits for the policies written in step 4, so it
/// is not a signal for step 4 itself.
const POLICY_UNIT_SKIPPED: &str = "operator-viewer";
/// The unit that logs the cluster's pods in to the store. It exits 0 and does
/// nothing while the bootstrap token lacks `sys/auth/jwt-k8s`.
const K8S_JWT_UNIT: &str = "swarm-bao-k8s-jwt-policy.service";
/// The file [`K8S_JWT_UNIT`] keeps while it stays off, and removes once it has
/// configured the mount. `pendingMarker` in `lib/bao-pod-auth.nix` is the same
/// path, and the `module-eval-k8s` check pins the unit's use of it.
const PENDING_MARKER: &str = "/run/swarm-bao-k8s-jwt-pending";
/// What one step's check found.
#[derive(Debug, PartialEq, Eq)]
@ -81,6 +88,9 @@ pub(crate) struct Unit {
/// it with a 403 or "permission denied": the granter's policy in the store
/// is older than the deployed one.
pub(crate) denied: bool,
/// The unit exited 0 without doing its work because the granter's token
/// lacks a grant only root can add: [`PENDING_MARKER`] exists.
pub(crate) waits_for_root: bool,
}
/// One row of `GET /api/agents/status`, the two fields read.
@ -275,6 +285,17 @@ fn granter_status(
unit.name
));
}
// A unit that stays off exits 0, so it counts as active: only its journal
// shows that the bootstrap policy in the store lacks a grant the unit needs.
if let Ok(units) = units
&& let Some(unit) = units.iter().find(|u| u.waits_for_root)
{
return Status::NotDone(format!(
"{} is waiting: the bootstrap policy in the store is older than the deployed one; \
`swarmctl bao grant-bootstrap` with the root token applies it",
unit.name
));
}
if matches!(policy_units, Status::Done(_)) {
return Status::Done("the granting units log in through it".to_owned());
}
@ -677,10 +698,12 @@ fn policy_units() -> Result<Vec<Unit>, String> {
.zip(states)
.map(|(name, state)| {
let denied = state == "failed" && last_error_is_denied(&name);
let waits_for_root = name == K8S_JWT_UNIT && Path::new(PENDING_MARKER).exists();
Unit {
name,
state: state.to_owned(),
denied,
waits_for_root,
}
})
.collect())
@ -786,6 +809,7 @@ mod tests {
name: format!("swarm-bao-u{i}-policy.service"),
state: (*s).to_owned(),
denied: false,
waits_for_root: false,
})
.collect()
}
@ -948,6 +972,25 @@ mod tests {
));
}
/// The k8s jwt unit exits 0 while the bootstrap token lacks its grant, so
/// `is-active` calls it active and only its marker file shows the pending
/// root step: step 3 names the verb. Without the marker (the control above
/// it) step 3 is done.
#[test]
fn a_k8s_jwt_unit_that_stayed_off_means_the_bootstrap_is_not_done() {
let mut o = finished();
assert_eq!(kind(&status_of(&o, 3)), "done");
if let Ok(units) = &mut o.policy_units {
units[1].waits_for_root = true;
}
assert!(matches!(
status_of(&o, 3),
Status::NotDone(why) if why.contains("u1-policy") && why.contains("swarmctl bao grant-bootstrap")
));
// Step 4 stays done: the unit is active, the root step is step 3's.
assert_eq!(kind(&status_of(&o, 4)), "done");
}
#[test]
fn a_refusal_is_read_from_the_journal_as_the_bao_cli_prints_it() {
let refused = "Error enabling: Error making API request.\n\n\