Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

feat(cloudformation, apigw-v2): support AWS::ApiGatewayV2::Authorizer (#1085)

ryan-bennett9c030750b050
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 4305c9ef..497a59a6 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -3756,6 +3756,8 @@ def _apigw_v2_route_create(logical_id, props, stack_name):
"routeKey": props.get("RouteKey", "$default"),
"target": props.get("Target", ""),
"authorizationType": props.get("AuthorizationType", "NONE"),
+ "authorizerId": props.get("AuthorizerId"),
+ "authorizationScopes": props.get("AuthorizationScopes", []),
"apiKeyRequired": props.get("ApiKeyRequired", False),
"operationName": props.get("OperationName", ""),
"requestModels": props.get("RequestModels", {}),
@@ -3774,6 +3776,48 @@ def _apigw_v2_route_delete(physical_id, props):
routes.pop(route_id, None)
+# ---------------------------------------------------------------------------
+# ApiGatewayV2 Authorizer
+# ---------------------------------------------------------------------------
+
+def _apigw_v2_authorizer_create(logical_id, props, stack_name):
+ """Maps CFN properties onto the same in-memory authorizer store the
+ control-plane CreateAuthorizer API (and Terraform's aws_apigatewayv2_authorizer)
+ already write to, so a CFN-created authorizer is enforced identically at
+ request time. See _validate_jwt_authorizer / _resolve_jwks_url.
+
+ jwtConfiguration is stored camelCase (the API's actual wire shape, which
+ the JSON response is returned as-is) — CFN's JwtConfiguration.Audience /
+ .Issuer are translated here rather than passed through PascalCase.
+ """
+ api_id = props.get("ApiId", "")
+ auth_id = new_uuid()[:8]
+ jwt_cfg = props.get("JwtConfiguration") or {}
+ authorizer = {
+ "authorizerId": auth_id,
+ "authorizerType": props.get("AuthorizerType", "JWT"),
+ "name": props.get("Name", logical_id),
+ "identitySource": props.get("IdentitySource", ["$request.header.Authorization"]),
+ "jwtConfiguration": {
+ "audience": jwt_cfg.get("Audience", []),
+ "issuer": jwt_cfg.get("Issuer", ""),
+ },
+ "authorizerUri": props.get("AuthorizerUri", ""),
+ "authorizerPayloadFormatVersion": props.get("AuthorizerPayloadFormatVersion", "2.0"),
+ "authorizerResultTtlInSeconds": props.get("AuthorizerResultTtlInSeconds", 300),
+ "enableSimpleResponses": props.get("EnableSimpleResponses", False),
+ "authorizerCredentialsArn": props.get("AuthorizerCredentialsArn", ""),
+ }
+ _apigw_v2._authorizers.setdefault(api_id, {})[auth_id] = authorizer
+ return auth_id, {"AuthorizerId": auth_id}
+
+
+def _apigw_v2_authorizer_delete(physical_id, props):
+ api_id = props.get("ApiId", "")
+ authorizers = _apigw_v2._authorizers.get(api_id, {})
+ authorizers.pop(physical_id, None)
+
+
# ---------------------------------------------------------------------------
# SES EmailIdentity
# ---------------------------------------------------------------------------
@@ -4374,6 +4418,7 @@ _RESOURCE_HANDLERS = {
"AWS::ApiGatewayV2::Stage": {"create": _apigw_v2_stage_create, "delete": _apigw_v2_stage_delete},
"AWS::ApiGatewayV2::Integration": {"create": _apigw_v2_integration_create, "delete": _apigw_v2_integration_delete},
"AWS::ApiGatewayV2::Route": {"create": _apigw_v2_route_create, "delete": _apigw_v2_route_delete},
+ "AWS::ApiGatewayV2::Authorizer": {"create": _apigw_v2_authorizer_create, "delete": _apigw_v2_authorizer_delete},
"AWS::SES::EmailIdentity": {"create": _ses_email_identity_create, "delete": _ses_email_identity_delete},
"AWS::WAFv2::WebACL": {"create": _waf_web_acl_create, "delete": _waf_web_acl_delete},
"AWS::CloudFront::Distribution": {"create": _cf_distribution_create, "delete": _cf_distribution_delete},
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index f13307e0..2cf0a6fe 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -3289,6 +3289,87 @@ def test_cfn_apigwv2_route_basic(cfn, apigw):
assert apigw.get_routes(ApiId=api_id)["Items"] == []
+def test_cfn_apigwv2_authorizer_jwt(cfn, apigw):
+ """CFN stack with an AWS::ApiGatewayV2::Authorizer deploys successfully and
+ a Route referencing it via AuthorizerId is enforced at request time.
+
+ Regression test: AWS::ApiGatewayV2::Authorizer previously had no CFN
+ provisioner at all ("Unsupported resource type"), even though the
+ control-plane CreateAuthorizer API (and Terraform's
+ aws_apigatewayv2_authorizer) already worked.
+ """
+ template = {
+ "AWSTemplateFormatVersion": "2010-09-09",
+ "Resources": {
+ "HttpApi": {
+ "Type": "AWS::ApiGatewayV2::Api",
+ "Properties": {
+ "Name": "cfn-apigwv2-authorizer-t01",
+ "ProtocolType": "HTTP",
+ },
+ },
+ "Integration": {
+ "Type": "AWS::ApiGatewayV2::Integration",
+ "Properties": {
+ "ApiId": {"Ref": "HttpApi"},
+ "IntegrationType": "AWS_PROXY",
+ "IntegrationUri": "arn:aws:lambda:us-east-1:000000000000:function:dummy",
+ "PayloadFormatVersion": "2.0",
+ },
+ },
+ "JwtAuthorizer": {
+ "Type": "AWS::ApiGatewayV2::Authorizer",
+ "Properties": {
+ "ApiId": {"Ref": "HttpApi"},
+ "Name": "cfn-apigwv2-authorizer-t01-jwt",
+ "AuthorizerType": "JWT",
+ "IdentitySource": ["$request.header.Authorization"],
+ "JwtConfiguration": {
+ "Audience": ["client-id"],
+ "Issuer": "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_example",
+ },
+ },
+ },
+ "ProtectedRoute": {
+ "Type": "AWS::ApiGatewayV2::Route",
+ "Properties": {
+ "ApiId": {"Ref": "HttpApi"},
+ "RouteKey": "GET /protected",
+ "Target": {"Fn::Join": ["/", ["integrations", {"Ref": "Integration"}]]},
+ "AuthorizationType": "JWT",
+ "AuthorizerId": {"Ref": "JwtAuthorizer"},
+ },
+ },
+ },
+ }
+ stack_name = "cfn-apigwv2-authorizer-t01"
+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE"
+
+ resources = cfn.describe_stack_resources(StackName=stack_name)["StackResources"]
+ api_res = [r for r in resources if r["ResourceType"] == "AWS::ApiGatewayV2::Api"][0]
+ api_id = api_res["PhysicalResourceId"]
+ authorizer_res = [r for r in resources if r["ResourceType"] == "AWS::ApiGatewayV2::Authorizer"][0]
+ authorizer_id = authorizer_res["PhysicalResourceId"]
+
+ authorizers = apigw.get_authorizers(ApiId=api_id)["Items"]
+ assert len(authorizers) == 1
+ assert authorizers[0]["AuthorizerId"] == authorizer_id
+ assert authorizers[0]["AuthorizerType"] == "JWT"
+ assert authorizers[0]["JwtConfiguration"]["Audience"] == ["client-id"]
+ assert authorizers[0]["JwtConfiguration"]["Issuer"] == "https://cognito-idp.us-east-1.amazonaws.com/us-east-1_example"
+
+ routes = apigw.get_routes(ApiId=api_id)["Items"]
+ assert len(routes) == 1
+ assert routes[0]["AuthorizationType"] == "JWT"
+ assert routes[0]["AuthorizerId"] == authorizer_id
+
+ cfn.delete_stack(StackName=stack_name)
+ _wait_stack(cfn, stack_name)
+ assert apigw.get_authorizers(ApiId=api_id)["Items"] == []
+
+
def test_cfn_apigwv2_integration_getatt(cfn, apigw):
"""Fn::GetAtt on IntegrationId resolves correctly."""
template = {