mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(apigatewayv2): include stage variables in Lambda events (#5416)
* fix(apigatewayv2): include stage variables in Lambda events * fix(apigatewayv2): tolerate missing stages during variable lookup
This commit is contained in:
1 parent
e61b66fb22
commit
22c4c32e3a
9 files changed
+188
-66
No files matched your search
+1
-1
@@ -667,7 +667,7 @@ class ApiGatewayExecuteControllerTest {
|
||||
ApiGatewayExecuteController controller = controller(new ObjectMapper());
|
||||
assertEnglishUtcTimeUnderNonUtcHost(() -> controller.buildRequestAuthorizerEventV2("GET", "/items",
|
||||
"GET /items", "api1", "$default", "us-east-1", emptyHeaders(),
|
||||
uriInfoFor("http://localhost/items")));
|
||||
uriInfoFor("http://localhost/items"), null));
|
||||
}
|
||||
|
||||
private static HttpHeaders emptyHeaders() {
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ class BuildV2AuthorizerEventTrailingSlashTest {
|
||||
private JsonNode buildEvent(String normalizedPath, String rawRequestUri) throws Exception {
|
||||
when(uriInfo.getRequestUri()).thenReturn(new URI(rawRequestUri));
|
||||
String json = controller.buildRequestAuthorizerEventV1(
|
||||
"GET", normalizedPath, "abc123", "test", "us-east-1", headers, uriInfo);
|
||||
"GET", normalizedPath, "abc123", "test", "us-east-1", headers, uriInfo, null);
|
||||
return new ObjectMapper().readTree(json);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ class BuildV2ProxyEventMultiValueTest {
|
||||
"abc123", "us-east-1", "$default", headers, uriInfo, null, "req-1");
|
||||
case AUTHORIZER -> controller.buildRequestAuthorizerEventV2(
|
||||
"GET", "/echo", "GET /echo",
|
||||
"abc123", "$default", "us-east-1", headers, uriInfo);
|
||||
"abc123", "$default", "us-east-1", headers, uriInfo, null);
|
||||
};
|
||||
return new ObjectMapper().readTree(json);
|
||||
}
|
||||
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
package io.github.hectorvent.floci.services.apigateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import jakarta.ws.rs.core.HttpHeaders;
|
||||
import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.UriInfo;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class BuildV2ProxyEventStageVariablesTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private ApiGatewayExecuteController controller;
|
||||
private HttpHeaders headers;
|
||||
private UriInfo uriInfo;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws Exception {
|
||||
RegionResolver regionResolver = mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
|
||||
headers = mock(HttpHeaders.class);
|
||||
when(headers.getRequestHeaders()).thenReturn(new MultivaluedHashMap<>());
|
||||
|
||||
uriInfo = mock(UriInfo.class);
|
||||
when(uriInfo.getQueryParameters()).thenReturn(new MultivaluedHashMap<>());
|
||||
when(uriInfo.getRequestUri()).thenReturn(new URI("http://localhost:4566/echo"));
|
||||
|
||||
controller = new ApiGatewayExecuteController(
|
||||
null, null, null, null,
|
||||
regionResolver, MAPPER, null,
|
||||
null, null, null, null, new ApiGatewayExecuteRouteContext(), null, null, null, null, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void includesConfiguredStageVariables() throws Exception {
|
||||
JsonNode event = buildEvent(Map.of("mode", "production", "region", "local"));
|
||||
|
||||
assertEquals("production", event.path("stageVariables").path("mode").asText());
|
||||
assertEquals("local", event.path("stageVariables").path("region").asText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void omitsStageVariablesWithoutConfiguredStageVariables() throws Exception {
|
||||
JsonNode event = buildEvent(Map.of());
|
||||
|
||||
assertFalse(event.has("stageVariables"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizerEventsIncludeConfiguredStageVariables() throws Exception {
|
||||
for (JsonNode event : buildAuthorizerEvents(Map.of("environment", "review"))) {
|
||||
assertEquals("review", event.path("stageVariables").path("environment").asText());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void authorizerEventsRenderNullWithoutConfiguredStageVariables() throws Exception {
|
||||
for (JsonNode event : buildAuthorizerEvents(Map.of())) {
|
||||
assertTrue(event.path("stageVariables").isNull());
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode buildEvent(Map<String, String> stageVariables) throws Exception {
|
||||
return MAPPER.readTree(controller.buildV2ProxyEvent(
|
||||
"GET", "/echo", "GET /echo", "abc123", "us-east-1", "test",
|
||||
headers, uriInfo, null, "req-1", null, null, null, null, stageVariables));
|
||||
}
|
||||
|
||||
private List<JsonNode> buildAuthorizerEvents(Map<String, String> stageVariables) throws Exception {
|
||||
return List.of(
|
||||
MAPPER.readTree(controller.buildRequestAuthorizerEventV1(
|
||||
"GET", "/echo", "abc123", "test", "us-east-1",
|
||||
headers, uriInfo, stageVariables)),
|
||||
MAPPER.readTree(controller.buildRequestAuthorizerEventV2(
|
||||
"GET", "/echo", "GET /echo", "abc123", "test", "us-east-1",
|
||||
headers, uriInfo, stageVariables)));
|
||||
}
|
||||
}
|
||||
+26
@@ -1,16 +1,22 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* Stage access logging, throttling defaults and per-route overrides, plus the API's version.
|
||||
@@ -22,6 +28,9 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class ApiGatewayV2StageSettingsTest {
|
||||
|
||||
@Inject
|
||||
ApiGatewayV2Service service;
|
||||
|
||||
private static final ObjectMapper mapper = new ObjectMapper();
|
||||
private static String apiId;
|
||||
|
||||
@@ -122,4 +131,21 @@ class ApiGatewayV2StageSettingsTest {
|
||||
.body("defaultRouteSettings.throttlingBurstLimit", org.hamcrest.Matchers.nullValue())
|
||||
.body("defaultRouteSettings.dataTraceEnabled", org.hamcrest.Matchers.nullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void stageVariablesLookupReturnsConfiguredValuesOrEmptyMap() {
|
||||
Map<String, String> variables = Map.of("backend", "blue");
|
||||
given().contentType(ContentType.JSON)
|
||||
.body("{\"stageName\":\"variables\",\"stageVariables\":{\"backend\":\"blue\"}}")
|
||||
.when().post("/v2/apis/" + apiId + "/stages")
|
||||
.then().statusCode(201);
|
||||
|
||||
assertEquals(variables, service.getStageVariables("us-east-1", apiId, "variables"));
|
||||
assertEquals(Map.of(), service.getStageVariables("us-east-1", apiId, "sparse"));
|
||||
assertEquals(Map.of(), service.getStageVariables("us-east-1", apiId, "missing"));
|
||||
assertEquals(Map.of(), service.getStageVariables("us-west-2", apiId, "variables"));
|
||||
assertEquals(Map.of(), service.getStageVariables("us-east-1", "other-api", "variables"));
|
||||
assertThrows(AwsException.class, () -> service.getStage("us-east-1", apiId, "missing"));
|
||||
}
|
||||
}
|
||||
+10
@@ -165,6 +165,16 @@ class HttpApiLambdaProxyEventMultiValueIntegrationTest {
|
||||
.body("multiValueQueryStringParameters.q", contains("1", "2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void stageVariablesReachTheLambdaEvent() {
|
||||
given()
|
||||
.when().get("/execute-api/" + apiId + "/test/echo")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("stageVariables.environment", equalTo("review"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(999)
|
||||
void cleanup() throws Exception {
|
||||
|
||||
+7
-33
@@ -80,7 +80,7 @@ class HttpApiRequestAuthorizerTest {
|
||||
given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"stageName":"test"}
|
||||
{"stageName":"test","stageVariables":{"environment":"review"}}
|
||||
""")
|
||||
.when().post("/v2/apis/" + httpApiId + "/stages")
|
||||
.then()
|
||||
@@ -578,8 +578,6 @@ class HttpApiRequestAuthorizerTest {
|
||||
.when().patch("/v2/apis/" + httpApiId + "/routes/" + routeId)
|
||||
.then().statusCode(200);
|
||||
|
||||
// The echo authorizer embeds the event it received in its context, so assert that shape
|
||||
// by invoking the echo function directly rather than unwrapping it through two hops.
|
||||
String response = given()
|
||||
.header("X-Custom-Header", "test-value")
|
||||
.queryParam("foo", "bar")
|
||||
@@ -587,23 +585,10 @@ class HttpApiRequestAuthorizerTest {
|
||||
.then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
|
||||
// Backend received the request — authorizer allowed it
|
||||
JsonNode backendResponse = MAPPER.readTree(response);
|
||||
assertEquals("Hello from backend", backendResponse.path("message").asText());
|
||||
|
||||
// Verify format by invoking echo function with a v1-shaped event
|
||||
String testEvent = """
|
||||
{"version":"1.0","type":"REQUEST","methodArn":"arn:aws:execute-api:us-east-1:000000000000:%s/test/GET/hello","resource":"/hello","path":"/hello","httpMethod":"GET","headers":{"X-Custom-Header":"test-value"},"queryStringParameters":{"foo":"bar"}}
|
||||
""".formatted(httpApiId);
|
||||
String echoResponse = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(testEvent)
|
||||
.when().post("/2015-03-31/functions/" + ECHO_FN + "/invocations")
|
||||
.then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
|
||||
JsonNode echoResult = MAPPER.readTree(echoResponse);
|
||||
String authEventStr = echoResult.path("context").path("receivedEvent").asText(null);
|
||||
String authEventStr = backendResponse.path("authorizer").path("lambda")
|
||||
.path("receivedEvent").asText(null);
|
||||
assertNotNull(authEventStr, "Echo authorizer should embed receivedEvent in context");
|
||||
JsonNode authEvent = MAPPER.readTree(authEventStr);
|
||||
|
||||
@@ -612,6 +597,7 @@ class HttpApiRequestAuthorizerTest {
|
||||
assertNotNull(authEvent.path("methodArn").asText(null), "Event should have methodArn");
|
||||
assertEquals("/hello", authEvent.path("resource").asText(), "Event should have resource");
|
||||
assertEquals("GET", authEvent.path("httpMethod").asText(), "Event should have httpMethod");
|
||||
assertEquals("review", authEvent.path("stageVariables").path("environment").asText());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Test: Payload format 2.0 event shape ────────────────────────────
|
||||
@@ -634,23 +620,10 @@ class HttpApiRequestAuthorizerTest {
|
||||
.then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
|
||||
// Backend received the request — authorizer allowed it
|
||||
JsonNode backendResponse = MAPPER.readTree(response);
|
||||
assertEquals("Hello from backend", backendResponse.path("message").asText());
|
||||
|
||||
// Verify format by invoking echo function with a v2-shaped event
|
||||
String testEvent = """
|
||||
{"version":"2.0","type":"REQUEST","routeArn":"arn:aws:execute-api:us-east-1:000000000000:%s/test/GET/hello","routeKey":"GET /hello","rawPath":"/hello","rawQueryString":"key=value","headers":{"x-custom-header":"test-value-v2"},"queryStringParameters":{"key":"value"},"requestContext":{"accountId":"000000000000","apiId":"%s","http":{"method":"GET","path":"/hello"}}}
|
||||
""".formatted(httpApiId, httpApiId);
|
||||
String echoResponse = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body(testEvent)
|
||||
.when().post("/2015-03-31/functions/" + ECHO_FN + "/invocations")
|
||||
.then().statusCode(200)
|
||||
.extract().body().asString();
|
||||
|
||||
JsonNode echoResult = MAPPER.readTree(echoResponse);
|
||||
String authEventStr = echoResult.path("context").path("receivedEvent").asText(null);
|
||||
String authEventStr = backendResponse.path("authorizer").path("lambda")
|
||||
.path("receivedEvent").asText(null);
|
||||
assertNotNull(authEventStr, "Echo authorizer should embed receivedEvent in context");
|
||||
JsonNode authEvent = MAPPER.readTree(authEventStr);
|
||||
|
||||
@@ -659,6 +632,7 @@ class HttpApiRequestAuthorizerTest {
|
||||
assertNotNull(authEvent.path("routeArn").asText(null), "Event should have routeArn");
|
||||
assertEquals("GET /hello", authEvent.path("routeKey").asText(), "Event should have routeKey");
|
||||
assertEquals("/hello", authEvent.path("rawPath").asText(), "Event should have rawPath");
|
||||
assertEquals("review", authEvent.path("stageVariables").path("environment").asText());
|
||||
}
|
||||
|
||||
// ──────────────────────────── Test: No authorizer (NONE) allows request ────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user