agentmux_srv_core/backend/vault/
endpoint_key.rs1use super::{kinds, record, seal, vaults, Store};
12
13pub const USED: &str = "put in an agent's launch environment";
15
16pub const NOT_AVAILABLE: &str = "that Vault key is not available to this agent";
20
21pub fn problem(store: &Store, entry_id: &str, agent_id: &str) -> Option<String> {
25 reason(store, entry_id, agent_id).map(|why| {
26 tracing::info!(entry = %entry_id, agent = %agent_id, "a Vault key was refused for an agent's environment: {why}");
27 NOT_AVAILABLE.to_string()
28 })
29}
30
31fn reason(store: &Store, entry_id: &str, agent_id: &str) -> Option<String> {
32 let row = match store.vault_get(entry_id) {
33 Ok(Some(row)) => row,
34 Ok(None) => return Some(format!("the Vault has no entry {entry_id}")),
35 Err(e) => return Some(e.to_string()),
36 };
37 if row.kind != "api-key" {
38 return Some(format!("{} is not an api-key entry", row.label));
39 }
40 match vaults::entry_usable(store, entry_id, Some(agent_id)) {
41 Ok(true) => None,
42 Ok(false) => Some(format!("{} is in a Vault the person has not opened to this agent", row.label)),
43 Err(e) => Some(e),
44 }
45}
46
47pub fn for_launch(store: &Store, entry_id: &str, agent_id: &str, agent_slug: &str) -> Result<zeroize::Zeroizing<String>, String> {
49 if let Some(why) = problem(store, entry_id, agent_id) {
50 return Err(why);
51 }
52 let row = store.vault_get(entry_id).map_err(|e| e.to_string())?.ok_or_else(|| format!("the Vault has no entry {entry_id}"))?;
53 let kind = kinds::kind(&row.kind).ok_or_else(|| format!("entry {} has an unknown kind", row.id))?;
54 let key = seal::key(false)?;
55 let parts = seal::open(&key, &row.id, &row.sealed)?;
56 let value = parts.get(kind.parts[0]).ok_or_else(|| format!("{} has no value saved", row.label))?;
57 let value = zeroize::Zeroizing::new(value.to_string());
58 record(store, &row.id, &row.label, agent_slug, USED, "", "done");
59 Ok(value)
60}
61
62#[cfg(test)]
63mod tests {
64 use super::*;
65
66 fn store_with(kind: &str, label: &str, part: &str) -> (Store, String) {
67 crate::backend::vault::seal::use_test_key();
68 let store = Store::open_in_memory().unwrap();
69 store.apply_identity_schema_for_tests().unwrap();
70 let entry: crate::backend::vault::NewEntry =
71 serde_json::from_value(serde_json::json!({ "kind": kind, "label": label, "parts": { part: "sk-test-0123456789" } })).unwrap();
72 let id = crate::backend::vault::create(&store, entry).unwrap();
73 (store, id)
74 }
75
76 #[test]
77 fn a_missing_entry_and_a_note_are_refused() {
78 let (store, note) = store_with("note", "A note", "text");
79 assert!(reason(&store, "nope", "a1").unwrap().contains("no entry"));
80 assert!(reason(&store, ¬e, "a1").unwrap().contains("not an api-key"));
81 assert_eq!(problem(&store, "nope", "a1").as_deref(), Some(NOT_AVAILABLE));
83 assert_eq!(problem(&store, ¬e, "a1").as_deref(), Some(NOT_AVAILABLE));
84 assert!(for_launch(&store, ¬e, "a1", "lolly").is_err());
85 }
86
87 #[test]
88 fn an_api_key_in_a_vault_open_to_all_agents_is_usable() {
89 let (store, id) = store_with("api-key", "Local LLM key", "value");
90 assert_eq!(problem(&store, &id, "a1"), None);
91 }
92
93 #[test]
94 fn a_vault_the_person_opened_to_other_agents_is_not_usable_by_this_one() {
95 let (store, id) = store_with("api-key", "Local LLM key", "value");
96 assert!(store.vault_set("personal", "Personal", "named", "[\"a2\"]").unwrap());
97 assert!(reason(&store, &id, "a1").unwrap().contains("not opened to this agent"));
98 assert_eq!(problem(&store, &id, "a1").as_deref(), Some(NOT_AVAILABLE));
99 assert_eq!(problem(&store, &id, "a2"), None);
100 assert!(for_launch(&store, &id, "a1", "lolly").is_err(), "no value without the person's opening");
101 }
102
103 #[test]
104 fn the_value_is_read_and_recorded_under_the_agents_name() {
105 let (store, id) = store_with("api-key", "Local LLM key", "value");
106 let value = for_launch(&store, &id, "a1", "lolly").unwrap();
107 assert_eq!(value.as_str(), "sk-test-0123456789");
108 let log = crate::backend::vault::activity(&store, 10).unwrap();
109 assert!(log.iter().any(|l| l.actor == "agent:lolly" && l.action == USED));
110 }
111}