forked from quarkusio/quarkus
-
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.
Merge pull request quarkusio#41095 from geoand/quarkusio#41004
Add TLS Registry configuration to WebSockets Next Client
- Loading branch information
Showing
8 changed files
with
346 additions
and
31 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
130 changes: 130 additions & 0 deletions
130
...t/src/test/java/io/quarkus/websockets/next/test/client/MtlsWithP12ClientEndpointTest.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,130 @@ | ||
package io.quarkus.websockets.next.test.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.PathParam; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketClient; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.quarkus.websockets.next.WebSocketConnector; | ||
import me.escoffier.certs.Format; | ||
import me.escoffier.certs.junit5.Certificate; | ||
import me.escoffier.certs.junit5.Certificates; | ||
|
||
@Certificates(baseDir = "target/certs", certificates = @Certificate(name = "mtls-test", password = "secret", formats = { | ||
Format.JKS, Format.PKCS12, Format.PEM }, client = true)) | ||
public class MtlsWithP12ClientEndpointTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ServerEndpoint.class, ClientEndpoint.class) | ||
.addAsResource(new File("target/certs/mtls-test-keystore.p12"), "server-keystore.p12") | ||
.addAsResource(new File("target/certs/mtls-test-server-truststore.p12"), "server-truststore.p12") | ||
.addAsResource(new File("target/certs/mtls-test-client-keystore.p12"), "client-keystore.p12") | ||
.addAsResource(new File("target/certs/mtls-test-client-truststore.p12"), "client-truststore.p12")) | ||
|
||
.overrideConfigKey("quarkus.tls.ws-server.key-store.p12.path", "server-keystore.p12") | ||
.overrideConfigKey("quarkus.tls.ws-server.key-store.p12.password", "secret") | ||
.overrideConfigKey("quarkus.tls.ws-server.trust-store.p12.path", "server-truststore.p12") | ||
.overrideConfigKey("quarkus.tls.ws-server.trust-store.p12.password", "secret") | ||
.overrideConfigKey("quarkus.http.tls-configuration-name", "ws-server") | ||
|
||
.overrideConfigKey("quarkus.tls.ws-client.key-store.p12.path", "client-keystore.p12") | ||
.overrideConfigKey("quarkus.tls.ws-client.key-store.p12.password", "secret") | ||
.overrideConfigKey("quarkus.tls.ws-client.trust-store.p12.path", "client-truststore.p12") | ||
.overrideConfigKey("quarkus.tls.ws-client.trust-store.p12.password", "secret") | ||
.overrideConfigKey("quarkus.websockets-next.client.tls-configuration-name", "ws-client"); | ||
|
||
@Inject | ||
WebSocketConnector<ClientEndpoint> connector; | ||
|
||
@TestHTTPResource(value = "/", tls = true) | ||
URI uri; | ||
|
||
@Test | ||
void testClient() throws InterruptedException { | ||
WebSocketClientConnection connection = connector | ||
.baseUri(uri) | ||
// The value will be encoded automatically | ||
.pathParam("name", "Lu=") | ||
.connectAndAwait(); | ||
assertTrue(connection.isSecure()); | ||
|
||
assertEquals("Lu=", connection.pathParam("name")); | ||
connection.sendTextAndAwait("Hi!"); | ||
|
||
assertTrue(ClientEndpoint.MESSAGE_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEquals("Lu=:Hello Lu=!", ClientEndpoint.MESSAGES.get(0)); | ||
assertEquals("Lu=:Hi!", ClientEndpoint.MESSAGES.get(1)); | ||
|
||
connection.closeAndAwait(); | ||
assertTrue(ClientEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(ServerEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@WebSocket(path = "/endpoint/{name}") | ||
public static class ServerEndpoint { | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnOpen | ||
String open(@PathParam String name) { | ||
return "Hello " + name + "!"; | ||
} | ||
|
||
@OnTextMessage | ||
String echo(String message) { | ||
return message; | ||
} | ||
|
||
@OnClose | ||
void close() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
@WebSocketClient(path = "/endpoint/{name}") | ||
public static class ClientEndpoint { | ||
|
||
static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(2); | ||
|
||
static final List<String> MESSAGES = new CopyOnWriteArrayList<>(); | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnTextMessage | ||
void onMessage(@PathParam String name, String message, WebSocketClientConnection connection) { | ||
if (!name.equals(connection.pathParam("name"))) { | ||
throw new IllegalArgumentException(); | ||
} | ||
MESSAGES.add(name + ":" + message); | ||
MESSAGE_LATCH.countDown(); | ||
} | ||
|
||
@OnClose | ||
void close() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
} |
121 changes: 121 additions & 0 deletions
121
...eployment/src/test/java/io/quarkus/websockets/next/test/client/TlsClientEndpointTest.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,121 @@ | ||
package io.quarkus.websockets.next.test.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.io.File; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.concurrent.CopyOnWriteArrayList; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.websockets.next.OnClose; | ||
import io.quarkus.websockets.next.OnOpen; | ||
import io.quarkus.websockets.next.OnTextMessage; | ||
import io.quarkus.websockets.next.PathParam; | ||
import io.quarkus.websockets.next.WebSocket; | ||
import io.quarkus.websockets.next.WebSocketClient; | ||
import io.quarkus.websockets.next.WebSocketClientConnection; | ||
import io.quarkus.websockets.next.WebSocketConnector; | ||
import me.escoffier.certs.Format; | ||
import me.escoffier.certs.junit5.Certificate; | ||
import me.escoffier.certs.junit5.Certificates; | ||
|
||
@Certificates(baseDir = "target/certs", certificates = @Certificate(name = "ssl-test", password = "secret", formats = { | ||
Format.JKS, Format.PKCS12, Format.PEM })) | ||
public class TlsClientEndpointTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(ServerEndpoint.class, ClientEndpoint.class) | ||
.addAsResource(new File("target/certs/ssl-test-keystore.jks"), "keystore.jks") | ||
.addAsResource(new File("target/certs/ssl-test-truststore.jks"), "truststore.jks")) | ||
.overrideConfigKey("quarkus.tls.key-store.jks.path", "keystore.jks") | ||
.overrideConfigKey("quarkus.tls.key-store.jks.password", "secret") | ||
.overrideConfigKey("quarkus.tls.ws-client.trust-store.jks.path", "truststore.jks") | ||
.overrideConfigKey("quarkus.tls.ws-client.trust-store.jks.password", "secret") | ||
.overrideConfigKey("quarkus.websockets-next.client.tls-configuration-name", "ws-client"); | ||
|
||
@Inject | ||
WebSocketConnector<ClientEndpoint> connector; | ||
|
||
@TestHTTPResource(value = "/", tls = true) | ||
URI uri; | ||
|
||
@Test | ||
void testClient() throws InterruptedException { | ||
WebSocketClientConnection connection = connector | ||
.baseUri(uri) | ||
// The value will be encoded automatically | ||
.pathParam("name", "Lu=") | ||
.connectAndAwait(); | ||
assertTrue(connection.isSecure()); | ||
|
||
assertEquals("Lu=", connection.pathParam("name")); | ||
connection.sendTextAndAwait("Hi!"); | ||
|
||
assertTrue(ClientEndpoint.MESSAGE_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertEquals("Lu=:Hello Lu=!", ClientEndpoint.MESSAGES.get(0)); | ||
assertEquals("Lu=:Hi!", ClientEndpoint.MESSAGES.get(1)); | ||
|
||
connection.closeAndAwait(); | ||
assertTrue(ClientEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
assertTrue(ServerEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); | ||
} | ||
|
||
@WebSocket(path = "/endpoint/{name}") | ||
public static class ServerEndpoint { | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnOpen | ||
String open(@PathParam String name) { | ||
return "Hello " + name + "!"; | ||
} | ||
|
||
@OnTextMessage | ||
String echo(String message) { | ||
return message; | ||
} | ||
|
||
@OnClose | ||
void close() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
|
||
@WebSocketClient(path = "/endpoint/{name}") | ||
public static class ClientEndpoint { | ||
|
||
static final CountDownLatch MESSAGE_LATCH = new CountDownLatch(2); | ||
|
||
static final List<String> MESSAGES = new CopyOnWriteArrayList<>(); | ||
|
||
static final CountDownLatch CLOSED_LATCH = new CountDownLatch(1); | ||
|
||
@OnTextMessage | ||
void onMessage(@PathParam String name, String message, WebSocketClientConnection connection) { | ||
if (!name.equals(connection.pathParam("name"))) { | ||
throw new IllegalArgumentException(); | ||
} | ||
MESSAGES.add(name + ":" + message); | ||
MESSAGE_LATCH.countDown(); | ||
} | ||
|
||
@OnClose | ||
void close() { | ||
CLOSED_LATCH.countDown(); | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.