Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added APIGW V2 Authorizer property mapping #5623

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from samcli.lib.utils.resources import AWS_APIGATEWAY_RESTAPI as CFN_AWS_APIGATEWAY_RESTAPI
from samcli.lib.utils.resources import AWS_APIGATEWAY_STAGE as CFN_AWS_APIGATEWAY_STAGE
from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_API as CFN_AWS_APIGATEWAY_V2_API
from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_AUTHORIZER as CFN_AWS_APIGATEWAY_V2_AUTHORIZER
from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_INTEGRATION as CFN_AWS_APIGATEWAY_V2_INTEGRATION
from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_ROUTE as CFN_AWS_APIGATEWAY_V2_ROUTE
from samcli.lib.utils.resources import AWS_APIGATEWAY_V2_STAGE as CFN_AWS_APIGATEWAY_V2_STAGE
Expand All @@ -49,6 +50,7 @@
TF_AWS_API_GATEWAY_V2_ROUTE = "aws_apigatewayv2_route"
TF_AWS_API_GATEWAY_V2_STAGE = "aws_apigatewayv2_stage"
TF_AWS_API_GATEWAY_V2_INTEGRATION = "aws_apigatewayv2_integration"
TF_AWS_API_GATEWAY_V2_AUTHORIZER = "aws_apigatewayv2_authorizer"


def _build_code_property(tf_properties: dict, resource: TFResource) -> Any:
Expand Down Expand Up @@ -412,6 +414,16 @@ def _add_property(cfn_prop, tf_prop):
"PayloadFormatVersion": _get_property_extractor("payload_format_version"),
}

AWS_API_GATEWAY_V2_AUTHORIZER_PROPERTY_BUILDER_MAPPING: PropertyBuilderMapping = {
"ApiId": _get_property_extractor("api_id"),
"AuthorizerType": _get_property_extractor("authorizer_type"),
"AuthorizerUri": _get_property_extractor("authorizer_uri"),
"Name": _get_property_extractor("name"),
"AuthorizerPayloadFormatVersion": _get_property_extractor("authorizer_payload_format_version"),
"IdentitySource": _get_property_extractor("identity_sources"),
"EnableSimpleResponses": _get_property_extractor("enable_simple_responses"),
}

