From 64f23669b8db141a26ff355f309cdd3d86e5b488 Mon Sep 17 00:00:00 2001 From: clux Date: Wed, 14 Dec 2022 07:58:31 +0000 Subject: [PATCH] allow rustls_https_connector to be created from a connector Signed-off-by: clux --- kube-client/src/client/builder.rs | 2 +- kube-client/src/client/config_ext.rs | 37 +++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/kube-client/src/client/builder.rs b/kube-client/src/client/builder.rs index 83357f3e5..67ea5ab5f 100644 --- a/kube-client/src/client/builder.rs +++ b/kube-client/src/client/builder.rs @@ -85,7 +85,7 @@ impl TryFrom for ClientBuilder, Response #[cfg(feature = "openssl-tls")] let connector = config.openssl_https_connector_with_connector(connector)?; #[cfg(all(not(feature = "openssl-tls"), feature = "rustls-tls"))] - let connector = config.rustls_https_connector()?; + let connector = config.rustls_https_connector_with_connector(connector)?; let mut connector = TimeoutConnector::new(connector); diff --git a/kube-client/src/client/config_ext.rs b/kube-client/src/client/config_ext.rs index 87a03b739..b0ad0ce5e 100644 --- a/kube-client/src/client/config_ext.rs +++ b/kube-client/src/client/config_ext.rs @@ -43,6 +43,29 @@ pub trait ConfigExt: private::Sealed { #[cfg(feature = "rustls-tls")] fn rustls_https_connector(&self) -> Result>; + /// Create [`hyper_rustls::HttpsConnector`] based on config and `connector`. + /// + /// # Example + /// + /// ```rust + /// # async fn doc() -> Result<(), Box> { + /// # use kube::{client::ConfigExt, Config}; + /// # use hyper::client::HttpConnector; + /// let config = Config::infer().await?; + /// let mut connector = HttpConnector::new(); + /// connector.enforce_http(false); + /// let https = config.rustls_https_connector_with_connector(connector)?; + /// let hyper_client: hyper::Client<_, hyper::Body> = hyper::Client::builder().build(https); + /// # Ok(()) + /// # } + /// ``` + #[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))] + #[cfg(feature = "rustls-tls")] + fn rustls_https_connector_with_connector( + &self, + connector: hyper::client::HttpConnector, + ) -> Result>; + /// Create [`rustls::ClientConfig`] based on config. /// # Example /// @@ -186,16 +209,24 @@ impl ConfigExt for Config { #[cfg(feature = "rustls-tls")] fn rustls_https_connector(&self) -> Result> { + let mut connector = hyper::client::HttpConnector::new(); + connector.enforce_http(false); + self.rustls_https_connector_with_connector(connector) + } + + #[cfg(feature = "rustls-tls")] + fn rustls_https_connector_with_connector( + &self, + connector: hyper::client::HttpConnector, + ) -> Result> { let rustls_config = self.rustls_client_config()?; - let mut http = hyper::client::HttpConnector::new(); - http.enforce_http(false); let mut builder = hyper_rustls::HttpsConnectorBuilder::new() .with_tls_config(rustls_config) .https_or_http(); if let Some(tsn) = self.tls_server_name.as_ref() { builder = builder.with_server_name(tsn.clone()); } - Ok(builder.enable_http1().wrap_connector(http)) + Ok(builder.enable_http1().wrap_connector(connector)) } #[cfg(feature = "openssl-tls")]