67
67
import java .util .List ;
68
68
import java .util .concurrent .CountDownLatch ;
69
69
import java .util .concurrent .TimeUnit ;
70
+ import java .util .concurrent .atomic .AtomicReference ;
70
71
import java .util .function .Consumer ;
71
72
72
73
import static org .hamcrest .Matchers .containsString ;
@@ -330,20 +331,31 @@ public void testReloadingKeyStoreException() throws Exception {
330
331
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
331
332
final SSLService sslService = new SSLService (settings , env );
332
333
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.security.transport.ssl." );
334
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
335
+ final CountDownLatch latch = new CountDownLatch (1 );
333
336
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
334
337
@ Override
335
338
void reloadSSLContext (SSLConfiguration configuration ) {
336
- fail ("reload should not be called! [keystore reload exception]" );
339
+ try {
340
+ super .reloadSSLContext (configuration );
341
+ } catch (Exception e ) {
342
+ exceptionRef .set (e );
343
+ throw e ;
344
+ } finally {
345
+ latch .countDown ();
346
+ }
337
347
}
338
348
};
339
349
340
350
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
341
351
342
352
// truncate the keystore
343
- try (OutputStream out = Files .newOutputStream (keystorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
353
+ try (OutputStream ignore = Files .newOutputStream (keystorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
344
354
}
345
355
346
- // we intentionally don't wait here as we rely on concurrency to catch a failure
356
+ latch .await ();
357
+ assertNotNull (exceptionRef .get ());
358
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a KeyManagerFactory" ));
347
359
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
348
360
}
349
361
@@ -371,20 +383,31 @@ public void testReloadingPEMKeyConfigException() throws Exception {
371
383
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
372
384
final SSLService sslService = new SSLService (settings , env );
373
385
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.security.transport.ssl." );
386
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
387
+ final CountDownLatch latch = new CountDownLatch (1 );
374
388
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
375
389
@ Override
376
390
void reloadSSLContext (SSLConfiguration configuration ) {
377
- fail ("reload should not be called! [pem key reload exception]" );
391
+ try {
392
+ super .reloadSSLContext (configuration );
393
+ } catch (Exception e ) {
394
+ exceptionRef .set (e );
395
+ throw e ;
396
+ } finally {
397
+ latch .countDown ();
398
+ }
378
399
}
379
400
};
380
401
381
402
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
382
403
383
404
// truncate the file
384
- try (OutputStream os = Files .newOutputStream (keyPath , StandardOpenOption .TRUNCATE_EXISTING )) {
405
+ try (OutputStream ignore = Files .newOutputStream (keyPath , StandardOpenOption .TRUNCATE_EXISTING )) {
385
406
}
386
407
387
- // we intentionally don't wait here as we rely on concurrency to catch a failure
408
+ latch .await ();
409
+ assertNotNull (exceptionRef .get ());
410
+ assertThat (exceptionRef .get ().getMessage (), containsString ("Error parsing Private Key" ));
388
411
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
389
412
}
390
413
@@ -406,20 +429,31 @@ public void testTrustStoreReloadException() throws Exception {
406
429
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
407
430
final SSLService sslService = new SSLService (settings , env );
408
431
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.security.transport.ssl." );
432
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
433
+ final CountDownLatch latch = new CountDownLatch (1 );
409
434
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
410
435
@ Override
411
436
void reloadSSLContext (SSLConfiguration configuration ) {
412
- fail ("reload should not be called! [truststore reload exception]" );
437
+ try {
438
+ super .reloadSSLContext (configuration );
439
+ } catch (Exception e ) {
440
+ exceptionRef .set (e );
441
+ throw e ;
442
+ } finally {
443
+ latch .countDown ();
444
+ }
413
445
}
414
446
};
415
447
416
448
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
417
449
418
450
// truncate the truststore
419
- try (OutputStream os = Files .newOutputStream (trustStorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
451
+ try (OutputStream ignore = Files .newOutputStream (trustStorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
420
452
}
421
453
422
- // we intentionally don't wait here as we rely on concurrency to catch a failure
454
+ latch .await ();
455
+ assertNotNull (exceptionRef .get ());
456
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a TrustManagerFactory" ));
423
457
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
424
458
}
425
459
@@ -438,10 +472,19 @@ public void testPEMTrustReloadException() throws Exception {
438
472
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
439
473
final SSLService sslService = new SSLService (settings , env );
440
474
final SSLConfiguration config = sslService .sslConfiguration (settings .getByPrefix ("xpack.security.transport.ssl." ));
475
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
476
+ final CountDownLatch latch = new CountDownLatch (1 );
441
477
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
442
478
@ Override
443
479
void reloadSSLContext (SSLConfiguration configuration ) {
444
- fail ("reload should not be called! [pem trust reload exception]" );
480
+ try {
481
+ super .reloadSSLContext (configuration );
482
+ } catch (Exception e ) {
483
+ exceptionRef .set (e );
484
+ throw e ;
485
+ } finally {
486
+ latch .countDown ();
487
+ }
445
488
}
446
489
};
447
490
@@ -454,9 +497,10 @@ void reloadSSLContext(SSLConfiguration configuration) {
454
497
}
455
498
atomicMoveIfPossible (updatedCert , clientCertPath );
456
499
457
- // we intentionally don't wait here as we rely on concurrency to catch a failure
500
+ latch .await ();
501
+ assertNotNull (exceptionRef .get ());
502
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a TrustManagerFactory" ));
458
503
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
459
-
460
504
}
461
505
462
506
private void validateSSLConfigurationIsReloaded (Settings settings , Environment env , Consumer <SSLContext > preChecks ,
0 commit comments