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

feat: optional SSLKEYLOGFILE support #1539

Open
wants to merge 1 commit into
base: master
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
6 changes: 6 additions & 0 deletions tonic/src/transport/channel/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ pub(crate) struct TlsConnector {
}

impl TlsConnector {
#[allow(clippy::too_many_arguments)]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a bit cheating. If you want, I can refactor this, either using a parameter structure like

TlsConnectorParameters<'a> {
    ca_certs: Vec<Certificate>,
    trust_anchors: Vec<TrustAnchor<'static>>,
    identity: Option<Identity>,
    domain: &'a str,
    assume_http2: bool,
    use_key_log: bool,
}

or a builder pattern.

pub(crate) fn new(
ca_certs: Vec<Certificate>,
trust_anchors: Vec<TrustAnchor<'static>>,
identity: Option<Identity>,
domain: &str,
assume_http2: bool,
use_key_log: bool,
#[cfg(feature = "tls-native-roots")] with_native_roots: bool,
#[cfg(feature = "tls-webpki-roots")] with_webpki_roots: bool,
) -> Result<Self, crate::BoxError> {
Expand Down Expand Up @@ -87,6 +89,10 @@ impl TlsConnector {
None => builder.with_no_client_auth(),
};

if use_key_log {
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
}

config.alpn_protocols.push(ALPN_H2.into());
Ok(Self {
config: Arc::new(config),
Expand Down
10 changes: 10 additions & 0 deletions tonic/src/transport/channel/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ClientTlsConfig {
with_native_roots: bool,
#[cfg(feature = "tls-webpki-roots")]
with_webpki_roots: bool,
use_key_log: bool,
}

impl ClientTlsConfig {
Expand Down Expand Up @@ -84,6 +85,14 @@ impl ClientTlsConfig {
}
}

/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
pub fn use_key_log(self) -> Self {
ClientTlsConfig {
use_key_log: true,
..self
}
}

/// Enables the platform's trusted certs.
#[cfg(feature = "tls-native-roots")]
pub fn with_native_roots(self) -> Self {
Expand Down Expand Up @@ -123,6 +132,7 @@ impl ClientTlsConfig {
self.identity,
domain,
self.assume_http2,
self.use_key_log,
#[cfg(feature = "tls-native-roots")]
self.with_native_roots,
#[cfg(feature = "tls-webpki-roots")]
Expand Down
5 changes: 5 additions & 0 deletions tonic/src/transport/server/service/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl TlsAcceptor {
client_ca_root: Option<&Certificate>,
client_auth_optional: bool,
ignore_client_order: bool,
use_key_log: bool,
) -> Result<Self, crate::BoxError> {
let builder = ServerConfig::builder();

Expand All @@ -45,6 +46,10 @@ impl TlsAcceptor {
let mut config = builder.with_single_cert(cert, key)?;
config.ignore_client_order = ignore_client_order;

if use_key_log {
config.key_log = Arc::new(tokio_rustls::rustls::KeyLogFile::new());
}

config.alpn_protocols.push(ALPN_H2.into());
Ok(Self {
inner: Arc::new(config),
Expand Down
10 changes: 10 additions & 0 deletions tonic/src/transport/server/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct ServerTlsConfig {
client_ca_root: Option<Certificate>,
client_auth_optional: bool,
ignore_client_order: bool,
use_key_log: bool,
}

impl fmt::Debug for ServerTlsConfig {
Expand Down Expand Up @@ -64,12 +65,21 @@ impl ServerTlsConfig {
}
}

/// Use key log as specified by the `SSLKEYLOGFILE` environment variable.
pub fn use_key_log(self) -> Self {
ServerTlsConfig {
use_key_log: true,
..self
}
}

pub(crate) fn tls_acceptor(&self) -> Result<TlsAcceptor, crate::BoxError> {
TlsAcceptor::new(
self.identity.as_ref().unwrap(),
self.client_ca_root.as_ref(),
self.client_auth_optional,
self.ignore_client_order,
self.use_key_log,
)
}
}
Loading