zeph_subagent/grants.rs
1// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! Zero-trust TTL-bounded permission grants for sub-agents.
5//!
6//! [`PermissionGrants`] tracks active grants (vault secrets or runtime tool access)
7//! for a running sub-agent. All grants are time-limited; expired grants are swept
8//! lazily by [`PermissionGrants::is_active`] and eagerly by
9//! [`PermissionGrants::sweep_expired`].
10//!
11//! Grants are revoked on drop and on agent completion/cancellation. Secret key names
12//! are never logged above DEBUG level; the `Display` impl for [`GrantKind::Secret`]
13//! always prints `"Secret(<redacted>)"`.
14
15use std::time::{Duration, Instant};
16
17use serde::{Deserialize, Serialize};
18
19/// Metadata sent by a sub-agent when it needs a secret from the vault.
20///
21/// Carried in an `InputRequired` A2A status update as structured metadata.
22/// The parent agent surfaces this to the user as an approval prompt; the user can
23/// then call [`SubAgentManager::approve_secret`][crate::SubAgentManager] or
24/// [`SubAgentManager::deny_secret`][crate::SubAgentManager].
25///
26/// # Examples
27///
28/// ```rust
29/// use zeph_subagent::grants::SecretRequest;
30///
31/// let req = SecretRequest {
32/// secret_key: "OPENAI_API_KEY".to_owned(),
33/// reason: Some("needed for embeddings".to_owned()),
34/// };
35/// assert_eq!(req.secret_key, "OPENAI_API_KEY");
36/// ```
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct SecretRequest {
39 /// The vault key name the sub-agent is requesting.
40 pub secret_key: String,
41 /// Human-readable reason (shown to the user in the approval prompt).
42 pub reason: Option<String>,
43}
44
45/// Identifies the kind of permission that was granted to a sub-agent.
46///
47/// `GrantKind` is intentionally NOT serializable — grant metadata should never
48/// leave the in-memory security boundary. Key names are logged only at DEBUG
49/// level to avoid leaking grant enumeration to centralized log systems.
50///
51/// The [`Display`][std::fmt::Display] implementation always redacts `Secret` payloads,
52/// printing `Secret(<redacted>)` instead of the actual key name.
53///
54/// # Examples
55///
56/// ```rust
57/// use zeph_subagent::grants::GrantKind;
58///
59/// let secret = GrantKind::Secret("my-key".to_owned());
60/// assert!(!secret.to_string().contains("my-key"), "key must be redacted");
61///
62/// let tool = GrantKind::Tool("shell".to_owned());
63/// assert_eq!(tool.to_string(), "Tool(shell)");
64/// ```
65#[derive(Debug, Clone, PartialEq, Eq)]
66pub enum GrantKind {
67 /// A vault secret key granted for in-memory access.
68 Secret(String),
69 /// A tool name granted at runtime beyond the definition's static policy.
70 Tool(String),
71}
72
73impl std::fmt::Display for GrantKind {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 match self {
76 Self::Secret(_) => write!(f, "Secret(<redacted>)"),
77 Self::Tool(name) => write!(f, "Tool({name})"),
78 }
79 }
80}
81
82/// A single permission grant with a TTL.
83///
84/// Created via [`PermissionGrants::add`] and swept automatically by
85/// [`PermissionGrants::sweep_expired`].
86#[derive(Debug)]
87pub struct Grant {
88 pub(crate) kind: GrantKind,
89 pub(crate) granted_at: Instant,
90 pub(crate) ttl: Duration,
91}
92
93impl Grant {
94 /// Create a new grant for `kind` that expires after `ttl`.
95 ///
96 /// # Examples
97 ///
98 /// ```rust
99 /// use std::time::Duration;
100 /// use zeph_subagent::grants::{Grant, GrantKind};
101 ///
102 /// let grant = Grant::new(GrantKind::Tool("shell".to_owned()), Duration::from_mins(1));
103 /// assert!(!grant.is_expired());
104 /// ```
105 #[must_use]
106 pub fn new(kind: GrantKind, ttl: Duration) -> Self {
107 Self {
108 kind,
109 granted_at: Instant::now(),
110 ttl,
111 }
112 }
113
114 /// Returns `true` if the grant's TTL has elapsed.
115 ///
116 /// # Examples
117 ///
118 /// ```rust
119 /// use std::time::Duration;
120 /// use zeph_subagent::grants::{Grant, GrantKind};
121 ///
122 /// let grant = Grant::new(GrantKind::Tool("web".to_owned()), Duration::from_mins(5));
123 /// // A brand-new grant is not yet expired.
124 /// assert!(!grant.is_expired());
125 /// ```
126 #[must_use]
127 pub fn is_expired(&self) -> bool {
128 self.granted_at.elapsed() >= self.ttl
129 }
130}
131
132/// Tracks active zero-trust permission grants for a sub-agent.
133///
134/// All grants are TTL-bounded. [`is_active`](Self::is_active) automatically
135/// sweeps expired grants before checking, so callers do not need to call
136/// [`sweep_expired`](Self::sweep_expired) manually.
137#[derive(Debug, Default)]
138pub struct PermissionGrants {
139 grants: Vec<Grant>,
140}
141
142impl Drop for PermissionGrants {
143 fn drop(&mut self) {
144 // Defense-in-depth: revoke all grants on drop even if revoke_all()
145 // was not explicitly called (e.g., on panic or early return).
146 if !self.grants.is_empty() {
147 tracing::warn!(
148 count = self.grants.len(),
149 "PermissionGrants dropped with active grants — revoking"
150 );
151 self.grants.clear();
152 }
153 }
154}
155
156impl PermissionGrants {
157 /// Add a new grant with the given `kind` and `ttl`.
158 ///
159 /// The grant is immediately tracked. Expired grants are not swept here;
160 /// call [`sweep_expired`][Self::sweep_expired] or [`is_active`][Self::is_active]
161 /// to remove stale entries.
162 ///
163 /// # Examples
164 ///
165 /// ```rust
166 /// use std::time::Duration;
167 /// use zeph_subagent::grants::{GrantKind, PermissionGrants};
168 ///
169 /// let mut grants = PermissionGrants::default();
170 /// grants.add(GrantKind::Tool("shell".to_owned()), Duration::from_mins(1));
171 /// assert!(grants.is_active(&GrantKind::Tool("shell".to_owned())));
172 /// ```
173 pub fn add(&mut self, kind: GrantKind, ttl: Duration) {
174 // Log tool grants at DEBUG; for secrets log only the redacted display form.
175 tracing::debug!(kind = %kind, ?ttl, "permission grant added");
176 self.grants.push(Grant::new(kind, ttl));
177 }
178
179 /// Remove all expired grants.
180 pub fn sweep_expired(&mut self) {
181 let expired: Vec<_> = self.grants.extract_if(.., |g| g.is_expired()).collect();
182 for g in &expired {
183 tracing::debug!(kind = %g.kind, "permission grant expired and revoked");
184 }
185 if !expired.is_empty() {
186 tracing::debug!(removed = expired.len(), "swept expired grants");
187 }
188 }
189
190 /// Check if a specific grant is still active (not expired).
191 ///
192 /// Automatically sweeps expired grants before checking.
193 #[must_use]
194 pub fn is_active(&mut self, kind: &GrantKind) -> bool {
195 self.sweep_expired();
196 self.grants.iter().any(|g| &g.kind == kind)
197 }
198
199 /// Grant access to a vault secret with the given TTL.
200 ///
201 /// Sweeps expired grants first. Logs an audit event at DEBUG (key is redacted
202 /// in the log output to avoid leaking grant enumeration to log aggregators).
203 pub fn grant_secret(&mut self, key: impl Into<String>, ttl: Duration) {
204 self.sweep_expired();
205 let key = key.into();
206 tracing::debug!("vault secret granted to sub-agent (key redacted), ttl={ttl:?}");
207 self.add(GrantKind::Secret(key), ttl);
208 }
209
210 /// Returns `true` if there are any grants currently tracked (expired or not).
211 ///
212 /// Used by [`Drop`] to emit a warning when handles are dropped without cleanup.
213 #[must_use]
214 pub fn is_empty_grants(&self) -> bool {
215 self.grants.is_empty()
216 }
217
218 /// Revoke all grants immediately (called on sub-agent completion or cancellation).
219 pub fn revoke_all(&mut self) {
220 let count = self.grants.len();
221 self.grants.clear();
222 if count > 0 {
223 tracing::debug!(count, "all permission grants revoked");
224 }
225 }
226}
227
228#[cfg(test)]
229mod tests {
230 use super::*;
231
232 #[test]
233 fn grant_is_active_before_expiry() {
234 let mut pg = PermissionGrants::default();
235 pg.add(GrantKind::Secret("api-key".into()), Duration::from_mins(5));
236 assert!(pg.is_active(&GrantKind::Secret("api-key".into())));
237 }
238
239 #[test]
240 fn sweep_expired_removes_instant_ttl() {
241 let mut pg = PermissionGrants::default();
242 pg.grants.push(Grant {
243 kind: GrantKind::Tool("shell".into()),
244 granted_at: Instant::now().checked_sub(Duration::from_secs(10)).unwrap(),
245 ttl: Duration::from_secs(1), // already expired
246 });
247 // is_active internally sweeps
248 assert!(!pg.is_active(&GrantKind::Tool("shell".into())));
249 assert!(pg.grants.is_empty());
250 }
251
252 #[test]
253 fn revoke_all_clears_all_grants() {
254 let mut pg = PermissionGrants::default();
255 pg.add(GrantKind::Secret("token".into()), Duration::from_mins(1));
256 pg.add(GrantKind::Tool("web".into()), Duration::from_mins(1));
257 pg.revoke_all();
258 assert!(pg.grants.is_empty());
259 }
260
261 #[test]
262 fn grant_secret_is_active() {
263 let mut pg = PermissionGrants::default();
264 pg.grant_secret("db-password", Duration::from_mins(2));
265 assert!(pg.is_active(&GrantKind::Secret("db-password".into())));
266 }
267
268 #[test]
269 fn whitespace_description_invalid() {
270 // Verify grant kind display redacts secrets
271 let k = GrantKind::Secret("my-secret-key".into());
272 let display = k.to_string();
273 assert!(
274 !display.contains("my-secret-key"),
275 "secret key must be redacted in Display"
276 );
277 assert!(display.contains("redacted"));
278 }
279
280 #[test]
281 fn tool_grant_display_shows_name() {
282 let k = GrantKind::Tool("shell".into());
283 assert_eq!(k.to_string(), "Tool(shell)");
284 }
285
286 #[test]
287 fn partial_sweep_keeps_non_expired_grants() {
288 let mut pg = PermissionGrants::default();
289
290 // Add one already-expired grant.
291 pg.grants.push(Grant {
292 kind: GrantKind::Tool("expired-tool".into()),
293 granted_at: Instant::now().checked_sub(Duration::from_secs(10)).unwrap(),
294 ttl: Duration::from_secs(1),
295 });
296
297 // Add one live grant with long TTL.
298 pg.add(GrantKind::Secret("live-key".into()), Duration::from_mins(5));
299
300 pg.sweep_expired();
301
302 assert_eq!(pg.grants.len(), 1, "only live grant should remain");
303 assert_eq!(pg.grants[0].kind, GrantKind::Secret("live-key".into()));
304 }
305
306 #[test]
307 fn duplicate_grant_for_same_key_both_tracked() {
308 let mut pg = PermissionGrants::default();
309 pg.add(GrantKind::Secret("my-key".into()), Duration::from_mins(1));
310 pg.add(GrantKind::Secret("my-key".into()), Duration::from_mins(1));
311
312 // Both grants are stored; is_active just checks any match.
313 assert_eq!(pg.grants.len(), 2);
314 assert!(pg.is_active(&GrantKind::Secret("my-key".into())));
315
316 // After revoking all, none remain.
317 pg.revoke_all();
318 assert!(pg.grants.is_empty());
319 }
320}