diff --git a/Cargo.toml b/Cargo.toml index a6d5bb4..c5274ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,10 +11,11 @@ license-file = "LICENSE" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] default = ["rustls-native"] -metrics = ["prometheus", "lazy_static"] +metrics = ["prometheus"] default-tls = ["hyper-tls"] rustls-native = ["hyper-rustls/rustls-native-certs"] rustls-webpki = ["hyper-rustls/webpki-roots"] +trace = ["opentelemetry"] # keep this list sorted! [dependencies] @@ -24,7 +25,7 @@ http = "0.2" hyper = { version = "0.14", features = ["full"] } hyper-rustls = { version = "0.24" } hyper-tls = { version = "0.5.0", optional = true, no-default-features = true } -lazy_static = { version = "1", optional = true } +lazy_static = { version = "1" } opentelemetry = { version = "0.19", features = ["rt-tokio"], optional = true } prometheus = { version = "0.13", optional = true } quick-error = "2" diff --git a/src/lib.rs b/src/lib.rs index c8e8edb..fb0b5c3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,7 @@ use std::{env, str::Utf8Error}; use base64::Engine; use hyper::{body::Buf, client::HttpConnector, Body, Method}; #[cfg(any(feature = "rustls-native", feature = "rustls-webpki"))] -use hyper_rustls::HttpsConnector; +use hyper_rustls::{ HttpsConnector, HttpsConnectorBuilder }; #[cfg(feature = "default-tls")] use hyper_tls::HttpsConnector; use lazy_static::lazy_static; @@ -242,27 +242,36 @@ where #[derive(Debug)] /// This struct defines the consul client and allows access to the consul api via method syntax. pub struct Consul { - https_client: hyper::Client, Body>, + + https_client: hyper::Client, Body>, config: Config, #[cfg(feature = "trace")] tracer: BoxedTracer, } -fn https_connector() -> hyper_rustls::HttpsConnector { - #[cfg(feature = "rustls-webpki")] - return hyper_rustls::HttpsConnectorBuilder::new() - .with_webpki_roots() +fn https_connector() -> HttpsConnector { + #[cfg(feature = "rustls-native")] + return HttpsConnectorBuilder::new() + .with_native_roots() .https_or_http() .enable_http1() .build(); - hyper_rustls::HttpsConnectorBuilder::new() - .with_native_roots() + #[cfg(feature = "rustls-webpki")] + return HttpsConnectorBuilder::new() + .with_webpki_roots() .https_or_http() .enable_http1() - .build() + .build(); + #[cfg(feature = "default-tls")] + { + let mut conn = HttpsConnector::new(); + conn.https_only(false); + return conn; + } } impl Clone for Consul { + #[cfg(feature = "trace")] fn clone(&self) -> Self { Consul { https_client: self.https_client.clone(), @@ -270,6 +279,13 @@ impl Clone for Consul { tracer: global::tracer("consul"), } } + #[cfg(not(feature = "trace"))] + fn clone(&self) -> Self { + Consul { + https_client: self.https_client.clone(), + config: self.config.clone(), + } + } } impl Consul { diff --git a/src/types.rs b/src/types.rs index 70c6966..b45f522 100644 --- a/src/types.rs +++ b/src/types.rs @@ -27,6 +27,10 @@ use std::time::Duration; use serde::{self, de::Deserializer, de::Error as SerdeError, Deserialize, Serialize, Serializer}; use smart_default::SmartDefault; +use base64::{ + Engine, + engine::general_purpose::STANDARD as B64, +}; // TODO retrofit other get APIs to use this struct /// Query options for Consul endpoints. @@ -623,10 +627,7 @@ pub struct Base64Vec(pub Vec); impl Serialize for Base64Vec { fn serialize(&self, serializer: S) -> Result { - serializer.collect_str(&base64::display::Base64Display::with_config( - &self.0, - base64::STANDARD, - )) + serializer.collect_str(&B64.encode(&self.0)) } } @@ -641,7 +642,7 @@ impl<'de> Deserialize<'de> for Base64Vec { } fn visit_str(self, v: &str) -> Result { - base64::decode(v).map(Base64Vec).map_err(SerdeError::custom) + B64.decode(v).map(Base64Vec).map_err(SerdeError::custom) } } deserializer.deserialize_str(Vis)