Skip to main content

openmoat_proxy/broker/
helper.rs

1//! Handing a secret's value to a tool's local helper (ADR-118).
2//!
3//! Without TLS termination the proxy cannot put a value into an HTTPS
4//! request. A tool with a helper protocol (git's credential helper, AWS's
5//! `credential_process`) asks its helper instead, and `openmoat credential`
6//! asks the proxy. The proxy hands the value out only for a secret whose
7//! `helper:` names that tool, and for git only when git asks for the
8//! secret's own host: the audience the policy gave it. A value past its
9//! deadline is never handed out. Every answer is recorded by the caller.
10
11use openmoat_core::rule_id::PROXY_HELPER;
12use openmoat_core::{Decision, Verdict};
13use zeroize::Zeroizing;
14
15use super::Broker;
16
17/// What a helper may be handed: the value, the secret's own host (for the
18/// audit row) and the user name git sends with it.
19pub struct HandOut<'a> {
20    /// The value to hand out.
21    pub value: Zeroizing<String>,
22    /// The secret's host.
23    pub host: &'a str,
24    /// The user name the helper gives with the value; `None` for AWS.
25    pub username: Option<&'a str>,
26    /// The allow decision to record.
27    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    /// The value of secret `id` for a `tool` helper that asks for `host`
40    /// (`None` for AWS, whose helper names no host); the deny to record when
41    /// the secret has no such helper, belongs to another host, or expired.
42    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;