This is an automated email from the ASF dual-hosted git repository. nferraro pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new e02ffce knative: add some checks about supported http methods and mandatory body new ca2e1dc Merge pull request #161 from lburgazzoli/knative-http-hardening e02ffce is described below commit e02ffce53d63d4df8e277663e30b13861611d795 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Wed Oct 16 11:33:30 2019 +0200 knative: add some checks about supported http methods and mandatory body --- .../http/KnativeHttpConsumerDispatcher.java | 10 +++ .../knative/http/KnativeHttpProducer.java | 7 +++ .../component/knative/http/KnativeHttpTest.java | 72 ++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerDispatcher.java b/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerDispatcher.java index ed4af7f..621fb1a 100644 --- a/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerDispatcher.java +++ b/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpConsumerDispatcher.java @@ -25,6 +25,7 @@ import java.util.concurrent.ExecutorService; import io.vertx.core.Handler; import io.vertx.core.Vertx; +import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerOptions; import io.vertx.core.http.HttpServerRequest; @@ -167,6 +168,15 @@ public final class KnativeHttpConsumerDispatcher { @Override public void handle(HttpServerRequest request) { + if (request.method() != HttpMethod.POST) { + HttpServerResponse response = request.response(); + response.setStatusCode(405); + response.putHeader(Exchange.CONTENT_TYPE, "text/plain"); + response.end("Unsupported method: " + request.method()); + + return; + } + LOGGER.debug("received exchange on path: {}, headers: {}", request.path(), request.headers() diff --git a/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java b/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java index fd4328e..124d91a 100644 --- a/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java +++ b/camel-knative/camel-knative-http/src/main/java/org/apache/camel/component/knative/http/KnativeHttpProducer.java @@ -69,6 +69,13 @@ public class KnativeHttpProducer extends DefaultAsyncProducer { @Override public boolean process(Exchange exchange, AsyncCallback callback) { + if (exchange.getMessage().getBody() == null) { + exchange.setException(new IllegalArgumentException("body must not be null")); + callback.done(true); + + return true; + } + final byte[] payload; try { diff --git a/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java b/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java index e13acaf..cd572ec 100644 --- a/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java +++ b/camel-knative/camel-knative-http/src/test/java/org/apache/camel/component/knative/http/KnativeHttpTest.java @@ -1017,5 +1017,77 @@ public class KnativeHttpTest { mock.assertIsSatisfied(); } + @ParameterizedTest + @MethodSource("provideCloudEventsImplementations") + void testWrongMethod(CloudEvent ce) throws Exception { + KnativeEnvironment env = KnativeEnvironment.on( + KnativeEnvironment.endpoint( + Knative.EndpointKind.source, + "myEndpoint", + "localhost", + port, + KnativeSupport.mapOf( + Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event", + Knative.CONTENT_TYPE, "text/plain" + )) + ); + + KnativeComponent component = context.getComponent("knative", KnativeComponent.class); + component.setCloudEventsSpecVersion(ce.version()); + component.setEnvironment(env); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("knative:endpoint/myEndpoint") + .to("mock:ce"); + from("direct:start") + .toF("undertow:http://localhost:%d", port); + } + }); + + context.start(); + + Exchange exchange = template.request("direct:start", e -> e.getMessage().setBody(null)); + assertThat(exchange.isFailed()).isTrue(); + assertThat(exchange.getException()).isInstanceOf(CamelException.class); + assertThat(exchange.getException()).hasMessageStartingWith("HTTP operation failed invoking"); + assertThat(exchange.getException()).hasMessageEndingWith("with statusCode: 405"); + } + + @ParameterizedTest + @MethodSource("provideCloudEventsImplementations") + void testNoBody(CloudEvent ce) throws Exception { + KnativeEnvironment env = KnativeEnvironment.on( + KnativeEnvironment.endpoint( + Knative.EndpointKind.sink, + "myEndpoint", + "localhost", + port, + KnativeSupport.mapOf( + Knative.KNATIVE_EVENT_TYPE, "org.apache.camel.event", + Knative.CONTENT_TYPE, "text/plain" + )) + ); + + KnativeComponent component = context.getComponent("knative", KnativeComponent.class); + component.setCloudEventsSpecVersion(ce.version()); + component.setEnvironment(env); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("knative:endpoint/myEndpoint"); + } + }); + + context.start(); + + Exchange exchange = template.request("direct:start", e -> e.getMessage().setBody(null)); + assertThat(exchange.isFailed()).isTrue(); + assertThat(exchange.getException()).isInstanceOf(IllegalArgumentException.class); + assertThat(exchange.getException()).hasMessage("body must not be null"); + } }