agentmux_srv_core/backend/remote/
agent_access.rs1use std::path::{Path, PathBuf};
16use std::sync::Mutex;
17
18use serde::{Deserialize, Serialize};
19
20const FILE_NAME: &str = "ssh-agent-access.json";
21
22#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
23struct Grants {
24 #[serde(default)]
25 always: Vec<Grant>,
26}
27
28#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
29struct Grant {
30 agent: String,
31 connection: String,
32}
33
34static LOCK: Mutex<()> = Mutex::new(());
36
37fn path_in(config_home: &Path) -> PathBuf {
38 config_home.join(FILE_NAME)
39}
40
41fn read(path: &Path) -> Grants {
42 std::fs::read_to_string(path)
43 .ok()
44 .and_then(|s| serde_json::from_str(&s).ok())
45 .unwrap_or_default()
46}
47
48pub fn always_allowed_in(config_home: &Path, agent: &str, connection: &str) -> bool {
50 let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner());
51 read(&path_in(config_home))
52 .always
53 .iter()
54 .any(|g| g.agent.eq_ignore_ascii_case(agent) && g.connection == connection.trim())
55}
56
57pub fn remember_in(config_home: &Path, agent: &str, connection: &str) -> std::io::Result<()> {
59 let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner());
60 let path = path_in(config_home);
61 let mut grants = read(&path);
62 let grant = Grant {
63 agent: agent.to_ascii_lowercase(),
64 connection: connection.trim().to_string(),
65 };
66 if !grants.always.contains(&grant) {
67 grants.always.push(grant);
68 }
69 std::fs::create_dir_all(config_home)?;
70 let tmp = path.with_extension("json.tmp");
71 std::fs::write(&tmp, serde_json::to_vec_pretty(&grants).unwrap_or_default())?;
72 std::fs::rename(&tmp, &path)
73}
74
75pub fn always_allowed(agent: &str, connection: &str) -> bool {
77 always_allowed_in(
78 &crate::backend::base::get_mux_config_dir(),
79 agent,
80 connection,
81 )
82}
83
84pub fn revoke_in(config_home: &Path, agent: &str, connection: &str) -> std::io::Result<bool> {
88 let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner());
89 let path = path_in(config_home);
90 let mut grants = read(&path);
91 let before = grants.always.len();
92 grants
93 .always
94 .retain(|g| !(g.agent.eq_ignore_ascii_case(agent) && g.connection == connection.trim()));
95 if grants.always.len() == before {
96 return Ok(false);
97 }
98 let tmp = path.with_extension("json.tmp");
99 std::fs::write(&tmp, serde_json::to_vec_pretty(&grants).unwrap_or_default())?;
100 std::fs::rename(&tmp, &path)?;
101 Ok(true)
102}
103
104pub fn revoke(agent: &str, connection: &str) -> std::io::Result<bool> {
106 revoke_in(&crate::backend::base::get_mux_config_dir(), agent, connection)
107}
108
109pub fn all_in(config_home: &Path) -> Vec<(String, String)> {
112 let _g = LOCK.lock().unwrap_or_else(|e| e.into_inner());
113 read(&path_in(config_home))
114 .always
115 .into_iter()
116 .map(|g| (g.agent, g.connection))
117 .collect()
118}
119
120#[cfg(test)]
121mod tests {
122 use super::*;
123
124 #[test]
125 fn always_is_remembered_per_agent_and_host() {
126 let dir = tempfile::tempdir().unwrap();
127 let home = dir.path();
128 assert!(
129 !always_allowed_in(home, "korp", "area54"),
130 "nothing until the user says so"
131 );
132 remember_in(home, "Korp", "area54").unwrap();
133 remember_in(home, "korp", "area54").unwrap();
134 assert!(always_allowed_in(home, "korp", "area54"));
135 assert!(
136 always_allowed_in(home, "KORP", " area54 "),
137 "agent ids ignore case"
138 );
139 assert!(
140 !always_allowed_in(home, "korp", "Area54"),
141 "host names are exact"
142 );
143 assert!(
144 !always_allowed_in(home, "agentx", "area54"),
145 "another agent is asked"
146 );
147 assert!(
148 !always_allowed_in(home, "korp", "nas"),
149 "another host is asked"
150 );
151 let text = std::fs::read_to_string(home.join(FILE_NAME)).unwrap();
152 assert_eq!(text.matches("area54").count(), 1, "stored once: {text}");
153 }
154
155 #[test]
156 fn revoking_asks_again_and_leaves_the_others() {
157 let dir = tempfile::tempdir().unwrap();
158 let home = dir.path();
159 remember_in(home, "korp", "area54").unwrap();
160 remember_in(home, "korp", "nas").unwrap();
161 remember_in(home, "agentx", "area54").unwrap();
162 assert!(revoke_in(home, "KORP", " area54 ").unwrap());
163 assert!(!always_allowed_in(home, "korp", "area54"));
164 assert!(always_allowed_in(home, "korp", "nas"), "another host keeps its grant");
165 assert!(always_allowed_in(home, "agentx", "area54"), "another agent keeps its grant");
166 assert!(!revoke_in(home, "korp", "area54").unwrap(), "nothing left to revoke");
167 }
168
169 #[test]
170 fn an_unreadable_file_grants_nothing() {
171 let dir = tempfile::tempdir().unwrap();
172 std::fs::write(dir.path().join(FILE_NAME), "not json").unwrap();
173 assert!(!always_allowed_in(dir.path(), "korp", "area54"));
174 }
175}