From 310ea4f876576ed3b10f490f455791d8388e093f Mon Sep 17 00:00:00 2001 From: Arnaud Brousseau Date: Thu, 27 Jun 2024 09:31:54 -0500 Subject: [PATCH] Minor comment tweaks, bumps borsh to v1 in qos_net --- src/Cargo.lock | 95 ++++----------------------------- src/qos_core/src/io/stream.rs | 4 +- src/qos_net/Cargo.toml | 2 +- src/qos_net/src/cli.rs | 6 +-- src/qos_net/src/proxy.rs | 29 +++++----- src/qos_net/src/proxy_stream.rs | 57 +++++++++----------- 6 files changed, 55 insertions(+), 138 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index d632434e..9e5d548b 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -52,17 +52,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "ahash" -version = "0.7.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "891477e0c6a8957309ee5c45a6368af3ae14bb510732d2684ffa19af310920f9" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "aho-corasick" version = "1.1.3" @@ -328,39 +317,16 @@ dependencies = [ "generic-array", ] -[[package]] -name = "borsh" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4114279215a005bc675e386011e594e1d9b800918cea18fcadadcce864a2046b" -dependencies = [ - "borsh-derive 0.10.3", - "hashbrown 0.12.3", -] - [[package]] name = "borsh" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a6362ed55def622cddc70a4746a68554d7b687713770de539e59a739b249f8ed" dependencies = [ - "borsh-derive 1.5.1", + "borsh-derive", "cfg_aliases", ] -[[package]] -name = "borsh-derive" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0754613691538d51f329cce9af41d7b7ca150bc973056f1156611489475f54f7" -dependencies = [ - "borsh-derive-internal", - "borsh-schema-derive-internal", - "proc-macro-crate 0.1.5", - "proc-macro2", - "syn 1.0.109", -] - [[package]] name = "borsh-derive" version = "1.5.1" @@ -368,35 +334,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3ef8005764f53cd4dca619f5bf64cafd4664dada50ece25e4d81de54c80cc0b" dependencies = [ "once_cell", - "proc-macro-crate 3.1.0", + "proc-macro-crate", "proc-macro2", "quote", "syn 2.0.68", "syn_derive", ] -[[package]] -name = "borsh-derive-internal" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "afb438156919598d2c7bad7e1c0adf3d26ed3840dbc010db1a882a65583ca2fb" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - -[[package]] -name = "borsh-schema-derive-internal" -version = "0.10.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "634205cc43f74a1b9046ef87c4540ebda95696ec0f315024860cad7c5b0f5ccd" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "bumpalo" version = "3.16.0" @@ -952,9 +896,6 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash", -] [[package]] name = "hashbrown" @@ -1198,7 +1139,7 @@ name = "integration" version = "0.1.0" dependencies = [ "aws-nitro-enclaves-nsm-api", - "borsh 1.5.1", + "borsh", "nix", "qos_client", "qos_core", @@ -1737,15 +1678,6 @@ dependencies = [ "elliptic-curve", ] -[[package]] -name = "proc-macro-crate" -version = "0.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d6ea3c4595b96363c13943497db34af4460fb474a95c43f4446ad341b8c9785" -dependencies = [ - "toml", -] - [[package]] name = "proc-macro-crate" version = "3.1.0" @@ -1793,7 +1725,7 @@ name = "qos_client" version = "0.1.0" dependencies = [ "aws-nitro-enclaves-nsm-api", - "borsh 1.5.1", + "borsh", "lazy_static", "p256 0.12.0", "qos_core", @@ -1816,7 +1748,7 @@ name = "qos_core" version = "0.1.0" dependencies = [ "aws-nitro-enclaves-nsm-api", - "borsh 1.5.1", + "borsh", "libc", "nix", "qos_crypto", @@ -1851,7 +1783,7 @@ name = "qos_host" version = "0.1.0" dependencies = [ "axum", - "borsh 1.5.1", + "borsh", "qos_core", "qos_hex", "serde", @@ -1863,7 +1795,7 @@ dependencies = [ name = "qos_net" version = "0.1.0" dependencies = [ - "borsh 0.10.3", + "borsh", "hickory-resolver", "qos_core", "qos_test_primitives", @@ -1879,7 +1811,7 @@ version = "0.1.0" dependencies = [ "aws-nitro-enclaves-cose", "aws-nitro-enclaves-nsm-api", - "borsh 1.5.1", + "borsh", "hex-literal", "p384 0.12.0", "qos_hex", @@ -1895,7 +1827,7 @@ name = "qos_p256" version = "0.1.0" dependencies = [ "aes-gcm", - "borsh 1.5.1", + "borsh", "hkdf", "hmac", "p256 0.12.0", @@ -2514,15 +2446,6 @@ dependencies = [ "syn 2.0.68", ] -[[package]] -name = "toml" -version = "0.5.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" -dependencies = [ - "serde", -] - [[package]] name = "toml_datetime" version = "0.6.6" diff --git a/src/qos_core/src/io/stream.rs b/src/qos_core/src/io/stream.rs index 058f26ae..a695694a 100644 --- a/src/qos_core/src/io/stream.rs +++ b/src/qos_core/src/io/stream.rs @@ -442,8 +442,8 @@ mod test { fn stream_implements_read_write_traits() { let socket_server_path = "./stream_implements_read_write_traits.sock"; - // Start a barebone socket server which replies "Roger that." to any - // incoming request + // Start a simple socket server which replies "PONG" to any incoming + // request let mut server = HarakiriPongServer::new(socket_server_path.to_string()); thread::spawn(move || { diff --git a/src/qos_net/Cargo.toml b/src/qos_net/Cargo.toml index 8643ede2..ec09b49d 100644 --- a/src/qos_net/Cargo.toml +++ b/src/qos_net/Cargo.toml @@ -7,7 +7,7 @@ publish = false [dependencies] qos_core = { path = "../qos_core", default-features = false } -borsh = { version = "0.10" } +borsh = { version = "1.0", features = ["std", "derive"] , default-features = false} serde = { version = "1", features = ["derive"], default-features = false } hickory-resolver = { version = "0.24.1", features = ["tokio-runtime"], default-features = false, optional = true} rand = { version = "0.8.5", default-features = false, optional = true } diff --git a/src/qos_net/src/cli.rs b/src/qos_net/src/cli.rs index f5345880..14cc9f99 100644 --- a/src/qos_net/src/cli.rs +++ b/src/qos_net/src/cli.rs @@ -17,7 +17,7 @@ pub const PORT: &str = "port"; /// "usock" pub const USOCK: &str = "usock"; -/// CLI options for starting up the enclave server. +/// CLI options for starting up the proxy. #[derive(Default, Clone, Debug, PartialEq)] struct ProxyOpts { parsed: Parser, @@ -55,10 +55,10 @@ impl ProxyOpts { } } -/// Enclave server CLI. +/// Proxy CLI. pub struct CLI; impl CLI { - /// Execute the enclave server CLI with the environment args. + /// Execute the enclave proxy CLI with the environment args. pub fn execute() { let mut args: Vec = env::args().collect(); let opts = ProxyOpts::new(&mut args); diff --git a/src/qos_net/src/proxy.rs b/src/qos_net/src/proxy.rs index 437d5287..6018ece2 100644 --- a/src/qos_net/src/proxy.rs +++ b/src/qos_net/src/proxy.rs @@ -1,7 +1,7 @@ //! Protocol proxy for our remote QOS net proxy use std::io::{Read, Write}; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use qos_core::server; use crate::{ @@ -214,9 +214,10 @@ impl Proxy { impl server::RequestProcessor for Proxy { fn process(&mut self, req_bytes: Vec) -> Vec { if req_bytes.len() > MAX_ENCODED_MSG_LEN { - return ProxyMsg::ProxyError(QosNetError::OversizedPayload) - .try_to_vec() - .expect("ProtocolMsg can always be serialized. qed."); + return borsh::to_vec(&ProxyMsg::ProxyError( + QosNetError::OversizedPayload, + )) + .expect("ProtocolMsg can always be serialized. qed."); } let resp = match ProxyMsg::try_from_slice(&req_bytes) { @@ -278,7 +279,7 @@ impl server::RequestProcessor for Proxy { Err(_) => ProxyMsg::ProxyError(QosNetError::InvalidMsg), }; - resp.try_to_vec() + borsh::to_vec(&resp) .expect("Protocol message can always be serialized. qed!") } } @@ -294,7 +295,7 @@ mod test { #[test] fn simple_status_request() { let mut proxy = Proxy::new(); - let request = ProxyMsg::StatusRequest.try_to_vec().unwrap(); + let request = borsh::to_vec(&ProxyMsg::StatusRequest).unwrap(); let response = proxy.process(request); let msg = ProxyMsg::try_from_slice(&response).unwrap(); assert_eq!(msg, ProxyMsg::StatusResponse(0)); @@ -305,13 +306,12 @@ mod test { let mut proxy = Proxy::new(); assert_eq!(proxy.num_connections(), 0); - let request = ProxyMsg::ConnectByNameRequest { + let request = borsh::to_vec(&ProxyMsg::ConnectByNameRequest { hostname: "api.turnkey.com".to_string(), port: 443, dns_resolvers: vec!["8.8.8.8".to_string()], dns_port: 53, - } - .try_to_vec() + }) .unwrap(); let response = proxy.process(request); let msg = ProxyMsg::try_from_slice(&response).unwrap(); @@ -325,11 +325,10 @@ mod test { }; let http_request = "GET / HTTP/1.1\r\nHost: api.turnkey.com\r\nConnection: close\r\n\r\n".to_string(); - let request = ProxyMsg::WriteRequest { + let request = borsh::to_vec(&ProxyMsg::WriteRequest { connection_id, data: http_request.as_bytes().to_vec(), - } - .try_to_vec() + }) .unwrap(); let response = proxy.process(request); let msg: ProxyMsg = ProxyMsg::try_from_slice(&response).unwrap(); @@ -341,9 +340,9 @@ mod test { // Check that we now have an active connection assert_eq!(proxy.num_connections(), 1); - let request = ProxyMsg::ReadRequest { connection_id, size: 512 } - .try_to_vec() - .unwrap(); + let request = + borsh::to_vec(&ProxyMsg::ReadRequest { connection_id, size: 512 }) + .unwrap(); let response = proxy.process(request); let msg: ProxyMsg = ProxyMsg::try_from_slice(&response).unwrap(); let data = match msg { diff --git a/src/qos_net/src/proxy_stream.rs b/src/qos_net/src/proxy_stream.rs index b3e35fe1..4d9a158d 100644 --- a/src/qos_net/src/proxy_stream.rs +++ b/src/qos_net/src/proxy_stream.rs @@ -2,7 +2,7 @@ //! traits with `ProxyMsg`s. use std::io::{ErrorKind, Read, Write}; -use borsh::{BorshDeserialize, BorshSerialize}; +use borsh::BorshDeserialize; use qos_core::io::{SocketAddress, Stream, TimeVal}; use crate::{error::QosNetError, proxy_msg::ProxyMsg}; @@ -49,13 +49,12 @@ impl ProxyStream { dns_port: u16, ) -> Result { let stream = Stream::connect(addr, timeout)?; - let req = ProxyMsg::ConnectByNameRequest { + let req = borsh::to_vec(&ProxyMsg::ConnectByNameRequest { hostname: hostname.clone(), port, dns_resolvers, dns_port, - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); stream.send(&req)?; let resp_bytes = stream.recv()?; @@ -93,8 +92,7 @@ impl ProxyStream { port: u16, ) -> Result { let stream: Stream = Stream::connect(addr, timeout)?; - let req = ProxyMsg::ConnectByIpRequest { ip, port } - .try_to_vec() + let req = borsh::to_vec(&ProxyMsg::ConnectByIpRequest { ip, port }) .expect("ProtocolMsg can always be serialized."); stream.send(&req)?; let resp_bytes = stream.recv()?; @@ -119,9 +117,10 @@ impl ProxyStream { /// Close the remote connection pub fn close(&mut self) -> Result<(), QosNetError> { let stream: Stream = Stream::connect(&self.addr, self.timeout)?; - let req = ProxyMsg::CloseRequest { connection_id: self.connection_id } - .try_to_vec() - .expect("ProtocolMsg can always be serialized."); + let req = borsh::to_vec(&ProxyMsg::CloseRequest { + connection_id: self.connection_id, + }) + .expect("ProtocolMsg can always be serialized."); stream.send(&req)?; let resp_bytes = stream.recv()?; @@ -145,11 +144,10 @@ impl Read for ProxyStream { ) })?; - let req = ProxyMsg::ReadRequest { + let req = borsh::to_vec(&ProxyMsg::ReadRequest { connection_id: self.connection_id, size: buf.len(), - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); stream.send(&req).map_err(|e| { std::io::Error::new( @@ -210,11 +208,10 @@ impl Write for ProxyStream { ) })?; - let req = ProxyMsg::WriteRequest { + let req = borsh::to_vec(&ProxyMsg::WriteRequest { connection_id: self.connection_id, data: buf.to_vec(), - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); stream.send(&req).map_err(|e| { std::io::Error::new( @@ -262,9 +259,10 @@ impl Write for ProxyStream { ) })?; - let req = ProxyMsg::FlushRequest { connection_id: self.connection_id } - .try_to_vec() - .expect("ProtocolMsg can always be serialized."); + let req = borsh::to_vec(&ProxyMsg::FlushRequest { + connection_id: self.connection_id, + }) + .expect("ProtocolMsg can always be serialized."); stream.send(&req).map_err(|e| { std::io::Error::new( @@ -388,13 +386,12 @@ mod test { dns_resolvers: Vec, dns_port: u16, ) -> Result { - let req = ProxyMsg::ConnectByNameRequest { + let req = borsh::to_vec(&ProxyMsg::ConnectByNameRequest { hostname: hostname.clone(), port, dns_resolvers, dns_port, - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); let mut proxy = Box::new(Proxy::new()); let resp_bytes = proxy.process(req); @@ -429,11 +426,10 @@ mod test { impl Read for LocalStream { fn read(&mut self, buf: &mut [u8]) -> Result { - let req = ProxyMsg::ReadRequest { + let req = borsh::to_vec(&ProxyMsg::ReadRequest { connection_id: self.connection_id, size: buf.len(), - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); let resp_bytes = self.proxy.process(req); @@ -475,11 +471,10 @@ mod test { impl Write for LocalStream { fn write(&mut self, buf: &[u8]) -> Result { - let req = ProxyMsg::WriteRequest { + let req = borsh::to_vec(&ProxyMsg::WriteRequest { connection_id: self.connection_id, data: buf.to_vec(), - } - .try_to_vec() + }) .expect("ProtocolMsg can always be serialized."); let resp_bytes = self.proxy.process(req); @@ -507,10 +502,10 @@ mod test { } fn flush(&mut self) -> Result<(), std::io::Error> { - let req = - ProxyMsg::FlushRequest { connection_id: self.connection_id } - .try_to_vec() - .expect("ProtocolMsg can always be serialized."); + let req = borsh::to_vec(&ProxyMsg::FlushRequest { + connection_id: self.connection_id, + }) + .expect("ProtocolMsg can always be serialized."); let resp_bytes = self.proxy.process(req); match ProxyMsg::try_from_slice(&resp_bytes) {