Skip to content

Commit e42789c

Browse files
committed
expose SSLSession from web socket connection
1 parent 073d40d commit e42789c

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/MtlsWithP12ClientEndpointTest.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.quarkus.websockets.next.test.client;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
46
import static org.junit.jupiter.api.Assertions.assertTrue;
57

68
import java.io.File;
@@ -9,6 +11,9 @@
911
import java.util.concurrent.CopyOnWriteArrayList;
1012
import java.util.concurrent.CountDownLatch;
1113
import java.util.concurrent.TimeUnit;
14+
import java.util.concurrent.atomic.AtomicReference;
15+
16+
import javax.net.ssl.SSLPeerUnverifiedException;
1217

1318
import jakarta.inject.Inject;
1419

@@ -24,6 +29,7 @@
2429
import io.quarkus.websockets.next.WebSocket;
2530
import io.quarkus.websockets.next.WebSocketClient;
2631
import io.quarkus.websockets.next.WebSocketClientConnection;
32+
import io.quarkus.websockets.next.WebSocketConnection;
2733
import io.quarkus.websockets.next.WebSocketConnector;
2834
import io.smallrye.certs.Format;
2935
import io.smallrye.certs.junit5.Certificate;
@@ -47,6 +53,7 @@ public class MtlsWithP12ClientEndpointTest {
4753
.overrideConfigKey("quarkus.tls.ws-server.trust-store.p12.path", "server-truststore.p12")
4854
.overrideConfigKey("quarkus.tls.ws-server.trust-store.p12.password", "secret")
4955
.overrideConfigKey("quarkus.http.tls-configuration-name", "ws-server")
56+
.overrideConfigKey("quarkus.http.ssl.client-auth", "required")
5057

5158
.overrideConfigKey("quarkus.tls.ws-client.key-store.p12.path", "client-keystore.p12")
5259
.overrideConfigKey("quarkus.tls.ws-client.key-store.p12.password", "secret")
@@ -61,13 +68,34 @@ public class MtlsWithP12ClientEndpointTest {
6168
URI uri;
6269

6370
@Test
64-
void testClient() throws InterruptedException {
71+
void testClient() throws InterruptedException, SSLPeerUnverifiedException {
6572
WebSocketClientConnection connection = connector
6673
.baseUri(uri)
6774
// The value will be encoded automatically
6875
.pathParam("name", "Lu=")
6976
.connectAndAwait();
7077
assertTrue(connection.isSecure());
78+
assertNotNull(connection.sslSession());
79+
assertNotNull(connection.sslSession().getLocalPrincipal());
80+
assertNotNull(connection.sslSession().getLocalCertificates());
81+
assertNotNull(connection.sslSession().getPeerPrincipal());
82+
assertNotNull(connection.sslSession().getPeerCertificates());
83+
84+
assertTrue(ServerEndpoint.OPENED_LATCH.await(5, TimeUnit.SECONDS));
85+
assertTrue(ServerEndpoint.CONNECTION_REF.get().isSecure());
86+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession());
87+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalPrincipal());
88+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalCertificates());
89+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerPrincipal());
90+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerCertificates());
91+
assertEquals(connection.sslSession().getPeerPrincipal(),
92+
ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalPrincipal());
93+
assertArrayEquals(connection.sslSession().getPeerCertificates(),
94+
ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalCertificates());
95+
assertEquals(connection.sslSession().getLocalPrincipal(),
96+
ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerPrincipal());
97+
assertArrayEquals(connection.sslSession().getLocalCertificates(),
98+
ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerCertificates());
7199

