Skip to content
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

knative: add some checks about supported http methods and mandatory body #161

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

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;
Expand Down Expand Up @@ -167,6 +168,15 @@ protected CompletionStage<Void> stopAsync() {

@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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need to ask people to manually set "POST" or we should do it for them.
They're sending data to a knative endpoint that wraps HTTP after all.

I'm thinking to simple bridging integrations:

rest().put("/person/{id}").to("knative:event/person")

Copy link
Contributor Author

@lburgazzoli lburgazzoli Oct 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The component does it by default, in your case you want to "put" an event to the broker and the underlying transport implementation will hit a POST to the target knative service.

This check is on the consumer side to restrict the methods we support


return;
}

LOGGER.debug("received exchange on path: {}, headers: {}",
request.path(),
request.headers()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ public KnativeHttpProducer(

@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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1017,5 +1017,77 @@ public void configure() throws Exception {
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");
}
}