Skip to content

Commit

Permalink
/openapi endpoint with forwarded header (#433)
Browse files Browse the repository at this point in the history
* /openapi endpoint with forwarded header

Signed-off-by: Stanislav Knot <sknot@redhat.com>

* comments

Signed-off-by: Stanislav Knot <sknot@redhat.com>

* tests

Signed-off-by: Stanislav Knot <sknot@redhat.com>

* screwed rebase, probably

Signed-off-by: Stanislav Knot <sknot@redhat.com>
  • Loading branch information
sknot-rh committed Jun 3, 2021
1 parent e24a57f commit 9964dcc
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/java/io/strimzi/kafka/bridge/http/HttpBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpConnection;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
Expand Down Expand Up @@ -495,7 +496,22 @@ private void openapi(RoutingContext routingContext) {
FileSystem fileSystem = vertx.fileSystem();
fileSystem.readFile("openapiv2.json", readFile -> {
if (readFile.succeeded()) {
HttpUtils.sendFile(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, "openapiv2.json");
String xForwardedPath = routingContext.request().getHeader("x-forwarded-path");
String xForwardedPrefix = routingContext.request().getHeader("x-forwarded-prefix");
if (xForwardedPath == null && xForwardedPrefix == null) {
HttpUtils.sendFile(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, "openapiv2.json");
} else {
String path = "/";
if (xForwardedPrefix != null) {
path = xForwardedPrefix;
}
if (xForwardedPath != null) {
path = xForwardedPath;
}
JsonObject json = (JsonObject) Json.decodeValue(readFile.result());
json.put("basePath", path);
HttpUtils.sendResponse(routingContext, HttpResponseStatus.OK.code(), BridgeContentType.JSON, json.toBuffer());
}
} else {
log.error("Failed to read OpenAPI JSON file", readFile.cause());
HttpBridgeError error = new HttpBridgeError(
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/io/strimzi/kafka/bridge/http/OtherServicesIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,63 @@ void getVersion(VertxTestContext context) {
context.completeNow();
});
}

@Test
void openApiTestWithForwardedPath(VertxTestContext context) {
String forwardedPath = "/app/kafka-bridge";
baseService()
.getRequest("/openapi")
.putHeader("x-Forwarded-Path", forwardedPath)
.as(BodyCodec.jsonObject())
.send(ar -> {
context.verify(() -> {
assertThat(ar.succeeded(), is(true));
HttpResponse<JsonObject> response = ar.result();
assertThat(response.statusCode(), is(HttpResponseStatus.OK.code()));
JsonObject bridgeResponse = response.body();
assertThat(bridgeResponse.getString("basePath"), is(forwardedPath));
});
context.completeNow();
});
}

@Test
void openApiTestWithForwardedPrefix(VertxTestContext context) {
String forwardedPrefix = "/app/kafka-bridge";
baseService()
.getRequest("/openapi")
.putHeader("x-Forwarded-Prefix", forwardedPrefix)
.as(BodyCodec.jsonObject())
.send(ar -> {
context.verify(() -> {
assertThat(ar.succeeded(), is(true));
HttpResponse<JsonObject> response = ar.result();
assertThat(response.statusCode(), is(HttpResponseStatus.OK.code()));
JsonObject bridgeResponse = response.body();
assertThat(bridgeResponse.getString("basePath"), is(forwardedPrefix));
});
context.completeNow();
});
}

@Test
void openApiTestWithForwardedPathAndPrefix(VertxTestContext context) {
String forwardedPath = "/app/kafka-bridge-path";
String forwardedPrefix = "/app/kafka-bridge-prefix";
baseService()
.getRequest("/openapi")
.putHeader("x-Forwarded-Path", forwardedPath)
.putHeader("x-Forwarded-Prefix", forwardedPrefix)
.as(BodyCodec.jsonObject())
.send(ar -> {
context.verify(() -> {
assertThat(ar.succeeded(), is(true));
HttpResponse<JsonObject> response = ar.result();
assertThat(response.statusCode(), is(HttpResponseStatus.OK.code()));
JsonObject bridgeResponse = response.body();
assertThat(bridgeResponse.getString("basePath"), is(forwardedPath));
});
context.completeNow();
});
}
}

0 comments on commit 9964dcc

Please sign in to comment.