-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix possible concurrent issue with SSL & default connector #5794
Open
jansupol
wants to merge
2
commits into
eclipse-ee4j:2.x
Choose a base branch
from
jansupol:connector.concurrent
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−16
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The check !DEFAULT_SSL_SOCKET_FACTORY.isInitialized() seems to be not thread-save?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable used in
isInitialized()
is volatile, so it should be thread-safe.