use openmoat_core::rule_id::PROXY_HELPER;
use openmoat_core::{Decision, Verdict};
use zeroize::Zeroizing;
use super::Broker;
pub struct HandOut<'a> {
pub value: Zeroizing<String>,
pub host: &'a str,
pub username: Option<&'a str>,
pub decision: Decision,
}
impl std::fmt::Debug for HandOut<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HandOut")
.field("host", &self.host)
.finish_non_exhaustive()
}
}
impl Broker {
pub fn hand_out(
&self,
tool: &str,
id: &str,
host: Option<&str>,
) -> Result<HandOut<'_>, Decision> {
let refuse = |why: String| Decision::single(Verdict::Deny, PROXY_HELPER, why);
let Some(held) = self.held.iter().find(|h| h.secret.id == id) else {
return Err(refuse(format!(
"the {tool} helper asked for secret `{id}`, which the policy does not broker"
)));
};
let Some(helper) = held.secret.helper.as_ref().filter(|h| h.tool() == tool) else {
return Err(refuse(format!(
"secret `{id}` has no `helper: {tool}`; the {tool} helper is not given it"
)));
};
let own = held.secret.host.as_str();
match (helper, host) {
(openmoat_core::mint::Helper::Git { .. }, Some(asked)) if asked == own => {}
(openmoat_core::mint::Helper::Git { .. }, asked) => {
return Err(refuse(format!(
"the git helper asked for secret `{id}` for {}, but only {own} may receive it",
asked.unwrap_or("no host")
)));
}
(_, None) => {}
(_, Some(_)) => {
return Err(refuse(format!(
"the {tool} helper names a host; secret `{id}` is handed out by id only"
)));
}
}
let Some(value) = held.live() else {
return Err(refuse(format!(
"secret `{id}` expired and was not renewed; the {tool} helper gets nothing"
)));
};
let username = match helper {
openmoat_core::mint::Helper::Git { username } => Some(username.as_str()),
openmoat_core::mint::Helper::Aws => None,
};
Ok(HandOut {
value,
host: own,
username,
decision: Decision::single(
Verdict::Allow,
PROXY_HELPER,
format!("secret `{id}` handed to the {tool} helper, for {own} only"),
),
})
}
}
#[cfg(test)]
mod tests;