From f2e6f050e0ca72d5ae3c385e935caee64055aa9a Mon Sep 17 00:00:00 2001 From: James Netherton Date: Thu, 27 Jul 2023 12:34:17 +0100 Subject: [PATCH] Disallow invalid host/port values on the vertx-websocket server consumer Fixes #5118 --- .../reference/extensions/vertx-websocket.adoc | 12 ++++------ .../runtime/src/main/doc/usage.adoc | 12 ++++------ .../websocket/VertxWebsocketRecorder.java | 7 ++++++ .../websocket/it/VertxWebsocketResource.java | 17 +++++++++++++ .../websocket/it/VertxWebsocketRoutes.java | 4 ---- .../websocket/it/VertxWebsocketTest.java | 24 ++++++++----------- 6 files changed, 42 insertions(+), 34 deletions(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/vertx-websocket.adoc b/docs/modules/ROOT/pages/reference/extensions/vertx-websocket.adoc index 8c396c918a8e..21529f4984bf 100644 --- a/docs/modules/ROOT/pages/reference/extensions/vertx-websocket.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/vertx-websocket.adoc @@ -63,13 +63,9 @@ from("vertx-websocket:/my-websocket-path") .setBody().constant("Hello World"); ---- -Or alternatively, you can refer to the full host & port configuration for the Quarkus HTTP server. - -[source,java] ----- -from("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path") - .setBody().constant("Hello World"); ----- +NOTE: While you do not need to explicitly configure the host/port on the vertx-websocket consumer. If you choose to, +the host & port must exactly match the value of the Quarkus HTTP server configuration values for `quarkus.http.host` and `quarkus.http.port`. +Otherwise an exception will be thrown at runtime. [id="extensions-vertx-websocket-usage-vert-x-websocket-producers"] === Vert.x WebSocket producers @@ -93,7 +89,7 @@ from("direct:sendToWebSocket") .log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path"); ---- -When producing messages to an external WebSocket server, then you must provide the host name and port (if required). +When producing messages to an external WebSocket server, then you must always provide the host name and port (if required). [id="extensions-vertx-websocket-additional-camel-quarkus-configuration"] diff --git a/extensions/vertx-websocket/runtime/src/main/doc/usage.adoc b/extensions/vertx-websocket/runtime/src/main/doc/usage.adoc index 902c7225f9aa..649300042f35 100644 --- a/extensions/vertx-websocket/runtime/src/main/doc/usage.adoc +++ b/extensions/vertx-websocket/runtime/src/main/doc/usage.adoc @@ -11,13 +11,9 @@ from("vertx-websocket:/my-websocket-path") .setBody().constant("Hello World"); ---- -Or alternatively, you can refer to the full host & port configuration for the Quarkus HTTP server. - -[source,java] ----- -from("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path") - .setBody().constant("Hello World"); ----- +NOTE: While you do not need to explicitly configure the host/port on the vertx-websocket consumer. If you choose to, +the host & port must exactly match the value of the Quarkus HTTP server configuration values for `quarkus.http.host` and `quarkus.http.port`. +Otherwise an exception will be thrown at runtime. === Vert.x WebSocket producers @@ -40,4 +36,4 @@ from("direct:sendToWebSocket") .log("vertx-websocket:{{quarkus.http.host}}:{{quarkus.http.port}}/my-websocket-path"); ---- -When producing messages to an external WebSocket server, then you must provide the host name and port (if required). +When producing messages to an external WebSocket server, then you must always provide the host name and port (if required). diff --git a/extensions/vertx-websocket/runtime/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/VertxWebsocketRecorder.java b/extensions/vertx-websocket/runtime/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/VertxWebsocketRecorder.java index 05e90337a325..9c6c6ed0b295 100644 --- a/extensions/vertx-websocket/runtime/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/VertxWebsocketRecorder.java +++ b/extensions/vertx-websocket/runtime/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/VertxWebsocketRecorder.java @@ -85,6 +85,13 @@ static final class QuarkusVertxWebsocketComponent extends VertxWebsocketComponen @Override protected VertxWebsocketHost createVertxWebsocketHost(VertxWebsocketHostConfiguration hostConfiguration, VertxWebsocketHostKey hostKey) { + // If a host / port was specified on the consumer, it must be the same as what the Quarkus HTTP server is bound to + if (!hostKey.getHost().equals(HOST) || hostKey.getPort() != PORT) { + String message = String.format( + "Invalid host/port %s:%d. The host/port can only be configured as %s:%d", + hostKey.getHost(), hostKey.getPort(), HOST, PORT); + throw new IllegalArgumentException(message); + } return new QuarkusVertxWebsocketHost(getCamelContext(), hostConfiguration, hostKey); } diff --git a/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketResource.java b/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketResource.java index ac0795f908a8..5789689ecf34 100644 --- a/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketResource.java +++ b/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketResource.java @@ -35,6 +35,7 @@ import jakarta.ws.rs.Produces; import jakarta.ws.rs.QueryParam; import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; import org.apache.camel.CamelContext; import org.apache.camel.ConsumerTemplate; import org.apache.camel.ProducerTemplate; @@ -42,10 +43,12 @@ import org.apache.camel.support.jsse.KeyStoreParameters; import org.apache.camel.support.jsse.SSLContextParameters; import org.apache.camel.support.jsse.TrustManagersParameters; +import org.jboss.logging.Logger; @Path("/vertx-websocket") @ApplicationScoped public class VertxWebsocketResource { + private static final Logger LOG = Logger.getLogger(VertxWebsocketResource.class); @Inject CamelContext context; @@ -121,6 +124,20 @@ public int getDefaultPort(String scheme) { return connectOptions.getPort(); } + @Path("/invalid/consumer/uri") + @GET + @Produces(MediaType.TEXT_PLAIN) + public Response invalidConsumerHostPort(@QueryParam("hostPort") String hostPort) { + try { + consumerTemplate.receive("vertx-websocket:" + hostPort + "/test"); + return Response.ok().build(); + } catch (Exception e) { + LOG.error("Error creating vertx-websocket consumer", e); + String message = e.getCause() != null ? e.getCause().getMessage() : e.getMessage(); + return Response.serverError().entity(message).build(); + } + } + @Named public SSLContextParameters clientSSLContextParameters() { KeyStoreParameters truststoreParameters = new KeyStoreParameters(); diff --git a/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketRoutes.java b/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketRoutes.java index 231e23fe769e..88a12a5cd73c 100644 --- a/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketRoutes.java +++ b/integration-tests/vertx-websocket/src/main/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketRoutes.java @@ -36,10 +36,6 @@ public void configure() throws Exception { .end() .to("vertx-websocket:/"); - from("vertx-websocket:redundant.host:9999/test/default/host/port/applied") - .setBody(simple("Hello ${body}")) - .to("vertx-websocket:/test/default/host/port/applied"); - from("direct:sendMessage") .to("vertx-websocket:/test"); diff --git a/integration-tests/vertx-websocket/src/test/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketTest.java b/integration-tests/vertx-websocket/src/test/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketTest.java index 414cbfbed01c..c2a053622d23 100644 --- a/integration-tests/vertx-websocket/src/test/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketTest.java +++ b/integration-tests/vertx-websocket/src/test/java/org/apache/camel/quarkus/component/vertx/websocket/it/VertxWebsocketTest.java @@ -42,6 +42,7 @@ import org.junit.jupiter.params.provider.ValueSource; import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -54,9 +55,6 @@ class VertxWebsocketTest { @TestHTTPResource("/echo") URI echo; - @TestHTTPResource("/test/default/host/port/applied") - URI defaultHostPortApplied; - @TestHTTPResource("/client/consumer") URI clientConsumer; @@ -82,17 +80,15 @@ public void testEchoWithShortFormUri() throws Exception { } } - @Test - public void testEchoWithIgnoredHostPortConfig() throws Exception { - String message = "From Ignored Host Port Config"; - - try (WebSocketConnection connection = new WebSocketConnection(defaultHostPortApplied, message)) { - connection.connect(); - - List messages = connection.getMessages(); - assertEquals(1, messages.size()); - assertEquals("Hello " + message, messages.get(0)); - } + @ParameterizedTest + @ValueSource(strings = { "invalid.host", "localhost:9999", "invalid.host:9999" }) + public void testInvalidHostPortConfig(String hostPort) throws Exception { + RestAssured.given() + .queryParam("hostPort", hostPort) + .get("/vertx-websocket/invalid/consumer/uri") + .then() + .statusCode(500) + .body(startsWith("Invalid host/port")); } @Test