auths_core/ports/ssh_agent.rs
1//! Port trait for system SSH agent key registration.
2
3use std::path::Path;
4
5/// Domain error for system SSH agent operations.
6///
7/// Args:
8/// * `CommandFailed` — The ssh-add command ran but returned a non-zero exit code.
9/// * `NotAvailable` — The SSH agent is not running or cannot be reached.
10/// * `IoError` — A filesystem or process I/O error occurred.
11///
12/// Usage:
13/// ```ignore
14/// use auths_core::ports::ssh_agent::SshAgentError;
15///
16/// fn handle(err: SshAgentError) {
17/// match err {
18/// SshAgentError::CommandFailed(msg) => eprintln!("ssh-add failed: {msg}"),
19/// SshAgentError::NotAvailable(msg) => eprintln!("agent unavailable: {msg}"),
20/// SshAgentError::IoError(msg) => eprintln!("I/O error: {msg}"),
21/// }
22/// }
23/// ```
24#[derive(Debug, thiserror::Error)]
25pub enum SshAgentError {
26 /// The ssh-add command ran but returned a failure status.
27 #[error("ssh-add command failed: {0}")]
28 CommandFailed(String),
29
30 /// The system SSH agent is not available.
31 #[error("SSH agent not available: {0}")]
32 NotAvailable(String),
33
34 /// A filesystem or process I/O error.
35 #[error("I/O error: {0}")]
36 IoError(String),
37}
38
39/// Registers key files with the system SSH agent.
40///
41/// Implementations wrap platform-specific mechanisms (e.g., `ssh-add` on
42/// macOS/Linux). Domain code calls this trait without knowing the transport.
43///
44/// Usage:
45/// ```ignore
46/// use auths_core::ports::ssh_agent::SshAgentPort;
47///
48/// fn register(agent: &dyn SshAgentPort, key_path: &Path) {
49/// agent.register_key(key_path).unwrap();
50/// }
51/// ```
52pub trait SshAgentPort: Send + Sync {
53 /// Registers a PEM key file with the system SSH agent.
54 ///
55 /// Args:
56 /// * `key_path`: Path to a temporary PEM file to add via ssh-add.
57 ///
58 /// Usage:
59 /// ```ignore
60 /// agent.register_key(Path::new("/tmp/auths-key-abc.pem"))?;
61 /// ```
62 fn register_key(&self, key_path: &Path) -> Result<(), SshAgentError>;
63}