Make TLS an optional feature of simple-request

Removes 14 crates from the tree when compiling the message-queue client.

Also performs a non-intrusive cargo update.
This commit is contained in:
Luke Parker 2023-11-15 17:18:24 -05:00
parent 96f1d26f7a
commit d25e3d86a2
No known key found for this signature in database
5 changed files with 72 additions and 61 deletions

77
Cargo.lock generated
View file

@ -1236,9 +1236,9 @@ checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7"
[[package]] [[package]]
name = "crypto-bigint" name = "crypto-bigint"
version = "0.5.3" version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "740fe28e594155f10cfc383984cbefd529d7396050557148f79cb0f621204124" checksum = "28f85c3514d2a6e64160359b45a3918c3b4178bcbf4ae5d03ab2d02e521c479a"
dependencies = [ dependencies = [
"generic-array 0.14.7", "generic-array 0.14.7",
"rand_core", "rand_core",
@ -1785,9 +1785,9 @@ checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "elliptic-curve" name = "elliptic-curve"
version = "0.13.6" version = "0.13.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d97ca172ae9dc9f9b779a6e3a65d308f2af74e5b8c921299075bdb4a0370e914" checksum = "e9775b22bc152ad86a0cf23f0f348b884b26add12bf741e7ffc4d4ab2ab4d205"
dependencies = [ dependencies = [
"base16ct", "base16ct",
"crypto-bigint", "crypto-bigint",
@ -1881,9 +1881,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]] [[package]]
name = "errno" name = "errno"
version = "0.3.6" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c18ee0ed65a5f1f81cac6b1d213b69c35fa47d4252ad41f1486dbd8226fe36e" checksum = "f258a7194e7f7c2a7837a8913aeab7fd8c383457034fa20ce4dd3dcb813e8eb8"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys", "windows-sys",
@ -2176,9 +2176,9 @@ dependencies = [
[[package]] [[package]]
name = "fiat-crypto" name = "fiat-crypto"
version = "0.2.3" version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f69037fe1b785e84986b4f2cbcf647381876a00671d25ceef715d7812dd7e1dd" checksum = "53a56f0780318174bad1c127063fd0c5fdfb35398e3cd79ffaab931a6c79df80"
[[package]] [[package]]
name = "file-per-thread-logger" name = "file-per-thread-logger"
@ -2482,9 +2482,12 @@ dependencies = [
[[package]] [[package]]
name = "fs-err" name = "fs-err"
version = "2.9.0" version = "2.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0845fa252299212f0389d64ba26f34fa32cfe41588355f21ed507c59a0f64541" checksum = "fb5fd9bcbe8b1087cbd395b51498c01bc997cef73e778a80b77a811af5e2d29f"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "fs2" name = "fs2"
@ -2787,9 +2790,9 @@ dependencies = [
[[package]] [[package]]
name = "h2" name = "h2"
version = "0.3.21" version = "0.3.22"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "91fc23aa11be92976ef4729127f1a74adf36d8436f7816b185d18df956790833" checksum = "4d6250322ef6e60f93f9a2162799302cd6f68f79f6e5d85c8c16f14d1d958178"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -2797,7 +2800,7 @@ dependencies = [
"futures-sink", "futures-sink",
"futures-util", "futures-util",
"http", "http",
"indexmap 1.9.3", "indexmap 2.1.0",
"slab", "slab",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -2938,9 +2941,9 @@ dependencies = [
[[package]] [[package]]
name = "http" name = "http"
version = "0.2.10" version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f95b9abcae896730d42b78e09c155ed4ddf82c07b4de772c64aee5b2d8b7c150" checksum = "8947b1a6fad4393052c7ba1f4cd97bed3e953a95c79c92ad9b051a04611d9fbb"
dependencies = [ dependencies = [
"bytes", "bytes",
"fnv", "fnv",
@ -3270,7 +3273,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [ dependencies = [
"hermit-abi", "hermit-abi",
"rustix 0.38.21", "rustix 0.38.24",
"windows-sys", "windows-sys",
] ]
@ -3714,19 +3717,19 @@ dependencies = [
[[package]] [[package]]
name = "libp2p-identity" name = "libp2p-identity"
version = "0.2.7" version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdd6317441f361babc74c2989c6484eb0726045399b6648de039e1805ea96972" checksum = "999ec70441b2fb35355076726a6bc466c932e9bdc66f6a11c6c0aa17c7ab9be0"
dependencies = [ dependencies = [
"bs58", "bs58",
"ed25519-dalek", "ed25519-dalek",
"hkdf", "hkdf",
"log",
"multihash 0.19.1", "multihash 0.19.1",
"quick-protobuf", "quick-protobuf",
"rand", "rand",
"sha2", "sha2",
"thiserror", "thiserror",
"tracing",
"zeroize", "zeroize",
] ]
@ -4276,7 +4279,7 @@ version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64" checksum = "b2cffa4ad52c6f791f4f8b15f0c05f9824b2ced1160e88cc393d64fff9a8ac64"
dependencies = [ dependencies = [
"rustix 0.38.21", "rustix 0.38.24",
] ]
[[package]] [[package]]
@ -6182,9 +6185,9 @@ dependencies = [
[[package]] [[package]]
name = "rustix" name = "rustix"
version = "0.38.21" version = "0.38.24"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" checksum = "9ad981d6c340a49cdc40a1028d9c6084ec7e9fa33fcb839cab656a267071e234"
dependencies = [ dependencies = [
"bitflags 2.4.1", "bitflags 2.4.1",
"errno", "errno",
@ -6655,7 +6658,7 @@ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
"log", "log",
"rustix 0.38.21", "rustix 0.38.24",
"sc-allocator", "sc-allocator",
"sc-executor-common", "sc-executor-common",
"sp-runtime-interface", "sp-runtime-interface",
@ -9254,7 +9257,7 @@ dependencies = [
"cfg-if", "cfg-if",
"fastrand 2.0.1", "fastrand 2.0.1",
"redox_syscall 0.4.1", "redox_syscall 0.4.1",
"rustix 0.38.21", "rustix 0.38.24",
"windows-sys", "windows-sys",
] ]
@ -9273,9 +9276,9 @@ dependencies = [
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.3.0" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449"
dependencies = [ dependencies = [
"winapi-util", "winapi-util",
] ]
@ -10169,7 +10172,7 @@ dependencies = [
"directories-next", "directories-next",
"file-per-thread-logger", "file-per-thread-logger",
"log", "log",
"rustix 0.38.21", "rustix 0.38.24",
"serde", "serde",
"sha2", "sha2",
"toml 0.5.11", "toml 0.5.11",
@ -10251,7 +10254,7 @@ dependencies = [
"log", "log",
"object 0.31.1", "object 0.31.1",
"rustc-demangle", "rustc-demangle",
"rustix 0.38.21", "rustix 0.38.24",
"serde", "serde",
"target-lexicon", "target-lexicon",
"wasmtime-environ", "wasmtime-environ",
@ -10269,7 +10272,7 @@ checksum = "aef27ea6c34ef888030d15560037fe7ef27a5609fbbba8e1e3e41dc4245f5bb2"
dependencies = [ dependencies = [
"object 0.31.1", "object 0.31.1",
"once_cell", "once_cell",
"rustix 0.38.21", "rustix 0.38.24",
"wasmtime-versioned-export-macros", "wasmtime-versioned-export-macros",
] ]
@ -10301,7 +10304,7 @@ dependencies = [
"memoffset", "memoffset",
"paste", "paste",
"rand", "rand",
"rustix 0.38.21", "rustix 0.38.24",
"sptr", "sptr",
"wasm-encoder", "wasm-encoder",
"wasmtime-asm-macros", "wasmtime-asm-macros",
@ -10359,7 +10362,7 @@ dependencies = [
"either", "either",
"home", "home",
"once_cell", "once_cell",
"rustix 0.38.21", "rustix 0.38.24",
] ]
[[package]] [[package]]
@ -10633,18 +10636,18 @@ dependencies = [
[[package]] [[package]]
name = "zerocopy" name = "zerocopy"
version = "0.7.25" version = "0.7.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8cd369a67c0edfef15010f980c3cbe45d7f651deac2cd67ce097cd801de16557" checksum = "e97e415490559a91254a2979b4829267a57d2fcd741a98eee8b722fb57289aa0"
dependencies = [ dependencies = [
"zerocopy-derive", "zerocopy-derive",
] ]
[[package]] [[package]]
name = "zerocopy-derive" name = "zerocopy-derive"
version = "0.7.25" version = "0.7.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2f140bda219a26ccc0cdb03dba58af72590c53b22642577d88a927bc5c87d6b" checksum = "dd7e48ccf166952882ca8bd778a43502c64f33bf94c12ebe2a7f08e5a0f6689f"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -10653,9 +10656,9 @@ dependencies = [
[[package]] [[package]]
name = "zeroize" name = "zeroize"
version = "1.6.0" version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0956f1ba7c7909bfb66c2e9e4124ab6f6482560f6628b5aaeba39207c9aad9" checksum = "12a3946ecfc929b583800f4629b6c25b88ac6e92a40ea5670f77112a85d40a8b"
dependencies = [ dependencies = [
"zeroize_derive", "zeroize_derive",
] ]

View file

@ -25,7 +25,7 @@ frost = { package = "modular-frost", path = "../../crypto/frost", version = "0.8
hex = { version = "0.4", default-features = false, optional = true } hex = { version = "0.4", default-features = false, optional = true }
serde = { version = "1", default-features = false, features = ["derive"], optional = true } serde = { version = "1", default-features = false, features = ["derive"], optional = true }
serde_json = { version = "1", default-features = false, optional = true } serde_json = { version = "1", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["basic-auth"], optional = true } simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["tls", "basic-auth"], optional = true }
[dev-dependencies] [dev-dependencies]
secp256k1 = { version = "0.28", default-features = false, features = ["std"] } secp256k1 = { version = "0.28", default-features = false, features = ["std"] }

View file

@ -55,7 +55,7 @@ base58-monero = { version = "2", default-features = false, features = ["check"]
# Used for the provided HTTP RPC # Used for the provided HTTP RPC
digest_auth = { version = "0.3", default-features = false, optional = true } digest_auth = { version = "0.3", default-features = false, optional = true }
simple-request = { path = "../../common/request", version = "0.1", default-features = false, optional = true } simple-request = { path = "../../common/request", version = "0.1", default-features = false, features = ["tls"], optional = true }
tokio = { version = "1", default-features = false, optional = true } tokio = { version = "1", default-features = false, optional = true }
[build-dependencies] [build-dependencies]

View file

@ -15,11 +15,14 @@ rustdoc-args = ["--cfg", "docsrs"]
[dependencies] [dependencies]
# Deprecated here means to enable deprecated warnings, not to restore deprecated APIs # Deprecated here means to enable deprecated warnings, not to restore deprecated APIs
hyper = { version = "0.14", default-features = false, features = ["http1", "tcp", "client", "backports", "deprecated"] } hyper = { version = "0.14", default-features = false, features = ["http1", "tcp", "client", "backports", "deprecated"] }
hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"] }
tokio = { version = "1", default-features = false } tokio = { version = "1", default-features = false }
hyper-rustls = { version = "0.24", default-features = false, features = ["http1", "native-tokio"], optional = true }
zeroize = { version = "1", optional = true } zeroize = { version = "1", optional = true }
base64ct = { version = "1", features = ["alloc"], optional = true } base64ct = { version = "1", features = ["alloc"], optional = true }
[features] [features]
tls = ["hyper-rustls"]
basic-auth = ["zeroize", "base64ct"] basic-auth = ["zeroize", "base64ct"]
default = ["tls"]

View file

@ -5,6 +5,7 @@ use std::sync::Arc;
use tokio::sync::Mutex; use tokio::sync::Mutex;
#[cfg(feature = "tls")]
use hyper_rustls::{HttpsConnectorBuilder, HttpsConnector}; use hyper_rustls::{HttpsConnectorBuilder, HttpsConnector};
use hyper::{ use hyper::{
Uri, Uri,
@ -26,18 +27,19 @@ pub enum Error {
InvalidUri, InvalidUri,
MissingHost, MissingHost,
InconsistentHost, InconsistentHost,
SslError(Box<dyn Send + Sync + std::error::Error>), ConnectionError(Box<dyn Send + Sync + std::error::Error>),
Hyper(hyper::Error), Hyper(hyper::Error),
} }
#[cfg(not(feature = "tls"))]
type Connector = HttpConnector;
#[cfg(feature = "tls")]
type Connector = HttpsConnector<HttpConnector>;
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
enum Connection { enum Connection {
ConnectionPool(hyper::Client<HttpsConnector<HttpConnector>>), ConnectionPool(hyper::Client<Connector>),
Connection { Connection { connector: Connector, host: Uri, connection: Arc<Mutex<Option<SendRequest<Body>>>> },
https_builder: HttpsConnector<HttpConnector>,
host: Uri,
connection: Arc<Mutex<Option<SendRequest<Body>>>>,
},
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -46,24 +48,25 @@ pub struct Client {
} }
impl Client { impl Client {
fn https_builder() -> HttpsConnector<HttpConnector> { fn connector() -> Connector {
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build() #[cfg(feature = "tls")]
let res =
HttpsConnectorBuilder::new().with_native_roots().https_or_http().enable_http1().build();
#[cfg(not(feature = "tls"))]
let res = HttpConnector::new();
res
} }
pub fn with_connection_pool() -> Client { pub fn with_connection_pool() -> Client {
Client { Client {
connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::https_builder())), connection: Connection::ConnectionPool(hyper::Client::builder().build(Self::connector())),
} }
} }
pub fn without_connection_pool(host: String) -> Result<Client, Error> { pub fn without_connection_pool(host: String) -> Result<Client, Error> {
Ok(Client { Ok(Client {
connection: Connection::Connection { connection: Connection::Connection {
https_builder: HttpsConnectorBuilder::new() connector: Self::connector(),
.with_native_roots()
.https_or_http()
.enable_http1()
.build(),
host: { host: {
let uri: Uri = host.parse().map_err(|_| Error::InvalidUri)?; let uri: Uri = host.parse().map_err(|_| Error::InvalidUri)?;
if uri.host().is_none() { if uri.host().is_none() {
@ -110,16 +113,18 @@ impl Client {
Ok(Response(match &self.connection { Ok(Response(match &self.connection {
Connection::ConnectionPool(client) => client.request(request).await.map_err(Error::Hyper)?, Connection::ConnectionPool(client) => client.request(request).await.map_err(Error::Hyper)?,
Connection::Connection { https_builder, host, connection } => { Connection::Connection { connector, host, connection } => {
let mut connection_lock = connection.lock().await; let mut connection_lock = connection.lock().await;
// If there's not a connection... // If there's not a connection...
if connection_lock.is_none() { if connection_lock.is_none() {
let (requester, connection) = hyper::client::conn::http1::handshake( let call_res = connector.clone().call(host.clone()).await;
https_builder.clone().call(host.clone()).await.map_err(Error::SslError)?, #[cfg(not(feature = "tls"))]
) let call_res = call_res.map_err(|e| Error::ConnectionError(format!("{e:?}").into()));
.await #[cfg(feature = "tls")]
.map_err(Error::Hyper)?; let call_res = call_res.map_err(Error::ConnectionError);
let (requester, connection) =
hyper::client::conn::http1::handshake(call_res?).await.map_err(Error::Hyper)?;
// This will die when we drop the requester, so we don't need to track an AbortHandle for // This will die when we drop the requester, so we don't need to track an AbortHandle for
// it // it
tokio::spawn(connection); tokio::spawn(connection);