Skip to content

Commit 76c22f7

Browse files
authored
HttpsServer can use TLSv1.3 on JDK16+ (#64496)
This commit changes code that previously pinned to TLSv1.2 when running on JDK 12+ to allow the use of TLSv1.3 if on JDK 16 or newer. There was a bug in the HttpsServer code that has finally been fixed, which prevented the use of TLSv1.3 as the HttpsServer would endlessly loop. The JDK bug is JDK-8254967. Closes #38646
1 parent 06b2deb commit 76c22f7

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

modules/reindex/src/test/java/org/elasticsearch/index/reindex/ReindexRestClientSslTests.java

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.sun.net.httpserver.HttpsExchange;
2424
import com.sun.net.httpserver.HttpsParameters;
2525
import com.sun.net.httpserver.HttpsServer;
26+
import org.elasticsearch.bootstrap.JavaVersion;
2627
import org.elasticsearch.client.Request;
2728
import org.elasticsearch.client.Response;
2829
import org.elasticsearch.client.RestClient;
@@ -102,7 +103,7 @@ public static void shutdownHttpServer() {
102103
}
103104

104105
private static SSLContext buildServerSslContext() throws Exception {
105-
final SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
106+
final SSLContext sslContext = SSLContext.getInstance(isHttpsServerBrokenWithTLSv13() ? "TLSv1.2" : "TLS");
106107
final char[] password = "http-password".toCharArray();
107108

108109
final Path cert = PathUtils.get(ReindexRestClientSslTests.class.getResource("http/http.crt").toURI());
@@ -119,10 +120,12 @@ private static SSLContext buildServerSslContext() throws Exception {
119120
public void testClientFailsWithUntrustedCertificate() throws IOException {
120121
assumeFalse("https://github.com/elastic/elasticsearch/issues/49094", inFipsJvm());
121122
final List<Thread> threads = new ArrayList<>();
122-
final Settings settings = Settings.builder()
123-
.put("path.home", createTempDir())
124-
.put("reindex.ssl.supported_protocols", "TLSv1.2")
125-
.build();
123+
final Settings.Builder builder = Settings.builder()
124+
.put("path.home", createTempDir());
125+
if (isHttpsServerBrokenWithTLSv13()) {
126+
builder.put("reindex.ssl.supported_protocols", "TLSv1.2");
127+
}
128+
final Settings settings = builder.build();
126129
final Environment environment = TestEnvironment.newEnvironment(settings);
127130
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
128131
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
@@ -133,11 +136,13 @@ public void testClientFailsWithUntrustedCertificate() throws IOException {
133136
public void testClientSucceedsWithCertificateAuthorities() throws IOException {
134137
final List<Thread> threads = new ArrayList<>();
135138
final Path ca = getDataPath("ca.pem");
136-
final Settings settings = Settings.builder()
139+
final Settings.Builder builder = Settings.builder()
137140
.put("path.home", createTempDir())
138-
.putList("reindex.ssl.certificate_authorities", ca.toString())
139-
.put("reindex.ssl.supported_protocols", "TLSv1.2")
140-
.build();
141+
.putList("reindex.ssl.certificate_authorities", ca.toString());
142+
if (isHttpsServerBrokenWithTLSv13()) {
143+
builder.put("reindex.ssl.supported_protocols", "TLSv1.2");
144+
}
145+
final Settings settings = builder.build();
141146
final Environment environment = TestEnvironment.newEnvironment(settings);
142147
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
143148
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
@@ -149,11 +154,13 @@ public void testClientSucceedsWithCertificateAuthorities() throws IOException {
149154
public void testClientSucceedsWithVerificationDisabled() throws IOException {
150155
assumeFalse("Cannot disable verification in FIPS JVM", inFipsJvm());
151156
final List<Thread> threads = new ArrayList<>();
152-
final Settings settings = Settings.builder()
157+
final Settings.Builder builder = Settings.builder()
153158
.put("path.home", createTempDir())
154-
.put("reindex.ssl.verification_mode", "NONE")
155-
.put("reindex.ssl.supported_protocols", "TLSv1.2")
156-
.build();
159+
.put("reindex.ssl.verification_mode", "NONE");
160+
if (isHttpsServerBrokenWithTLSv13()) {
161+
builder.put("reindex.ssl.supported_protocols", "TLSv1.2");
162+
}
163+
final Settings settings = builder.build();
157164
final Environment environment = TestEnvironment.newEnvironment(settings);
158165
final ReindexSslConfig ssl = new ReindexSslConfig(settings, environment, mock(ResourceWatcherService.class));
159166
try (RestClient client = Reindexer.buildRestClient(getRemoteInfo(), ssl, 1L, threads)) {
@@ -167,14 +174,16 @@ public void testClientPassesClientCertificate() throws IOException {
167174
final Path ca = getDataPath("ca.pem");
168175
final Path cert = getDataPath("client/client.crt");
169176
final Path key = getDataPath("client/client.key");
170-
final Settings settings = Settings.builder()
177+
final Settings.Builder builder = Settings.builder()
171178
.put("path.home", createTempDir())
172179
.putList("reindex.ssl.certificate_authorities", ca.toString())
173180
.put("reindex.ssl.certificate", cert)
174181
.put("reindex.ssl.key", key)
175-
.put("reindex.ssl.key_passphrase", "client-password")
176-
.put("reindex.ssl.supported_protocols", "TLSv1.2")
177-
.build();
182+
.put("reindex.ssl.key_passphrase", "client-password");
183+
if (isHttpsServerBrokenWithTLSv13()) {
184+
builder.put("reindex.ssl.supported_protocols", "TLSv1.2");
185+
}
186+
final Settings settings = builder.build();
178187
AtomicReference<Certificate[]> clientCertificates = new AtomicReference<>();
179188
handler = https -> {
180189
try {
@@ -216,4 +225,12 @@ public void configure(HttpsParameters params) {
216225
params.setWantClientAuth(true);
217226
}
218227
}
228+
229+
/**
230+
* Checks whether the JVM this test is run under is affected by JDK-8254967, which causes these
231+
* tests to fail if a TLSv1.3 SSLContext is used.
232+
*/
233+
private static boolean isHttpsServerBrokenWithTLSv13() {
234+
return JavaVersion.current().compareTo(JavaVersion.parse("16.0.0")) < 0;
235+
}
219236
}

0 commit comments

Comments
 (0)