From f95f624ca8b74948549a4ef17dc6cb2ed2735027 Mon Sep 17 00:00:00 2001 From: Karan Janthe Date: Sat, 29 Jun 2024 19:53:33 +0530 Subject: [PATCH 1/4] fix: improve socket error handling --- src/docker.rs | 17 ++++++++++++++--- src/errors.rs | 3 +++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/docker.rs b/src/docker.rs index 46584fe8..ca7f3db3 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -4,6 +4,7 @@ use std::fs; use std::future::Future; #[cfg(feature = "ssl")] use std::io; +use std::path::Path; #[cfg(feature = "ssl")] use std::path::{Path, PathBuf}; use std::pin::Pin; @@ -641,12 +642,17 @@ impl Docker { timeout: u64, client_version: &ClientVersion, ) -> Result { + // Check if the socket file exists + if !std::path::Path::new(path).exists() { + return Err(Error::SocketNotFoundError(path.to_string())); + } + #[cfg(unix)] - let docker = Docker::connect_with_unix(path, timeout, client_version); + let docker = Docker::connect_with_unix(path, timeout, client_version)?; #[cfg(windows)] - let docker = Docker::connect_with_named_pipe(path, timeout, client_version); + let docker = Docker::connect_with_named_pipe(path, timeout, client_version)?; - docker + Ok(docker) } /// Connect using a Unix socket, a Windows named pipe, or via HTTP. @@ -749,6 +755,11 @@ impl Docker { ) -> Result { let client_addr = path.replacen("unix://", "", 1); + // check if the socket file exists and is accessible + if !Path::new(&client_addr).exists() { + return Err(Error::SocketNotFoundError(client_addr)); + } + let unix_connector = UnixConnector; let mut client_builder = Client::builder(TokioExecutor::new()); diff --git a/src/errors.rs b/src/errors.rs index 759ec079..3d4048d7 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -163,4 +163,7 @@ pub enum Error { /// The URI that was attempted to be connected to uri: String, }, + /// Error emitted when the Docker socket file is not found at the expected location. + #[error("Socket not found: {0}")] + SocketNotFoundError(String), } From dc0aaec6ba671c18c06e0656afb11fe23643f887 Mon Sep 17 00:00:00 2001 From: Karan Janthe Date: Mon, 1 Jul 2024 19:57:37 +0530 Subject: [PATCH 2/4] trimming the prefix before checking the socket path --- src/docker.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/docker.rs b/src/docker.rs index ca7f3db3..63419041 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -642,9 +642,14 @@ impl Docker { timeout: u64, client_version: &ClientVersion, ) -> Result { + // Remove the scheme if present + let clean_path = path + .trim_start_matches("unix://") + .trim_start_matches("npipe://"); + // Check if the socket file exists - if !std::path::Path::new(path).exists() { - return Err(Error::SocketNotFoundError(path.to_string())); + if !std::path::Path::new(clean_path).exists() { + return Err(Error::SocketNotFoundError(clean_path.to_string())); } #[cfg(unix)] From ce7964465bd710535d74c1398fac0d0b239a39ab Mon Sep 17 00:00:00 2001 From: Karan Janthe Date: Tue, 2 Jul 2024 18:49:34 +0530 Subject: [PATCH 3/4] removed duplicated path imports --- src/docker.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docker.rs b/src/docker.rs index 63419041..eaaa33a2 100644 --- a/src/docker.rs +++ b/src/docker.rs @@ -6,7 +6,7 @@ use std::future::Future; use std::io; use std::path::Path; #[cfg(feature = "ssl")] -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::pin::Pin; use std::sync::atomic::AtomicUsize; use std::sync::atomic::Ordering; From d49bf14fd380c60975e8d764163599571551672a Mon Sep 17 00:00:00 2001 From: Karan Janthe Date: Tue, 2 Jul 2024 20:23:34 +0530 Subject: [PATCH 4/4] added no_run to pass doctest --- src/grpc/driver/docker_container.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/grpc/driver/docker_container.rs b/src/grpc/driver/docker_container.rs index 38be760c..8257866d 100644 --- a/src/grpc/driver/docker_container.rs +++ b/src/grpc/driver/docker_container.rs @@ -118,7 +118,7 @@ impl Service for DockerContainer { /// /// ## Examples /// -/// ```rust +/// ```rust,no_run /// use bollard::grpc::driver::docker_container::DockerContainerBuilder; /// use bollard::Docker; ///