Skip to content

Commit

Permalink
Eliminate methods to pass in ruslts config directly
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Dec 21, 2021
1 parent 6d17e58 commit 9a7833b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 62 deletions.
23 changes: 1 addition & 22 deletions tonic/src/transport/channel/tls.rs
Expand Up @@ -14,7 +14,6 @@ pub struct ClientTlsConfig {
domain: Option<String>,
cert: Option<Certificate>,
identity: Option<Identity>,
rustls_raw: Option<tokio_rustls::rustls::ClientConfig>,
}

#[cfg(feature = "tls")]
Expand All @@ -36,7 +35,6 @@ impl ClientTlsConfig {
domain: None,
cert: None,
identity: None,
rustls_raw: None,
}
}

Expand All @@ -49,8 +47,6 @@ impl ClientTlsConfig {
}

/// Sets the CA Certificate against which to verify the server's TLS certificate.
///
/// This has no effect if `rustls_client_config` is used to configure Rustls.
pub fn ca_certificate(self, ca_certificate: Certificate) -> Self {
ClientTlsConfig {
cert: Some(ca_certificate),
Expand All @@ -59,35 +55,18 @@ impl ClientTlsConfig {
}

/// Sets the client identity to present to the server.
///
/// This has no effect if `rustls_client_config` is used to configure Rustls.
pub fn identity(self, identity: Identity) -> Self {
ClientTlsConfig {
identity: Some(identity),
..self
}
}

/// Use options specified by the given `ClientConfig` to configure TLS.
///
/// This overrides all other TLS options set via other means.
pub fn rustls_client_config(self, config: tokio_rustls::rustls::ClientConfig) -> Self {
ClientTlsConfig {
rustls_raw: Some(config),
..self
}
}

pub(crate) fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> {
let domain = match &self.domain {
None => uri.host().ok_or_else(Error::new_invalid_uri)?.to_string(),
Some(domain) => domain.clone(),
};
match &self.rustls_raw {
None => {
TlsConnector::new_with_rustls_cert(self.cert.clone(), self.identity.clone(), domain)
}
Some(c) => TlsConnector::new_with_rustls_raw(c.clone(), domain),
}
TlsConnector::new(self.cert.clone(), self.identity.clone(), domain)
}
}
24 changes: 4 additions & 20 deletions tonic/src/transport/server/tls.rs
Expand Up @@ -11,7 +11,6 @@ use std::fmt;
pub struct ServerTlsConfig {
identity: Option<Identity>,
client_ca_root: Option<Certificate>,
rustls_raw: Option<tokio_rustls::rustls::ServerConfig>,
}

#[cfg(feature = "tls")]
Expand All @@ -28,7 +27,6 @@ impl ServerTlsConfig {
ServerTlsConfig {
identity: None,
client_ca_root: None,
rustls_raw: None,
}
}

Expand All @@ -48,24 +46,10 @@ impl ServerTlsConfig {
}
}

/// Use options specified by the given `ServerConfig` to configure TLS.
///
/// This overrides all other TLS options set via other means.
pub fn rustls_server_config(
&mut self,
config: tokio_rustls::rustls::ServerConfig,
) -> &mut Self {
self.rustls_raw = Some(config);
self
}

pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::Error> {
match &self.rustls_raw {
None => TlsAcceptor::new_with_rustls_identity(
self.identity.clone().unwrap(),
self.client_ca_root.clone(),
),
Some(config) => TlsAcceptor::new_with_rustls_raw(config.clone()),
}
TlsAcceptor::new(
self.identity.clone().unwrap(),
self.client_ca_root.clone(),
)
}
}
2 changes: 1 addition & 1 deletion tonic/src/transport/service/connector.rs
Expand Up @@ -52,7 +52,7 @@ impl<C> Connector<C> {

host.try_into()
.ok()
.and_then(|dns| TlsConnector::new_with_rustls_cert(None, None, dns).ok())
.and_then(|dns| TlsConnector::new(None, None, dns).ok())
}
}

Expand Down
21 changes: 2 additions & 19 deletions tonic/src/transport/service/tls.rs
Expand Up @@ -37,7 +37,7 @@ pub(crate) struct TlsConnector {

impl TlsConnector {
#[cfg(feature = "tls")]
pub(crate) fn new_with_rustls_cert(
pub(crate) fn new(
ca_cert: Option<Certificate>,
identity: Option<Identity>,
domain: String,
Expand Down Expand Up @@ -82,14 +82,6 @@ impl TlsConnector {
};

config.alpn_protocols.push(ALPN_H2.as_bytes().to_vec());
Self::new_with_rustls_raw(config, domain)
}

#[cfg(feature = "tls")]
pub(crate) fn new_with_rustls_raw(
config: tokio_rustls::rustls::ClientConfig,
domain: String,
) -> Result<Self, crate::Error> {
Ok(Self {
config: Arc::new(config),
domain: Arc::new(domain.as_str().try_into()?),
Expand Down Expand Up @@ -132,7 +124,7 @@ pub(crate) struct TlsAcceptor {

impl TlsAcceptor {
#[cfg(feature = "tls")]
pub(crate) fn new_with_rustls_identity(
pub(crate) fn new(
identity: Identity,
client_ca_root: Option<Certificate>,
) -> Result<Self, crate::Error> {
Expand All @@ -157,15 +149,6 @@ impl TlsAcceptor {
})
}

#[cfg(feature = "tls")]
pub(crate) fn new_with_rustls_raw(
config: tokio_rustls::rustls::ServerConfig,
) -> Result<Self, crate::Error> {
Ok(Self {
inner: Arc::new(config),
})
}

pub(crate) async fn accept<IO>(&self, io: IO) -> Result<TlsStream<IO>, crate::Error>
where
IO: AsyncRead + AsyncWrite + Connected + Unpin + Send + 'static,
Expand Down

0 comments on commit 9a7833b

Please sign in to comment.