From f67ee1b5ad506128d383b86079ac08718e4e65bc Mon Sep 17 00:00:00 2001 From: neuronull <9162534+neuronull@users.noreply.github.com> Date: Thu, 4 Sep 2025 17:00:32 -0600 Subject: [PATCH] extract socket_paths logic --- .../desktop_native/core/src/ssh_agent/unix.rs | 66 ++++++++++--------- 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/apps/desktop/desktop_native/core/src/ssh_agent/unix.rs b/apps/desktop/desktop_native/core/src/ssh_agent/unix.rs index 1ef7cdb42f3..8def4727fc5 100644 --- a/apps/desktop/desktop_native/core/src/ssh_agent/unix.rs +++ b/apps/desktop/desktop_native/core/src/ssh_agent/unix.rs @@ -27,38 +27,10 @@ impl BitwardenDesktopAgent { auth_request_tx: tokio::sync::mpsc::Sender, auth_response_rx: Arc>>, ) -> Result { - // socket_paths are one of the following: - // - only the env var socket path if it is defined - // - or use the legacy $HOME path if flatpak build - // - or use both legacy and forward looking default paths. - // TODO: in PM, remove the legacy path logic and the for loop below. - let socket_paths = if let Ok(path) = std::env::var(ENV_BITWARDEN_SSH_AUTH_SOCK) { - vec![PathBuf::from(path)] - } else { - println!("[SSH Agent Native Module] {ENV_BITWARDEN_SSH_AUTH_SOCK} not set, using default path"); - - let mut paths = vec![get_legacy_default_socket_path()?]; - - // TODO: handle flatpak/snap - if !is_flatpak() { - let basedir = get_socket_basedir()?; - let socket_path = get_default_socket_path(basedir); - - // create the bitwarden subdir if needed - fs::create_dir_all( - socket_path - .parent() - .ok_or(anyhow!("Malformed default socket path."))?, - ) - .map_err(|e| anyhow!(format!("Error creating {socket_path:?}: {e}")))?; - - paths.push(socket_path); - } - paths - }; - let agent_state = BitwardenDesktopAgent::new(auth_request_tx, auth_response_rx); + let socket_paths = get_socket_paths()?; + for sock_path in &socket_paths { // if the socket is already present and wasn't cleanly removed during a previous // runtime, remove it before beginning anew. @@ -110,6 +82,40 @@ impl BitwardenDesktopAgent { } } +// socket_paths are one of the following: +// - only the env var socket path if it is defined +// - or use the legacy $HOME path if flatpak build +// - or use both legacy and forward looking default paths. +// TODO: in PM, remove the legacy path logic and the for loop below. +fn get_socket_paths() -> Result, anyhow::Error> { + if let Ok(path) = std::env::var(ENV_BITWARDEN_SSH_AUTH_SOCK) { + Ok(vec![PathBuf::from(path)]) + } else { + println!( + "[SSH Agent Native Module] {ENV_BITWARDEN_SSH_AUTH_SOCK} not set, using default path" + ); + + let mut paths = vec![get_legacy_default_socket_path()?]; + + // TODO: handle flatpak/snap + if !is_flatpak() { + let basedir = get_socket_basedir()?; + let socket_path = get_default_socket_path(basedir); + + // create the bitwarden subdir if needed + fs::create_dir_all( + socket_path + .parent() + .ok_or(anyhow!("Malformed default socket path."))?, + ) + .map_err(|e| anyhow!(format!("Error creating {socket_path:?}: {e}")))?; + + paths.push(socket_path); + } + Ok(paths) + } +} + fn is_flatpak() -> bool { std::env::var("container") == Ok("flatpak".to_string()) }