Skip to content

Commit

Permalink
allow rustls_https_connector to be created from a connector
Browse files Browse the repository at this point in the history
Signed-off-by: clux <sszynrae@gmail.com>
  • Loading branch information
clux committed Dec 14, 2022
1 parent 19942be commit 64f2366
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion kube-client/src/client/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl TryFrom<Config> for ClientBuilder<BoxService<Request<hyper::Body>, 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);

Expand Down
37 changes: 34 additions & 3 deletions kube-client/src/client/config_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ pub trait ConfigExt: private::Sealed {
#[cfg(feature = "rustls-tls")]
fn rustls_https_connector(&self) -> Result<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>;

/// Create [`hyper_rustls::HttpsConnector`] based on config and `connector`.
///
/// # Example
///
/// ```rust
/// # async fn doc() -> Result<(), Box<dyn std::error::Error>> {
/// # 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<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>;

/// Create [`rustls::ClientConfig`] based on config.
/// # Example
///
Expand Down Expand Up @@ -186,16 +209,24 @@ impl ConfigExt for Config {

#[cfg(feature = "rustls-tls")]
fn rustls_https_connector(&self) -> Result<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>> {
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<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>> {
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")]
Expand Down

0 comments on commit 64f2366

Please sign in to comment.