Skip to content

Commit de73617

Browse files
authoredJun 21, 2024··
feat(tls): Remove tls roots implicit configuration (#1731)
* feat(tls): Add option to enable tls roots * feat(tls): Remove tls roots implicit configuration
1 parent 34b863b commit de73617

File tree

5 files changed

+49
-63
lines changed

5 files changed

+49
-63
lines changed
 

‎tonic/Cargo.toml

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ zstd = ["dep:zstd"]
2929
default = ["transport", "codegen", "prost"]
3030
prost = ["dep:prost"]
3131
tls = ["dep:rustls-pemfile", "dep:tokio-rustls", "dep:tokio", "tokio?/rt", "tokio?/macros"]
32-
tls-roots = ["tls-roots-common", "dep:rustls-native-certs"]
33-
tls-roots-common = ["tls", "channel"]
34-
tls-webpki-roots = ["tls-roots-common", "dep:webpki-roots"]
32+
tls-roots = ["tls", "channel", "dep:rustls-native-certs"]
33+
tls-webpki-roots = ["tls", "channel", "dep:webpki-roots"]
3534
router = ["dep:axum"]
3635
server = [
3736
"router",

‎tonic/src/transport/channel/endpoint.rs

+5-29
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ pub struct Endpoint {
2525
pub(crate) rate_limit: Option<(u64, Duration)>,
2626
#[cfg(feature = "tls")]
2727
pub(crate) tls: Option<TlsConnector>,
28-
// Only applies if the tls config is not explicitly set. This allows users
29-
// to connect to a server that doesn't support ALPN while using the
30-
// tls-roots-common feature for setting up TLS.
31-
#[cfg(feature = "tls-roots-common")]
32-
pub(crate) tls_assume_http2: bool,
3328
pub(crate) buffer_size: Option<usize>,
3429
pub(crate) init_stream_window_size: Option<u32>,
3530
pub(crate) init_connection_window_size: Option<u32>,
@@ -256,18 +251,6 @@ impl Endpoint {
256251
})
257252
}
258253

259-
/// Configures TLS to assume that the server offers HTTP/2 even if it
260-
/// doesn't perform ALPN negotiation. This only applies if a tls_config has
261-
/// not been set.
262-
#[cfg(feature = "tls-roots-common")]
263-
#[cfg_attr(docsrs, doc(cfg(feature = "tls-roots-common")))]
264-
pub fn tls_assume_http2(self, assume_http2: bool) -> Self {
265-
Endpoint {
266-
tls_assume_http2: assume_http2,
267-
..self
268-
}
269-
}
270-
271254
/// Set the value of `TCP_NODELAY` option for accepted connections. Enabled by default.
272255
pub fn tcp_nodelay(self, enabled: bool) -> Self {
273256
Endpoint {
@@ -320,16 +303,11 @@ impl Endpoint {
320303
}
321304

322305
pub(crate) fn connector<C>(&self, c: C) -> service::Connector<C> {
323-
#[cfg(all(feature = "tls", not(feature = "tls-roots-common")))]
324-
let connector = service::Connector::new(c, self.tls.clone());
325-
326-
#[cfg(all(feature = "tls", feature = "tls-roots-common"))]
327-
let connector = service::Connector::new(c, self.tls.clone(), self.tls_assume_http2);
328-
329-
#[cfg(not(feature = "tls"))]
330-
let connector = service::Connector::new(c);
331-
332-
connector
306+
service::Connector::new(
307+
c,
308+
#[cfg(feature = "tls")]
309+
self.tls.clone(),
310+
)
333311
}
334312

335313
/// Create a channel from this config.
@@ -435,8 +413,6 @@ impl From<Uri> for Endpoint {
435413
timeout: None,
436414
#[cfg(feature = "tls")]
437415
tls: None,
438-
#[cfg(feature = "tls-roots-common")]
439-
tls_assume_http2: false,
440416
buffer_size: None,
441417
init_stream_window_size: None,
442418
init_connection_window_size: None,

‎tonic/src/transport/channel/service/connector.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,16 @@ pub(crate) struct Connector<C> {
3333
inner: C,
3434
#[cfg(feature = "tls")]
3535
tls: Option<TlsConnector>,
36-
// When connecting to a URI with the https scheme, assume that the server
37-
// is capable of speaking HTTP/2 even if it doesn't offer ALPN.
38-
#[cfg(feature = "tls-roots-common")]
39-
assume_http2: bool,
4036
}
4137

4238
impl<C> Connector<C> {
43-
pub(crate) fn new(
44-
inner: C,
45-
#[cfg(feature = "tls")] tls: Option<TlsConnector>,
46-
#[cfg(feature = "tls-roots-common")] assume_http2: bool,
47-
) -> Self {
39+
pub(crate) fn new(inner: C, #[cfg(feature = "tls")] tls: Option<TlsConnector>) -> Self {
4840
Self {
4941
inner,
5042
#[cfg(feature = "tls")]
5143
tls,
52-
#[cfg(feature = "tls-roots-common")]
53-
assume_http2,
5444
}
5545
}
56-
57-
#[cfg(feature = "tls-roots-common")]
58-
fn tls_or_default(&self, scheme: Option<&str>, host: Option<&str>) -> Option<TlsConnector> {
59-
if self.tls.is_some() {
60-
return self.tls.clone();
61-
}
62-
63-
let host = match (scheme, host) {
64-
(Some("https"), Some(host)) => host,
65-
_ => return None,
66-
};
67-
68-
TlsConnector::new(Vec::new(), None, host, self.assume_http2).ok()
69-
}
7046
}
7147

7248
impl<C> Service<Uri> for Connector<C>
@@ -87,12 +63,9 @@ where
8763
}
8864

8965
fn call(&mut self, uri: Uri) -> Self::Future {
90-
#[cfg(all(feature = "tls", not(feature = "tls-roots-common")))]
66+
#[cfg(feature = "tls")]
9167
let tls = self.tls.clone();
9268

93-
#[cfg(feature = "tls-roots-common")]
94-
let tls = self.tls_or_default(uri.scheme_str(), uri.host());
95-
9669
#[cfg(feature = "tls")]
9770
let is_https = uri.scheme_str() == Some("https");
9871
let connect = self.inner.call(uri);

‎tonic/src/transport/channel/service/tls.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@ impl TlsConnector {
2626
identity: Option<Identity>,
2727
domain: &str,
2828
assume_http2: bool,
29+
#[cfg(feature = "tls-roots")] with_native_roots: bool,
30+
#[cfg(feature = "tls-webpki-roots")] with_webpki_roots: bool,
2931
) -> Result<Self, crate::Error> {
3032
let builder = ClientConfig::builder();
3133
let mut roots = RootCertStore::empty();
3234

3335
#[cfg(feature = "tls-roots")]
34-
roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?);
36+
if with_native_roots {
37+
roots.add_parsable_certificates(rustls_native_certs::load_native_certs()?);
38+
}
3539

3640
#[cfg(feature = "tls-webpki-roots")]
37-
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
41+
if with_webpki_roots {
42+
roots.extend(webpki_roots::TLS_SERVER_ROOTS.iter().cloned());
43+
}
3844

3945
for cert in ca_certs {
4046
add_certs_from_pem(&mut Cursor::new(cert), &mut roots)?;

‎tonic/src/transport/channel/tls.rs

+32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ pub struct ClientTlsConfig {
1313
certs: Vec<Certificate>,
1414
identity: Option<Identity>,
1515
assume_http2: bool,
16+
#[cfg(feature = "tls-roots")]
17+
with_native_roots: bool,
18+
#[cfg(feature = "tls-webpki-roots")]
19+
with_webpki_roots: bool,
1620
}
1721

1822
impl fmt::Debug for ClientTlsConfig {
@@ -33,6 +37,10 @@ impl ClientTlsConfig {
3337
certs: Vec::new(),
3438
identity: None,
3539
assume_http2: false,
40+
#[cfg(feature = "tls-roots")]
41+
with_native_roots: false,
42+
#[cfg(feature = "tls-webpki-roots")]
43+
with_webpki_roots: false,
3644
}
3745
}
3846

@@ -75,6 +83,26 @@ impl ClientTlsConfig {
7583
}
7684
}
7785

86+
/// Enables the platform's trusted certs.
87+
#[cfg(feature = "tls-roots")]
88+
#[cfg_attr(docsrs, doc(cfg(feature = "tls-roots")))]
89+
pub fn with_native_roots(self) -> Self {
90+
ClientTlsConfig {
91+
with_native_roots: true,
92+
..self
93+
}
94+
}
95+
96+
/// Enables the webpki roots.
97+
#[cfg(feature = "tls-webpki-roots")]
98+
#[cfg_attr(docsrs, doc(cfg(feature = "tls-webpki-roots")))]
99+
pub fn with_webpki_roots(self) -> Self {
100+
ClientTlsConfig {
101+
with_webpki_roots: true,
102+
..self
103+
}
104+
}
105+
78106
pub(crate) fn tls_connector(&self, uri: Uri) -> Result<TlsConnector, crate::Error> {
79107
let domain = match &self.domain {
80108
Some(domain) => domain,
@@ -85,6 +113,10 @@ impl ClientTlsConfig {
85113
self.identity.clone(),
86114
domain,
87115
self.assume_http2,
116+
#[cfg(feature = "tls-roots")]
117+
self.with_native_roots,
118+
#[cfg(feature = "tls-webpki-roots")]
119+
self.with_webpki_roots,
88120
)
89121
}
90122
}

0 commit comments

Comments
 (0)
Please sign in to comment.