Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SocketAddr:V4, not custom inputs for HostServer address #27

Merged
merged 5 commits into from
Jun 15, 2022
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
16 changes: 0 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion qos-host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ qos-core = { path = "../qos-core" }
# Third party
axum = { version = "0.5.4", features = ["http1"], default-features = false }
tokio = { version = "1.18", features = ["macros", "rt-multi-thread"], default-features = false }
regex = { version = "1", default-features = false, features = ["std", "unicode"] }
borsh = { version = "0.9" }
80 changes: 14 additions & 66 deletions qos-host/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//! Command line interface for creating a host server and helpers for parsing
//! host specific command line arguments.
//! Command line interface for running a QOS Host server.

use std::{env, net::SocketAddr};
use std::{
env,
net::{IpAddr, Ipv4Addr, SocketAddr},
};

use qos_core::cli::EnclaveOptions;
use regex::Regex;

use crate::HostServer;

const IP_REGEX: &str = r"^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$";

/// CLI options for starting a host server
/// CLI options for starting a host server.
#[derive(Clone, Debug, PartialEq)]
pub struct HostServerOptions {
enclave: EnclaveOptions,
Expand All @@ -26,7 +25,7 @@ impl HostServerOptions {
/// CLI options for host IP address and Port.
#[derive(Default, Clone, Copy, Debug, PartialEq)]
pub struct HostOptions {
ip: Option<[u8; 4]>,
ip: Option<Ipv4Addr>,
port: Option<u16>,
}

Expand All @@ -44,11 +43,8 @@ impl HostOptions {
/// Panics if the url cannot be parsed from options
#[must_use]
pub fn url(&self) -> String {
if let Self { ip: Some(ip), port: Some(port) } = self {
return format!(
"http://{}.{}.{}.{}:{}",
ip[0], ip[1], ip[2], ip[3], port
);
if let Self { ip: Some(ip), port: Some(port) } = *self {
return format!("http://{}:{}", ip, port);
}

panic!("Couldn't parse URL from options.")
Expand All @@ -69,24 +65,9 @@ impl HostOptions {

fn parse_ip(&mut self, cmd: &str, arg: &str) {
if cmd == "--host-ip" {
let re = Regex::new(IP_REGEX)
.expect("Could not parse value from `--host-ip`");
let mut iter = re.captures_iter(arg);

let parse = |string: &str| {
string
.to_string()
.parse::<u8>()
.expect("Could not parse value from `--host-ip`")
};

if let Some(cap) = iter.next() {
let ip1 = parse(&cap[1]);
let ip2 = parse(&cap[2]);
let ip3 = parse(&cap[3]);
let ip4 = parse(&cap[4]);
self.ip = Some([ip1, ip2, ip3, ip4]);
}
self.ip = Some(
arg.parse().expect("Could not parse value from `--host-ip`"),
);
}
}

Expand Down Expand Up @@ -122,7 +103,7 @@ impl CLI {
let options = parse_args(&args);
let addr = host_addr_from_options(options.host);
let enclave_addr = options.enclave.addr();
HostServer::new_with_socket_addr(enclave_addr, addr).serve().await;
HostServer::new(enclave_addr, addr).serve().await;
}
}

Expand All @@ -141,41 +122,8 @@ fn parse_args(args: &[String]) -> HostServerOptions {

fn host_addr_from_options(options: HostOptions) -> SocketAddr {
if let HostOptions { ip: Some(ip), port: Some(port), .. } = options {
SocketAddr::from((ip, port))
SocketAddr::new(IpAddr::V4(ip), port)
} else {
panic!("Invalid host address options")
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn parse_ip_with_regex() {
expect_ip("1.1.1.1", [1, 1, 1, 1]);
expect_ip("1.2.3.4", [1, 2, 3, 4]);
expect_ip("12.34.56.78", [12, 34, 56, 78]);
expect_ip("111.222.244.255", [111, 222, 244, 255]);
}

#[test]
#[should_panic]
fn no_parse_ip() {
expect_ip("something111.222.244.255", [111, 222, 244, 255]);
}

fn expect_ip(arg: &str, expected: [u8; 4]) {
let mut options = HostOptions::new();
options.parse_ip("--host-ip", arg);

if let Some(ip) = options.ip {
assert_eq!(ip[0], expected[0]);
assert_eq!(ip[1], expected[1]);
assert_eq!(ip[2], expected[2]);
assert_eq!(ip[3], expected[3]);
} else {
panic!("Couldn't parse ip address");
}
}
}
14 changes: 3 additions & 11 deletions qos-host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,10 @@ pub struct HostServer {
}

impl HostServer {
/// Create a new [`HostServer`].
/// Create a new [`HostServer`]. See [`Self::serve`] for starting the
/// server.
#[must_use]
pub fn new(enclave_addr: SocketAddress, ip: [u8; 4], port: u16) -> Self {
Self { addr: SocketAddr::from((ip, port)), enclave_addr }
}

/// Create a new [`HostServer`].
#[must_use]
pub fn new_with_socket_addr(
enclave_addr: SocketAddress,
addr: SocketAddr,
) -> Self {
pub fn new(enclave_addr: SocketAddress, addr: SocketAddr) -> Self {
Self { enclave_addr, addr }
}

Expand Down
11 changes: 6 additions & 5 deletions qos-test/tests/load.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

use qos_core::{
io::SocketAddress,
protocol::{Executor, Load, MockNsm, ProtocolMsg, SignatureWithPubKey},
Expand Down Expand Up @@ -55,11 +57,10 @@ fn load_e2e() {
//
let enclave_addr = SocketAddress::new_unix("./rsa_verify_payload.sock");
let enclave_addr2 = enclave_addr.clone();
let ip = [127, 0, 0, 1];
let ip = Ipv4Addr::from([127, 0, 0, 1]);
let port = 3001; // Use a unique port so we don't collide with other tests
let url =
format!("http://{}.{}.{}.{}:{}", ip[0], ip[1], ip[2], ip[3], port);
let message_url = format!("{}/{}", url, "message");
let socket_addr = SocketAddrV4::new(ip, port);
let message_url = format!("http://{}/message", socket_addr);
let pivot_file = "./verification.pivot".to_string();
let secret_file = "./verification.secret".to_string();
let ephemeral_file = "./verification.ephemeral".to_string();
Expand All @@ -81,7 +82,7 @@ fn load_e2e() {
});

std::thread::spawn(move || {
let host = HostServer::new(enclave_addr2, ip, port);
let host = HostServer::new(enclave_addr2, SocketAddr::V4(socket_addr));

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down
15 changes: 10 additions & 5 deletions qos-test/tests/provision.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{fs::File, io::Read, path::Path};
use std::{
fs::File,
io::Read,
net::{Ipv4Addr, SocketAddr, SocketAddrV4},
path::Path,
};

use qos_core::{
io::SocketAddress,
Expand All @@ -12,10 +17,10 @@ async fn provision_e2e() {
let usock = "./provisions_e2e.sock";
let enclave_addr = SocketAddress::new_unix(usock);
let enclave_addr2 = enclave_addr.clone();
let ip = [127, 0, 0, 1];
let ip = Ipv4Addr::from([127u8, 0, 0, 1]);
let port = 3002;
let url =
format!("http://{}.{}.{}.{}:{}", ip[0], ip[1], ip[2], ip[3], port);
let socket_addr = SocketAddrV4::new(ip, port);
let url = format!("http://{}", socket_addr);
let health_url = format!("{}/{}", url, "health");
let message_url = format!("{}/{}", url, "message");

Expand All @@ -40,7 +45,7 @@ async fn provision_e2e() {
});

std::thread::spawn(move || {
let host = HostServer::new(enclave_addr2, ip, port);
let host = HostServer::new(enclave_addr2, SocketAddr::V4(socket_addr));

let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
Expand Down