Skip to content

Commit 0d6471c

Browse files
authored
tls: Disambiguate client and server identities (#855)
The `tls::PeerIdentity` type is used to describe both remote clients and servers. This can easily lead to confusion, as it can be ambiguous as to whether an identity is a client's identity or a target server's identity. This change introduces new marker types: - `identity::LocalId`: The local proxy's ID; - `tls::server::ClientId`: A remote client ID; and - `tls::client::ServerId`: A target server ID. Furthermore, the `tls::ReasonForNoPeerName` has been split into distinct `tls::server::NoClientId` and `tls::client::NoServerId` types. This change eliminates the `tls::HasPeerIdentity` and `tls::{client, server}::HasConfig` types, in favor of simple `Into` coercions. This change requires changes to the metric labeling.
1 parent 3541f76 commit 0d6471c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+767
-658
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,8 @@ dependencies = [
10981098
"http",
10991099
"http-body",
11001100
"indexmap",
1101-
"linkerd-identity",
11021101
"linkerd-proxy-core",
1102+
"linkerd-tls",
11031103
"linkerd2-proxy-api",
11041104
"pin-project 1.0.2",
11051105
"prost",

linkerd/app/core/src/control.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub struct Config {
1919
#[derive(Clone, Debug)]
2020
pub struct ControlAddr {
2121
pub addr: Addr,
22-
pub identity: tls::PeerIdentity,
22+
pub identity: tls::ConditionalServerId,
2323
}
2424

2525
impl Into<Addr> for ControlAddr {
@@ -52,7 +52,8 @@ impl Config {
5252
B: http::HttpBody + Send + 'static,
5353
B::Data: Send,
5454
B::Error: Into<Error> + Send + Sync,
55-
I: Clone + tls::client::HasConfig + Send + 'static,
55+
I: Clone + Send + 'static,
56+
for<'i> &'i I: Into<tls::client::Config>,
5657
{
5758
let backoff = {
5859
let backoff = self.connect.backoff;
@@ -208,12 +209,12 @@ mod client {
208209
#[derive(Clone, Hash, Debug, Eq, PartialEq)]
209210
pub struct Target {
210211
addr: SocketAddr,
211-
server_name: tls::PeerIdentity,
212+
server_id: tls::ConditionalServerId,
212213
}
213214

214215
impl Target {
215-
pub(super) fn new(addr: SocketAddr, server_name: tls::PeerIdentity) -> Self {
216-
Self { addr, server_name }
216+
pub(super) fn new(addr: SocketAddr, server_id: tls::ConditionalServerId) -> Self {
217+
Self { addr, server_id }
217218
}
218219
}
219220

@@ -230,9 +231,9 @@ mod client {
230231
}
231232
}
232233

233-
impl tls::HasPeerIdentity for Target {
234-
fn peer_identity(&self) -> tls::PeerIdentity {
235-
self.server_name.clone()
234+
impl Into<tls::ConditionalServerId> for &'_ Target {
235+
fn into(self) -> tls::ConditionalServerId {
236+
self.server_id.clone()
236237
}
237238
}
238239

linkerd/app/core/src/errors.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::proxy::identity;
21
use http::{header::HeaderValue, StatusCode};
32
use linkerd_errno::Errno;
43
use linkerd_error::Error;
@@ -7,6 +6,7 @@ use linkerd_error_respond as respond;
76
pub use linkerd_error_respond::RespondLayer;
87
use linkerd_proxy_http::{client_handle::Close, ClientHandle, HasH2Reason};
98
use linkerd_timeout::{error::ResponseTimeout, FailFastError};
9+
use linkerd_tls as tls;
1010
use pin_project::pin_project;
1111
use std::pin::Pin;
1212
use std::task::{Context, Poll};
@@ -334,8 +334,8 @@ fn code_header(code: grpc::Code) -> HeaderValue {
334334

335335
#[derive(Debug)]
336336
pub struct IdentityRequired {
337-
pub required: identity::Name,
338-
pub found: Option<identity::Name>,
337+
pub required: tls::client::ServerId,
338+
pub found: Option<tls::client::ServerId>,
339339
}
340340

341341
impl std::fmt::Display for IdentityRequired {

linkerd/app/core/src/metrics.rs

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ pub use crate::{
22
classify::{Class, SuccessOrFailure},
33
control, dst, errors, http_metrics, http_metrics as metrics, opencensus, proxy,
44
proxy::identity,
5-
stack_metrics, telemetry,
6-
transport::{self, labels::TlsStatus},
5+
stack_metrics, telemetry, tls,
6+
transport::{
7+
self,
8+
labels::{TlsAccept, TlsConnect},
9+
},
710
};
811
use linkerd_addr::Addr;
912
use linkerd_metrics::FmtLabels;
@@ -42,13 +45,24 @@ pub struct Metrics {
4245
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4346
pub struct ControlLabels {
4447
addr: Addr,
45-
tls_status: TlsStatus,
48+
server_id: tls::ConditionalServerId,
4649
}
4750

4851
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
49-
pub struct EndpointLabels {
50-
pub direction: Direction,
51-
pub tls_id: TlsStatus,
52+
pub enum EndpointLabels {
53+
Inbound(InboundEndpointLabels),
54+
Outbound(OutboundEndpointLabels),
55+
}
56+
57+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
58+
pub struct InboundEndpointLabels {
59+
pub client_id: tls::server::ConditionalTls,
60+
pub authority: Option<http::uri::Authority>,
61+
}
62+
63+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
64+
pub struct OutboundEndpointLabels {
65+
pub server_id: tls::ConditionalServerId,
5266
pub authority: Option<http::uri::Authority>,
5367
pub labels: Option<String>,
5468
}
@@ -73,12 +87,6 @@ pub enum Direction {
7387
Out,
7488
}
7589

76-
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
77-
pub enum TlsId {
78-
ClientId(identity::Name),
79-
ServerId(identity::Name),
80-
}
81-
8290
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8391
struct Authority<'a>(&'a http::uri::Authority);
8492

@@ -189,15 +197,15 @@ impl From<&'_ control::ControlAddr> for ControlLabels {
189197
fn from(c: &'_ control::ControlAddr) -> Self {
190198
ControlLabels {
191199
addr: c.addr.clone(),
192-
tls_status: c.identity.clone().map(TlsId::ServerId).into(),
200+
server_id: c.identity.clone(),
193201
}
194202
}
195203
}
196204

197205
impl FmtLabels for ControlLabels {
198206
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199207
write!(f, "addr=\"{}\",", self.addr)?;
200-
self.tls_status.fmt_labels(f)?;
208+
TlsConnect::from(&self.server_id).fmt_labels(f)?;
201209

202210
Ok(())
203211
}
@@ -230,31 +238,72 @@ impl FmtLabels for RouteLabels {
230238

231239
// === impl EndpointLabels ===
232240

241+
impl From<InboundEndpointLabels> for EndpointLabels {
242+
fn from(i: InboundEndpointLabels) -> Self {
243+
Self::Inbound(i)
244+
}
245+
}
246+
247+
impl From<OutboundEndpointLabels> for EndpointLabels {
248+
fn from(i: OutboundEndpointLabels) -> Self {
249+
Self::Outbound(i)
250+
}
251+
}
252+
233253
impl FmtLabels for EndpointLabels {
234254
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235-
let authority = self.authority.as_ref().map(Authority);
236-
(&self.direction, authority).fmt_labels(f)?;
255+
match self {
256+
Self::Inbound(i) => (Direction::In, i).fmt_labels(f),
257+
Self::Outbound(o) => (Direction::Out, o).fmt_labels(f),
258+
}
259+
}
260+
}
237261

238-
if let Some(labels) = self.labels.as_ref() {
239-
write!(f, ",{}", labels)?;
262+
impl FmtLabels for InboundEndpointLabels {
263+
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
264+
if let Some(a) = self.authority.as_ref() {
265+
Authority(a).fmt_labels(f)?;
266+
write!(f, ",")?;
240267
}
241268

242-
write!(f, ",")?;
243-
self.tls_id.fmt_labels(f)?;
269+
TlsAccept::from(&self.client_id).fmt_labels(f)?;
244270

245271
Ok(())
246272
}
247273
}
248274

249-
impl FmtLabels for Direction {
275+
impl FmtLabels for OutboundEndpointLabels {
250276
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
277+
if let Some(a) = self.authority.as_ref() {
278+
Authority(a).fmt_labels(f)?;
279+
write!(f, ",")?;
280+
}
281+
282+
TlsConnect::from(&self.server_id).fmt_labels(f)?;
283+
284+
if let Some(labels) = self.labels.as_ref() {
285+
write!(f, ",{}", labels)?;
286+
}
287+
288+
Ok(())
289+
}
290+
}
291+
292+
impl fmt::Display for Direction {
293+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
251294
match self {
252-
Direction::In => write!(f, "direction=\"inbound\""),
253-
Direction::Out => write!(f, "direction=\"outbound\""),
295+
Self::In => write!(f, "inbound"),
296+
Self::Out => write!(f, "outbound"),
254297
}
255298
}
256299
}
257300

301+
impl FmtLabels for Direction {
302+
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
303+
write!(f, "direction=\"{}\"", self)
304+
}
305+
}
306+
258307
impl<'a> FmtLabels for Authority<'a> {
259308
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260309
write!(f, "authority=\"{}\"", self.0)
@@ -286,15 +335,6 @@ impl fmt::Display for SuccessOrFailure {
286335
}
287336
}
288337

289-
impl FmtLabels for TlsId {
290-
fn fmt_labels(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
291-
match self {
292-
TlsId::ClientId(ref id) => write!(f, "client_id=\"{}\"", id.as_ref()),
293-
TlsId::ServerId(ref id) => write!(f, "server_id=\"{}\"", id.as_ref()),
294-
}
295-
}
296-
}
297-
298338
// === impl StackLabels ===
299339

300340
impl StackLabels {

0 commit comments

Comments
 (0)