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

fix(tonic-build,tonic) Add back TLS handling in generated Client::connect code #1866

Merged
merged 4 commits into from
Aug 21, 2024
Merged
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
5 changes: 5 additions & 0 deletions tests/integration_tests/tests/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ async fn connect_returns_err() {
assert!(res.is_err());
}

#[tokio::test]
async fn connect_handles_tls() {
TestClient::connect("https://example.com").await.unwrap();
}

#[tokio::test]
async fn connect_returns_err_via_call_after_connected() {
let (tx, rx) = oneshot::channel();
Expand Down
5 changes: 5 additions & 0 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ impl Endpoint {
D::Error: Into<crate::Error>,
{
let me = dst.try_into().map_err(|e| Error::from_source(e.into()))?;
#[cfg(feature = "tls")]
if me.uri.scheme() == Some(&http::uri::Scheme::HTTPS) {
return me.tls_config(ClientTlsConfig::new().with_enabled_roots());
}

Ok(me)
}

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 @@ -81,6 +81,16 @@ impl ClientTlsConfig {
}
}

/// Activates all TLS roots enabled through `tls-*-roots` feature flags
pub fn with_enabled_roots(self) -> Self {
let config = ClientTlsConfig::new();
#[cfg(feature = "tls-native-roots")]
let config = config.with_native_roots();
#[cfg(feature = "tls-webpki-roots")]
let config = config.with_webpki_roots();
config
}

pub(crate) fn into_tls_connector(self, uri: &Uri) -> Result<TlsConnector, crate::Error> {
let domain = match &self.domain {
Some(domain) => domain,
Expand Down