mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elbv2): bound Lambda and target body sizes (#3149)
* fix(elbv2): bound request and response bodies * fix(elbv2): sanitize oversized Lambda responses (#3149) * fix(elbv2): return 502 for oversized Lambda responses * fix(elbv2): drain failed proxy request bodies
This commit is contained in:
1 parent
4cc850e8df
commit
ec41b1eef7
3 files changed
+375
-57
No files matched your search
+90
@@ -17,6 +17,8 @@ import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@@ -60,7 +62,16 @@ class ElbV2LambdaTargetDataPlaneIntegrationTest {
|
||||
try (ZipOutputStream zos = new ZipOutputStream(baos)) {
|
||||
zos.putNextEntry(new ZipEntry("index.py"));
|
||||
zos.write((
|
||||
"invocations = 0\n" +
|
||||
"def handler(event, context):\n" +
|
||||
" global invocations\n" +
|
||||
" invocations += 1\n" +
|
||||
" if event.get(\"path\") == \"/invocation-count\":\n" +
|
||||
" return {\"statusCode\": 200, \"body\": str(invocations)}\n" +
|
||||
" if event.get(\"path\") == \"/response-at-limit\":\n" +
|
||||
" return {\"statusCode\": 200, \"body\": \"x\" * (1024 * 1024)}\n" +
|
||||
" if event.get(\"path\") == \"/response-oversized\":\n" +
|
||||
" return {\"statusCode\": 200, \"headers\": {\"Content-Length\": \"1048577\"}, \"body\": \"x\" * (1024 * 1024 + 1)}\n" +
|
||||
" return {\"statusCode\": 200, \"body\": \"ok\"}\n"
|
||||
).getBytes(StandardCharsets.UTF_8));
|
||||
zos.closeEntry();
|
||||
@@ -180,6 +191,85 @@ class ElbV2LambdaTargetDataPlaneIntegrationTest {
|
||||
.body(equalTo("ok"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void requestBodyAtOneMiBIsAccepted() {
|
||||
byte[] exactLimit = new byte[1024 * 1024];
|
||||
|
||||
given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.contentType("application/octet-stream")
|
||||
.body(exactLimit)
|
||||
.when()
|
||||
.post("/exact")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(equalTo("ok"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void oversizedRequestBodyIsRejectedBeforeLambdaInvocation() {
|
||||
byte[] oversized = new byte[1024 * 1024 + 1];
|
||||
int invocationCountBefore = Integer.parseInt(given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.when()
|
||||
.get("/invocation-count")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.asString());
|
||||
|
||||
given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.contentType("application/octet-stream")
|
||||
.body(oversized)
|
||||
.when()
|
||||
.post("/oversized")
|
||||
.then()
|
||||
.statusCode(413);
|
||||
|
||||
int invocationCountAfter = Integer.parseInt(given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.when()
|
||||
.get("/invocation-count")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.asString());
|
||||
assertEquals(invocationCountBefore + 1, invocationCountAfter);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void lambdaResponseBodyAtOneMiBIsAccepted() {
|
||||
given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.when()
|
||||
.get("/response-at-limit")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(org.hamcrest.Matchers.hasLength(1024 * 1024));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void oversizedLambdaResponseIsRejected() {
|
||||
Response response = given()
|
||||
.baseUri("http://" + lbDnsName)
|
||||
.port(LISTENER_PORT)
|
||||
.when()
|
||||
.get("/response-oversized");
|
||||
|
||||
response.then().statusCode(502);
|
||||
assertNotEquals("1048577", response.getHeader("Content-Length"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup. Lives as the highest-ordered @Test rather than {@code @AfterAll} because
|
||||
* Quarkus tears the test endpoint down before JUnit's @AfterAll runs, so HTTP-based
|
||||
|
||||
+154
-1
@@ -3,19 +3,32 @@ package io.github.hectorvent.floci.services.elbv2;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.QuarkusTestProfile;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import io.vertx.core.buffer.Buffer;
|
||||
import io.vertx.core.Vertx;
|
||||
import io.vertx.core.http.HttpClientRequest;
|
||||
import io.vertx.core.http.HttpClientResponse;
|
||||
import io.vertx.core.http.HttpServer;
|
||||
import io.vertx.core.http.RequestOptions;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.awaitility.Awaitility.await;
|
||||
import static org.hamcrest.Matchers.anyOf;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(ElbV2PreserveHostHeaderIntegrationTest.RealElbV2DataPlaneProfile.class)
|
||||
@@ -39,7 +52,20 @@ class ElbV2PreserveHostHeaderIntegrationTest {
|
||||
@Test
|
||||
void preservesHostHeaderWhenLoadBalancerAttributeIsEnabled() throws Exception {
|
||||
HttpServer backend = vertx.createHttpServer()
|
||||
.requestHandler(request -> request.response().end(request.getHeader("Host")))
|
||||
.requestHandler(request -> {
|
||||
if ("/stream".equals(request.path())) {
|
||||
request.body().onSuccess(body -> {
|
||||
request.response()
|
||||
.putHeader("X-Received-Content-Length",
|
||||
String.valueOf(request.getHeader("Content-Length")))
|
||||
.setChunked(true);
|
||||
request.response().write("stream-");
|
||||
request.response().end(body.toString());
|
||||
});
|
||||
} else {
|
||||
request.response().end(request.getHeader("Host"));
|
||||
}
|
||||
})
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
@@ -54,6 +80,7 @@ class ElbV2PreserveHostHeaderIntegrationTest {
|
||||
registerTarget(targetGroupArn, backend.actualPort());
|
||||
listenerArn = createListener(loadBalancerArn, targetGroupArn);
|
||||
|
||||
assertStreamsRequestAndResponse(loadBalancerArn);
|
||||
assertForwardedHost("127.0.0.1:" + backend.actualPort());
|
||||
|
||||
enableHostHeaderPreservation(loadBalancerArn);
|
||||
@@ -67,6 +94,104 @@ class ElbV2PreserveHostHeaderIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void drainsRequestWhenTargetConnectionFails() throws Exception {
|
||||
HttpServer backend = vertx.createHttpServer()
|
||||
.requestHandler(request -> request.response().end("healthy"))
|
||||
.listen(0, "127.0.0.1")
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.get(2, TimeUnit.SECONDS);
|
||||
|
||||
String loadBalancerArn = null;
|
||||
String targetGroupArn = null;
|
||||
String listenerArn = null;
|
||||
int failedPort;
|
||||
try (ServerSocket reservation = new ServerSocket(0)) {
|
||||
failedPort = reservation.getLocalPort();
|
||||
}
|
||||
try {
|
||||
loadBalancerArn = createLoadBalancer();
|
||||
targetGroupArn = createTargetGroup(backend.actualPort());
|
||||
registerTargets(targetGroupArn, failedPort, backend.actualPort());
|
||||
listenerArn = createListener(loadBalancerArn, targetGroupArn);
|
||||
|
||||
String host = loadBalancerDnsName(loadBalancerArn);
|
||||
try (Socket socket = new Socket("127.0.0.1", LISTENER_PORT)) {
|
||||
socket.setSoTimeout(2_000);
|
||||
OutputStream output = socket.getOutputStream();
|
||||
output.write(("POST /failed HTTP/1.1\r\n"
|
||||
+ "Host: " + host + "\r\n"
|
||||
+ "Content-Length: 11\r\n"
|
||||
+ "Connection: keep-alive\r\n\r\n"
|
||||
+ "hello world").getBytes(StandardCharsets.ISO_8859_1));
|
||||
output.flush();
|
||||
|
||||
BufferedReader input = new BufferedReader(
|
||||
new InputStreamReader(socket.getInputStream(), StandardCharsets.ISO_8859_1));
|
||||
assertEquals(503, readStatus(input));
|
||||
|
||||
output.write(("GET /healthy HTTP/1.1\r\n"
|
||||
+ "Host: " + host + "\r\n"
|
||||
+ "Connection: close\r\n\r\n").getBytes(StandardCharsets.ISO_8859_1));
|
||||
output.flush();
|
||||
|
||||
assertEquals(200, readStatus(input));
|
||||
}
|
||||
} finally {
|
||||
deleteListener(listenerArn);
|
||||
deleteTargetGroup(targetGroupArn);
|
||||
deleteLoadBalancer(loadBalancerArn);
|
||||
backend.close().toCompletionStage().toCompletableFuture().get(2, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
private static int readStatus(BufferedReader input) throws IOException {
|
||||
String statusLine = input.readLine();
|
||||
if (statusLine == null) {
|
||||
throw new IOException("Response ended before the status line");
|
||||
}
|
||||
int contentLength = 0;
|
||||
String header;
|
||||
while (!(header = input.readLine()).isEmpty()) {
|
||||
int separator = header.indexOf(':');
|
||||
if (separator > 0 && "content-length".equalsIgnoreCase(header.substring(0, separator))) {
|
||||
contentLength = Integer.parseInt(header.substring(separator + 1).trim());
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < contentLength; i++) {
|
||||
if (input.read() == -1) {
|
||||
throw new IOException("Response ended before the body");
|
||||
}
|
||||
}
|
||||
return Integer.parseInt(statusLine.split(" ", 3)[1]);
|
||||
}
|
||||
|
||||
private void assertStreamsRequestAndResponse(String loadBalancerArn) throws Exception {
|
||||
HttpClientRequest request = vertx.createHttpClient()
|
||||
.request(new RequestOptions()
|
||||
.setHost(loadBalancerDnsName(loadBalancerArn))
|
||||
.setPort(LISTENER_PORT)
|
||||
.setMethod(io.vertx.core.http.HttpMethod.POST)
|
||||
.setURI("/stream"))
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture()
|
||||
.get(2, TimeUnit.SECONDS);
|
||||
CompletableFuture<Buffer> bodyFuture = new CompletableFuture<>();
|
||||
CompletableFuture<HttpClientResponse> responseFuture = request.response()
|
||||
.onSuccess(response -> response.bodyHandler(bodyFuture::complete))
|
||||
.toCompletionStage()
|
||||
.toCompletableFuture();
|
||||
request.putHeader("Content-Length", "11");
|
||||
request.write(Buffer.buffer("hello "));
|
||||
request.end(Buffer.buffer("world"));
|
||||
|
||||
HttpClientResponse response = responseFuture.get(2, TimeUnit.SECONDS);
|
||||
assertEquals(200, response.statusCode());
|
||||
assertEquals("11", response.getHeader("X-Received-Content-Length"));
|
||||
assertEquals("stream-hello world", bodyFuture.get(2, TimeUnit.SECONDS).toString());
|
||||
}
|
||||
|
||||
private static String createLoadBalancer() {
|
||||
return given()
|
||||
.formParam("Action", "CreateLoadBalancer")
|
||||
@@ -81,6 +206,19 @@ class ElbV2PreserveHostHeaderIntegrationTest {
|
||||
.path("CreateLoadBalancerResponse.CreateLoadBalancerResult.LoadBalancers.member.LoadBalancerArn");
|
||||
}
|
||||
|
||||
private static String loadBalancerDnsName(String loadBalancerArn) {
|
||||
return given()
|
||||
.formParam("Action", "DescribeLoadBalancers")
|
||||
.formParam("LoadBalancerArns.member.1", loadBalancerArn)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract()
|
||||
.path("DescribeLoadBalancersResponse.DescribeLoadBalancersResult.LoadBalancers.member.DNSName");
|
||||
}
|
||||
|
||||
private static String createTargetGroup(int backendPort) {
|
||||
return given()
|
||||
.formParam("Action", "CreateTargetGroup")
|
||||
@@ -111,6 +249,21 @@ class ElbV2PreserveHostHeaderIntegrationTest {
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static void registerTargets(String targetGroupArn, int failedPort, int backendPort) {
|
||||
given()
|
||||
.formParam("Action", "RegisterTargets")
|
||||
.formParam("TargetGroupArn", targetGroupArn)
|
||||
.formParam("Targets.member.1.Id", "127.0.0.1")
|
||||
.formParam("Targets.member.1.Port", failedPort)
|
||||
.formParam("Targets.member.2.Id", "127.0.0.1")
|
||||
.formParam("Targets.member.2.Port", backendPort)
|
||||
.header("Authorization", AUTH)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static String createListener(String loadBalancerArn, String targetGroupArn) {
|
||||
return given()
|
||||
.formParam("Action", "CreateListener")
|
||||
|
||||
Reference in new issue
Block a user