use openmoat_hosts::{FailureOutcome, HookFailure, Host};
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 times_out: &'static str,
pub timeout_s: Option<u32>,
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 => "blocks",
FailureOutcome::BlocksFrom(_) if blocks_from => "blocks",
FailureOutcome::BlocksFrom(_) | FailureOutcome::Runs => "runs",
FailureOutcome::NotDocumented => "not-documented",
};
Failure {
fails: word(facts.fails),
times_out: word(facts.times_out),
timeout_s: facts.timeout_s,
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 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:?}");
}
}
}
}