11pub use crate :: metrics:: { Direction , OutboundEndpointLabels } ;
22use linkerd_conditional:: Conditional ;
33use linkerd_metrics:: FmtLabels ;
4+ use linkerd_server_policy as policy;
45use linkerd_tls as tls;
56use std:: { fmt, net:: SocketAddr } ;
67
@@ -11,13 +12,17 @@ use std::{fmt, net::SocketAddr};
1112/// Implements `FmtLabels`.
1213#[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
1314pub enum Key {
14- Accept {
15- direction : Direction ,
16- tls : tls:: ConditionalServerTls ,
17- target_addr : SocketAddr ,
18- } ,
19- OutboundConnect ( OutboundEndpointLabels ) ,
20- InboundConnect ,
15+ Server ( ServerLabels ) ,
16+ OutboundClient ( OutboundEndpointLabels ) ,
17+ InboundClient ,
18+ }
19+
20+ #[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
21+ pub struct ServerLabels {
22+ direction : Direction ,
23+ tls : tls:: ConditionalServerTls ,
24+ target_addr : SocketAddr ,
25+ policy : Option < PolicyLabels > ,
2126}
2227
2328#[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
@@ -29,40 +34,45 @@ pub(crate) struct TlsConnect<'t>(&'t tls::ConditionalClientTls);
2934#[ derive( Copy , Clone , Debug , Eq , PartialEq , Hash ) ]
3035pub ( crate ) struct TargetAddr ( pub ( crate ) SocketAddr ) ;
3136
37+ #[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
38+ pub ( crate ) struct PolicyLabels {
39+ server : policy:: Labels ,
40+ authz : policy:: Labels ,
41+ }
42+
3243// === impl Key ===
3344
3445impl Key {
35- pub fn accept (
36- direction : Direction ,
46+ pub fn inbound_server (
3747 tls : tls:: ConditionalServerTls ,
3848 target_addr : SocketAddr ,
49+ server : policy:: Labels ,
50+ authz : policy:: Labels ,
3951 ) -> Self {
40- Self :: Accept {
41- direction,
52+ Self :: Server ( ServerLabels :: inbound (
4253 tls,
4354 target_addr,
44- }
55+ PolicyLabels { server, authz } ,
56+ ) )
57+ }
58+
59+ pub fn outbound_server ( target_addr : SocketAddr ) -> Self {
60+ Self :: Server ( ServerLabels :: outbound ( target_addr) )
4561 }
4662}
4763
4864impl FmtLabels for Key {
4965 fn fmt_labels ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
5066 match self {
51- Self :: Accept {
52- direction,
53- tls,
54- target_addr,
55- } => {
56- direction. fmt_labels ( f) ?;
57- f. write_str ( ",peer=\" src\" ," ) ?;
58- ( TargetAddr ( * target_addr) , TlsAccept :: from ( tls) ) . fmt_labels ( f)
59- }
60- Self :: OutboundConnect ( endpoint) => {
67+ Self :: Server ( l) => l. fmt_labels ( f) ,
68+
69+ Self :: OutboundClient ( endpoint) => {
6170 Direction :: Out . fmt_labels ( f) ?;
6271 write ! ( f, ",peer=\" dst\" ," ) ?;
6372 endpoint. fmt_labels ( f)
6473 }
65- Self :: InboundConnect => {
74+
75+ Self :: InboundClient => {
6676 const NO_TLS : tls:: client:: ConditionalClientTls =
6777 Conditional :: None ( tls:: NoClientTls :: Loopback ) ;
6878
@@ -74,6 +84,49 @@ impl FmtLabels for Key {
7484 }
7585}
7686
87+ impl ServerLabels {
88+ fn inbound (
89+ tls : tls:: ConditionalServerTls ,
90+ target_addr : SocketAddr ,
91+ policy : PolicyLabels ,
92+ ) -> Self {
93+ ServerLabels {
94+ direction : Direction :: In ,
95+ tls,
96+ target_addr,
97+ policy : Some ( policy) ,
98+ }
99+ }
100+
101+ fn outbound ( target_addr : SocketAddr ) -> Self {
102+ ServerLabels {
103+ direction : Direction :: Out ,
104+ tls : tls:: ConditionalServerTls :: None ( tls:: NoServerTls :: Loopback ) ,
105+ target_addr,
106+ policy : None ,
107+ }
108+ }
109+ }
110+
111+ impl FmtLabels for ServerLabels {
112+ fn fmt_labels ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
113+ self . direction . fmt_labels ( f) ?;
114+ f. write_str ( ",peer=\" src\" ," ) ?;
115+ ( TargetAddr ( self . target_addr ) , TlsAccept ( & self . tls ) ) . fmt_labels ( f) ?;
116+
117+ if let Some ( policy) = self . policy . as_ref ( ) {
118+ for ( k, v) in policy. server . iter ( ) {
119+ write ! ( f, ",srv_{}=\" {}\" " , k, v) ?;
120+ }
121+ for ( k, v) in policy. authz . iter ( ) {
122+ write ! ( f, ",saz_{}=\" {}\" " , k, v) ?;
123+ }
124+ }
125+
126+ Ok ( ( ) )
127+ }
128+ }
129+
77130// === impl TlsAccept ===
78131
79132impl < ' t > From < & ' t tls:: ConditionalServerTls > for TlsAccept < ' t > {
@@ -133,3 +186,38 @@ impl FmtLabels for TargetAddr {
133186 write ! ( f, "target_addr=\" {}\" " , self . 0 )
134187 }
135188}
189+
190+ #[ cfg( test) ]
191+ mod tests {
192+ pub use super :: * ;
193+
194+ impl std:: fmt:: Display for ServerLabels {
195+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
196+ self . fmt_labels ( f)
197+ }
198+ }
199+
200+ #[ test]
201+ fn server_labels ( ) {
202+ let labels = ServerLabels :: inbound (
203+ tls:: ConditionalServerTls :: Some ( tls:: ServerTls :: Established {
204+ client_id : Some ( "foo.id.example.com" . parse ( ) . unwrap ( ) ) ,
205+ negotiated_protocol : None ,
206+ } ) ,
207+ ( [ 192 , 0 , 2 , 4 ] , 40000 ) . into ( ) ,
208+ PolicyLabels {
209+ server : vec ! [ ( "name" . to_string( ) , "testserver" . to_string( ) ) ]
210+ . into_iter ( )
211+ . collect ( ) ,
212+ authz : vec ! [ ( "name" . to_string( ) , "testauthz" . to_string( ) ) ]
213+ . into_iter ( )
214+ . collect ( ) ,
215+ } ,
216+ ) ;
217+ assert_eq ! (
218+ labels. to_string( ) ,
219+ "direction=\" inbound\" ,peer=\" src\" ,target_addr=\" 192.0.2.4:40000\" ,tls=\" true\" ,\
220+ client_id=\" foo.id.example.com\" ,srv_name=\" testserver\" ,saz_name=\" testauthz\" "
221+ ) ;
222+ }
223+ }
0 commit comments