Skip to main content

aft_base/secret/
scope.rs

1//! One daemon-stamped scope, as the vault and the audit record name it.
2//!
3//! A [`SecretScope`] is stamped by the daemon onto a route. It is never built
4//! from a tool argument or a session id, and a background command keeps the one
5//! it resolved at launch for its whole life. The stamp itself travels to the
6//! vault unchanged: `secret.use` trusts the sender to relay it, and takes the
7//! asking scope from nowhere else.
8
9use std::sync::Arc;
10
11use secret_rules::audit::ScopeRef;
12use subc_protocol::scope::ScopeStamp;
13
14use super::wire::Coverage;
15
16/// The `(owner, kind, ref)` identity of one stamped scope, plus the stamp the
17/// vault is given.
18#[derive(Clone, PartialEq, Eq)]
19pub struct SecretScope {
20    stamp: Arc<ScopeStamp>,
21    owner: String,
22    kind: String,
23    reference: String,
24}
25
26impl SecretScope {
27    /// Read a scope off the stamp the daemon put on a route.
28    pub fn from_stamp(stamp: &ScopeStamp) -> Self {
29        Self {
30            stamp: Arc::new(stamp.clone()),
31            // The contract renders an owner as `reserved:<module_id>`, `direct`
32            // or `unverified`; that spelling is what a consent or audit reader
33            // compares, and it is also what the statements use.
34            owner: render_owner(&stamp.owner),
35            kind: match stamp.kind {
36                subc_protocol::scope::ScopeKind::Head => "head",
37                subc_protocol::scope::ScopeKind::Worker => "worker",
38                subc_protocol::scope::ScopeKind::Ephemeral => "ephemeral",
39                subc_protocol::scope::ScopeKind::Federated => "federated",
40                _ => "unsupported",
41            }
42            .to_string(),
43            reference: stamp.scope_ref.clone(),
44        }
45    }
46
47    /// The daemon's stamp, relayed to the vault unchanged.
48    pub fn stamp(&self) -> &ScopeStamp {
49        &self.stamp
50    }
51
52    /// The scope's owner, rendered as the contract spells it.
53    pub fn owner(&self) -> &str {
54        &self.owner
55    }
56
57    /// The scope's diagnostic kind, not a statement of eligibility for secrets.
58    pub fn kind(&self) -> &str {
59        &self.kind
60    }
61
62    /// The scope's own reference, stable across a new epoch of the same work.
63    pub fn reference(&self) -> &str {
64        &self.reference
65    }
66
67    /// The authorization scope of an audit record.
68    pub fn to_scope_ref(&self) -> ScopeRef {
69        ScopeRef {
70            owner: self.owner.clone(),
71            kind: self.kind.clone(),
72            reference: self.reference.clone(),
73        }
74    }
75
76    /// The coverage a call asks the vault for. A head or worker call asks for
77    /// its own scope; a bound codemode run's authorization scope is its head's,
78    /// so it asks as that head.
79    pub fn coverage(&self) -> Coverage {
80        Coverage::Scope
81    }
82}
83
84/// The contract's owner rendering: `reserved:<module_id>`, `direct` or
85/// `unverified`.
86fn render_owner(owner: &subc_protocol::Principal) -> String {
87    match owner {
88        subc_protocol::Principal::Reserved { module_id } => format!("reserved:{module_id}"),
89        subc_protocol::Principal::Direct => "direct".to_string(),
90        subc_protocol::Principal::Unverified => "unverified".to_string(),
91    }
92}
93
94impl std::hash::Hash for SecretScope {
95    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
96        // The identity fields are what make two scopes the same, so the relayed
97        // stamp takes no part in hashing and equality stays cheap.
98        self.owner.hash(state);
99        self.kind.hash(state);
100        self.reference.hash(state);
101    }
102}
103
104impl std::fmt::Debug for SecretScope {
105    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
106        f.debug_struct("SecretScope")
107            .field("owner", &self.owner)
108            .field("kind", &self.kind)
109            .field("ref", &self.reference)
110            .finish()
111    }
112}
113
114impl std::fmt::Display for SecretScope {
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        write!(f, "{}:{}:{}", self.owner, self.kind, self.reference)
117    }
118}