Skip to main content

agentmux_srv_core/backend/remote/
agent_access.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Which agents the user has let use which SSH hosts (spec §8.2 of
5//! SPEC_REMOTE_TERMINALS_AND_DURABLE_SESSIONS_2026_10_02.md).
6//!
7//! An agent on another machine uses the user's SSH identity, so access is
8//! opt-in per agent and host: the first time, the user is asked (which agent,
9//! which host, what it wants to run) and answers once or always. "Always" is
10//! remembered here, in `ssh-agent-access.json` under AgentMux's config dir;
11//! "once" is not stored at all. A host is its connection name, trimmed and
12//! compared exactly (an ssh config alias is case-sensitive); an agent id
13//! ignores case.
14
15use 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
34/// Serializes read-modify-write of the file within this srv.
35static 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
48/// Whether the user said "always" for `agent` on `connection`.
49pub 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
57/// Remember "always" for `agent` on `connection`.
58pub 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
75/// [`always_allowed_in`] under this instance's config dir.
76pub 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
84/// Forget "always" for `agent` on `connection` (Remotes' Revoke,
85/// SPEC_REMOTES_PANE_2026_10_05.md §4.10): the agent is asked again on its
86/// next use of the host. `false` if there was no such grant.
87pub 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
104/// [`revoke_in`] under this instance's config dir.
105pub fn revoke(agent: &str, connection: &str) -> std::io::Result<bool> {
106    revoke_in(&crate::backend::base::get_mux_config_dir(), agent, connection)
107}
108
109/// Every "always" grant, as `(agent, connection)` (Connectors → Remotes lists them
110/// per host, SPEC_REMOTES_PANE_2026_10_05.md §4.10).
111pub 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}