agentmux_srv_core/backend/vault/
agent_set.rs1use agentmux_common::time::now_ms;
16
17use super::uses::UseRequest;
18use crate::backend::storage::store::Store;
19use crate::backend::storage::vault_changes::{SITE, VALUE};
20
21pub fn mark(store: &Store, entry_id: &str, agent: &str) {
24 mark_what(store, entry_id, agent, VALUE);
25}
26
27pub fn mark_site(store: &Store, entry_id: &str, agent: &str) {
29 mark_what(store, entry_id, agent, SITE);
30}
31
32fn mark_what(store: &Store, entry_id: &str, agent: &str, what: &str) {
33 let marked = store.vault_get(entry_id).ok().flatten().map(|row| store.vault_unseen_mark(entry_id, row.created_at, agent, now_ms(), what));
34 if !matches!(marked, Some(Ok(()))) {
35 tracing::warn!(entry = entry_id, "vault: couldn't mark an agent's value as unseen");
36 }
37}
38
39pub fn clear(store: &Store, entry_id: &str) -> bool {
41 store.vault_unseen_clear(entry_id).unwrap_or(false)
42}
43
44pub fn is_unseen(store: &Store, entry_id: &str) -> bool {
46 matches!(store.vault_unseen_get(entry_id), Ok(Some(_)))
47}
48
49pub fn apply(store: &Store, request: &mut UseRequest) {
52 if request.entry_id.is_empty() {
53 return;
54 }
55 if let Ok(Some(unseen)) = store.vault_unseen_get(&request.entry_id) {
56 request.set_by = unseen.by_agent;
57 request.set_ms = u64::try_from(unseen.at).unwrap_or(0);
58 request.set_what = unseen.what;
59 request.offer_always = false;
60 }
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use crate::backend::vault::uses::goes_ahead;
67
68 fn store() -> Store {
69 crate::backend::vault::seal::use_test_key();
70 let store = Store::open_in_memory().unwrap();
71 store.apply_identity_schema_for_tests().unwrap();
72 store
73 }
74
75 fn entry(store: &Store, id: &str) {
76 let entry: crate::backend::vault::NewEntry = serde_json::from_value(serde_json::json!({ "kind": "api-key", "label": "Mail key", "parts": { "value": "sk_test_invented_value" } })).unwrap();
77 assert_eq!(crate::backend::vault::create(store, entry).unwrap(), id);
78 }
79
80 fn use_of(id: &str) -> UseRequest {
81 UseRequest { entry_id: id.into(), kind: "api-key".into(), offer_always: true, ..Default::default() }
82 }
83
84 #[test]
85 fn an_unseen_value_never_goes_ahead_on_silence_and_offers_no_always_until_it_is_seen() {
86 let store = store();
87 entry(&store, "api-key-mail-key");
88 let mut plain = use_of("api-key-mail-key");
89 apply(&store, &mut plain);
90 assert!(goes_ahead(&plain) && plain.offer_always && plain.set_by.is_empty(), "a value nobody marked is used as before");
91 mark(&store, "api-key-mail-key", "builder");
92 let mut unseen = use_of("api-key-mail-key");
93 apply(&store, &mut unseen);
94 assert_eq!(unseen.set_by, "builder");
95 assert!(unseen.set_ms > 0 && !unseen.offer_always && !goes_ahead(&unseen), "silence is a no, and there is no Always");
96 assert!(clear(&store, "api-key-mail-key"));
97 let mut seen = use_of("api-key-mail-key");
98 apply(&store, &mut seen);
99 assert!(goes_ahead(&seen), "seen, it is used as any other");
100 assert!(!clear(&store, "api-key-mail-key"), "nothing left to clear");
101 }
102
103 #[test]
104 fn marking_a_value_unseen_drops_the_always_grants_given_for_the_value_the_person_saw() {
105 let store = store();
106 entry(&store, "api-key-mail-key");
107 let created = store.vault_get("api-key-mail-key").unwrap().unwrap().created_at;
108 store.vault_grant_add("api-key-mail-key", "https://mail.example", 1, created).unwrap();
109 assert!(store.vault_grant_check("api-key-mail-key", "https://mail.example").unwrap());
110 mark(&store, "api-key-mail-key", "builder");
111 assert!(!store.vault_grant_check("api-key-mail-key", "https://mail.example").unwrap(), "a use on a grant skips the card, so the grant goes");
112 }
113
114 #[test]
115 fn a_mark_belongs_to_the_entry_it_was_made_on_and_not_to_one_made_again_under_its_id() {
116 let store = store();
117 entry(&store, "api-key-mail-key");
118 mark(&store, "api-key-mail-key", "builder");
119 store.vault_delete("api-key-mail-key").unwrap();
120 std::thread::sleep(std::time::Duration::from_millis(3));
121 entry(&store, "api-key-mail-key");
122 let mut again = use_of("api-key-mail-key");
123 apply(&store, &mut again);
124 assert!(again.set_by.is_empty(), "the new entry was made by the person, not by that agent");
125 }
126}