use std::process::{Command, Stdio};
use serde_json::Value;
use super::{Finding, Level};
const COMPILED_DEFAULT: &str = "compiled default";
pub(super) fn tenants_from_env(value: Option<&str>) -> Vec<Option<String>> {
let named: Vec<Option<String>> = value
.unwrap_or_default()
.split(',')
.map(str::trim)
.filter(|tenant| !tenant.is_empty())
.map(|tenant| Some(tenant.to_owned()))
.collect();
if named.is_empty() {
vec![None]
} else {
named
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct HarnessHooks {
pub harness: String,
pub items: u64,
pub counts: Option<(u64, u64, u64)>,
}
fn number_before(text: &str, marker: &str) -> Option<u64> {
let head = text.split(marker).next()?;
head.split_whitespace().last()?.parse().ok()
}
fn number_after(text: &str, marker: &str) -> Option<u64> {
let (_, tail) = text.split_once(marker)?;
tail.trim_start()
.split(|c: char| !c.is_ascii_digit())
.next()?
.parse()
.ok()
}
pub(super) fn parse_hooks_status(text: &str) -> Option<Vec<HarnessHooks>> {
let mut harnesses = Vec::new();
for line in text.lines().map(str::trim).filter(|line| !line.is_empty()) {
let (harness, rest) = line.split_once(": ")?;
if harness.is_empty() || harness.contains(' ') {
return None;
}
let entry = if rest.contains("hook item(s)") {
let counts = match (
number_after(rest, "configured ok"),
number_after(rest, "live ok"),
number_after(rest, "firing ok"),
) {
(Some(c), Some(l), Some(f)) => Some((c, l, f)),
_ => None,
};
HarnessHooks {
harness: harness.to_owned(),
items: number_before(rest, "hook item(s)")?,
counts,
}
} else if rest.contains("yupana hook(s)") {
let fraction = rest.split_whitespace().next()?;
let (have, _want) = fraction.split_once('/')?;
HarnessHooks {
harness: harness.to_owned(),
items: have.parse().ok()?,
counts: None,
}
} else {
return None;
};
harnesses.push(entry);
}
(!harnesses.is_empty()).then_some(harnesses)
}
fn text<'a>(value: &'a Value, pointer: &str) -> Option<&'a str> {
value.pointer(pointer).and_then(Value::as_str)
}
pub(super) fn guard_findings(
tenant: Option<&str>,
status: &str,
hooks: Option<&str>,
present: &dyn Fn(&str) -> bool,
) -> Vec<Finding> {
let label = |name: &str| match name {
"" => "yupana guard".to_owned(),
name => format!("yupana guard [{name}]"),
};
let Ok(json) = serde_json::from_str::<Value>(status) else {
return vec![unknown(
&label(tenant.unwrap_or_default()),
"`yupana status --json` output is not JSON",
)];
};
let tenant = text(&json, "/tenant")
.or(tenant)
.unwrap_or_default()
.to_owned();
let subject = label(&tenant);
let mode =
text(&json, "/policy_mode_provenance/effective").or_else(|| text(&json, "/policy/mode"));
let source = text(&json, "/policy_mode_provenance/source");
let auth = text(&json, "/quipu/auth/state");
let (Some(mode), Some(auth)) = (mode, auth) else {
return vec![unknown(
&subject,
"`yupana status --json` has no policy mode or quipu auth state",
)];
};
let source = source.unwrap_or("unreported");
let scope = match json.pointer("/policy/scope") {
Some(Value::String(scope)) => scope.clone(),
Some(Value::Null) | None => "none".to_owned(),
Some(other) => other.to_string(),
};
let rules = match (
json.pointer("/rule_set/projected").and_then(Value::as_u64),
text(&json, "/rule_set/digest"),
) {
(Some(count), Some(digest)) => format!("{count} projected rule(s), digest {digest}"),
(Some(count), None) => format!("{count} projected rule(s), no digest"),
_ => "projected rules unreported".to_owned(),
};
let signed = match json
.pointer("/signed_rule_set/loaded")
.and_then(Value::as_bool)
{
Some(true) => "signed resident cache present",
Some(false) => "signed resident cache absent",
None => "signed resident cache unreported",
};
let mut findings = Vec::new();
let summary = format!(
"policy mode {mode} (source: {source}), scope {scope}, {rules}, quipu auth {auth}, {signed}"
);
findings.push(match mode {
"off" if source == COMPILED_DEFAULT => Finding::new(
Level::Warn,
subject.clone(),
format!("NOT GUARDING: {summary}. Mode is off only because nothing set it; record a mode in the bobbin config (`[yupana.policy] mode = ...`) or record off explicitly"),
),
"off" => Finding::new(
Level::Ok,
subject.clone(),
format!("off by explicit choice recorded in {source}: {summary}"),
),
_ => Finding::new(Level::Ok, subject.clone(), summary),
});
if json
.pointer("/policy/enforcing_without_scope")
.and_then(Value::as_bool)
== Some(true)
{
findings.push(Finding::new(
Level::Warn,
format!("{subject} scope"),
"enforcing with no scope configured; the guard has nothing to enforce against",
));
}
if auth != "accepted" {
findings.push(Finding::new(
Level::Warn,
format!("{subject} quipu auth"),
format!("{auth}: yupana has not proven Quipu accepts its credential, so guard verdicts may not reach the graph"),
));
}
if mode != "off" {
if let Some(state) = text(&json, "/rule_set/state").filter(|state| *state != "loaded") {
findings.push(Finding::new(
Level::Warn,
format!("{subject} rules"),
format!("rule set is {state} while policy mode is {mode}"),
));
}
}
match hooks.map(parse_hooks_status) {
None => findings.push(unknown(
&format!("{subject} hooks"),
"`yupana hooks status` did not run",
)),
Some(None) => findings.push(unknown(
&format!("{subject} hooks"),
"`yupana hooks status` output not understood",
)),
Some(Some(harnesses)) => {
for hooks in harnesses {
let name = &hooks.harness;
let counts = hooks.counts.map_or(String::new(), |(c, l, f)| {
format!(" (configured {c}, live {l}, firing {f})")
});
let finding = if !present(name) {
Finding::new(
Level::Ok,
format!("{subject} hooks {name}"),
format!(
"{} hook item(s){counts}; {name} is not on PATH",
hooks.items
),
)
} else if hooks.items == 0 {
Finding::new(
Level::Warn,
format!("{subject} hooks {name}"),
format!("{name} is present with 0 yupana hook items; it runs unguarded. Install them with `yupana hooks install --harness {name}`"),
)
} else {
Finding::new(
Level::Ok,
format!("{subject} hooks {name}"),
format!("{} hook item(s){counts}", hooks.items),
)
};
findings.push(finding);
}
}
}
findings
}
fn unknown(subject: &str, why: &str) -> Finding {
Finding::new(
Level::Warn,
subject,
format!("UNKNOWN (not healthy): {why}; guard state cannot be confirmed"),
)
}
fn run(args: &[&str], tenant: Option<&str>) -> Option<String> {
let mut command = Command::new("yupana");
command
.args(args)
.stdin(Stdio::null())
.stderr(Stdio::null());
if let Some(tenant) = tenant {
command.args(["--tenant", tenant]);
}
let out = command.output().ok()?;
out.status
.success()
.then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
pub(super) fn check(path: Option<&std::ffi::OsStr>) -> Vec<Finding> {
if crate::adapter::which_all("yupana", path).is_empty() {
return vec![unknown("yupana guard", "yupana is not on PATH")];
}
let present = |harness: &str| !crate::adapter::which_all(harness, path).is_empty();
tenants_from_env(std::env::var("TAMBO_YUPANA_TENANTS").ok().as_deref())
.into_iter()
.flat_map(|tenant| {
let tenant = tenant.as_deref();
match run(&["status", "--json"], tenant) {
None => vec![unknown(
&format!(
"yupana guard{}",
tenant.map_or(String::new(), |t| format!(" [{t}]"))
),
"`yupana status --json` failed",
)],
Some(status) => {
let hooks = run(&["hooks", "status"], tenant);
guard_findings(tenant, &status, hooks.as_deref(), &present)
}
}
})
.collect()
}
#[cfg(test)]
#[path = "doctor_yupana_tests.rs"]
mod tests;