openmoat_proxy/broker/
helper.rs1use openmoat_core::rule_id::PROXY_HELPER;
12use openmoat_core::{Decision, Verdict};
13use zeroize::Zeroizing;
14
15use super::Broker;
16
17pub struct HandOut<'a> {
20 pub value: Zeroizing<String>,
22 pub host: &'a str,
24 pub username: Option<&'a str>,
26 pub decision: Decision,
28}
29
30impl std::fmt::Debug for HandOut<'_> {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 f.debug_struct("HandOut")
33 .field("host", &self.host)
34 .finish_non_exhaustive()
35 }
36}
37
38impl Broker {
39 pub fn hand_out(
43 &self,
44 tool: &str,
45 id: &str,
46 host: Option<&str>,
47 ) -> Result<HandOut<'_>, Decision> {
48 let refuse = |why: String| Decision::single(Verdict::Deny, PROXY_HELPER, why);
49 let Some(held) = self.held.iter().find(|h| h.secret.id == id) else {
50 return Err(refuse(format!(
51 "the {tool} helper asked for secret `{id}`, which the policy does not broker"
52 )));
53 };
54 let Some(helper) = held.secret.helper.as_ref().filter(|h| h.tool() == tool) else {
55 return Err(refuse(format!(
56 "secret `{id}` has no `helper: {tool}`; the {tool} helper is not given it"
57 )));
58 };
59 let own = held.secret.host.as_str();
60 match (helper, host) {
61 (openmoat_core::mint::Helper::Git { .. }, Some(asked)) if asked == own => {}
62 (openmoat_core::mint::Helper::Git { .. }, asked) => {
63 return Err(refuse(format!(
64 "the git helper asked for secret `{id}` for {}, but only {own} may receive it",
65 asked.unwrap_or("no host")
66 )));
67 }
68 (_, None) => {}
69 (_, Some(_)) => {
70 return Err(refuse(format!(
71 "the {tool} helper names a host; secret `{id}` is handed out by id only"
72 )));
73 }
74 }
75 let Some(value) = held.live() else {
76 return Err(refuse(format!(
77 "secret `{id}` expired and was not renewed; the {tool} helper gets nothing"
78 )));
79 };
80 let username = match helper {
81 openmoat_core::mint::Helper::Git { username } => Some(username.as_str()),
82 openmoat_core::mint::Helper::Aws => None,
83 };
84 Ok(HandOut {
85 value,
86 host: own,
87 username,
88 decision: Decision::single(
89 Verdict::Allow,
90 PROXY_HELPER,
91 format!("secret `{id}` handed to the {tool} helper, for {own} only"),
92 ),
93 })
94 }
95}
96
97#[cfg(test)]
98mod tests;