48
48
import java .util .Collections ;
49
49
import java .util .List ;
50
50
import java .util .concurrent .CountDownLatch ;
51
+ import java .util .concurrent .atomic .AtomicReference ;
51
52
import java .util .function .Consumer ;
52
53
53
54
import static org .hamcrest .Matchers .containsString ;
@@ -309,20 +310,31 @@ public void testReloadingKeyStoreException() throws Exception {
309
310
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
310
311
final SSLService sslService = new SSLService (settings , env );
311
312
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.ssl" );
313
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
314
+ final CountDownLatch latch = new CountDownLatch (1 );
312
315
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
313
316
@ Override
314
317
void reloadSSLContext (SSLConfiguration configuration ) {
315
- fail ("reload should not be called! [keystore reload exception]" );
318
+ try {
319
+ super .reloadSSLContext (configuration );
320
+ } catch (Exception e ) {
321
+ exceptionRef .set (e );
322
+ throw e ;
323
+ } finally {
324
+ latch .countDown ();
325
+ }
316
326
}
317
327
};
318
328
319
329
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
320
330
321
331
// truncate the keystore
322
- try (OutputStream out = Files .newOutputStream (keystorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
332
+ try (OutputStream ignore = Files .newOutputStream (keystorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
323
333
}
324
334
325
- // we intentionally don't wait here as we rely on concurrency to catch a failure
335
+ latch .await ();
336
+ assertNotNull (exceptionRef .get ());
337
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a KeyManagerFactory" ));
326
338
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
327
339
}
328
340
@@ -350,20 +362,31 @@ public void testReloadingPEMKeyConfigException() throws Exception {
350
362
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
351
363
final SSLService sslService = new SSLService (settings , env );
352
364
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.ssl" );
365
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
366
+ final CountDownLatch latch = new CountDownLatch (1 );
353
367
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
354
368
@ Override
355
369
void reloadSSLContext (SSLConfiguration configuration ) {
356
- fail ("reload should not be called! [pem key reload exception]" );
370
+ try {
371
+ super .reloadSSLContext (configuration );
372
+ } catch (Exception e ) {
373
+ exceptionRef .set (e );
374
+ throw e ;
375
+ } finally {
376
+ latch .countDown ();
377
+ }
357
378
}
358
379
};
359
380
360
381
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
361
382
362
383
// truncate the file
363
- try (OutputStream os = Files .newOutputStream (keyPath , StandardOpenOption .TRUNCATE_EXISTING )) {
384
+ try (OutputStream ignore = Files .newOutputStream (keyPath , StandardOpenOption .TRUNCATE_EXISTING )) {
364
385
}
365
386
366
- // we intentionally don't wait here as we rely on concurrency to catch a failure
387
+ latch .await ();
388
+ assertNotNull (exceptionRef .get ());
389
+ assertThat (exceptionRef .get ().getMessage (), containsString ("Error parsing Private Key" ));
367
390
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
368
391
}
369
392
@@ -385,20 +408,31 @@ public void testTrustStoreReloadException() throws Exception {
385
408
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
386
409
final SSLService sslService = new SSLService (settings , env );
387
410
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.ssl" );
411
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
412
+ final CountDownLatch latch = new CountDownLatch (1 );
388
413
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
389
414
@ Override
390
415
void reloadSSLContext (SSLConfiguration configuration ) {
391
- fail ("reload should not be called! [truststore reload exception]" );
416
+ try {
417
+ super .reloadSSLContext (configuration );
418
+ } catch (Exception e ) {
419
+ exceptionRef .set (e );
420
+ throw e ;
421
+ } finally {
422
+ latch .countDown ();
423
+ }
392
424
}
393
425
};
394
426
395
427
final SSLContext context = sslService .sslContextHolder (config ).sslContext ();
396
428
397
429
// truncate the truststore
398
- try (OutputStream os = Files .newOutputStream (trustStorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
430
+ try (OutputStream ignore = Files .newOutputStream (trustStorePath , StandardOpenOption .TRUNCATE_EXISTING )) {
399
431
}
400
432
401
- // we intentionally don't wait here as we rely on concurrency to catch a failure
433
+ latch .await ();
434
+ assertNotNull (exceptionRef .get ());
435
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a TrustManagerFactory" ));
402
436
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
403
437
}
404
438
@@ -417,10 +451,19 @@ public void testPEMTrustReloadException() throws Exception {
417
451
Environment env = randomBoolean () ? null : TestEnvironment .newEnvironment (settings );
418
452
final SSLService sslService = new SSLService (settings , env );
419
453
final SSLConfiguration config = sslService .getSSLConfiguration ("xpack.ssl" );
454
+ final AtomicReference <Exception > exceptionRef = new AtomicReference <>();
455
+ final CountDownLatch latch = new CountDownLatch (1 );
420
456
new SSLConfigurationReloader (env , sslService , resourceWatcherService ) {
421
457
@ Override
422
458
void reloadSSLContext (SSLConfiguration configuration ) {
423
- fail ("reload should not be called! [pem trust reload exception]" );
459
+ try {
460
+ super .reloadSSLContext (configuration );
461
+ } catch (Exception e ) {
462
+ exceptionRef .set (e );
463
+ throw e ;
464
+ } finally {
465
+ latch .countDown ();
466
+ }
424
467
}
425
468
};
426
469
@@ -433,9 +476,10 @@ void reloadSSLContext(SSLConfiguration configuration) {
433
476
}
434
477
atomicMoveIfPossible (updatedCert , clientCertPath );
435
478
436
- // we intentionally don't wait here as we rely on concurrency to catch a failure
479
+ latch .await ();
480
+ assertNotNull (exceptionRef .get ());
481
+ assertThat (exceptionRef .get ().getMessage (), containsString ("failed to initialize a TrustManagerFactory" ));
437
482
assertThat (sslService .sslContextHolder (config ).sslContext (), sameInstance (context ));
438
-
439
483
}
440
484
441
485
private void validateSSLConfigurationIsReloaded (Settings settings , Environment env , Consumer <SSLContext > preChecks ,
0 commit comments