72100
assertEquals("Lu=", connection.pathParam("name"));
73101
connection.sendTextAndAwait("Hi!");
@@ -84,10 +112,16 @@ void testClient() throws InterruptedException {
84112
@WebSocket(path = "/endpoint/{name}")
85113
public static class ServerEndpoint {
86114

115+
static final AtomicReference<WebSocketConnection> CONNECTION_REF = new AtomicReference<>();
116+
117+
static final CountDownLatch OPENED_LATCH = new CountDownLatch(1);
118+
87119
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1);
88120

89121
@OnOpen
90-
String open(@PathParam String name) {
122+
String open(@PathParam String name, WebSocketConnection connection) {
123+
CONNECTION_REF.set(connection);
124+
OPENED_LATCH.countDown();
91125
return "Hello " + name + "!";
92126
}
93127

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/TlsClientEndpointTest.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package io.quarkus.websockets.next.test.client;
22

3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import static org.junit.jupiter.api.Assertions.assertThrows;
48
import static org.junit.jupiter.api.Assertions.assertTrue;
59

610
import java.io.File;
@@ -10,6 +14,9 @@
1014
import java.util.concurrent.CopyOnWriteArrayList;
1115
import java.util.concurrent.CountDownLatch;
1216
import java.util.concurrent.TimeUnit;
17+
import java.util.concurrent.atomic.AtomicReference;
18+
19+
import javax.net.ssl.SSLPeerUnverifiedException;
1320

1421
import jakarta.inject.Inject;
1522

@@ -25,6 +32,7 @@
2532
import io.quarkus.websockets.next.WebSocket;
2633
import io.quarkus.websockets.next.WebSocketClient;
2734
import io.quarkus.websockets.next.WebSocketClientConnection;
35+
import io.quarkus.websockets.next.WebSocketConnection;
2836
import io.quarkus.websockets.next.WebSocketConnector;
2937
import io.smallrye.certs.Format;
3038
import io.smallrye.certs.junit5.Certificate;
@@ -53,20 +61,39 @@ public class TlsClientEndpointTest {
5361
URI uri;
5462

5563
@Test
56-
void testClient() throws InterruptedException, URISyntaxException {
64+
void testClient() throws InterruptedException, SSLPeerUnverifiedException, URISyntaxException {
5765
assertClient(uri);
5866
URI wssUri = new URI("wss", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(),
5967
uri.getFragment());
6068
assertClient(wssUri);
6169
}
6270

63-
void assertClient(URI uri) throws InterruptedException, URISyntaxException {
71+
void assertClient(URI uri) throws InterruptedException, SSLPeerUnverifiedException {
6472
WebSocketClientConnection connection = connector
6573
.baseUri(uri)
6674
// The value will be encoded automatically
6775
.pathParam("name", "Lu=")
6876
.connectAndAwait();
6977
assertTrue(connection.isSecure());
78+
assertNotNull(connection.sslSession());
79+
assertNull(connection.sslSession().getLocalPrincipal());
80+
assertNull(connection.sslSession().getLocalCertificates());
81+
assertNotNull(connection.sslSession().getPeerPrincipal());
82+
assertNotNull(connection.sslSession().getPeerCertificates());
83+
84+
assertTrue(ServerEndpoint.openedLatch.await(5, TimeUnit.SECONDS));
85+
assertTrue(ServerEndpoint.CONNECTION_REF.get().isSecure());
86+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession());
87+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalPrincipal());
88+
assertNotNull(ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalCertificates());
89+
assertThrows(SSLPeerUnverifiedException.class,
90+
() -> ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerPrincipal());
91+
assertThrows(SSLPeerUnverifiedException.class,
92+
() -> ServerEndpoint.CONNECTION_REF.get().sslSession().getPeerCertificates());
93+
assertEquals(connection.sslSession().getPeerPrincipal(),
94+
ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalPrincipal());
95+
assertArrayEquals(connection.sslSession().getPeerCertificates(),
96+
ServerEndpoint.CONNECTION_REF.get().sslSession().getLocalCertificates());
7097

7198
assertEquals("Lu=", connection.pathParam("name"));
7299
connection.sendTextAndAwait("Hi!");
@@ -86,10 +113,16 @@ void assertClient(URI uri) throws InterruptedException, URISyntaxException {
86113
@WebSocket(path = "/endpoint/{name}")
87114
public static class ServerEndpoint {
88115

116+
static final AtomicReference<WebSocketConnection> CONNECTION_REF = new AtomicReference<>();
117+
118+
static volatile CountDownLatch openedLatch = new CountDownLatch(1);
119+
89120
static volatile CountDownLatch closedLatch = new CountDownLatch(1);
90121

91122
@OnOpen
92-
String open(@PathParam String name) {
123+
String open(@PathParam String name, WebSocketConnection connection) {
124+
CONNECTION_REF.set(connection);
125+
openedLatch.countDown();
93126
return "Hello " + name + "!";
94127
}
95128

@@ -104,6 +137,8 @@ void close() {
104137
}
105138

106139
static void reset() {
140+
CONNECTION_REF.set(null);
141+
openedLatch = new CountDownLatch(1);
107142
closedLatch = new CountDownLatch(1);
108143
}
109144

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/Connection.java

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

33
import java.time.Instant;
44

5+
import javax.net.ssl.SSLSession;
6+
57
import io.smallrye.common.annotation.CheckReturnValue;
68
import io.smallrye.mutiny.Uni;
79

@@ -32,6 +34,12 @@ public interface Connection extends Sender {
3234
*/
3335
boolean isSecure();
3436

37+
/**
38+
* @return {@link SSLSession} associated with the underlying socket, or {@code null} if connection is not secure.
39+
* @see #isSecure()
40+
*/
41+
SSLSession sslSession();
42+
3543
/**
3644
* @return {@code true} if the WebSocket is closed
3745
*/

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectionBase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.Map;
55
import java.util.UUID;
66

7+
import javax.net.ssl.SSLSession;
8+
79
import org.jboss.logging.Logger;
810

911
import io.quarkus.vertx.utils.NoBoundChecksBuffer;
@@ -137,6 +139,11 @@ public boolean isSecure() {
137139
return webSocket().isSsl();
138140
}
139141

142+
@Override
143+
public SSLSession sslSession() {
144+
return webSocket().sslSession();
145+
}
146+
140147
@Override
141148
public boolean isClosed() {
142149
return webSocket().isClosed();

0 commit comments

Comments
 (0)