mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(cloudformation): provision AWS::ApiGatewayV2::Authorizer and wire Route.AuthorizerId (#1760)
* fix(cloudformation): provision AWS::ApiGatewayV2::Authorizer and wire Route.AuthorizerId CloudFormationResourceProvisioner had no case for AWS::ApiGatewayV2::Authorizer, so the resource fell through the default stub branch and never reached ApiGatewayV2Service — the same failure mode #788/#796 fixed for the v1 AWS::ApiGateway::Authorizer type. Stacks reported CREATE_COMPLETE, but apigatewayv2 get-authorizers came back empty and routes referencing the authorizer carried no authorizerId, so a JWT-authorized route silently served every request unauthenticated. Adds provisionApiGatewayV2Authorizer, modeled on the neighboring v2 Route/Integration/Stage provisioners, mapping the CFN PascalCase properties (Name, AuthorizerType, IdentitySource, JwtConfiguration, AuthorizerUri, AuthorizerPayloadFormatVersion, AuthorizerResultTtlInSeconds, EnableSimpleResponses) onto the existing ApiGatewayV2Service.createAuthorizer camelCase request shape. Also fixes provisionApiGatewayV2Route, which built its request map without reading AuthorizerId at all even though ApiGatewayV2Service.createRoute/updateRoute already handle it. Fixes #1758. * fix(cloudformation): accept scalar IdentitySource on ApiGatewayV2 Authorizer resolveStringListOrEmpty silently returned an empty list when a template supplied IdentitySource as a bare scalar string rather than the documented array form, dropping the identity source instead of persisting it. ApiGatewayV2Service.createAuthorizer/updateAuthorizer already accept a scalar string for identitySource (identitySourceRaw instanceof String), so the CFN provisioner was stricter than the service it calls. Adds resolveIdentitySource, a local resolver that accepts both a scalar string and an array. Adds a regression test asserting the scalar form is persisted, alongside strengthening the existing test to assert IdentitySource itself (previously unchecked). Addresses review feedback on #1760. * fix(cloudformation): populate AuthorizerId attribute, add scoped delete for AWS::ApiGatewayV2::Authorizer - Fn::GetAtt Authorizer.AuthorizerId now resolves: provisionApiGatewayV2Authorizer only set the physical id, never the resource's attributes map, so GetAtt (which reads a separate map from Ref) fell back to a literal placeholder string. - Stack deletion now actually removes the authorizer: the generic type/physicalId delete switch had no case for AWS::ApiGatewayV2::Authorizer at all and silently no-op'd. Added a scoped case in delete(StackResource, region) following the same shape already used for AWS::EKS::Nodegroup (a resource needing extra context beyond type+physicalId, stored as an attribute during provisioning). Both fixes store ApiId as a resource attribute during provisioning, since it's needed by the new delete path and wasn't previously retained anywhere. * fix(apigateway): support $request.querystring.* identity sources for JWT authorizers extractToken only checked HttpHeaders — it had no query-parameter access at all, so any IdentitySource entry like $request.querystring.token was silently unusable, and a request using a valid token there always 401'd. Threads UriInfo through enforceJwtAuthorizer/extractToken and adds a $request.querystring. branch mirroring the one enforceRequestAuthorizerV2 already has for Lambda REQUEST authorizers. * chore: retrigger CI The Build and Test job failed on a single flaky error in RuntimeApiServerTest.extensionRegister_racedAgainstStop_eventuallyDeliversShutdown (EOFException on an HTTP/2 read while racing extension registration against server stop) — 1 of 8325 tests. This branch changes no Lambda code; that test is from #1773 and lives on main independently. Passes locally on repeat runs.
This commit is contained in:
4 files changed
+527
-4
No files matched your search
+9
-4
@@ -1212,7 +1212,7 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
|
||||
if ("JWT".equalsIgnoreCase(route.getAuthorizationType()) && route.getAuthorizerId() != null) {
|
||||
Response authError = enforceJwtAuthorizer(region, apiId, route, headers);
|
||||
Response authError = enforceJwtAuthorizer(region, apiId, route, headers, uriInfo);
|
||||
if (authError != null) return authError;
|
||||
}
|
||||
|
||||
@@ -1441,7 +1441,8 @@ public class ApiGatewayExecuteController {
|
||||
/** Extracts parameter names from a route template; the pattern itself is constant. */
|
||||
private static final Pattern ROUTE_PARAM_NAMES = Pattern.compile("\\{([a-zA-Z_]+)\\+?\\}");
|
||||
|
||||
private Response enforceJwtAuthorizer(String region, String apiId, Route route, HttpHeaders headers) {
|
||||
private Response enforceJwtAuthorizer(String region, String apiId, Route route, HttpHeaders headers,
|
||||
UriInfo uriInfo) {
|
||||
Authorizer authorizer;
|
||||
try {
|
||||
authorizer = apiGatewayV2Service.getAuthorizer(region, apiId, route.getAuthorizerId());
|
||||
@@ -1451,7 +1452,7 @@ public class ApiGatewayExecuteController {
|
||||
.type(MediaType.APPLICATION_JSON).build();
|
||||
}
|
||||
|
||||
String token = extractToken(authorizer, headers);
|
||||
String token = extractToken(authorizer, headers, uriInfo);
|
||||
if (token == null) {
|
||||
return Response.status(401)
|
||||
.entity(jsonMessage("Unauthorized"))
|
||||
@@ -1763,7 +1764,7 @@ public class ApiGatewayExecuteController {
|
||||
}
|
||||
}
|
||||
|
||||
private String extractToken(Authorizer authorizer, HttpHeaders headers) {
|
||||
private String extractToken(Authorizer authorizer, HttpHeaders headers, UriInfo uriInfo) {
|
||||
List<String> sources = authorizer.getIdentitySource();
|
||||
if (sources == null || sources.isEmpty()) {
|
||||
// Default: Authorization header
|
||||
@@ -1775,6 +1776,10 @@ public class ApiGatewayExecuteController {
|
||||
String headerName = source.substring("$request.header.".length());
|
||||
String value = headers.getHeaderString(headerName);
|
||||
if (value != null) return stripBearer(value);
|
||||
} else if (source.startsWith("$request.querystring.")) {
|
||||
String paramName = source.substring("$request.querystring.".length());
|
||||
String value = uriInfo.getQueryParameters().getFirst(paramName);
|
||||
if (value != null) return stripBearer(value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
+82
@@ -314,6 +314,7 @@ public class CloudFormationResourceProvisioner {
|
||||
case "AWS::ApiGateway::Deployment" -> provisionApiGatewayDeployment(resource, properties, engine, region);
|
||||
case "AWS::ApiGateway::Stage" -> provisionApiGatewayStage(resource, properties, engine, region);
|
||||
case "AWS::ApiGatewayV2::Api" -> provisionApiGatewayV2Api(resource, properties, engine, region, accountId, stackName);
|
||||
case "AWS::ApiGatewayV2::Authorizer" -> provisionApiGatewayV2Authorizer(resource, properties, engine, region);
|
||||
case "AWS::ApiGatewayV2::Route" -> provisionApiGatewayV2Route(resource, properties, engine, region);
|
||||
case "AWS::ApiGatewayV2::Integration" -> provisionApiGatewayV2Integration(resource, properties, engine, region);
|
||||
case "AWS::ApiGatewayV2::Stage" -> provisionApiGatewayV2Stage(resource, properties, engine, region);
|
||||
@@ -436,6 +437,21 @@ public class CloudFormationResourceProvisioner {
|
||||
resource.getAttributes().get("EventBusName"), region);
|
||||
return;
|
||||
}
|
||||
// Authorizer deletion needs the api id (a stored attribute, not the physical id, which is
|
||||
// the authorizer id) — same shape as the Nodegroup case above. Without this, the generic
|
||||
// type/physicalId delete path has no case for this type at all and silently no-ops,
|
||||
// leaving the authorizer behind in AWS after the stack reports deleted.
|
||||
if ("AWS::ApiGatewayV2::Authorizer".equals(resourceType)) {
|
||||
String apiId = resource.getAttributes().get("ApiId");
|
||||
if (apiId != null && !apiId.isBlank()) {
|
||||
try {
|
||||
apiGatewayV2Service.deleteAuthorizer(region, apiId, resource.getPhysicalId());
|
||||
} catch (Exception e) {
|
||||
LOG.debugv("Error deleting authorizer {0}: {1}", resource.getPhysicalId(), e.getMessage());
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
// AWS::IAM::Policy is an inline policy; detaching it needs the principals it was attached to,
|
||||
// which the type/physicalId delete path can't provide (only the delete-stack path has them).
|
||||
if ("AWS::IAM::Policy".equals(resourceType)) {
|
||||
@@ -3578,12 +3594,78 @@ public class CloudFormationResourceProvisioner {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves {@code IdentitySource} accepting either the documented array form or a single
|
||||
* scalar string — {@code ApiGatewayV2Service.createAuthorizer}/{@code updateAuthorizer}
|
||||
* already accept both ({@code identitySourceRaw instanceof String}), so the CFN provisioner
|
||||
* should not be stricter than the service it calls.
|
||||
*/
|
||||
private List<String> resolveIdentitySource(JsonNode props, String source, CloudFormationTemplateEngine engine) {
|
||||
if (props == null || !props.has(source) || props.get(source).isNull()) {
|
||||
return List.of();
|
||||
}
|
||||
JsonNode resolved = engine.resolveNode(props.get(source));
|
||||
if (resolved == null) {
|
||||
return List.of();
|
||||
}
|
||||
if (resolved.isTextual()) {
|
||||
return List.of(resolved.asText());
|
||||
}
|
||||
if (!resolved.isArray()) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> values = new ArrayList<>();
|
||||
resolved.forEach(v -> values.add(v.asText()));
|
||||
return values;
|
||||
}
|
||||
|
||||
private void provisionApiGatewayV2Authorizer(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String region) {
|
||||
String apiId = resolveOptional(props, "ApiId", engine);
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("name", resolveOptional(props, "Name", engine));
|
||||
req.put("authorizerType", resolveOptional(props, "AuthorizerType", engine));
|
||||
req.put("identitySource", resolveIdentitySource(props, "IdentitySource", engine));
|
||||
req.put("authorizerUri", resolveOptional(props, "AuthorizerUri", engine));
|
||||
req.put("authorizerPayloadFormatVersion", resolveOptional(props, "AuthorizerPayloadFormatVersion", engine));
|
||||
|
||||
String ttl = resolveOptional(props, "AuthorizerResultTtlInSeconds", engine);
|
||||
if (ttl != null) {
|
||||
req.put("authorizerResultTtlInSeconds", Integer.parseInt(ttl));
|
||||
}
|
||||
String simpleResponses = resolveOptional(props, "EnableSimpleResponses", engine);
|
||||
if (simpleResponses != null) {
|
||||
req.put("enableSimpleResponses", simpleResponses);
|
||||
}
|
||||
|
||||
JsonNode jwtConfigNode = props != null ? props.get("JwtConfiguration") : null;
|
||||
if (jwtConfigNode != null && !jwtConfigNode.isNull()) {
|
||||
Map<String, Object> jwtConfig = new HashMap<>();
|
||||
jwtConfig.put("audience", resolveStringListOrEmpty(jwtConfigNode, "Audience", engine));
|
||||
jwtConfig.put("issuer", resolveOptional(jwtConfigNode, "Issuer", engine));
|
||||
req.put("jwtConfiguration", jwtConfig);
|
||||
}
|
||||
|
||||
Authorizer authorizer;
|
||||
if (r.getPhysicalId() == null) {
|
||||
authorizer = apiGatewayV2Service.createAuthorizer(region, apiId, req);
|
||||
} else {
|
||||
authorizer = apiGatewayV2Service.updateAuthorizer(region, apiId, r.getPhysicalId(), req);
|
||||
}
|
||||
r.setPhysicalId(authorizer.getAuthorizerId());
|
||||
r.getAttributes().put("AuthorizerId", authorizer.getAuthorizerId());
|
||||
// ApiId is needed by delete(StackResource, region) to scope deleteAuthorizer — the
|
||||
// type/physicalId-only delete overload has no apiId to call it with.
|
||||
r.getAttributes().put("ApiId", apiId);
|
||||
}
|
||||
|
||||
private void provisionApiGatewayV2Route(StackResource r, JsonNode props, CloudFormationTemplateEngine engine,
|
||||
String region) {
|
||||
String apiId = resolveOptional(props, "ApiId", engine);
|
||||
Map<String, Object> req = new HashMap<>();
|
||||
req.put("routeKey", resolveOptional(props, "RouteKey", engine));
|
||||
req.put("authorizationType", resolveOrDefault(props, "AuthorizationType", engine, "NONE"));
|
||||
req.put("authorizerId", resolveOptional(props, "AuthorizerId", engine));
|
||||
req.put("target", resolveOptional(props, "Target", engine));
|
||||
|
||||
Route route;
|
||||
|
||||
Reference in new issue
Block a user