grant-bootstrap re-applies the granter and restarts failed policy units; setup treats a refused granting unit as not done
This commit is contained in:
parent
ae37c228b1
commit
c7d37ca455
1 changed files with 271 additions and 33 deletions
|
|
@ -77,6 +77,10 @@ pub(crate) struct Granter {
|
|||
pub(crate) struct Unit {
|
||||
pub(crate) name: String,
|
||||
pub(crate) state: String,
|
||||
/// The unit failed and its journal's last lines show the store refusing
|
||||
/// it with a 403 or "permission denied": the granter's policy in the store
|
||||
/// is older than the deployed one.
|
||||
pub(crate) denied: bool,
|
||||
}
|
||||
|
||||
/// One row of `GET /api/agents/status`, the two fields read.
|
||||
|
|
@ -165,7 +169,7 @@ const STEPS: [(&str, &str, bool); 10] = [
|
|||
pub(crate) fn classify(observed: &Observed) -> Vec<Step> {
|
||||
let unsealed = unsealed_status(&observed.store);
|
||||
let policy_units = policy_units_status(&observed.policy_units);
|
||||
let granter = granter_status(&observed.granter, &policy_units);
|
||||
let granter = granter_status(&observed.granter, &observed.policy_units, &policy_units);
|
||||
let reunsealed = match (&unsealed, &policy_units) {
|
||||
(Status::Done(_), Status::Done(_)) => Status::Done(String::new()),
|
||||
(Status::Unknown(e), _) | (_, Status::Unknown(e)) => Status::Unknown(e.clone()),
|
||||
|
|
@ -254,7 +258,23 @@ fn policy_units_status(units: &Result<Vec<Unit>, String>) -> Status {
|
|||
/// the token is absent, so after a reboot it is `inactive` whether or not it
|
||||
/// ever ran. An active granting unit logs in through the granter, so that
|
||||
/// also proves the granter exists.
|
||||
fn granter_status(granter: &Result<Granter, String>, policy_units: &Status) -> Status {
|
||||
fn granter_status(
|
||||
granter: &Result<Granter, String>,
|
||||
units: &Result<Vec<Unit>, String>,
|
||||
policy_units: &Status,
|
||||
) -> Status {
|
||||
// A granting unit the store refuses means the granter exists but is
|
||||
// behind: its upload in the journal and an active unit of its own say
|
||||
// nothing about the policy the store holds now.
|
||||
if let Ok(units) = units
|
||||
&& let Some(unit) = units.iter().find(|u| u.denied)
|
||||
{
|
||||
return Status::NotDone(format!(
|
||||
"{} was refused by the store (403): its granter policy is older than the deployed \
|
||||
one; `swarmctl bao grant-bootstrap` applies it",
|
||||
unit.name
|
||||
));
|
||||
}
|
||||
if matches!(policy_units, Status::Done(_)) {
|
||||
return Status::Done("the granting units log in through it".to_owned());
|
||||
}
|
||||
|
|
@ -655,13 +675,36 @@ fn policy_units() -> Result<Vec<Unit>, String> {
|
|||
Ok(names
|
||||
.into_iter()
|
||||
.zip(states)
|
||||
.map(|(name, state)| Unit {
|
||||
name,
|
||||
state: state.to_owned(),
|
||||
.map(|(name, state)| {
|
||||
let denied = state == "failed" && last_error_is_denied(&name);
|
||||
Unit {
|
||||
name,
|
||||
state: state.to_owned(),
|
||||
denied,
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// How many of a failed unit's last journal lines are read for its error.
|
||||
const JOURNAL_TAIL: &str = "30";
|
||||
|
||||
/// Whether the unit's last journal lines show the store refusing it. An
|
||||
/// unreadable journal counts as no: the unit's state decides.
|
||||
fn last_error_is_denied(unit: &str) -> bool {
|
||||
run(Command::new("journalctl").args(["-u", unit, "-n", JOURNAL_TAIL, "--no-pager", "-q"]))
|
||||
.is_ok_and(|j| journal_shows_denial(&j.stdout))
|
||||
}
|
||||
|
||||
/// A 403 or "permission denied" in `journal`, as the `bao` CLI prints a refusal
|
||||
/// (`Code: 403` and `permission denied`).
|
||||
fn journal_shows_denial(journal: &str) -> bool {
|
||||
journal.lines().any(|line| {
|
||||
let line = line.to_ascii_lowercase();
|
||||
line.contains("permission denied") || line.contains("code: 403")
|
||||
})
|
||||
}
|
||||
|
||||
/// The unit names in `systemctl list-unit-files --no-legend`, without the one
|
||||
/// that waits on authelia.
|
||||
fn parse_unit_files(listed: &str) -> Vec<String> {
|
||||
|
|
@ -742,6 +785,7 @@ mod tests {
|
|||
.map(|(i, s)| Unit {
|
||||
name: format!("swarm-bao-u{i}-policy.service"),
|
||||
state: (*s).to_owned(),
|
||||
denied: false,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
|
@ -882,6 +926,45 @@ mod tests {
|
|||
assert_eq!(kind(&status_of(&o, 3)), "done");
|
||||
}
|
||||
|
||||
/// A granting unit the store refuses with a 403 means the granter exists but
|
||||
/// its policy in the store is behind the deployed one: an upload in its
|
||||
/// journal and an active granter unit don't make step 3 done.
|
||||
#[test]
|
||||
fn a_granting_unit_refused_with_a_403_means_the_granter_is_not_done() {
|
||||
let mut o = finished();
|
||||
o.granter = Ok(Granter {
|
||||
state: "active".to_owned(),
|
||||
uploaded: true,
|
||||
});
|
||||
o.policy_units = Ok(units(&["active", "failed"]));
|
||||
// The control: a failed unit that is not a refusal leaves the granter done.
|
||||
assert_eq!(kind(&status_of(&o, 3)), "done");
|
||||
if let Ok(units) = &mut o.policy_units {
|
||||
units[1].denied = true;
|
||||
}
|
||||
assert!(matches!(
|
||||
status_of(&o, 3),
|
||||
Status::NotDone(why) if why.contains("u1-policy") && why.contains("swarmctl bao grant-bootstrap")
|
||||
));
|
||||
}
|
||||
|
||||
#[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\
|
||||
URL: POST https://127.0.0.1:8200/v1/sys/mounts/ssh-agents\n\
|
||||
Code: 403. Errors:\n\n* 1 error occurred:\n\t* permission denied\n";
|
||||
assert!(journal_shows_denial(refused));
|
||||
assert!(journal_shows_denial("something\nCode: 403. Errors:"));
|
||||
// The controls: other failures, and nothing at all.
|
||||
assert!(!journal_shows_denial(
|
||||
"dial tcp 127.0.0.1:8200: connection refused\n"
|
||||
));
|
||||
assert!(!journal_shows_denial(
|
||||
"the store is sealed or unreachable; retrying.\n"
|
||||
));
|
||||
assert!(!journal_shows_denial(""));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_inactive_policy_unit_fails_step_four_and_names_it() {
|
||||
let mut o = finished();
|
||||
|
|
|
|||
Loading…
Reference in a new issue