mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
HTTP_PROXY request parameter mappings on HTTP APIs now read $request.body, $request.body.<JSONPath>, $stageVariables.<name> and $context.<name>, the sources the AWS service integration subtypes already read, resolved by ContextValueResolver for both integration types. A header mapping that carries a control character is refused with 502 before anything reaches the backend, and an overwrite:path reads each embedded source expression whole, bracketed JSONPath selectors included. Signed-off-by: Tyler Webb <tyler.webb@verkada.com>
156 lines
6.9 KiB
Java
156 lines
6.9 KiB
Java
package io.floci.aws.services.apigatewayv2;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.node.ObjectNode;
|
|
import com.sun.net.httpserver.HttpServer;
|
|
import io.quarkus.test.junit.QuarkusTest;
|
|
import io.restassured.http.ContentType;
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import java.net.InetSocketAddress;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
import static io.restassured.RestAssured.given;
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
|
@QuarkusTest
|
|
class HttpProxyRequestSourceMappingIntegrationTest {
|
|
private static final ObjectMapper MAPPER = new ObjectMapper();
|
|
private static final List<String> HEADERS = List.of("x-order", "x-backend", "x-stage", "x-route", "x-api",
|
|
"x-account", "x-path", "x-epoch", "x-missing", "x-missing-stage");
|
|
private final AtomicReference<JsonNode> received = new AtomicReference<>();
|
|
private HttpServer backend;
|
|
private String apiId;
|
|
|
|
@BeforeEach
|
|
void startBackend() throws Exception {
|
|
backend = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
|
backend.createContext("/", exchange -> {
|
|
ObjectNode request = MAPPER.createObjectNode();
|
|
for (String header : HEADERS) {
|
|
request.put(header, exchange.getRequestHeaders().getFirst(header));
|
|
}
|
|
request.put("path", exchange.getRequestURI().getPath());
|
|
request.put("query", exchange.getRequestURI().getRawQuery());
|
|
received.set(request);
|
|
byte[] bytes = "ok".getBytes(StandardCharsets.UTF_8);
|
|
exchange.sendResponseHeaders(200, bytes.length);
|
|
exchange.getResponseBody().write(bytes);
|
|
exchange.close();
|
|
});
|
|
backend.start();
|
|
}
|
|
|
|
@AfterEach
|
|
void cleanUp() {
|
|
try {
|
|
if (apiId != null) {
|
|
given().delete("/v2/apis/" + apiId).then().statusCode(204);
|
|
}
|
|
} finally {
|
|
if (backend != null) {
|
|
backend.stop(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
@Test
|
|
void mapsBodyStageVariablesAndContextToHeadersQueryAndPath() {
|
|
configureApi();
|
|
long before = System.currentTimeMillis();
|
|
invoke("{\"order\":{\"id\":\"o-42\"}}");
|
|
JsonNode request = received.get();
|
|
assertNotNull(request);
|
|
assertEquals("o-42", request.path("x-order").asText());
|
|
assertEquals("blue", request.path("x-backend").asText());
|
|
assertEquals("test", request.path("x-stage").asText());
|
|
assertEquals("POST /orders", request.path("x-route").asText());
|
|
assertEquals(apiId, request.path("x-api").asText());
|
|
assertEquals("000000000000", request.path("x-account").asText());
|
|
assertEquals("/test/orders", request.path("x-path").asText());
|
|
assertTrue(Long.parseLong(request.path("x-epoch").asText()) >= before - 1000);
|
|
assertTrue(request.path("x-missing").isNull());
|
|
assertTrue(request.path("x-missing-stage").isNull());
|
|
assertEquals("/orders/o-42/test", request.path("path").asText());
|
|
assertEquals("order=o-42", request.path("query").asText());
|
|
}
|
|
|
|
@Test
|
|
void bodyThatIsNotJsonSkipsBodyMappings() {
|
|
configureApi();
|
|
invoke("not json");
|
|
JsonNode request = received.get();
|
|
assertTrue(request.path("x-order").isNull());
|
|
assertTrue(request.path("query").isNull());
|
|
assertEquals("blue", request.path("x-backend").asText());
|
|
}
|
|
|
|
@Test
|
|
void indexedBodyValueFillsAnEmbeddedPath() {
|
|
configureApi(Map.of("overwrite:path", "/orders/$request.body.items[1].id/lines"));
|
|
invoke("{\"items\":[{\"id\":\"i-1\"},{\"id\":\"i-2\"}]}");
|
|
assertEquals("/orders/i-2/lines", received.get().path("path").asText());
|
|
}
|
|
|
|
@Test
|
|
void wholeBodyCarryingALineBreakIsRejectedBeforeTheBackend() {
|
|
configureApi(Map.of("overwrite:header.x-order", "$request.body"));
|
|
invokeExpectingRejectedHeader("o-1\r\nX-Injected: 1", "x-order");
|
|
}
|
|
|
|
@Test
|
|
void wholeBodyCarryingALineBreakIsRejectedWhenTheHostIsMapped() {
|
|
configureApi(Map.of("overwrite:header.Host", "orders.internal",
|
|
"overwrite:header.x-order", "$request.body"));
|
|
invokeExpectingRejectedHeader("o-1\r\nX-Injected: 1", "x-order");
|
|
}
|
|
|
|
private void configureApi() {
|
|
configureApi(Map.ofEntries(
|
|
Map.entry("overwrite:header.x-order", "$request.body.order.id"),
|
|
Map.entry("overwrite:header.x-backend", "$stageVariables.backend"),
|
|
Map.entry("overwrite:header.x-stage", "$context.stage"),
|
|
Map.entry("overwrite:header.x-route", "$context.routeKey"),
|
|
Map.entry("overwrite:header.x-api", "$context.apiId"),
|
|
Map.entry("overwrite:header.x-account", "$context.accountId"),
|
|
Map.entry("overwrite:header.x-path", "$context.path"),
|
|
Map.entry("overwrite:header.x-epoch", "$context.requestTimeEpoch"),
|
|
Map.entry("overwrite:header.x-missing", "$request.body.order.missing"),
|
|
Map.entry("overwrite:header.x-missing-stage", "$stageVariables.missing"),
|
|
Map.entry("overwrite:querystring.order", "$request.body.order.id"),
|
|
Map.entry("overwrite:path", "/orders/$request.body.order.id/$context.stage")));
|
|
}
|
|
|
|
private void configureApi(Map<String, String> requestParameters) {
|
|
apiId = HttpProxyApiFixtures.createProxyApi("request-source-mapping", "", "test", Map.of("backend", "blue"),
|
|
"POST", backend.getAddress().getPort(), "/orders");
|
|
String apiPath = "/v2/apis/" + apiId;
|
|
String integrationId = given().get(apiPath + "/integrations").then().statusCode(200)
|
|
.extract().path("items[0].integrationId");
|
|
given().contentType(ContentType.JSON).body(Map.of("requestParameters", requestParameters))
|
|
.patch(apiPath + "/integrations/" + integrationId).then().statusCode(200);
|
|
}
|
|
|
|
private void invoke(String body) {
|
|
given().contentType(ContentType.JSON).body(body)
|
|
.post("/execute-api/" + apiId + "/test/orders").then().statusCode(200);
|
|
}
|
|
|
|
private void invokeExpectingRejectedHeader(String body, String header) {
|
|
given().contentType(ContentType.TEXT).body(body)
|
|
.post("/execute-api/" + apiId + "/test/orders").then().statusCode(502)
|
|
.body("message", equalTo("Bad Gateway: invalid request header value for " + header));
|
|
assertNull(received.get());
|
|
}
|
|
}
|