Skip to content

Commit

Permalink
Add ConfigExt::auth_layer hiding details
Browse files Browse the repository at this point in the history
  • Loading branch information
kazk committed Jun 5, 2021
1 parent f009ff6 commit 79096a9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
1 change: 1 addition & 0 deletions examples/custom_client.rs
Expand Up @@ -18,6 +18,7 @@ async fn main() -> anyhow::Result<()> {
let client = Client::new(
ServiceBuilder::new()
.layer(config.base_uri_layer())
.option_layer(config.auth_layer()?)
.service(hyper::Client::builder().build(https)),
);

Expand Down
30 changes: 13 additions & 17 deletions kube/src/client/config_ext.rs
Expand Up @@ -5,7 +5,7 @@ use tower::util::Either;
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] use super::tls;
use super::{
auth::Auth,
middleware::{AddAuthorizationLayer, RefreshTokenLayer, SetBaseUriLayer},
middleware::{AddAuthorizationLayer, AuthLayer, RefreshTokenLayer, SetBaseUriLayer},
};
use crate::{Config, Result};

Expand All @@ -16,6 +16,9 @@ pub trait ConfigExt: private::Sealed {
/// Layer to set the base URI of requests to the configured server.
fn base_uri_layer(&self) -> SetBaseUriLayer;

/// Optional layer to set up `Authorization` header depending on the config.
fn auth_layer(&self) -> Result<Option<AuthLayer>>;

/// Create `native_tls::TlsConnector`
#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))]
#[cfg(feature = "native-tls")]
Expand All @@ -35,13 +38,6 @@ pub trait ConfigExt: private::Sealed {
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))]
#[cfg(feature = "rustls-tls")]
fn rustls_https_connector(&self) -> Result<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>;

// TODO Try reducing exported types to minimize API surface before making this public.
#[doc(hidden)]
/// Optional layer to set up `Authorization` header depending on the config.
///
/// Users are not allowed to call this for now.
fn auth_layer(&self) -> Result<Option<Either<AddAuthorizationLayer, RefreshTokenLayer>>>;
}

mod private {
Expand All @@ -54,6 +50,15 @@ impl ConfigExt for Config {
SetBaseUriLayer::new(self.cluster_url.clone())
}

fn auth_layer(&self) -> Result<Option<AuthLayer>> {
Ok(match Auth::try_from(&self.auth_info)? {
Auth::None => None,
Auth::Basic(user, pass) => Some(AuthLayer(Either::A(AddAuthorizationLayer::basic(&user, &pass)))),
Auth::Bearer(token) => Some(AuthLayer(Either::A(AddAuthorizationLayer::bearer(&token)))),
Auth::RefreshableToken(r) => Some(AuthLayer(Either::B(RefreshTokenLayer::new(r)))),
})
}

#[cfg(feature = "native-tls")]
fn native_tls_connector(&self) -> Result<tokio_native_tls::native_tls::TlsConnector> {
tls::native_tls::native_tls_connector(
Expand Down Expand Up @@ -87,13 +92,4 @@ impl ConfigExt for Config {
http.enforce_http(false);
Ok(hyper_rustls::HttpsConnector::from((http, rustls_config)))
}

fn auth_layer(&self) -> Result<Option<Either<AddAuthorizationLayer, RefreshTokenLayer>>> {
Ok(match Auth::try_from(&self.auth_info)? {
Auth::None => None,
Auth::Basic(user, pass) => Some(Either::A(AddAuthorizationLayer::basic(&user, &pass))),
Auth::Bearer(token) => Some(Either::A(AddAuthorizationLayer::bearer(&token))),
Auth::RefreshableToken(r) => Some(Either::B(RefreshTokenLayer::new(r))),
})
}
}
13 changes: 13 additions & 0 deletions kube/src/client/middleware/mod.rs
@@ -1,8 +1,21 @@
//! Middleware types returned from `ConfigExt` methods.
use tower::{util::Either, Layer};

mod add_authorization;
mod base_uri;
mod refresh_token;

pub(crate) use add_authorization::AddAuthorizationLayer;
pub use base_uri::{SetBaseUri, SetBaseUriLayer};
pub(crate) use refresh_token::RefreshTokenLayer;
/// Layer to set up `Authorization` header depending on the config.
pub struct AuthLayer(pub(crate) Either<AddAuthorizationLayer, RefreshTokenLayer>);

impl<S> Layer<S> for AuthLayer {
type Service =
Either<<AddAuthorizationLayer as Layer<S>>::Service, <RefreshTokenLayer as Layer<S>>::Service>;

fn layer(&self, inner: S) -> Self::Service {
self.0.layer(inner)
}
}

0 comments on commit 79096a9

Please sign in to comment.