Skip to content

Commit 4ce6ee8

Browse files
committed
Move SSLService.sslSocketFactory to profile
1 parent 435a592 commit 4ce6ee8

File tree

12 files changed

+48
-69
lines changed

12 files changed

+48
-69
lines changed

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/TLSConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
package org.elasticsearch.transport.netty4;
1111

12-
import org.elasticsearch.common.ssl.SslConfiguration;
13-
1412
import javax.net.ssl.SSLEngine;
1513

1614
public record TLSConfig(EngineProvider engineProvider) {

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/security/CommandLineHttpClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.elasticsearch.common.network.NetworkService;
1515
import org.elasticsearch.common.settings.SecureString;
1616
import org.elasticsearch.common.settings.Settings;
17-
import org.elasticsearch.common.ssl.SslConfiguration;
1817
import org.elasticsearch.core.CharArrays;
1918
import org.elasticsearch.core.CheckedFunction;
2019
import org.elasticsearch.core.Releasables;
@@ -26,6 +25,7 @@
2625
import org.elasticsearch.xpack.core.security.HttpResponse.HttpResponseBuilder;
2726
import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken;
2827
import org.elasticsearch.xpack.core.ssl.SSLService;
28+
import org.elasticsearch.xpack.core.ssl.SslProfile;
2929

3030
import java.io.IOException;
3131
import java.io.InputStream;
@@ -154,10 +154,12 @@ private HttpResponse execute(
154154
sslContext.init(null, new TrustManager[] { fingerprintTrustingTrustManager(pinnedCaCertFingerprint) }, null);
155155
httpsConn.setSSLSocketFactory(sslContext.getSocketFactory());
156156
} else {
157-
final SslConfiguration sslConfiguration = sslService.getHttpTransportSSLConfiguration();
157+
final SslProfile sslProfile = sslService.profile(XPackSettings.HTTP_SSL_PREFIX);
158158
// Requires permission java.lang.RuntimePermission "setFactory";
159-
httpsConn.setSSLSocketFactory(sslService.sslSocketFactory(sslConfiguration));
160-
final boolean isHostnameVerificationEnabled = sslConfiguration.verificationMode().isHostnameVerificationEnabled();
159+
httpsConn.setSSLSocketFactory(sslProfile.socketFactory());
160+
final boolean isHostnameVerificationEnabled = sslProfile.configuration()
161+
.verificationMode()
162+
.isHostnameVerificationEnabled();
161163
if (isHostnameVerificationEnabled == false) {
162164
httpsConn.setHostnameVerifier((hostname, session) -> true);
163165
}

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SSLService.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -300,25 +300,6 @@ protected void verifySession(HttpHost host, IOSession iosession, SSLSession sess
300300
};
301301
}
302302

303-
/**
304-
* Create a new {@link SSLSocketFactory} based on the provided configuration.
305-
* The socket factory will also properly configure the ciphers and protocols on each socket that is created
306-
*
307-
* @param configuration The SSL configuration to use. Typically obtained from {@link #getSSLConfiguration(String)}
308-
* @return Never {@code null}.
309-
*/
310-
public SSLSocketFactory sslSocketFactory(SslConfiguration configuration) {
311-
final SSLContextHolder contextHolder = sslContextHolder(configuration);
312-
SSLSocketFactory socketFactory = contextHolder.sslContext().getSocketFactory();
313-
final SecuritySSLSocketFactory securitySSLSocketFactory = new SecuritySSLSocketFactory(
314-
() -> contextHolder.sslContext().getSocketFactory(),
315-
configuration.supportedProtocols().toArray(Strings.EMPTY_ARRAY),
316-
supportedCiphers(socketFactory.getSupportedCipherSuites(), configuration.getCipherSuites(), false)
317-
);
318-
contextHolder.addReloadListener(securitySSLSocketFactory::reload);
319-
return securitySSLSocketFactory;
320-
}
321-
322303
/**
323304
* Returns whether the provided settings results in a valid configuration that can be used for server connections
324305
*
@@ -788,7 +769,14 @@ public SslConfiguration configuration() {
788769

789770
@Override
790771
public SSLSocketFactory socketFactory() {
791-
return SSLService.this.sslSocketFactory(this.sslConfiguration);
772+
SSLSocketFactory socketFactory = context.getSocketFactory();
773+
final SecuritySSLSocketFactory securitySSLSocketFactory = new SecuritySSLSocketFactory(
774+
() -> context.getSocketFactory(),
775+
sslConfiguration.supportedProtocols().toArray(Strings.EMPTY_ARRAY),
776+
supportedCiphers(socketFactory.getSupportedCipherSuites(), sslConfiguration.getCipherSuites(), false)
777+
);
778+
this.addReloadListener(securitySSLSocketFactory::reload);
779+
return securitySSLSocketFactory;
792780
}
793781

794782
@Override

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ssl/SslProfile.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public interface SslProfile {
2121

2222
SSLContext sslContext();
2323

24+
/**
25+
* Create a new {@link SSLSocketFactory} based on the provided configuration.
26+
* The socket factory will also properly configure the ciphers and protocols on each socket that is created
27+
*
28+
* @return Never {@code null}.
29+
*/
2430
SSLSocketFactory socketFactory();
2531

2632
HostnameVerifier hostnameVerifier();

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ssl/SSLServiceTests.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
7979
import static org.hamcrest.Matchers.arrayWithSize;
8080
import static org.hamcrest.Matchers.contains;
81-
import static org.hamcrest.Matchers.containsInAnyOrder;
8281
import static org.hamcrest.Matchers.containsString;
8382
import static org.hamcrest.Matchers.equalTo;
8483
import static org.hamcrest.Matchers.greaterThan;
@@ -536,19 +535,15 @@ public void testThatSSLSocketFactoryHasProperCiphersAndProtocols() throws Except
536535

537536
SSLService sslService = new SSLService(TestEnvironment.newEnvironment(buildEnvSettings(settings)));
538537

539-
SslConfiguration config = sslService.getSSLConfiguration("xpack.security.transport.ssl");
540-
541-
final SSLSocketFactory configFactory = sslService.sslSocketFactory(config);
542-
final String[] ciphers = sslService.supportedCiphers(configFactory.getSupportedCipherSuites(), config.getCipherSuites(), false);
543-
assertThat(configFactory.getDefaultCipherSuites(), is(ciphers));
538+
final SslProfile profile = sslService.profile("xpack.security.transport.ssl");
544539

545-
SslProfile profile = sslService.profile("xpack.security.transport.ssl");
546-
final SSLSocketFactory profileFactory = profile.socketFactory();
547-
assertThat(profileFactory.getSupportedCipherSuites(), is(configFactory.getSupportedCipherSuites()));
548-
assertThat(profileFactory.getDefaultCipherSuites(), is(configFactory.getDefaultCipherSuites()));
540+
final SSLSocketFactory factory = profile.socketFactory();
541+
final SslConfiguration config = profile.configuration();
542+
final String[] ciphers = sslService.supportedCiphers(factory.getSupportedCipherSuites(), config.getCipherSuites(), false);
543+
assertThat(factory.getDefaultCipherSuites(), is(ciphers));
549544

550545
final String[] getSupportedProtocols = config.supportedProtocols().toArray(Strings.EMPTY_ARRAY);
551-
try (SSLSocket socket = (SSLSocket) randomFrom(configFactory, profileFactory).createSocket()) {
546+
try (SSLSocket socket = (SSLSocket) factory.createSocket()) {
552547
assertThat(socket.getEnabledCipherSuites(), is(ciphers));
553548
// the order we set the protocols in is not going to be what is returned as internally the JDK may sort the versions
554549
assertThat(socket.getEnabledProtocols(), arrayContainingInAnyOrder(getSupportedProtocols));

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/security/transport/ssl/SslIntegrationTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@
1919
import org.elasticsearch.common.io.Streams;
2020
import org.elasticsearch.common.network.NetworkAddress;
2121
import org.elasticsearch.common.settings.Settings;
22-
import org.elasticsearch.common.ssl.SslConfiguration;
2322
import org.elasticsearch.common.transport.TransportAddress;
2423
import org.elasticsearch.core.Strings;
2524
import org.elasticsearch.env.TestEnvironment;
2625
import org.elasticsearch.http.HttpServerTransport;
2726
import org.elasticsearch.test.SecurityIntegTestCase;
2827
import org.elasticsearch.xpack.core.common.socket.SocketAccess;
2928
import org.elasticsearch.xpack.core.ssl.SSLService;
29+
import org.elasticsearch.xpack.core.ssl.SslProfile;
3030

3131
import java.io.InputStreamReader;
3232
import java.net.InetSocketAddress;
@@ -80,14 +80,11 @@ public void testThatConnectionToHTTPWorks() throws Exception {
8080
AuthScope.ANY,
8181
new UsernamePasswordCredentials(nodeClientUsername(), new String(nodeClientPassword().getChars()))
8282
);
83-
SslConfiguration sslConfiguration = service.getSSLConfiguration("xpack.security.http.ssl");
83+
SslProfile sslProfile = service.profile("xpack.security.http.ssl");
8484
try (
8585
CloseableHttpClient client = HttpClients.custom()
8686
.setSSLSocketFactory(
87-
new SSLConnectionSocketFactory(
88-
service.sslSocketFactory(sslConfiguration),
89-
SSLConnectionSocketFactory.getDefaultHostnameVerifier()
90-
)
87+
new SSLConnectionSocketFactory(sslProfile.socketFactory(), SSLConnectionSocketFactory.getDefaultHostnameVerifier())
9188
)
9289
.setDefaultCredentialsProvider(provider)
9390
.build();

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLReloadIntegTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
import org.elasticsearch.ElasticsearchException;
1010
import org.elasticsearch.common.settings.MockSecureSettings;
1111
import org.elasticsearch.common.settings.Settings;
12-
import org.elasticsearch.common.ssl.SslConfiguration;
1312
import org.elasticsearch.common.transport.TransportAddress;
1413
import org.elasticsearch.env.TestEnvironment;
1514
import org.elasticsearch.test.SecurityIntegTestCase;
1615
import org.elasticsearch.transport.Transport;
1716
import org.elasticsearch.xpack.core.ssl.SSLService;
17+
import org.elasticsearch.xpack.core.ssl.SslProfile;
1818

1919
import java.io.IOException;
2020
import java.net.SocketException;
@@ -112,8 +112,8 @@ public void testThatSSLConfigurationReloadsOnModification() throws Exception {
112112
.build();
113113
String node = randomFrom(internalCluster().getNodeNames());
114114
SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings));
115-
SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.transport.ssl");
116-
SSLSocketFactory sslSocketFactory = sslService.sslSocketFactory(sslConfiguration);
115+
SslProfile sslProfile = sslService.profile("xpack.security.transport.ssl");
116+
SSLSocketFactory sslSocketFactory = sslProfile.socketFactory();
117117
TransportAddress address = internalCluster().getInstance(Transport.class, node).boundAddress().publishAddress();
118118
// Fails as our nodes do not trust testnode_updated.crt
119119
try (SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(address.getAddress(), address.getPort())) {

x-pack/plugin/security/src/internalClusterTest/java/org/elasticsearch/xpack/ssl/SSLTrustRestrictionsTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import org.elasticsearch.ElasticsearchException;
1010
import org.elasticsearch.common.settings.Settings;
1111
import org.elasticsearch.common.ssl.PemUtils;
12-
import org.elasticsearch.common.ssl.SslConfiguration;
1312
import org.elasticsearch.common.transport.TransportAddress;
1413
import org.elasticsearch.core.PathUtils;
1514
import org.elasticsearch.core.TimeValue;
@@ -22,6 +21,7 @@
2221
import org.elasticsearch.xpack.core.ssl.CertParsingUtils;
2322
import org.elasticsearch.xpack.core.ssl.RestrictedTrustManager;
2423
import org.elasticsearch.xpack.core.ssl.SSLService;
24+
import org.elasticsearch.xpack.core.ssl.SslProfile;
2525
import org.junit.AfterClass;
2626
import org.junit.BeforeClass;
2727

@@ -243,8 +243,8 @@ private void tryConnect(CertificateInfo certificate, boolean shouldFail) throws
243243

244244
String node = randomFrom(internalCluster().getNodeNames());
245245
SSLService sslService = new SSLService(TestEnvironment.newEnvironment(settings));
246-
SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.transport.ssl");
247-
SSLSocketFactory sslSocketFactory = sslService.sslSocketFactory(sslConfiguration);
246+
SslProfile sslProfile = sslService.profile("xpack.security.transport.ssl");
247+
SSLSocketFactory sslSocketFactory = sslProfile.socketFactory();
248248
TransportAddress address = internalCluster().getInstance(Transport.class, node).boundAddress().publishAddress();
249249
try (SSLSocket socket = (SSLSocket) sslSocketFactory.createSocket(address.getAddress(), address.getPort())) {
250250
assertThat(socket.isConnected(), is(true));

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/ldap/support/SessionFactory.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.xpack.core.security.authc.ldap.support.SessionFactorySettings;
2828
import org.elasticsearch.xpack.core.ssl.SSLConfigurationSettings;
2929
import org.elasticsearch.xpack.core.ssl.SSLService;
30+
import org.elasticsearch.xpack.core.ssl.SslProfile;
3031
import org.elasticsearch.xpack.security.support.ReloadableSecurityComponent;
3132

3233
import java.io.Closeable;
@@ -213,9 +214,9 @@ private ServerSet serverSet(RealmConfig realmConfig, SSLService clientSSLService
213214
SocketFactory socketFactory = null;
214215
if (ldapServers.ssl()) {
215216
final String sslKey = RealmSettings.realmSslPrefix(config.identifier());
216-
final SslConfiguration ssl = clientSSLService.getSSLConfiguration(sslKey);
217-
socketFactory = clientSSLService.sslSocketFactory(ssl);
218-
if (ssl.verificationMode().isHostnameVerificationEnabled()) {
217+
final SslProfile ssl = clientSSLService.profile(sslKey);
218+
socketFactory = ssl.socketFactory();
219+
if (ssl.configuration().verificationMode().isHostnameVerificationEnabled()) {
219220
logger.debug("using encryption for LDAP connections with hostname verification");
220221
} else {
221222
logger.debug("using encryption for LDAP connections without hostname verification");

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/authc/ldap/LdapTestUtils.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
import org.apache.lucene.tests.util.LuceneTestCase;
1414
import org.elasticsearch.common.settings.MockSecureSettings;
1515
import org.elasticsearch.common.settings.Settings;
16-
import org.elasticsearch.common.ssl.SslConfiguration;
1716
import org.elasticsearch.common.ssl.SslVerificationMode;
1817
import org.elasticsearch.core.TimeValue;
1918
import org.elasticsearch.env.Environment;
2019
import org.elasticsearch.env.TestEnvironment;
2120
import org.elasticsearch.xpack.core.ssl.SSLService;
21+
import org.elasticsearch.xpack.core.ssl.SslProfile;
2222
import org.elasticsearch.xpack.security.authc.ldap.support.LdapUtils;
2323

2424
import java.nio.file.Path;
@@ -56,16 +56,9 @@ public static LDAPConnection openConnection(String url, String bindDN, String bi
5656
options.setConnectTimeoutMillis(Math.toIntExact(DEFAULT_LDAP_TIMEOUT.millis()));
5757
options.setResponseTimeoutMillis(DEFAULT_LDAP_TIMEOUT.millis());
5858

59-
final SslConfiguration sslConfiguration = sslService.getSSLConfiguration("xpack.security.authc.realms.ldap.foo.ssl");
59+
final SslProfile profile = sslService.profile("xpack.security.authc.realms.ldap.foo.ssl");
6060
return LdapUtils.privilegedConnect(
61-
() -> new LDAPConnection(
62-
sslService.sslSocketFactory(sslConfiguration),
63-
options,
64-
ldapurl.getHost(),
65-
ldapurl.getPort(),
66-
bindDN,
67-
bindPassword
68-
)
61+
() -> new LDAPConnection(profile.socketFactory(), options, ldapurl.getHost(), ldapurl.getPort(), bindDN, bindPassword)
6962
);
7063
}
7164
}

0 commit comments

Comments
 (0)