crypto: Handle default `SSH_AUTH_SOCK` on Windows

On Windows, SSH Agents commonly use a named pipe with a global default
name. This patch is to give users a more streamline experience as they
will not need to set the environment variable manually anymore.
This commit is contained in:
Fred Gobry 2026-05-25 10:56:18 -04:00 committed by Lorenz Leutgeb
parent 067a9d13bf
commit 0a03f1c84e
1 changed files with 13 additions and 7 deletions

View File

@ -60,13 +60,19 @@ impl Agent {
pub fn connect() -> Result<Self, ConnectError> { pub fn connect() -> Result<Self, ConnectError> {
const SSH_AUTH_SOCK: &str = "SSH_AUTH_SOCK"; const SSH_AUTH_SOCK: &str = "SSH_AUTH_SOCK";
let path = let path = match std::env::var(SSH_AUTH_SOCK) {
PathBuf::from( Ok(path) => Ok(PathBuf::from(path)),
std::env::var(SSH_AUTH_SOCK).map_err(|err| ConnectError::EnvVar { #[cfg(windows)]
Err(VarError::NotPresent) => {
// Windows exposes the SSH Agent on a default named pipe.
const DEFAULT_SSH_AGENT_PIPE: &str = "\\\\.\\pipe\\openssh-ssh-agent";
Ok(PathBuf::from(DEFAULT_SSH_AGENT_PIPE))
}
Err(err) => Err(ConnectError::EnvVar {
var: SSH_AUTH_SOCK.to_string(), var: SSH_AUTH_SOCK.to_string(),
source: err, source: err,
})?, }),
); }?;
let client = Client::new( let client = Client::new(
Stream::connect(&path).map_err(|err| ConnectError::Agent(AgentError::IO(err)))?, Stream::connect(&path).map_err(|err| ConnectError::Agent(AgentError::IO(err)))?,