@@ -332,11 +332,19 @@ async fn build_tls_server_config(tls_config: &TlsConfig) -> Result<rustls::Serve
332
332
let key = PrivateKeyDer :: try_from ( key_vec. pop ( ) . unwrap ( ) )
333
333
. map_err ( |e| anyhow ! ( "Cannot parse server key: {e}" ) ) ?;
334
334
335
- if let Some ( client_ca_file) = tls_config. client_ca_file . clone ( ) {
335
+ if tls_config. client_ca_file . is_empty ( ) {
336
+ return Ok ( ServerConfig :: builder ( )
337
+ . with_no_client_auth ( )
338
+ . with_single_cert ( cert, key) ?) ;
339
+ }
340
+
341
+ let mut store = RootCertStore :: empty ( ) ;
342
+
343
+ //mTLS enabled
344
+ for client_ca_file in tls_config. client_ca_file . clone ( ) {
336
345
// we have the client CA. Therefore, we should enable mTLS.
337
346
let client_ca_reader = & mut BufReader :: new ( File :: open ( client_ca_file) ?) ;
338
347
339
- let mut store = RootCertStore :: empty ( ) ;
340
348
let client_ca_certs: Vec < _ > = rustls_pemfile:: certs ( client_ca_reader)
341
349
. filter_map ( |it| {
342
350
if let Err ( ref e) = it {
@@ -351,15 +359,10 @@ async fn build_tls_server_config(tls_config: &TlsConfig) -> Result<rustls::Serve
351
359
client_ca_certs_ignored = cert_ignored,
352
360
"Loaded client CA certificates"
353
361
) ;
354
- let client_verifier = WebPkiClientVerifier :: builder ( Arc :: new ( store) ) . build ( ) ?;
355
-
356
- return Ok ( ServerConfig :: builder ( )
357
- . with_client_cert_verifier ( client_verifier)
358
- . with_single_cert ( cert, key) ?) ;
359
362
}
360
-
363
+ let client_verifier = WebPkiClientVerifier :: builder ( Arc :: new ( store ) ) . build ( ) ? ;
361
364
Ok ( ServerConfig :: builder ( )
362
- . with_no_client_auth ( )
365
+ . with_client_cert_verifier ( client_verifier )
363
366
. with_single_cert ( cert, key) ?)
364
367
}
365
368
@@ -385,11 +388,12 @@ async fn create_tls_config_and_watch_certificate_changes(
385
388
) -> Result < RustlsConfig > {
386
389
use :: tracing:: error;
387
390
388
- let config = build_tls_server_config ( & tls_config ) . await ? ;
389
-
390
- let rust_config = RustlsConfig :: from_config ( Arc :: new ( config ) ) ;
391
+ // Build initial TLS configuration
392
+ let initial_config = build_tls_server_config ( & tls_config ) . await ? ;
393
+ let rust_config = RustlsConfig :: from_config ( Arc :: new ( initial_config ) ) ;
391
394
let reloadable_rust_config = rust_config. clone ( ) ;
392
395
396
+ // Init inotify to watch for changes in the certificate files
393
397
let inotify =
394
398
inotify:: Inotify :: init ( ) . map_err ( |e| anyhow ! ( "Cannot initialize inotify: {e}" ) ) ?;
395
399
let cert_watch = inotify
@@ -404,15 +408,18 @@ async fn create_tls_config_and_watch_certificate_changes(
404
408
. add ( tls_config. key_file . clone ( ) , inotify:: WatchMask :: CLOSE_WRITE )
405
409
. map_err ( |e| anyhow ! ( "Cannot watch key file: {e}" ) ) ?;
406
410
407
- let mut client_cert_watch = None ;
408
- if let Some ( ref client_ca_file) = tls_config. client_ca_file {
409
- client_cert_watch = Some (
411
+ let client_cert_watches = tls_config
412
+ . client_ca_file
413
+ . clone ( )
414
+ . into_iter ( )
415
+ . map ( |path| {
410
416
inotify
411
417
. watches ( )
412
- . add ( client_ca_file, inotify:: WatchMask :: CLOSE_WRITE )
413
- . map_err ( |e| anyhow ! ( "Cannot watch client certificate file: {e}" ) ) ?,
414
- ) ;
415
- }
418
+ . add ( path, inotify:: WatchMask :: CLOSE_WRITE )
419
+ . map_err ( |e| anyhow ! ( "Cannot watch client certificate file: {e}" ) )
420
+ . unwrap ( )
421
+ } )
422
+ . collect :: < Vec < _ > > ( ) ;
416
423
417
424
let buffer = [ 0 ; 1024 ] ;
418
425
let stream = inotify
@@ -442,7 +449,8 @@ async fn create_tls_config_and_watch_certificate_changes(
442
449
info ! ( "TLS key file has been modified" ) ;
443
450
key_changed = true ;
444
451
}
445
- if let Some ( ref client_cert_watch) = client_cert_watch {
452
+
453
+ for client_cert_watch in client_cert_watches. iter ( ) {
446
454
if event. wd == * client_cert_watch {
447
455
info ! ( "TLS client certificate file has been modified" ) ;
448
456
client_cert_changed = true ;
@@ -454,11 +462,12 @@ async fn create_tls_config_and_watch_certificate_changes(
454
462
if ( key_changed && cert_changed)
455
463
|| ( client_cert_changed && ( key_changed == cert_changed) )
456
464
{
457
- info ! ( "reloading TLS certificates" ) ;
465
+ info ! ( "Reloading TLS certificates" ) ;
458
466
459
467
cert_changed = false ;
460
468
key_changed = false ;
461
469
client_cert_changed = false ;
470
+
462
471
let server_config = build_tls_server_config ( & tls_config) . await ;
463
472
if let Err ( e) = server_config {
464
473
error ! ( "Failed to reload TLS certificate: {}" , e) ;
@@ -468,7 +477,6 @@ async fn create_tls_config_and_watch_certificate_changes(
468
477
}
469
478
}
470
479
} ) ;
471
-
472
480
Ok ( rust_config)
473
481
}
474
482
0 commit comments