Skip to content

Commit eee08f9

Browse files
authored
Move tls::accept to async/await (#607)
The `tls::accept` module can be simplified by moving to async/await. This presents a few opportunities for improvement: 1. The recently-introduced `Detect` trait is used. This sets up further changes to the accept logic, decoupling protocol detection from the `Accept` stack/type. 2. The `tls::ReasonForNoIdentity` type has been removed, as it provided no value; and the `tls::ReasonForNoPeerName` has been updated with additional states. 3. We now avoid having to use `PrefixedIo` in most cases by using `TcpStream::peek` with a smaller buffer. The prior behavior is retained as a fallback in case peek doesn't return enough data. This comes at the cost of ~2 additional Boxes per connection; but these will probably be shaved off in a follow-up as the detection logic becomes more flexible/dynamic.
1 parent e48c8d3 commit eee08f9

File tree

20 files changed

+219
-342
lines changed

20 files changed

+219
-342
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,7 @@ name = "linkerd2-proxy-transport"
13531353
version = "0.1.0"
13541354
dependencies = [
13551355
"async-stream",
1356+
"async-trait",
13561357
"bytes 0.5.4",
13571358
"futures 0.3.5",
13581359
"indexmap",
@@ -1365,6 +1366,7 @@ dependencies = [
13651366
"linkerd2-io",
13661367
"linkerd2-metrics",
13671368
"linkerd2-proxy-core",
1369+
"linkerd2-proxy-detect",
13681370
"linkerd2-stack",
13691371
"pin-project",
13701372
"ring",

linkerd/app/core/src/metric_labels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct ControlLabels {
1616
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1717
pub struct EndpointLabels {
1818
pub direction: Direction,
19-
pub tls_id: Conditional<TlsId, tls::ReasonForNoIdentity>,
19+
pub tls_id: Conditional<TlsId, tls::ReasonForNoPeerName>,
2020
pub authority: Option<http::uri::Authority>,
2121
pub labels: Option<String>,
2222
}

linkerd/app/core/src/proxy/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ impl ProtocolDetect {
5454
}
5555

5656
#[async_trait]
57-
impl detect::Detect<tls::accept::Meta> for ProtocolDetect {
57+
impl detect::Detect<tls::accept::Meta, BoxedIo> for ProtocolDetect {
5858
type Target = Protocol;
59+
type Io = BoxedIo;
5960
type Error = io::Error;
6061

6162
async fn detect(

linkerd/app/core/src/transport/labels.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,6 @@ impl Into<tls::Conditional<()>> for TlsStatus {
8888
}
8989
}
9090

91-
impl TlsStatus {
92-
pub fn no_tls_reason(&self) -> Option<tls::ReasonForNoIdentity> {
93-
self.0.reason()
94-
}
95-
}
96-
9791
impl fmt::Display for TlsStatus {
9892
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9993
match self.0 {
@@ -105,7 +99,10 @@ impl fmt::Display for TlsStatus {
10599

106100
impl FmtLabels for TlsStatus {
107101
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
108-
if let Some(tls::ReasonForNoIdentity::NoPeerName(why)) = self.no_tls_reason() {
102+
if let Self(Conditional::None(tls::ReasonForNoPeerName::LocalIdentityDisabled)) = self {
103+
return write!(f, "tls=\"disabled\"");
104+
}
105+
if let Self(Conditional::None(why)) = self {
109106
return write!(f, "tls=\"no_identity\",no_tls_reason=\"{}\"", why);
110107
}
111108

linkerd/app/gateway/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ mod test {
6060

6161
#[tokio::test]
6262
async fn no_identity() {
63-
let peer_id = tls::PeerIdentity::None(tls::ReasonForNoPeerName::NotProvidedByRemote.into());
63+
let peer_id = tls::PeerIdentity::None(tls::ReasonForNoPeerName::NoPeerIdFromRemote);
6464
let test = Test {
6565
peer_id,
6666
..Default::default()

linkerd/app/inbound/src/endpoint.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ impl tap::Inspect for Target {
189189
fn src_tls<'a, B>(
190190
&self,
191191
req: &'a http::Request<B>,
192-
) -> Conditional<&'a identity::Name, tls::ReasonForNoIdentity> {
192+
) -> Conditional<&'a identity::Name, tls::ReasonForNoPeerName> {
193193
req.extensions()
194194
.get::<tls::accept::Meta>()
195195
.map(|s| s.peer_identity.as_ref())
196-
.unwrap_or_else(|| Conditional::None(tls::ReasonForNoIdentity::Disabled))
196+
.unwrap_or_else(|| Conditional::None(tls::ReasonForNoPeerName::LocalIdentityDisabled))
197197
}
198198

199199
fn dst_addr<B>(&self, _: &http::Request<B>) -> Option<SocketAddr> {
@@ -207,7 +207,7 @@ impl tap::Inspect for Target {
207207
fn dst_tls<B>(
208208
&self,
209209
_: &http::Request<B>,
210-
) -> Conditional<&identity::Name, tls::ReasonForNoIdentity> {
210+
) -> Conditional<&identity::Name, tls::ReasonForNoPeerName> {
211211
Conditional::None(tls::ReasonForNoPeerName::Loopback.into())
212212
}
213213

linkerd/app/inbound/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use linkerd2_app_core::{
1717
opencensus::proto::trace::v1 as oc,
1818
profiles,
1919
proxy::{
20-
detect::DetectProtocolLayer,
20+
detect,
2121
http::{self, normalize_uri, orig_proto, strip_header},
2222
identity,
2323
server::{Protocol as ServerProtocol, ProtocolDetect, Server},
@@ -412,15 +412,15 @@ impl Config {
412412
);
413413

414414
let tcp_detect = svc::stack(tcp_server)
415-
.push(DetectProtocolLayer::new(ProtocolDetect::new(
415+
.push(detect::AcceptLayer::new(ProtocolDetect::new(
416416
disable_protocol_detection_for_ports.clone(),
417417
)))
418418
.push(admit::AdmitLayer::new(require_identity_for_inbound_ports))
419419
// Terminates inbound mTLS from other outbound proxies.
420-
.push(tls::AcceptTls::layer(
420+
.push(detect::AcceptLayer::new(tls::DetectTls::new(
421421
local_identity,
422422
disable_protocol_detection_for_ports,
423-
))
423+
)))
424424
// Limits the amount of time that the TCP server spends waiting for TLS handshake &
425425
// protocol detection. Ensures that connections that never emit data are dropped
426426
// eventually.

linkerd/app/outbound/src/endpoint.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<T: tap::Inspect> tap::Inspect for Target<T> {
128128
fn src_tls<'a, B>(
129129
&self,
130130
req: &'a http::Request<B>,
131-
) -> Conditional<&'a identity::Name, tls::ReasonForNoIdentity> {
131+
) -> Conditional<&'a identity::Name, tls::ReasonForNoPeerName> {
132132
self.inner.src_tls(req)
133133
}
134134

@@ -143,7 +143,7 @@ impl<T: tap::Inspect> tap::Inspect for Target<T> {
143143
fn dst_tls<B>(
144144
&self,
145145
req: &http::Request<B>,
146-
) -> Conditional<&identity::Name, tls::ReasonForNoIdentity> {
146+
) -> Conditional<&identity::Name, tls::ReasonForNoPeerName> {
147147
self.inner.dst_tls(req)
148148
}
149149

@@ -236,7 +236,7 @@ impl tap::Inspect for HttpEndpoint {
236236
fn src_tls<'a, B>(
237237
&self,
238238
_: &'a http::Request<B>,
239-
) -> Conditional<&'a identity::Name, tls::ReasonForNoIdentity> {
239+
) -> Conditional<&'a identity::Name, tls::ReasonForNoPeerName> {
240240
Conditional::None(tls::ReasonForNoPeerName::Loopback.into())
241241
}
242242

@@ -251,7 +251,7 @@ impl tap::Inspect for HttpEndpoint {
251251
fn dst_tls<B>(
252252
&self,
253253
_: &http::Request<B>,
254-
) -> Conditional<&identity::Name, tls::ReasonForNoIdentity> {
254+
) -> Conditional<&identity::Name, tls::ReasonForNoPeerName> {
255255
self.identity.as_ref()
256256
}
257257

linkerd/app/outbound/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ use linkerd2_app_core::{
1818
opencensus::proto::trace::v1 as oc,
1919
profiles,
2020
proxy::{
21-
self, core::resolve::Resolve, detect::DetectProtocolLayer, discover, http, identity,
22-
resolve::map_endpoint, server::ProtocolDetect, tap, tcp, Server,
21+
self, core::resolve::Resolve, detect, discover, http, identity, resolve::map_endpoint,
22+
server::ProtocolDetect, tap, tcp, Server,
2323
},
2424
reconnect, retry, router, serve,
2525
spans::SpanConverter,
@@ -539,18 +539,18 @@ impl Config {
539539
);
540540

541541
let no_tls: tls::Conditional<identity::Local> =
542-
Conditional::None(tls::ReasonForNoPeerName::Loopback.into());
542+
Conditional::None(tls::ReasonForNoPeerName::Loopback);
543543

544544
let tcp_detect = svc::stack(tcp_server)
545-
.push(DetectProtocolLayer::new(ProtocolDetect::new(
545+
.push(detect::AcceptLayer::new(ProtocolDetect::new(
546546
disable_protocol_detection_for_ports.clone(),
547547
)))
548548
// The local application never establishes mTLS with the proxy, so don't try to
549549
// terminate TLS, just annotate with the connection with the reason.
550-
.push(tls::AcceptTls::layer(
550+
.push(detect::AcceptLayer::new(tls::DetectTls::new(
551551
no_tls,
552552
disable_protocol_detection_for_ports,
553-
))
553+
)))
554554
// Limits the amount of time that the TCP server spends waiting for protocol
555555
// detection. Ensures that connections that never emit data are dropped eventually.
556556
.push_timeout(detect_protocol_timeout);

linkerd/app/src/admin.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::identity::LocalIdentity;
22
use linkerd2_app_core::{
3-
admin, config::ServerConfig, drain, metrics::FmtMetrics, serve, trace::LevelHandle,
4-
transport::tls, Error,
3+
admin, config::ServerConfig, drain, metrics::FmtMetrics, proxy::detect, serve,
4+
trace::LevelHandle, transport::tls, Error,
55
};
66
use std::net::SocketAddr;
77
use std::pin::Pin;
@@ -34,7 +34,10 @@ impl Config {
3434

3535
let (ready, latch) = admin::Readiness::new();
3636
let admin = admin::Admin::new(report, ready, log_level);
37-
let accept = tls::AcceptTls::new(identity, admin.into_accept());
37+
let accept = detect::Accept::new(
38+
tls::DetectTls::new(identity, Default::default()),
39+
admin.into_accept(),
40+
);
3841
let serve = Box::pin(serve::serve(listen, accept, drain.signal()));
3942
Ok(Admin {
4043
listen_addr,

0 commit comments

Comments
 (0)