Skip to content

Commit

Permalink
cleanup after merging in #main
Browse files Browse the repository at this point in the history
  • Loading branch information
rrichardson committed Jun 26, 2023
1 parent 2704704 commit 1cb9860
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
34 changes: 25 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -242,34 +242,50 @@ 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<hyper_rustls::HttpsConnector<HttpConnector>, Body>,

https_client: hyper::Client<HttpsConnector<HttpConnector>, Body>,
config: Config,
#[cfg(feature = "trace")]
tracer: BoxedTracer,
}

fn https_connector() -> hyper_rustls::HttpsConnector<HttpConnector> {
#[cfg(feature = "rustls-webpki")]
return hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
fn https_connector() -> HttpsConnector<HttpConnector> {
#[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(),
config: self.config.clone(),
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 {
Expand Down
11 changes: 6 additions & 5 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -623,10 +627,7 @@ pub struct Base64Vec(pub Vec<u8>);

impl Serialize for Base64Vec {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(&base64::display::Base64Display::with_config(
&self.0,
base64::STANDARD,
))
serializer.collect_str(&B64.encode(&self.0))
}
}

Expand All @@ -641,7 +642,7 @@ impl<'de> Deserialize<'de> for Base64Vec {
}

fn visit_str<E: SerdeError>(self, v: &str) -> Result<Self::Value, E> {
base64::decode(v).map(Base64Vec).map_err(SerdeError::custom)
B64.decode(v).map(Base64Vec).map_err(SerdeError::custom)
}
}
deserializer.deserialize_str(Vis)
Expand Down

0 comments on commit 1cb9860

Please sign in to comment.