Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;

import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -330,20 +331,31 @@ public void testReloadingKeyStoreException() throws Exception {
Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
new SSLConfigurationReloader(env, sslService, resourceWatcherService) {
@Override
void reloadSSLContext(SSLConfiguration configuration) {
fail("reload should not be called! [keystore reload exception]");
try {
super.reloadSSLContext(configuration);
} catch (Exception e) {
exceptionRef.set(e);
throw e;
} finally {
latch.countDown();
}
}
};

final SSLContext context = sslService.sslContextHolder(config).sslContext();

// truncate the keystore
try (OutputStream out = Files.newOutputStream(keystorePath, StandardOpenOption.TRUNCATE_EXISTING)) {
try (OutputStream ignore = Files.newOutputStream(keystorePath, StandardOpenOption.TRUNCATE_EXISTING)) {
}

// we intentionally don't wait here as we rely on concurrency to catch a failure
latch.await();
assertNotNull(exceptionRef.get());
assertThat(exceptionRef.get().getMessage(), containsString("failed to initialize a KeyManagerFactory"));
assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));
}

Expand Down Expand Up @@ -371,20 +383,31 @@ public void testReloadingPEMKeyConfigException() throws Exception {
Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
new SSLConfigurationReloader(env, sslService, resourceWatcherService) {
@Override
void reloadSSLContext(SSLConfiguration configuration) {
fail("reload should not be called! [pem key reload exception]");
try {
super.reloadSSLContext(configuration);
} catch (Exception e) {
exceptionRef.set(e);
throw e;
} finally {
latch.countDown();
}
}
};

final SSLContext context = sslService.sslContextHolder(config).sslContext();

// truncate the file
try (OutputStream os = Files.newOutputStream(keyPath, StandardOpenOption.TRUNCATE_EXISTING)) {
try (OutputStream ignore = Files.newOutputStream(keyPath, StandardOpenOption.TRUNCATE_EXISTING)) {
}

// we intentionally don't wait here as we rely on concurrency to catch a failure
latch.await();
assertNotNull(exceptionRef.get());
assertThat(exceptionRef.get().getMessage(), containsString("Error parsing Private Key"));
assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));
}

Expand All @@ -406,20 +429,31 @@ public void testTrustStoreReloadException() throws Exception {
Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
new SSLConfigurationReloader(env, sslService, resourceWatcherService) {
@Override
void reloadSSLContext(SSLConfiguration configuration) {
fail("reload should not be called! [truststore reload exception]");
try {
super.reloadSSLContext(configuration);
} catch (Exception e) {
exceptionRef.set(e);
throw e;
} finally {
latch.countDown();
}
}
};

final SSLContext context = sslService.sslContextHolder(config).sslContext();

// truncate the truststore
try (OutputStream os = Files.newOutputStream(trustStorePath, StandardOpenOption.TRUNCATE_EXISTING)) {
try (OutputStream ignore = Files.newOutputStream(trustStorePath, StandardOpenOption.TRUNCATE_EXISTING)) {
}

// we intentionally don't wait here as we rely on concurrency to catch a failure
latch.await();
assertNotNull(exceptionRef.get());
assertThat(exceptionRef.get().getMessage(), containsString("failed to initialize a TrustManagerFactory"));
assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));
}

Expand All @@ -438,10 +472,19 @@ public void testPEMTrustReloadException() throws Exception {
Environment env = randomBoolean() ? null : TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLConfiguration config = sslService.sslConfiguration(settings.getByPrefix("xpack.security.transport.ssl."));
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
new SSLConfigurationReloader(env, sslService, resourceWatcherService) {
@Override
void reloadSSLContext(SSLConfiguration configuration) {
fail("reload should not be called! [pem trust reload exception]");
try {
super.reloadSSLContext(configuration);
} catch (Exception e) {
exceptionRef.set(e);
throw e;
} finally {
latch.countDown();
}
}
};

Expand All @@ -454,9 +497,10 @@ void reloadSSLContext(SSLConfiguration configuration) {
}
atomicMoveIfPossible(updatedCert, clientCertPath);

// we intentionally don't wait here as we rely on concurrency to catch a failure
latch.await();
assertNotNull(exceptionRef.get());
assertThat(exceptionRef.get().getMessage(), containsString("failed to initialize a TrustManagerFactory"));
assertThat(sslService.sslContextHolder(config).sslContext(), sameInstance(context));

}

private void validateSSLConfigurationIsReloaded(Settings settings, Environment env, Consumer<SSLContext> preChecks,
Expand Down