mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(apigatewayv2): provision integration, route and stage on quick create (#2877)
* fix(apigatewayv2): provision integration, route and stage on quick create CreateApi's `target` parameter (quick create) was accepted but silently ignored: no AWS_PROXY integration, no "$default" route, and no "$default" stage were ever created. Without a stage, ApiGatewayExecuteApiHostFilter.stageExists() throws "Stage not found", the filter returns without rewriting the request, and it falls through to whichever other virtual-hosted-style filter claims the host next (S3's, in the common case) instead of reaching the API. Provision the AWS_PROXY integration, "$default" route, and auto-deploy "$default" stage that real API Gateway creates for quick create, using the existing createIntegration/createRoute/createStage helpers. Fixes #1902 * fix(apigatewayv2): use HTTP_PROXY for HTTP URL quick-create targets Quick create hardcoded AWS_PROXY regardless of target shape, so an HTTP URL target got routed through Lambda ARN parsing and failed invocation instead of reaching the configured endpoint (spotted by review). Detect the target kind (arn: prefix vs HTTP URL) and pick AWS_PROXY/POST or HTTP_PROXY/ANY accordingly, matching real API Gateway's quick-create behavior. Also drop the em-dashes from the new comment and Javadoc per AGENTS.md's style rule, and split the single test into one covering each target kind. --------- Co-authored-by: Karan-Tekuncorked <karan@tekuncorked.com>
This commit is contained in:
1 parent
2bf9b57bd9
commit
2a637d3e05
2 files changed
+146
No files matched your search
+25
@@ -124,6 +124,31 @@ public class ApiGatewayV2Service {
|
||||
|
||||
apiStore.put(apiKey(region, api.getApiId()), api);
|
||||
LOG.infov("Created {0} API: {1} ({2}) in {3}", protocolType, api.getName(), api.getApiId(), region);
|
||||
|
||||
// Quick create: when the caller supplies Target (a Lambda ARN or HTTP URL), AWS
|
||||
// auto-provisions an integration, a "$default" catch-all route pointing at it, and an
|
||||
// auto-deploy "$default" stage. Without this the API has no route and no stage, so
|
||||
// ApiGatewayExecuteApiHostFilter's stage lookup fails and every invocation falls
|
||||
// through to whichever other virtual-hosted-style filter claims the request next.
|
||||
Object targetValue = request.get("target");
|
||||
String target = targetValue != null ? String.valueOf(targetValue) : null;
|
||||
if ("HTTP".equals(protocolType) && target != null && !target.isBlank()) {
|
||||
boolean isLambdaTarget = target.startsWith("arn:");
|
||||
Integration integration = createIntegration(region, apiId, Map.of(
|
||||
"integrationType", isLambdaTarget ? "AWS_PROXY" : "HTTP_PROXY",
|
||||
"integrationUri", target,
|
||||
"integrationMethod", isLambdaTarget ? "POST" : "ANY",
|
||||
"payloadFormatVersion", "2.0"));
|
||||
createRoute(region, apiId, Map.of(
|
||||
"routeKey", "$default",
|
||||
"target", "integrations/" + integration.getIntegrationId()));
|
||||
createStage(region, apiId, Map.of(
|
||||
"stageName", "$default",
|
||||
"autoDeploy", "true"));
|
||||
LOG.infov("Quick create: provisioned {0} integration, $default route and stage for API {1} -> {2}",
|
||||
integration.getIntegrationType(), apiId, target);
|
||||
}
|
||||
|
||||
return api;
|
||||
}
|
||||
|
||||
|
||||
+121
@@ -0,0 +1,121 @@
|
||||
package io.github.hectorvent.floci.services.apigatewayv2;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.http.ContentType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/**
|
||||
* CreateApi's {@code target} parameter ("quick create") must provision an integration
|
||||
* (AWS_PROXY for a Lambda ARN, HTTP_PROXY for an HTTP URL), a "$default" catch-all route,
|
||||
* and an auto-deploy "$default" stage, exactly what AWS does, so the API is immediately
|
||||
* invocable. Without a "$default" stage, {@code ApiGatewayExecuteApiHostFilter} can't
|
||||
* resolve one and every invocation silently falls through to whichever other
|
||||
* virtual-hosted-style filter (e.g. S3) claims the request next, instead of reaching the
|
||||
* API. See issue #1902.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class ApiGatewayV2QuickCreateTest {
|
||||
|
||||
private static final String LAMBDA_TARGET =
|
||||
"arn:aws:lambda:us-east-1:000000000000:function:quick-create-target";
|
||||
private static final String HTTP_TARGET = "https://example.com/webhook";
|
||||
|
||||
@Test
|
||||
void quickCreateWithLambdaTargetProvisionsAwsProxyIntegration() {
|
||||
String apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"quick-create-lambda-api","protocolType":"HTTP","target":"%s"}
|
||||
""".formatted(LAMBDA_TARGET))
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("apiId", notNullValue())
|
||||
.extract().path("apiId");
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items", hasSize(1))
|
||||
.body("items[0].integrationType", equalTo("AWS_PROXY"))
|
||||
.body("items[0].integrationUri", equalTo(LAMBDA_TARGET));
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/routes")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items", hasSize(1))
|
||||
.body("items[0].routeKey", equalTo("$default"))
|
||||
.body("items[0].target", startsWith("integrations/"));
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/stages/$default")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("stageName", equalTo("$default"))
|
||||
.body("autoDeploy", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void quickCreateWithHttpUrlTargetProvisionsHttpProxyIntegration() {
|
||||
String apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"quick-create-http-api","protocolType":"HTTP","target":"%s"}
|
||||
""".formatted(HTTP_TARGET))
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.body("apiId", notNullValue())
|
||||
.extract().path("apiId");
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items", hasSize(1))
|
||||
.body("items[0].integrationType", equalTo("HTTP_PROXY"))
|
||||
.body("items[0].integrationUri", equalTo(HTTP_TARGET))
|
||||
.body("items[0].integrationMethod", equalTo("ANY"));
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/stages/$default")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("autoDeploy", equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
void createApiWithoutTargetProvisionsNothing() {
|
||||
String apiId = given()
|
||||
.contentType(ContentType.JSON)
|
||||
.body("""
|
||||
{"name":"no-target-api","protocolType":"HTTP"}
|
||||
""")
|
||||
.when().post("/v2/apis")
|
||||
.then()
|
||||
.statusCode(201)
|
||||
.extract().path("apiId");
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/integrations")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items", hasSize(0));
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/routes")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("items", hasSize(0));
|
||||
|
||||
given()
|
||||
.when().get("/v2/apis/" + apiId + "/stages/$default")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user