Skip to content

Commit

Permalink
Fix unix uri parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
blazzy committed Jul 1, 2024
1 parent dd9e636 commit c80faf3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ hyper = { version = "^1.3.1", features = ["full"] }

[dependencies]
async-trait = "0.1.80"
http = "1.1.0"
hyper = { workspace = true }
hyper-util = { version = "0.1.5", features = ["client-legacy", "http1"] }
nix = { version = "0.29.0", features = ["user"] }
Expand All @@ -36,4 +37,5 @@ tokio = "1.38.0"
tower-service = "0.3.2"

[dev-dependencies]
assert_matches = "1.5.0"
tokio-test = "0.4.4"
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ pub enum Error {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Invalid URI: {0}")]
InvalidUri(#[from] hyper::http::uri::InvalidUri),
InvalidUri(#[from] http::uri::InvalidUri),
#[error("SSH Authentication Failed")]
AuthenticationFailed,

#[error("Missing scheme in URI")]
#[error("Missing or unkown scheme in URI")]
InvalidScheme,
#[error("Missing SSH user name in URI")]
SshUserNameRequired,
Expand Down
25 changes: 10 additions & 15 deletions src/podman_rest_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ pub struct Config {

impl PodmanRestClient {
pub async fn new(config: Config) -> Result<PodmanRestClient, Error> {
let uri = hyper::Uri::from_str(&config.uri)?;
let (scheme, rest) = config.uri.split_once("://").ok_or(Error::InvalidScheme)?;

if let Some(scheme) = uri.scheme() {
match scheme.as_str() {
"unix" => PodmanRestClient::new_unix(uri).await,
"ssh" => PodmanRestClient::new_ssh(uri, config.identity_file).await,
_ => Err(Error::InvalidScheme),
}
} else {
Err(Error::InvalidScheme)
match scheme {
"unix" => PodmanRestClient::new_unix(rest).await,
"ssh" => PodmanRestClient::new_ssh(config.uri, config.identity_file).await,
_ => Err(Error::InvalidScheme),
}
}

pub async fn new_ssh(
uri: hyper::Uri,
key_path: Option<String>,
) -> Result<PodmanRestClient, Error> {
pub async fn new_ssh(uri: String, key_path: Option<String>) -> Result<PodmanRestClient, Error> {
let uri = hyper::Uri::from_str(&uri)?;

let user_name = uri.authority().and_then(|authority| {
if let Some((user_name, _)) = authority.to_string().split_once('@') {
Some(user_name.to_string())
Expand All @@ -59,8 +54,8 @@ impl PodmanRestClient {
PodmanRestClient::new_connector(connector).await
}

pub async fn new_unix(uri: hyper::Uri) -> Result<PodmanRestClient, Error> {
let connector = unix_socket::UnixConnector::new(uri.to_string());
pub async fn new_unix(path: &str) -> Result<PodmanRestClient, Error> {
let connector = unix_socket::UnixConnector::new(path);

PodmanRestClient::new_connector(connector).await
}
Expand Down
6 changes: 4 additions & 2 deletions src/unix_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ pub(crate) struct UnixConnector {
pub struct UnixStreamWrapper(pub UnixStream);

impl UnixConnector {
pub fn new(path: String) -> UnixConnector {
UnixConnector { path }
pub fn new(path: &str) -> UnixConnector {
UnixConnector {
path: path.to_string(),
}
}
}

Expand Down
31 changes: 31 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use podman_rest_client::{guess_configuration, Config, PodmanRestClient};
use podman_rest_client::Error;
use assert_matches::assert_matches;

#[tokio::test]
async fn it_connects_to_a_unix_socket() {
let client = PodmanRestClient::new(Config {
uri: "unix:///tmp/sock".to_string(),
identity_file: None,
})
.await;
assert!(client.is_ok());
}

#[tokio::test]
async fn it_can_list_images() {
let config = guess_configuration().await.unwrap();
let client = PodmanRestClient::new(config).await.unwrap();
let images = client.images_api().image_list_libpod(None, None).await;
assert!(images.is_ok());
}

#[tokio::test]
async fn it_errors_on_invalid_uris() {
let err = PodmanRestClient::new(Config {
uri: "tcp:///127.0.0.1:80".to_string(),
identity_file: None,
}).await.err().unwrap();

assert_matches!(err, Error::InvalidScheme);
}

0 comments on commit c80faf3

Please sign in to comment.