Skip to main content

agentmux_srv_core/backend/vault/
endpoint_key.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! A key an agent's launch environment names as `ANTHROPIC_AUTH_TOKEN=vault:<entry id>`
5//! (`backend::agent_env`): the model endpoint an agent was pointed at wants one. Unlike a model
6//! route's key (`route_key`), which goes only into srv's outgoing request, this value is put in
7//! the agent's own process environment, where the agent can read it. So the entry must be one the
8//! person has opened to that agent (the Vault's agents setting, `vaults::entry_usable`), and every
9//! launch it is put in is recorded under the agent's name.
10
11use super::{kinds, record, seal, vaults, Store};
12
13/// The activity line for a use, listed with the other uses of a value.
14pub const USED: &str = "put in an agent's launch environment";
15
16/// What a caller is told when an entry is not one an agent may have: one answer for a missing entry, a
17/// wrong kind and a closed Vault, so a refusal never says which entries exist or what they are called
18/// (`vaults::resolve` keeps the same rule). The reason is in srv's log.
19pub const NOT_AVAILABLE: &str = "that Vault key is not available to this agent";
20
21/// Why `entry_id` is not an entry `agent_id` may have put in its environment, if it is not: the one
22/// answer [`NOT_AVAILABLE`]. Reads no value. Used when the reference is written (for the agent that
23/// writes it and the agent it is written for) and again at every launch.
24pub 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
47/// The entry's value for `agent_id`'s launch, recorded as a use by `agent_slug`.
48pub 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, &note, "a1").unwrap().contains("not an api-key"));
81        // The caller hears the same thing for both, and for a closed Vault.
82        assert_eq!(problem(&store, "nope", "a1").as_deref(), Some(NOT_AVAILABLE));
83        assert_eq!(problem(&store, &note, "a1").as_deref(), Some(NOT_AVAILABLE));
84        assert!(for_launch(&store, &note, "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}