RESOURCE_TRANSLATOR_MAPPING: Dict[str, ResourceTranslator] = {
TF_AWS_LAMBDA_FUNCTION: ResourceTranslator(CFN_AWS_LAMBDA_FUNCTION, AWS_LAMBDA_FUNCTION_PROPERTY_BUILDER_MAPPING),
TF_AWS_LAMBDA_LAYER_VERSION: ResourceTranslator(
Expand Down Expand Up @@ -450,4 +462,7 @@ def _add_property(cfn_prop, tf_prop):
TF_AWS_API_GATEWAY_V2_INTEGRATION: ResourceTranslator(
CFN_AWS_APIGATEWAY_V2_INTEGRATION, AWS_API_GATEWAY_V2_INTEGRATION_PROPERTY_BUILDER_MAPPING
),
TF_AWS_API_GATEWAY_V2_AUTHORIZER: ResourceTranslator(
CFN_AWS_APIGATEWAY_V2_AUTHORIZER, AWS_API_GATEWAY_V2_AUTHORIZER_PROPERTY_BUILDER_MAPPING
),
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from samcli.hook_packages.terraform.hooks.prepare.translate import AWS_PROVIDER_NAME, NULL_RESOURCE_PROVIDER_NAME
from samcli.lib.utils.resources import (
AWS_APIGATEWAY_V2_AUTHORIZER,
AWS_LAMBDA_FUNCTION as CFN_AWS_LAMBDA_FUNCTION,
AWS_LAMBDA_LAYERVERSION,
AWS_APIGATEWAY_RESOURCE,
Expand Down Expand Up @@ -61,6 +62,7 @@ def setUp(self) -> None:
self.apigwv2_route_name = "my_apigwv2_route"
self.apigwv2_stage_name = "my_apigwv2_stage"
self.apigwv2_integration_name = "my_apigwv2_integration"
self.apigwv2_authorizer_name = "my_authorizer_v2"

self.tf_function_common_properties: dict = {
"function_name": self.zip_function_name,
Expand Down Expand Up @@ -941,6 +943,44 @@ def setUp(self) -> None:
"Metadata": {"SamResourceId": f"aws_apigatewayv2_integration.{self.apigwv2_integration_name}"},
}

self.tf_apigwv2_authorizer_common_attributes: dict = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shoudl add this authorizer to the self.tf_json_with_root_module_only list below for the transformation to be tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

"type": "aws_apigatewayv2_authorizer",
"provider_name": AWS_PROVIDER_NAME,
}

self.tf_apigwv2_authorizer_properties: dict = {
"api_id": "aws_apigatewayv2_api.my_api.id",
"authorizer_type": "REQUEST",
"authorizer_uri": "aws_lambda_function.authorizerv2.invoke_arn",
"name": self.apigwv2_authorizer_name,
"authorizer_payload_format_version": "2.0",
"identity_sources": ["$request.header.hello"],
"enable_simple_responses": False,
}

self.expected_cfn_apigwv2_authorizer_properties: dict = {
"ApiId": "aws_apigatewayv2_api.my_api.id",
"AuthorizerType": "REQUEST",
"AuthorizerUri": "aws_lambda_function.authorizerv2.invoke_arn",
"Name": self.apigwv2_authorizer_name,
"AuthorizerPayloadFormatVersion": "2.0",
"IdentitySource": ["$request.header.hello"],
"EnableSimpleResponses": False,
}

self.tf_apigwv2_authorizer_resource: dict = {
**self.tf_apigwv2_authorizer_common_attributes,
"values": self.tf_apigwv2_authorizer_properties,
"address": f"aws_apigatewayv2_authorizer.{self.apigwv2_authorizer_name}",
"name": self.apigwv2_authorizer_name,
}

self.expected_cfn_apigwv2_authorizer: dict = {
"Type": AWS_APIGATEWAY_V2_AUTHORIZER,
"Properties": self.expected_cfn_apigwv2_authorizer_properties,
"Metadata": {"SamResourceId": f"aws_apigatewayv2_authorizer.{self.apigwv2_authorizer_name}"},
}

self.tf_json_with_root_module_only: dict = {
"planned_values": {
"root_module": {
Expand All @@ -961,6 +1001,7 @@ def setUp(self) -> None:
self.tf_apigwv2_route_resource,
self.tf_apigwv2_stage_resource,
self.tf_apigwv2_integration_resource,
self.tf_apigwv2_authorizer_resource,
]
}
}
Expand All @@ -982,9 +1023,9 @@ def setUp(self) -> None:
f"AwsApigatewayv2RouteMyApigwv2Route{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_route,
f"AwsApigatewayv2StageMyApigwv2Stage{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_stage,
f"AwsApigatewayv2IntegrationMyApigwv2Integration{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_integration,
f"AwsApigatewayv2AuthorizerMyAuthorizerV2{self.mock_logical_id_hash}": self.expected_cfn_apigwv2_authorizer,
},
}

self.tf_json_with_root_module_with_sam_metadata_resources: dict = {
"planned_values": {
"root_module": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from tests.unit.hook_packages.terraform.hooks.prepare.prepare_base import PrepareHookUnitBase
from samcli.hook_packages.terraform.hooks.prepare.property_builder import (
AWS_API_GATEWAY_V2_AUTHORIZER_PROPERTY_BUILDER_MAPPING,
AWS_LAMBDA_FUNCTION_PROPERTY_BUILDER_MAPPING,
REMOTE_DUMMY_VALUE,
AWS_API_GATEWAY_RESOURCE_PROPERTY_BUILDER_MAPPING,
Expand Down Expand Up @@ -1146,6 +1147,12 @@ def test_translating_apigwv2_integration(self):
)
self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_integration_properties)

def test_translating_apigwv2_authorizer(self):
translated_cfn_properties = _translate_properties(
self.tf_apigwv2_authorizer_properties, AWS_API_GATEWAY_V2_AUTHORIZER_PROPERTY_BUILDER_MAPPING, Mock()
)
self.assertEqual(translated_cfn_properties, self.expected_cfn_apigwv2_authorizer_properties)


class TestUnresolvableAttributeCheck(TestCase):
@patch("samcli.hook_packages.terraform.hooks.prepare.translate.RESOURCE_TRANSLATOR_MAPPING")
Expand Down
Loading