forked from eclipse-ee4j/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix possible concurrent issue with SSL & default connector (eclipse-e…
…e4j#5794) * Fix possible concurrent issue with SSL & default connector Signed-off-by: jansupol <[email protected]> --------- Signed-off-by: jansupol <[email protected]>
- Loading branch information
Showing
6 changed files
with
231 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...t/java/org/glassfish/jersey/tests/e2e/tls/connector/ConcurrentHttpsUrlConnectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.tests.e2e.tls.connector; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import javax.ws.rs.client.ClientBuilder; | ||
import javax.ws.rs.client.Client; | ||
import javax.ws.rs.core.GenericType; | ||
import javax.ws.rs.core.MediaType; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.security.KeyStore; | ||
import java.util.Random; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.TrustManagerFactory; | ||
|
||
/** | ||
* Jersey client seems to be not thread-safe: | ||
* When the first GET request is in progress, | ||
* all parallel requests from other Jersey client instances fail | ||
* with SSLHandshakeException: PKIX path building failed. | ||
* <p> | ||
* Once the first GET request is completed, | ||
* all subsequent requests work without error. | ||
* <p> | ||
* BUG 5749 | ||
*/ | ||
public class ConcurrentHttpsUrlConnectionTest { | ||
private static int THREAD_NUMBER = 5; | ||
|
||
private static volatile int responseCounter = 0; | ||
|
||
private static SSLContext createContext() throws Exception { | ||
URL url = ConcurrentHttpsUrlConnectionTest.class.getResource("keystore.jks"); | ||
KeyStore keyStore = KeyStore.getInstance("JKS"); | ||
try (InputStream is = url.openStream()) { | ||
keyStore.load(is, "password".toCharArray()); | ||
} | ||
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); | ||
kmf.init(keyStore, "password".toCharArray()); | ||
TrustManagerFactory tmf = TrustManagerFactory.getInstance("PKIX"); | ||
tmf.init(keyStore); | ||
SSLContext context = SSLContext.getInstance("TLS"); | ||
context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); | ||
return context; | ||
} | ||
|
||
@Test | ||
public void testSSLConnections() throws Exception { | ||
if (THREAD_NUMBER == 1) { | ||
System.out.println("\nThis is the working case (THREAD_NUMBER==1). Set THREAD_NUMBER > 1 to reproduce the error! \n"); | ||
} | ||
|
||
final HttpsServer server = new HttpsServer(createContext()); | ||
Executors.newFixedThreadPool(1).submit(server); | ||
|
||
// set THREAD_NUMBER > 1 to reproduce an issue | ||
ExecutorService executorService2clients = Executors.newFixedThreadPool(THREAD_NUMBER); | ||
|
||
final ClientBuilder builder = ClientBuilder.newBuilder().sslContext(createContext()) | ||
.hostnameVerifier(new HostnameVerifier() { | ||
public boolean verify(String arg0, SSLSession arg1) { | ||
return true; | ||
} | ||
}); | ||
|
||
AtomicInteger counter = new AtomicInteger(0); | ||
|
||
for (int i = 0; i < THREAD_NUMBER; i++) { | ||
executorService2clients.submit(new Runnable() { | ||
@Override | ||
public void run() { | ||
try { | ||
Client client = builder.build(); | ||
String ret = client.target("https://127.0.0.1:" + server.getPort() + "/" + new Random().nextInt()) | ||
.request(MediaType.TEXT_HTML) | ||
.get(new GenericType<String>() { | ||
}); | ||
System.out.print(++responseCounter + ". Server returned: " + ret); | ||
} catch (Exception e) { | ||
//get an exception here, if jersey lib is buggy and THREAD_NUMBER > 1: | ||
//jakarta.ws.rs.ProcessingException: javax.net.ssl.SSLHandshakeException: PKIX path building failed: | ||
e.printStackTrace(); | ||
} finally { | ||
System.out.println(counter.incrementAndGet()); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
while (counter.get() != THREAD_NUMBER) { | ||
Thread.sleep(100L); | ||
} | ||
server.stop(); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
tests/e2e-tls/src/test/java/org/glassfish/jersey/tests/e2e/tls/connector/HttpsServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0, which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the | ||
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License, | ||
* version 2 with the GNU Classpath Exception, which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
*/ | ||
|
||
package org.glassfish.jersey.tests.e2e.tls.connector; | ||
|
||
import java.io.BufferedInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.PrintWriter; | ||
|
||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLServerSocket; | ||
import javax.net.ssl.SSLSocket; | ||
|
||
class HttpsServer implements Runnable { | ||
private final SSLServerSocket sslServerSocket; | ||
private boolean closed = false; | ||
|
||
public HttpsServer(SSLContext context) throws Exception { | ||
sslServerSocket = (SSLServerSocket) context.getServerSocketFactory().createServerSocket(0); | ||
} | ||
|
||
public int getPort() { | ||
return sslServerSocket.getLocalPort(); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
System.out.printf("Server started on port %d%n", getPort()); | ||
while (!closed) { | ||
SSLSocket s; | ||
try { | ||
s = (SSLSocket) sslServerSocket.accept(); | ||
} catch (IOException e2) { | ||
s = null; | ||
} | ||
final SSLSocket socket = s; | ||
new Thread(new Runnable() { | ||
public void run() { | ||
try { | ||
if (socket != null) { | ||
InputStream is = new BufferedInputStream(socket.getInputStream()); | ||
byte[] data = new byte[2048]; | ||
int len = is.read(data); | ||
if (len <= 0) { | ||
throw new IOException("no data received"); | ||
} | ||
//System.out.printf("Server received: %s\n", new String(data, 0, len)); | ||
PrintWriter writer = new PrintWriter(socket.getOutputStream()); | ||
writer.println("HTTP/1.1 200 OK"); | ||
writer.println("Content-Type: text/html"); | ||
writer.println(); | ||
writer.println("Hello from server!"); | ||
writer.flush(); | ||
writer.close(); | ||
socket.close(); | ||
} | ||
} catch (Exception e1) { | ||
e1.printStackTrace(); | ||
} | ||
} | ||
}).start(); | ||
} | ||
} | ||
|
||
void stop() { | ||
try { | ||
closed = true; | ||
sslServerSocket.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
Binary file added
BIN
+2.68 KB
tests/e2e-tls/src/test/resources/org/glassfish/jersey/tests/e2e/tls/connector/keystore.jks
Binary file not shown.