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

Make tls optional and add rustls support (#418) #420

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion metrics-exporter-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ keywords = ["metrics", "telemetry", "prometheus"]
default = ["http-listener", "push-gateway"]
async-runtime = ["tokio", "hyper"]
http-listener = ["async-runtime", "hyper/server", "ipnet"]
push-gateway = ["async-runtime", "hyper/client", "hyper-tls", "tracing"]
push-gateway = ["async-runtime", "hyper/client", "tracing"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When it comes to the core crates (like metrics), I generally err on the side of making users opt-in to additional functionality, but that flips the opposite direction for exporters, where users generally want/expect to get the "full fat" support out of the box.

I'd say we should add another feature called push-gateway-https, which pulls in hyper-rustls, and have the feature be enabled by default.

native-tls = ["hyper-tls"]
rustls-tls = ["hyper-rustls"]
Comment on lines +24 to +25
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After doing some reading, it seems like we should probably just switch to using rustls entirely.

I don't think the initial implementor choose to use hyper-tls specifically for the OS-backed/dynamically-linked aspect, and I don't personally care about esoteric SSL/TLS setups or FIPS compliance or any of that... so I'd rather go with a single solution than force users to have to spend time understanding which one they ought to choose.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you won't mind a somewhat random drive-past, but its generally a good practice for mid-layer crates that sit between TLS and applications to not set TLS policy.

The way to make this work comfortably is:

  • at a dependency level consume hyper, or whatever other crates you use, with default-features-false
  • have a feature to turn on the push-gateway without specifying the TLS stack to use
  • have a different feature to turn on the push-gateway with whatever tls stack you prefer to have by default

Then higher layer crates such as axum-prometheus can depend on your crate, with default-features=false, and the application layer crate using axum-prometheus can specify hyper-tls or hyper-rustls itself.


[dependencies]
metrics = { version = "^0.21", path = "../metrics" }
Expand All @@ -36,6 +38,7 @@ ipnet = { version = "2", optional = true }
tokio = { version = "1", features = ["rt", "net", "time"], optional = true }
tracing = { version = "0.1.26", optional = true }
hyper-tls = { version = "0.5.0", optional = true }
hyper-rustls = { version = "0.24.2", optional = true }

[dev-dependencies]
tracing = "0.1"
Expand Down
26 changes: 23 additions & 3 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use hyper::{
http::HeaderValue,
Method, Request, Uri,
};
use hyper_tls::HttpsConnector;

use indexmap::IndexMap;
#[cfg(feature = "http-listener")]
Expand Down Expand Up @@ -461,8 +460,8 @@ impl PrometheusBuilder {
#[cfg(feature = "push-gateway")]
ExporterConfig::PushGateway { endpoint, interval, username, password } => {
let exporter = async move {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let client = make_http_client();

let auth = username.as_ref().map(|name| basic_auth(name, password.as_deref()));

loop {
Expand Down Expand Up @@ -568,6 +567,27 @@ fn basic_auth(username: &str, password: Option<&str>) -> HeaderValue {
header
}

#[cfg(all(feature = "rustls-tls", not(feature = "native-tls")))]
fn make_http_client(
) -> Client<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>, hyper::Body> {
let tls = hyper_rustls::HttpsConnectorBuilder::new()
.with_native_roots()
.https_or_http()
.enable_http1()
.build();
Client::builder().build::<_, hyper::Body>(tls)
}

#[cfg(all(not(feature = "rustls-tls"), feature = "native-tls"))]
fn make_http_client() -> Client<hyper_tls::HttpsConnector, hyper::Body> {
Client::builder().build::<_, hyper::Body>(hyper_tls::HttpsConnector::new())
}

#[cfg(not(any(feature = "rustls-tls", feature = "native-tls")))]
fn make_http_client() -> Client<hyper::client::HttpConnector, hyper::Body> {
Client::builder().build_http()
}

#[cfg(test)]
mod tests {
use std::time::Duration;
Expand Down
Loading