use anyhow::{Result, bail};
use openmoat_core::{EvalContext, RequiredLevel};
use openmoat_hosts::Host;
use crate::home::Home;
use crate::protection::required::{self, by, level_of};
use crate::sandbox::own_login::Owner;
pub fn check(home: &Home, ctx: &EvalContext, owner: Owner, typed: &str) -> Result<Option<String>> {
let policy = required::policy_in(home, ctx)?;
let strict = policy.run_boundary_required().map(|asks| by(&asks));
if let Some(asks) = &strict
&& let Some(why) = crate::commands::run_unavailable()
{
bail!(
"{asks} requires `openmoat run` to start with its full boundary \
(`require.run: full-boundary`), and {why}; `{typed}` does not start"
);
}
let host = match owner {
Owner::Agent(host) => Some(host),
Owner::Unknown => None,
};
let Some(requirement) = policy.required_for(host.map(Host::id)) else {
return Ok(strict);
};
let level = level_of(requirement.level).label();
let asks = by(&requirement.by);
if requirement.level == RequiredLevel::HookAndOsSandbox
&& let Some(why) = crate::commands::run_unavailable()
{
bail!("{asks} requires {level}, and {why}; the program does not start");
}
let Some(host) = host else {
bail!(
"{asks} requires {level} of every agent, and `{typed}` is no agent OpenMoat knows, \
so no hook of OpenMoat's would check its calls; it does not start"
);
};
let binary = crate::install::hook_binary()?;
let agent = crate::protection::of_host(home, &binary, host)?;
if !agent.level.hooked() {
let name = host.display_name();
let why = agent.reason.as_deref().unwrap_or(agent.level.label());
let fix = required::missing(host, agent.level, requirement.level)
.map_or_else(String::new, |(_, fix)| format!("; to meet it, {fix}"));
bail!(
"{asks} requires {level} for {name}, and OpenMoat's hook does not run for it \
({why}), so `openmoat run` cannot meet it{fix}; {name} does not start"
);
}
Ok(strict)
}