Skip to content

Commit

Permalink
Remove obsolete constructor from SSLService
Browse files Browse the repository at this point in the history
This removes the old `SSLService(Settings, Environment)` constructor
and converts all uses cases to the `SSLService(Environment)`
constructor that was added in elastic#49667
  • Loading branch information
tvernum committed Dec 19, 2019
1 parent b92b91a commit 90810a8
Show file tree
Hide file tree
Showing 42 changed files with 215 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public class SSLService {
private static final Setting<Boolean> DIAGNOSE_TRUST_EXCEPTIONS_SETTING = Setting.boolSetting(
"xpack.security.ssl.diagnose.trust", true, Setting.Property.NodeScope);

private final Environment env;
private final Settings settings;
private final boolean diagnoseTrustExceptions;

Expand All @@ -120,33 +121,33 @@ public class SSLService {
*/
private final Map<SSLConfiguration, SSLContextHolder> sslContexts;
private final SetOnce<SSLConfiguration> transportSSLConfiguration = new SetOnce<>();
private final Environment env;

/**
* Create a new SSLService using the {@code Settings} from {@link Environment#settings()}.
* @see #SSLService(Settings, Environment)
* Create a new SSLService that parses the settings for the ssl contexts that need to be created, creates them, and then caches them
* for use later
*/
public SSLService(Environment environment) {
this(environment.settings(), environment);
this.env = environment;
this.settings = env.settings();
this.diagnoseTrustExceptions = DIAGNOSE_TRUST_EXCEPTIONS_SETTING.get(environment.settings());
this.sslConfigurations = new HashMap<>();
this.sslContexts = loadSSLConfigurations();
}

/**
* Create a new SSLService that parses the settings for the ssl contexts that need to be created, creates them, and then caches them
* for use later
*/
@Deprecated
public SSLService(Settings settings, Environment environment) {
this.settings = settings;
this.env = environment;
this.settings = env.settings();
this.diagnoseTrustExceptions = DIAGNOSE_TRUST_EXCEPTIONS_SETTING.get(settings);
this.sslConfigurations = new HashMap<>();
this.sslContexts = loadSSLConfigurations();
}

private SSLService(Settings settings, Environment environment, Map<String, SSLConfiguration> sslConfigurations,
private SSLService(Environment environment, Map<String, SSLConfiguration> sslConfigurations,
Map<SSLConfiguration, SSLContextHolder> sslContexts) {
this.settings = settings;
this.env = environment;
this.diagnoseTrustExceptions = DIAGNOSE_TRUST_EXCEPTIONS_SETTING.get(settings);
this.settings = env.settings();
this.diagnoseTrustExceptions = DIAGNOSE_TRUST_EXCEPTIONS_SETTING.get(environment.settings());
this.sslConfigurations = sslConfigurations;
this.sslContexts = sslContexts;
}
Expand All @@ -157,7 +158,7 @@ private SSLService(Settings settings, Environment environment, Map<String, SSLCo
* have been created during initialization
*/
public SSLService createDynamicSSLService() {
return new SSLService(settings, env, sslConfigurations, sslContexts) {
return new SSLService(env, sslConfigurations, sslContexts) {

@Override
Map<SSLConfiguration, SSLContextHolder> loadSSLConfigurations() {
Expand Down Expand Up @@ -489,9 +490,9 @@ X509ExtendedTrustManager wrapWithDiagnostics(X509ExtendedTrustManager trustManag
* Parses the settings to load all SSLConfiguration objects that will be used.
*/
Map<SSLConfiguration, SSLContextHolder> loadSSLConfigurations() {
Map<SSLConfiguration, SSLContextHolder> sslContextHolders = new HashMap<>();
final Map<SSLConfiguration, SSLContextHolder> sslContextHolders = new HashMap<>();

Map<String, Settings> sslSettingsMap = new HashMap<>();
final Map<String, Settings> sslSettingsMap = new HashMap<>();
sslSettingsMap.put(XPackSettings.HTTP_SSL_PREFIX, getHttpTransportSSLSettings(settings));
sslSettingsMap.put("xpack.http.ssl", settings.getByPrefix("xpack.http.ssl."));
sslSettingsMap.putAll(getRealmsSSLSettings(settings));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void testGetSecureTransportProfileConfigurations() {
.put("transport.profiles.cert.xpack.security.ssl.verification_mode", VerificationMode.CERTIFICATE.name())
.build();
final Environment env = TestEnvironment.newEnvironment(settings);
SSLService sslService = new SSLService(settings, env);
SSLService sslService = new SSLService(env);
final SSLConfiguration defaultConfig = sslService.getSSLConfiguration("xpack.security.transport.ssl");
final Map<String, SSLConfiguration> profileConfigurations = ProfileConfigurations.get(settings, sslService, defaultConfig);
assertThat(profileConfigurations.size(), Matchers.equalTo(3));
Expand All @@ -48,7 +48,7 @@ public void testGetInsecureTransportProfileConfigurations() {
.put("transport.profiles.none.xpack.security.ssl.verification_mode", VerificationMode.NONE.name())
.build();
final Environment env = TestEnvironment.newEnvironment(settings);
SSLService sslService = new SSLService(settings, env);
SSLService sslService = new SSLService(env);
final SSLConfiguration defaultConfig = sslService.getSSLConfiguration("xpack.security.transport.ssl");
final Map<String, SSLConfiguration> profileConfigurations = ProfileConfigurations.get(settings, sslService, defaultConfig);
assertThat(profileConfigurations.size(), Matchers.equalTo(2));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void testReloadingKeyStore() throws Exception {
throw new RuntimeException("Exception starting or connecting to the mock server", e);
}
};
validateSSLConfigurationIsReloaded(settings, env, keyMaterialPreChecks, modifier, keyMaterialPostChecks);
validateSSLConfigurationIsReloaded(env, keyMaterialPreChecks, modifier, keyMaterialPostChecks);
}
}
/**
Expand All @@ -174,7 +174,7 @@ public void testPEMKeyConfigReloading() throws Exception {
.putList("xpack.security.transport.ssl.certificate_authorities", certPath.toString())
.setSecureSettings(secureSettings)
.build();
final Environment env = newEnvironment();
final Environment env = TestEnvironment.newEnvironment(settings);
// Load HTTPClient once. Client uses a keystore containing testnode key/cert as a truststore
try (CloseableHttpClient client = getSSLClient(Collections.singletonList(certPath))) {
final Consumer<SSLContext> keyMaterialPreChecks = (context) -> {
Expand Down Expand Up @@ -207,7 +207,7 @@ public void testPEMKeyConfigReloading() throws Exception {
throw new RuntimeException("Exception starting or connecting to the mock server", e);
}
};
validateSSLConfigurationIsReloaded(settings, env, keyMaterialPreChecks, modifier, keyMaterialPostChecks);
validateSSLConfigurationIsReloaded(env, keyMaterialPreChecks, modifier, keyMaterialPostChecks);
}
}

Expand Down Expand Up @@ -259,7 +259,7 @@ public void testReloadingTrustStore() throws Exception {
throw new RuntimeException("Error closing CloseableHttpClient", e);
}
};
validateSSLConfigurationIsReloaded(settings, env, trustMaterialPreChecks, modifier, trustMaterialPostChecks);
validateSSLConfigurationIsReloaded(env, trustMaterialPreChecks, modifier, trustMaterialPostChecks);
}
}

Expand Down Expand Up @@ -309,7 +309,7 @@ public void testReloadingPEMTrustConfig() throws Exception {
throw new RuntimeException("Error closing CloseableHttpClient", e);
}
};
validateSSLConfigurationIsReloaded(settings, env, trustMaterialPreChecks, modifier, trustMaterialPostChecks);
validateSSLConfigurationIsReloaded(env, trustMaterialPreChecks, modifier, trustMaterialPostChecks);
}
}

Expand All @@ -331,7 +331,7 @@ public void testReloadingKeyStoreException() throws Exception {
.put("path.home", createTempDir())
.build();
Environment env = TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLService sslService = new SSLService(env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Expand All @@ -353,6 +353,7 @@ void reloadSSLContext(SSLConfiguration configuration) {

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

latch.await();
Expand Down Expand Up @@ -384,7 +385,7 @@ public void testReloadingPEMKeyConfigException() throws Exception {
.setSecureSettings(secureSettings)
.build();
Environment env = TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLService sslService = new SSLService(env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -430,7 +431,7 @@ public void testTrustStoreReloadException() throws Exception {
.put("path.home", createTempDir())
.build();
Environment env = TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLService sslService = new SSLService(env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl.");
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -474,7 +475,7 @@ public void testPEMTrustReloadException() throws Exception {
.put("path.home", createTempDir())
.build();
Environment env = TestEnvironment.newEnvironment(settings);
final SSLService sslService = new SSLService(settings, env);
final SSLService sslService = new SSLService(env);
final SSLConfiguration config = sslService.sslConfiguration(settings.getByPrefix("xpack.security.transport.ssl."));
final AtomicReference<Exception> exceptionRef = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
Expand Down Expand Up @@ -524,10 +525,10 @@ private Settings.Builder baseKeystoreSettings(Path tempDir, MockSecureSettings s
.setSecureSettings(secureSettings);
}

private void validateSSLConfigurationIsReloaded(Settings settings, Environment env, Consumer<SSLContext> preChecks,
private void validateSSLConfigurationIsReloaded(Environment env, Consumer<SSLContext> preChecks,
Runnable modificationFunction, Consumer<SSLContext> postChecks) throws Exception {
final CountDownLatch reloadLatch = new CountDownLatch(1);
final SSLService sslService = new SSLService(settings, env);
final SSLService sslService = new SSLService(env);
final SSLConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl");
new SSLConfigurationReloader(env, sslService, resourceWatcherService) {
@Override
Expand Down
Loading

0 comments on commit 90810a8

Please sign in to comment.