Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ log = "0.4.11"
derivative = "2.1.1"
zeroize = "1.1.0"
users = "0.10.0"
url = "2.2.0"

[dev-dependencies]
mockstream = "0.0.3"
Expand Down
2 changes: 1 addition & 1 deletion src/core/basic_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl BasicClient {
/// [`set_default_provider`](#method.set_default_provider)
pub fn new(app_name: Option<String>) -> Result<Self> {
let mut client = BasicClient {
op_client: Default::default(),
op_client: OperationClient::new()?,
auth_data: Authentication::None,
implicit_provider: ProviderID::Core,
};
Expand Down
17 changes: 16 additions & 1 deletion src/core/ipc_handler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Types implementing an abstraction over IPC channels
use crate::error::Result;
use crate::error::{ClientErrorKind, Error, Result};
use std::io::{Read, Write};
use std::time::Duration;
use url::Url;

pub mod unix_socket;

/// Default timeout for client IPC requests.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);

/// This trait is created to allow the iterator returned by incoming to iterate over a trait object
/// that implements both Read and Write.
pub trait ReadWrite: Read + Write {}
Expand All @@ -23,3 +27,14 @@ pub trait Connect {
/// Set timeout for all produced streams.
fn set_timeout(&mut self, timeout: Option<Duration>);
}

/// Create an implementation of `Connect` from the socket URL
pub fn connector_from_url(socket_url: Url) -> Result<Box<dyn Connect + Send + Sync>> {
match socket_url.scheme() {
"unix" => Ok(Box::from(unix_socket::Handler::new(
socket_url.path().into(),
Some(DEFAULT_TIMEOUT),
)?)),
_ => Err(Error::Client(ClientErrorKind::InvalidSocketUrl)),
}
}
22 changes: 16 additions & 6 deletions src/core/ipc_handler/unix_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
//! Handler for Unix domain sockets
use super::{Connect, ReadWrite};
use crate::error::{ClientErrorKind, Result};
use crate::error::{ClientErrorKind, Error, Result};
use std::os::unix::fs::FileTypeExt;
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::time::Duration;

const DEFAULT_SOCKET_PATH: &str = "/run/parsec/parsec.sock";
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60);
/// Default socket path used by the service.
pub const DEFAULT_SOCKET_PATH: &str = "/run/parsec/parsec.sock";

/// IPC handler for Unix domain sockets
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -40,16 +41,25 @@ impl Connect for Handler {

impl Handler {
/// Create new client using given socket path and timeout duration
pub fn new(path: PathBuf, timeout: Option<Duration>) -> Self {
Handler { path, timeout }
pub fn new(path: PathBuf, timeout: Option<Duration>) -> Result<Self> {
if path.exists()
&& std::fs::metadata(&path)
.map_err(|_| Error::Client(ClientErrorKind::InvalidSocketAddress))?
.file_type()
.is_socket()
{
Ok(Handler { path, timeout })
} else {
Err(Error::Client(ClientErrorKind::InvalidSocketAddress))
}
Comment on lines +44 to +54
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

}
}

impl Default for Handler {
fn default() -> Self {
Handler {
path: DEFAULT_SOCKET_PATH.into(),
timeout: Some(DEFAULT_TIMEOUT),
timeout: Some(super::DEFAULT_TIMEOUT),
}
}
}
7 changes: 5 additions & 2 deletions src/core/operation_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ impl OperationClient {
/// seconds on reads and writes on the socket. It uses the version 1.0 wire protocol
/// to form requests, the direct authentication method and protobuf format as
/// content type.
pub fn new() -> OperationClient {
Default::default()
pub fn new() -> Result<OperationClient> {
Ok(OperationClient {
request_client: RequestClient::new()?,
..Default::default()
})
}

fn operation_to_request(
Expand Down
15 changes: 14 additions & 1 deletion src/core/request_client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright 2020 Contributors to the Parsec project.
// SPDX-License-Identifier: Apache-2.0
//! Request-level client
use super::ipc_handler::{unix_socket, Connect};
use super::ipc_handler::{self, unix_socket, Connect};
use crate::error::{ClientErrorKind, Result};
use derivative::Derivative;
use parsec_interface::requests::{Request, Response};
use std::env;
use std::time::Duration;

const DEFAULT_MAX_BODY_SIZE: usize = usize::max_value();
Expand All @@ -29,6 +30,18 @@ pub struct RequestClient {
}

impl RequestClient {
/// Creates a new `RequestClient`, bootstrapping the socket
/// location.
pub fn new() -> Result<Self> {
Ok(RequestClient {
ipc_handler: ipc_handler::connector_from_url(url::Url::parse(
&env::var("PARSEC_SERVICE_ENDPOINT")
.unwrap_or(format!("unix:{}", unix_socket::DEFAULT_SOCKET_PATH)),
)?)?,
max_body_size: DEFAULT_MAX_BODY_SIZE,
})
}

/// Send a request and get a response.
pub fn process_request(&self, request: Request) -> Result<Response> {
// Try to connect once, wait for a timeout until trying again.
Expand Down
14 changes: 13 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub enum ClientErrorKind {
MissingParam,
/// The requested resource was not found.
NotFound,
/// The socket address provided is not valid
InvalidSocketAddress,
/// The socket URL is invalid
InvalidSocketUrl,
}

impl From<ClientErrorKind> for Error {
Expand All @@ -52,6 +56,12 @@ impl From<ClientErrorKind> for Error {
}
}

impl From<url::ParseError> for Error {
fn from(_: url::ParseError) -> Self {
Error::Client(ClientErrorKind::InvalidSocketUrl)
}
}

impl fmt::Display for ClientErrorKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Expand All @@ -67,7 +77,9 @@ impl fmt::Display for ClientErrorKind {
ClientErrorKind::NoProvider => write!(f, "client is missing an implicit provider"),
ClientErrorKind::NoAuthenticator => write!(f, "service is not reporting any authenticators or none of the reported ones are supported by the client"),
ClientErrorKind::MissingParam => write!(f, "one of the `Option` parameters was required but was not provided"),
ClientErrorKind::NotFound => write!(f, "one of the resources required in the operation was not found")
ClientErrorKind::NotFound => write!(f, "one of the resources required in the operation was not found"),
ClientErrorKind::InvalidSocketAddress => write!(f, "the socket address provided in the URL is not valid"),
ClientErrorKind::InvalidSocketUrl => write!(f, "the socket URL is invalid"),
}
}
}
Expand Down