1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 16:23:44 +00:00

Use tracing in ssh_agent (#16455)

* [BEEEP][PM-255518] Use tracing for improved observability

* feedback dani-garcia: use DefaultVisitor

* set default log level

* convert printlns in objc crate

* convert printlns in autotype crate

* convert printlns in autostart crate

* convert printlns in core/password crate

* convert printlns in core/biometric crate

* convert printlns in napi crate

* convert log usage in macos provider crate

* convert existing log macros to tracing

* fix the cargo.toml sort lint errors

* Revert "fix the cargo.toml sort lint errors"

This reverts commit fd149ab697.

* fix the sort lint using correct cargo sort version

* feedback coltonhurst: more comments/clarity on behavior

* revert changes to ssh_agent

* Use tracing in ssh_agent
This commit is contained in:
neuronull
2025-09-30 06:33:32 -06:00
committed by GitHub
parent 2ccd841f58
commit 54a53a1c34
3 changed files with 32 additions and 35 deletions

View File

@@ -11,6 +11,7 @@ use bitwarden_russh::{
session_bind::SessionBindResult, session_bind::SessionBindResult,
ssh_agent::{self, SshKey}, ssh_agent::{self, SshKey},
}; };
use tracing::{error, info};
#[cfg_attr(target_os = "windows", path = "windows.rs")] #[cfg_attr(target_os = "windows", path = "windows.rs")]
#[cfg_attr(target_os = "macos", path = "unix.rs")] #[cfg_attr(target_os = "macos", path = "unix.rs")]
@@ -86,7 +87,7 @@ impl ssh_agent::Agent<peerinfo::models::PeerInfo, BitwardenSshKey>
info: &peerinfo::models::PeerInfo, info: &peerinfo::models::PeerInfo,
) -> bool { ) -> bool {
if !self.is_running() { if !self.is_running() {
println!("[BitwardenDesktopAgent] Agent is not running, but tried to call confirm"); error!("Agent is not running, but tried to call confirm");
return false; return false;
} }
@@ -94,7 +95,7 @@ impl ssh_agent::Agent<peerinfo::models::PeerInfo, BitwardenSshKey>
let request_data = match request_parser::parse_request(data) { let request_data = match request_parser::parse_request(data) {
Ok(data) => data, Ok(data) => data,
Err(e) => { Err(e) => {
println!("[SSH Agent] Error while parsing request: {e}"); error!(error = %e, "Error while parsing request");
return false; return false;
} }
}; };
@@ -105,12 +106,12 @@ impl ssh_agent::Agent<peerinfo::models::PeerInfo, BitwardenSshKey>
_ => None, _ => None,
}; };
println!( info!(
"[SSH Agent] Confirming request from application: {}, is_forwarding: {}, namespace: {}, host_key: {}", is_forwarding = %info.is_forwarding(),
namespace = ?namespace.as_ref(),
host_key = %STANDARD.encode(info.host_key()),
"Confirming request from application: {}",
info.process_name(), info.process_name(),
info.is_forwarding(),
namespace.clone().unwrap_or_default(),
STANDARD.encode(info.host_key())
); );
let mut rx_channel = self.get_ui_response_rx.lock().await.resubscribe(); let mut rx_channel = self.get_ui_response_rx.lock().await.resubscribe();
@@ -172,7 +173,7 @@ impl ssh_agent::Agent<peerinfo::models::PeerInfo, BitwardenSshKey>
connection_info.set_host_key(session_bind_info.host_key.clone()); connection_info.set_host_key(session_bind_info.host_key.clone());
} }
SessionBindResult::SignatureFailure => { SessionBindResult::SignatureFailure => {
println!("[BitwardenDesktopAgent] Session bind failure: Signature failure"); error!("Session bind failure: Signature failure");
} }
} }
} }
@@ -181,7 +182,7 @@ impl ssh_agent::Agent<peerinfo::models::PeerInfo, BitwardenSshKey>
impl BitwardenDesktopAgent<BitwardenSshKey> { impl BitwardenDesktopAgent<BitwardenSshKey> {
pub fn stop(&self) { pub fn stop(&self) {
if !self.is_running() { if !self.is_running() {
println!("[BitwardenDesktopAgent] Tried to stop agent while it is not running"); error!("Tried to stop agent while it is not running");
return; return;
} }
@@ -227,7 +228,7 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
); );
} }
Err(e) => { Err(e) => {
eprintln!("[SSH Agent Native Module] Error while parsing key: {e}"); error!(error=%e, "Error while parsing key");
} }
} }
} }
@@ -265,7 +266,7 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
fn get_request_id(&self) -> u32 { fn get_request_id(&self) -> u32 {
if !self.is_running() { if !self.is_running() {
println!("[BitwardenDesktopAgent] Agent is not running, but tried to get request id"); error!("Agent is not running, but tried to get request id");
return 0; return 0;
} }

View File

@@ -14,6 +14,7 @@ use tokio::{
select, select,
}; };
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{error, info};
use windows::Win32::{Foundation::HANDLE, System::Pipes::GetNamedPipeClientProcessId}; use windows::Win32::{Foundation::HANDLE, System::Pipes::GetNamedPipeClientProcessId};
use crate::ssh_agent::peerinfo::{self, models::PeerInfo}; use crate::ssh_agent::peerinfo::{self, models::PeerInfo};
@@ -31,42 +32,38 @@ impl NamedPipeServerStream {
pub fn new(cancellation_token: CancellationToken, is_running: Arc<AtomicBool>) -> Self { pub fn new(cancellation_token: CancellationToken, is_running: Arc<AtomicBool>) -> Self {
let (tx, rx) = tokio::sync::mpsc::channel(16); let (tx, rx) = tokio::sync::mpsc::channel(16);
tokio::spawn(async move { tokio::spawn(async move {
println!( info!("Creating named pipe server on {}", PIPE_NAME);
"[SSH Agent Native Module] Creating named pipe server on {}",
PIPE_NAME
);
let mut listener = match ServerOptions::new().create(PIPE_NAME) { let mut listener = match ServerOptions::new().create(PIPE_NAME) {
Ok(pipe) => pipe, Ok(pipe) => pipe,
Err(err) => { Err(e) => {
println!("[SSH Agent Native Module] Encountered an error creating the first pipe. The system's openssh service must likely be disabled"); error!(error = %e, "Encountered an error creating the first pipe. The system's openssh service must likely be disabled");
println!("[SSH Agent Natvie Module] error: {}", err);
cancellation_token.cancel(); cancellation_token.cancel();
is_running.store(false, Ordering::Relaxed); is_running.store(false, Ordering::Relaxed);
return; return;
} }
}; };
loop { loop {
println!("[SSH Agent Native Module] Waiting for connection"); info!("Waiting for connection");
select! { select! {
_ = cancellation_token.cancelled() => { _ = cancellation_token.cancelled() => {
println!("[SSH Agent Native Module] Cancellation token triggered, stopping named pipe server"); info!("[SSH Agent Native Module] Cancellation token triggered, stopping named pipe server");
break; break;
} }
_ = listener.connect() => { _ = listener.connect() => {
println!("[SSH Agent Native Module] Incoming connection"); info!("[SSH Agent Native Module] Incoming connection");
let handle = HANDLE(listener.as_raw_handle()); let handle = HANDLE(listener.as_raw_handle());
let mut pid = 0; let mut pid = 0;
unsafe { unsafe {
if let Err(e) = GetNamedPipeClientProcessId(handle, &mut pid) { if let Err(e) = GetNamedPipeClientProcessId(handle, &mut pid) {
println!("Error getting named pipe client process id {}", e); error!(error = %e, pid, "Faile to get named pipe client process id");
continue continue
} }
}; };
let peer_info = peerinfo::gather::get_peer_info(pid); let peer_info = peerinfo::gather::get_peer_info(pid);
let peer_info = match peer_info { let peer_info = match peer_info {
Err(err) => { Err(e) => {
println!("Failed getting process info for pid {} {}", pid, err); error!(error = %e, pid = %pid, "Failed getting process info");
continue continue
}, },
Ok(info) => info, Ok(info) => info,
@@ -76,8 +73,8 @@ impl NamedPipeServerStream {
listener = match ServerOptions::new().create(PIPE_NAME) { listener = match ServerOptions::new().create(PIPE_NAME) {
Ok(pipe) => pipe, Ok(pipe) => pipe,
Err(err) => { Err(e) => {
println!("[SSH Agent Native Module] Encountered an error creating a new pipe {}", err); error!(error = %e, "Encountered an error creating a new pipe");
cancellation_token.cancel(); cancellation_token.cancel();
is_running.store(false, Ordering::Relaxed); is_running.store(false, Ordering::Relaxed);
return; return;

View File

@@ -12,6 +12,7 @@ use bitwarden_russh::ssh_agent;
use homedir::my_home; use homedir::my_home;
use tokio::{net::UnixListener, sync::Mutex}; use tokio::{net::UnixListener, sync::Mutex};
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
use tracing::{error, info};
use crate::ssh_agent::peercred_unix_listener_stream::PeercredUnixListenerStream; use crate::ssh_agent::peercred_unix_listener_stream::PeercredUnixListenerStream;
@@ -36,14 +37,12 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
let ssh_path = match std::env::var("BITWARDEN_SSH_AUTH_SOCK") { let ssh_path = match std::env::var("BITWARDEN_SSH_AUTH_SOCK") {
Ok(path) => path, Ok(path) => path,
Err(_) => { Err(_) => {
println!("[SSH Agent Native Module] BITWARDEN_SSH_AUTH_SOCK not set, using default path"); info!("BITWARDEN_SSH_AUTH_SOCK not set, using default path");
let ssh_agent_directory = match my_home() { let ssh_agent_directory = match my_home() {
Ok(Some(home)) => home, Ok(Some(home)) => home,
_ => { _ => {
println!( info!("Could not determine home directory");
"[SSH Agent Native Module] Could not determine home directory"
);
return; return;
} }
}; };
@@ -65,10 +64,10 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
} }
}; };
println!("[SSH Agent Native Module] Starting SSH Agent server on {ssh_path:?}"); info!(socket = %ssh_path, "Starting SSH Agent server");
let sockname = std::path::Path::new(&ssh_path); let sockname = std::path::Path::new(&ssh_path);
if let Err(e) = std::fs::remove_file(sockname) { if let Err(e) = std::fs::remove_file(sockname) {
println!("[SSH Agent Native Module] Could not remove existing socket file: {e}"); error!(error = %e, socket = %ssh_path, "Could not remove existing socket file");
if e.kind() != std::io::ErrorKind::NotFound { if e.kind() != std::io::ErrorKind::NotFound {
return; return;
} }
@@ -79,7 +78,7 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
// Only the current user should be able to access the socket // Only the current user should be able to access the socket
if let Err(e) = fs::set_permissions(sockname, fs::Permissions::from_mode(0o600)) if let Err(e) = fs::set_permissions(sockname, fs::Permissions::from_mode(0o600))
{ {
println!("[SSH Agent Native Module] Could not set socket permissions: {e}"); error!(error = %e, socket = ?sockname, "Could not set socket permissions");
return; return;
} }
@@ -100,10 +99,10 @@ impl BitwardenDesktopAgent<BitwardenSshKey> {
cloned_agent_state cloned_agent_state
.is_running .is_running
.store(false, std::sync::atomic::Ordering::Relaxed); .store(false, std::sync::atomic::Ordering::Relaxed);
println!("[SSH Agent Native Module] SSH Agent server exited"); info!("SSH Agent server exited");
} }
Err(e) => { Err(e) => {
eprintln!("[SSH Agent Native Module] Error while starting agent server: {e}"); error!(error = %e, socket = %ssh_path, "Unable to start start agent server");
} }
} }
}); });