From ed1635ddcb40786f9552b903bfc7ffd09a8c0dcb Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Tue, 12 Mar 2024 12:15:18 +0100 Subject: [PATCH 1/4] Relax the error type in handle Signed-off-by: Wiktor Kwapisiewicz --- README.md | 3 +-- examples/key_storage.rs | 8 ++------ src/agent.rs | 2 +- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 224565b..765af91 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,6 @@ This example starts listening on a Unix socket `ssh-agent.sock` and processes re use tokio::net::UnixListener; use ssh_agent_lib::agent::{Session, Agent}; -use ssh_agent_lib::error::AgentError; use ssh_agent_lib::proto::message::Message; #[derive(Default)] @@ -23,7 +22,7 @@ struct MyAgent; #[ssh_agent_lib::async_trait] impl Session for MyAgent { - async fn handle(&mut self, message: Message) -> Result { + async fn handle(&mut self, message: Message) -> Result> { match message { Message::SignRequest(request) => { // get the signature by signing `request.data` diff --git a/examples/key_storage.rs b/examples/key_storage.rs index 88f455f..5abb2da 100644 --- a/examples/key_storage.rs +++ b/examples/key_storage.rs @@ -3,7 +3,6 @@ use log::info; use tokio::net::UnixListener; use ssh_agent_lib::agent::{Agent, Session}; -use ssh_agent_lib::error::AgentError; use ssh_agent_lib::proto::message::{self, Message, SignRequest}; use ssh_agent_lib::proto::private_key::{PrivateKey, RsaPrivateKey}; use ssh_agent_lib::proto::public_key::PublicKey; @@ -148,11 +147,8 @@ impl KeyStorage { #[async_trait] impl Session for KeyStorage { - async fn handle(&mut self, message: Message) -> Result { - self.handle_message(message).or_else(|error| { - println!("Error handling message - {:?}", error); - Ok(Message::Failure) - }) + async fn handle(&mut self, message: Message) -> Result> { + self.handle_message(message) } } diff --git a/src/agent.rs b/src/agent.rs index 7cd2a33..c894e1d 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -77,7 +77,7 @@ impl ListeningSocket for TcpListener { #[async_trait] pub trait Session: 'static + Sync + Send + Sized { - async fn handle(&mut self, message: Message) -> Result; + async fn handle(&mut self, message: Message) -> Result>; async fn handle_socket( &mut self, From c08cb6bfc466d9ce976a4236524242760406722e Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Tue, 12 Mar 2024 12:20:03 +0100 Subject: [PATCH 2/4] Return `Message::Failure` to SSH client to gracefully inform about error Without this change the client informs about error in communication: ``` $ ssh-add -s test Enter passphrase for PKCS#11: Could not add card "test": communication with agent failed ``` After this change the error informs about agent refusing the operation: ``` $ ssh-add -s test Enter passphrase for PKCS#11: Could not add card "test": agent refused operation ``` Signed-off-by: Wiktor Kwapisiewicz --- src/agent.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index c894e1d..671a97b 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -88,10 +88,13 @@ pub trait Session: 'static + Sync + Send + Sized { { loop { if let Some(incoming_message) = adapter.try_next().await? { - let response = self.handle(incoming_message).await.map_err(|e| { - error!("Error handling message; error = {:?}", e); - AgentError::User - })?; + let response = match self.handle(incoming_message).await { + Ok(message) => message, + Err(e) => { + error!("Error handling message; error = {:?}", e); + Message::Failure + } + }; adapter.send(response).await?; } else { From 022a4ef6e285ead9d9d181fb133e8b818b564451 Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Tue, 12 Mar 2024 12:26:41 +0100 Subject: [PATCH 3/4] Shorten log lines Signed-off-by: Wiktor Kwapisiewicz --- src/agent.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/agent.rs b/src/agent.rs index 671a97b..64fc149 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -91,7 +91,7 @@ pub trait Session: 'static + Sync + Send + Sized { let response = match self.handle(incoming_message).await { Ok(message) => message, Err(e) => { - error!("Error handling message; error = {:?}", e); + error!("Error handling message: {:?}", e); Message::Failure } }; @@ -121,12 +121,12 @@ pub trait Agent: 'static + Sync + Send + Sized { tokio::spawn(async move { let adapter = Framed::new(socket, MessageCodec); if let Err(e) = session.handle_socket::(adapter).await { - error!("Agent protocol error; error = {:?}", e); + error!("Agent protocol error: {:?}", e); } }); } Err(e) => { - error!("Failed to accept socket; error = {:?}", e); + error!("Failed to accept socket: {:?}", e); return Err(AgentError::IO(e)); } } From 678d32c9bb6085aa65eebf22f1671ac9dcdc0181 Mon Sep 17 00:00:00 2001 From: Wiktor Kwapisiewicz Date: Thu, 14 Mar 2024 09:57:39 +0100 Subject: [PATCH 4/4] Remove `AgentError::User` Signed-off-by: Wiktor Kwapisiewicz --- src/error.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/error.rs b/src/error.rs index 5e2293a..5d7b84f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -3,7 +3,6 @@ use std::io; #[derive(Debug)] pub enum AgentError { - User, Proto(ProtoError), IO(io::Error), } @@ -23,7 +22,6 @@ impl From for AgentError { impl std::fmt::Display for AgentError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - AgentError::User => write!(f, "Agent: User error"), AgentError::Proto(proto) => write!(f, "Agent: Protocol error: {}", proto), AgentError::IO(error) => write!(f, "Agent: I/O error: {}", error), }