-
Notifications
You must be signed in to change notification settings - Fork 264
/
identity.rs
470 lines (409 loc) · 16.2 KB
/
identity.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use crate::*;
use std::{
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
time::{Duration, SystemTime},
};
#[tokio::test]
async fn nonblocking_identity_detection() {
let _trace = trace_init();
let id = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let identity::Identity {
env,
mut certify_rsp,
..
} = identity::Identity::new("foo-ns1", id.to_string());
certify_rsp.valid_until = Some((SystemTime::now() + Duration::from_secs(666)).into());
let id_svc = controller::identity()
.certify(move |_| certify_rsp)
.run()
.await;
let proxy = proxy::new().identity(id_svc);
let msg1 = "custom tcp hello";
let msg2 = "custom tcp bye";
let srv = server::tcp()
.accept(move |read| {
assert_eq!(read, msg1.as_bytes());
msg2
})
.run()
.await;
let proxy = proxy.inbound(srv).run_with_test_env(env).await;
let client = client::tcp(proxy.inbound);
// Create an idle connection and then an active connection. Ensure that
// protocol detection on the idle connection does not block communication on
// the active connection.
let _idle = client.connect().await;
let active = client.connect().await;
active.write(msg1).await;
assert_eq!(
active.read_timeout(Duration::from_secs(2)).await,
msg2.as_bytes()
);
active.shutdown().await; // ensure the client task joins without panicking.
}
macro_rules! generate_tls_accept_test {
( client_non_tls: $make_client_non_tls:path, client_tls: $make_client_tls:path) => {
let _trace = trace_init();
let id = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let id_svc = identity::Identity::new("foo-ns1", id.to_string());
let proxy = proxy::new()
.identity(id_svc.service().run().await)
.run_with_test_env(id_svc.env)
.await;
println!("non-tls request to {}", proxy.metrics);
let non_tls_client = $make_client_non_tls(proxy.metrics, "localhost");
assert_eventually!(
non_tls_client
.request(non_tls_client.request_builder("/ready").method("GET"))
.await
.unwrap()
.status()
== http::StatusCode::OK
);
println!("tls request to {}", proxy.metrics);
let tls_client = $make_client_tls(
proxy.metrics,
"localhost",
client::TlsConfig::new(id_svc.client_config, id),
);
assert_eventually!(
tls_client
.request(tls_client.request_builder("/ready").method("GET"))
.await
.unwrap()
.status()
== http::StatusCode::OK
);
};
}
macro_rules! generate_tls_reject_test {
( client: $make_client:path) => {
let _trace = trace_init();
let id = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let identity::Identity {
env,
mut certify_rsp,
client_config,
..
} = identity::Identity::new("foo-ns1", id.to_string());
certify_rsp.valid_until = Some((SystemTime::now() + Duration::from_secs(666)).into());
let (_tx, rx) = oneshot::channel();
let id_svc = controller::identity()
.certify_async(move |_| rx)
.run()
.await;
let proxy = proxy::new().identity(id_svc).run_with_test_env(env).await;
let client = $make_client(
proxy.metrics,
"localhost",
client::TlsConfig::new(client_config, id),
);
assert!(client
.request(client.request_builder("/ready").method("GET"))
.await
.err()
.unwrap()
.is_connect());
};
}
#[tokio::test]
async fn http1_accepts_tls_after_identity_is_certified() {
generate_tls_accept_test! {
client_non_tls: client::http1,
client_tls: client::http1_tls
}
}
#[tokio::test]
async fn http1_rejects_tls_before_identity_is_certified() {
generate_tls_reject_test! {client: client::http1_tls}
}
#[tokio::test]
async fn http2_accepts_tls_after_identity_is_certified() {
generate_tls_accept_test! {
client_non_tls: client::http2,
client_tls: client::http2_tls
}
}
#[tokio::test]
async fn http2_rejects_tls_before_identity_is_certified() {
generate_tls_reject_test! {client: client::http2_tls}
}
#[tokio::test]
async fn ready() {
let _trace = trace_init();
let id = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let identity::Identity {
env,
mut certify_rsp,
..
} = identity::Identity::new("foo-ns1", id.to_string());
certify_rsp.valid_until = Some((SystemTime::now() + Duration::from_secs(666)).into());
let (tx, rx) = oneshot::channel();
let id_svc = controller::identity()
.certify_async(move |_| rx)
.run()
.await;
let proxy = proxy::new().identity(id_svc).run_with_test_env(env).await;
let client = client::http1(proxy.metrics, "localhost");
let ready = || async {
client
.request(client.request_builder("/ready").method("GET"))
.await
.unwrap()
};
// The proxy's identity has not yet been verified, so it should not be
// considered ready.
assert_ne!(ready().await.status(), http::StatusCode::OK);
// Make the mock identity service respond to the certify request.
tx.send(certify_rsp)
.expect("certify rx should not be dropped");
// Now, the proxy should be ready.
assert_eventually!(ready().await.status() == http::StatusCode::OK);
}
#[tokio::test]
async fn refresh() {
let _trace = trace_init();
let id = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let identity::Identity {
mut env,
certify_rsp,
client_config: _,
server_config: _,
} = identity::Identity::new("foo-ns1", id.to_string());
let (expiry_tx, expiry_rx) = oneshot::channel();
let refreshed = Arc::new(AtomicBool::new(false));
let rsp1 = certify_rsp.clone();
let rsp2 = certify_rsp.clone();
let refreshed2 = refreshed.clone();
let id_svc = controller::identity()
.certify(move |_| {
let mut rsp = rsp1;
let expiry = SystemTime::now() + Duration::from_millis(20);
rsp.valid_until = Some(expiry.into());
expiry_tx.send(expiry).unwrap();
rsp
})
.certify(move |_| {
let mut rsp = rsp2;
rsp.valid_until = Some((SystemTime::now() + Duration::from_millis(6000)).into());
refreshed2.store(true, Ordering::SeqCst);
rsp
})
.run()
.await;
// Disable the minimum bound on when to refresh identity for this test.
env.put(app::env::ENV_IDENTITY_MIN_REFRESH, "0ms".into());
let _proxy = proxy::new().identity(id_svc).run_with_test_env(env).await;
let expiry = expiry_rx.await.expect("wait for expiry");
let how_long = expiry.duration_since(SystemTime::now()).unwrap();
tokio::time::sleep(how_long).await;
assert_eventually!(refreshed.load(Ordering::SeqCst) == true);
}
mod require_id_header {
macro_rules! generate_tests {
(
client: $make_client:path,
connect_error: $connect_error:expr,
server: $make_server:path,
tls_server: $make_tls_server:path,
) => {
#[tokio::test]
#[cfg_attr(not(feature = "flaky_tests"), ignore)]
async fn orig_dst_client_connects_to_tls_server() {
let _trace = trace_init();
let proxy_name = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let proxy_identity = identity::Identity::new("foo-ns1", proxy_name.to_string());
let app_name = "bar.ns1.serviceaccount.identity.linkerd.cluster.local";
let app_identity = identity::Identity::new("bar-ns1", app_name.to_string());
// Make a server that expects `app_identity` identity
let srv = $make_tls_server(app_identity.server_config)
.route("/", "hello")
.run()
.await;
let ctrl = controller::new();
let _profile = ctrl.profile_tx_default(srv.addr, "disco.test.svc.cluster.local");
let dst = ctrl
.destination_tx(format!("disco.test.svc.cluster.local:{}", srv.addr.port()));
dst.send_addr(srv.addr);
// Make a proxy that has `proxy_identity` identity
let proxy = proxy::new()
.outbound(srv)
.controller(ctrl.run().await)
.identity(proxy_identity.service().run().await)
.run_with_test_env(proxy_identity.env)
.await;
// Make a non-TLS client
let client = $make_client(proxy.outbound, app_name);
// Assert a request to `srv` with `l5d-require-id` header
// succeeds
assert_eq!(
client
.request(
client
.request_builder("/")
.header("l5d-require-id", app_name)
.method("GET")
)
.await
.unwrap()
.status(),
http::StatusCode::OK
);
// Assert a request to `srv` with incorrect `l5d-require-id`
// header fails
//
// Fails because of reconnect backoff; results in status code
// 502
assert_eq!(
client
.request(
client
.request_builder("/")
.header("l5d-require-id", "hey-its-me")
.method("GET")
)
.await
.unwrap()
.status(),
$connect_error
);
}
#[tokio::test]
#[cfg_attr(not(feature = "flaky_tests"), ignore)]
async fn disco_client_connects_to_tls_server() {
let _trace = trace_init();
let proxy_name = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let proxy_identity = identity::Identity::new("foo-ns1", proxy_name.to_string());
let app_name = "bar.ns1.serviceaccount.identity.linkerd.cluster.local";
let app_identity = identity::Identity::new("bar-ns1", app_name.to_string());
// Make a server that expects `app_identity` identity
let srv = $make_tls_server(app_identity.server_config)
.route("/", "hello")
.run()
.await;
// Add `srv` to discovery service
let ctrl = controller::new();
let _profile = ctrl.profile_tx_default(srv.addr, "disco.test.svc.cluster.local");
let dst = ctrl
.destination_tx(format!("disco.test.svc.cluster.local:{}", srv.addr.port()));
dst.send(controller::destination_add_tls(srv.addr, app_name));
// Make a proxy that has `proxy_identity` identity and no
// SO_ORIGINAL_DST backup
let proxy = proxy::new()
.controller(ctrl.run().await)
.identity(proxy_identity.service().run().await)
.run_with_test_env(proxy_identity.env)
.await;
// Make a non-TLS client
let client = $make_client(proxy.outbound, "disco.test.svc.cluster.local");
// Assert a request to `srv` through discovery succeeds
assert_eq!(
client
.request(client.request_builder("/").method("GET"))
.await
.unwrap()
.status(),
http::StatusCode::OK
);
// Assert a request to `srv` through discovery with incorrect
// `l5d-require-id` fails
assert_eq!(
client
.request(
client
.request_builder("/")
.header("l5d-require-id", "hey-its-me")
.method("GET")
)
.await
.unwrap()
.status(),
http::StatusCode::FORBIDDEN
);
// Assert a request to `srv` through discovery with
// `l5d-require-id` succeeds
assert_eq!(
client
.request(
client
.request_builder("/")
.header("l5d-require-id", app_name)
.method("GET")
)
.await
.unwrap()
.status(),
http::StatusCode::OK
);
}
#[tokio::test]
#[cfg_attr(not(feature = "flaky_tests"), ignore)]
async fn orig_dst_client_cannot_connect_to_plaintext_server() {
let _trace = trace_init();
let proxy_name = "foo.ns1.serviceaccount.identity.linkerd.cluster.local";
let proxy_identity = identity::Identity::new("foo-ns1", proxy_name.to_string());
// Make a non-TLS server
let srv = $make_server().route("/", "hello").run().await;
let ctrl = controller::new();
let _profile = ctrl.profile_tx_default(srv.addr, "disco.test.svc.cluster.local");
let dst = ctrl
.destination_tx(format!("disco.test.svc.cluster.local:{}", srv.addr.port()));
dst.send_addr(srv.addr);
// Make a proxy that has `proxy_identity` identity
let proxy = proxy::new()
.outbound(srv)
.controller(ctrl.run().await)
.identity(proxy_identity.service().run().await)
.run_with_test_env(proxy_identity.env)
.await;
// Make a non-TLS client
let client = $make_client(proxy.outbound, "localhost");
// Assert a request to `srv` with `l5d-require-id` header fails
assert_eq!(
client
.request(
client
.request_builder("/")
.header("l5d-require-id", "hey-its-me")
.method("GET")
)
.await
.unwrap()
.status(),
$connect_error
);
// Assert a request to `srv` with no `l5d-require-id` header
// succeeds
assert_eq!(
client
.request(client.request_builder("/").method("GET"))
.await
.unwrap()
.status(),
http::StatusCode::OK
);
}
};
}
mod http1 {
use super::super::*;
generate_tests! {
client: client::http1,
connect_error: http::StatusCode::BAD_GATEWAY,
server: server::http1,
tls_server: server::http1_tls,
}
}
mod http2 {
use super::super::*;
generate_tests! {
client: client::http2,
connect_error: http::StatusCode::SERVICE_UNAVAILABLE,
server: server::http2,
tls_server: server::http2_tls,
}
}
}