use openmoat_hosts::{FailureOutcome, HookFailure, Host, Unanswered};
use serde::Serialize;
pub const FIX: &str = "start it with `openmoat run`, whose OS sandbox holds when the hook \
does not (docs/SANDBOX.md), and add `require.run: full-boundary` to the \
policy so `openmoat run` refuses to start without it";
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Failure {
pub fails: &'static str,
pub cannot_start: &'static str,
pub times_out: &'static str,
pub timeout_s: Option<u32>,
pub deadline_s: u64,
pub unanswered: Option<&'static str>,
pub turned_off_by: &'static str,
pub fails_open: Vec<&'static str>,
pub sources: &'static [&'static str],
}
impl Failure {
pub fn line(&self) -> Option<String> {
(!self.fails_open.is_empty()).then(|| {
format!(
"fails open: runs the call when the hook {}; {FIX}",
self.fails_open.join(" or ")
)
})
}
}
pub fn of(host: Host, blocks_from: bool) -> Failure {
let facts: &HookFailure = &host.spec().hook_failure;
let word = |outcome: FailureOutcome| match outcome {
FailureOutcome::Blocks | FailureOutcome::GuardDenies(_) => "blocks",
FailureOutcome::BlocksFrom(_) if blocks_from => "blocks",
FailureOutcome::BlocksFrom(_)
| FailureOutcome::BlocksWhenMissing
| FailureOutcome::Runs => "runs",
FailureOutcome::NotDocumented => "not-documented",
};
Failure {
fails: word(facts.fails),
cannot_start: match facts.fails {
FailureOutcome::BlocksWhenMissing => "blocks",
other => word(other),
},
times_out: word(facts.times_out),
timeout_s: facts.timeout_s,
deadline_s: host.spec().guard_deadline_s(),
unanswered: match facts.times_out {
FailureOutcome::GuardDenies(Unanswered::Runs) => Some("runs"),
FailureOutcome::GuardDenies(Unanswered::NotDocumented) => Some("not-documented"),
_ => None,
},
turned_off_by: facts.turned_off_by,
fails_open: facts.open_cases(blocks_from),
sources: facts.sources,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_agent_that_fails_open_gets_a_row_with_the_fix() {
for host in Host::EVERY {
for blocks_from in [true, false] {
let failure = of(host, blocks_from);
if let Some(line) = failure.line() {
assert!(line.starts_with("fails open: runs the call when the hook "));
assert!(line.contains("`openmoat run`"), "{line}");
assert!(line.contains("require.run: full-boundary"), "{line}");
} else {
assert_eq!(failure.fails, "blocks", "{host:?}");
assert_eq!(failure.times_out, "blocks", "{host:?}");
}
}
}
}
#[test]
fn a_version_gated_block_holds_only_where_the_version_reads_it() {
let gated = Host::EVERY
.into_iter()
.find(|h| matches!(h.spec().hook_failure.fails, FailureOutcome::BlocksFrom(_)));
let Some(host) = gated else {
return;
};
assert_eq!(of(host, true).fails, "blocks");
assert_eq!(of(host, false).fails, "runs");
assert!(of(host, false).line().is_some());
}
#[test]
fn a_block_when_missing_reports_each_case() {
for host in Host::EVERY {
let failure = of(host, true);
if host.spec().hook_failure.fails == FailureOutcome::BlocksWhenMissing {
assert_eq!((failure.cannot_start, failure.fails), ("blocks", "runs"));
let line = failure.line().unwrap_or_default();
assert!(line.contains("crashes or exits non-zero"), "{line}");
assert!(!line.contains("cannot start"), "{line}");
} else {
assert_eq!(failure.cannot_start, failure.fails, "{host:?}");
}
}
}
#[test]
fn a_timeout_the_guard_answers_first_blocks_and_names_what_remains() {
for host in Host::EVERY {
let failure = of(host, true);
if let FailureOutcome::GuardDenies(_) = host.spec().hook_failure.times_out {
assert_eq!(failure.times_out, "blocks", "{host:?}");
assert!(failure.unanswered.is_some(), "{host:?}");
assert!(!failure.fails_open.contains(&"times out"), "{host:?}");
} else {
assert_eq!(failure.unanswered, None, "{host:?}");
}
assert!(
failure.deadline_s <= openmoat_hosts::GUARD_BUDGET_S,
"{host:?}"
);
}
}
#[test]
fn not_documented_fails_open() {
for host in Host::EVERY {
let failure = of(host, true);
if failure.fails == "not-documented" {
assert!(!failure.fails_open.is_empty(), "{host:?}");
}
}
}
}