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: Link Lambda Authorizer to Rest API #5219

Merged
merged 6 commits into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions samcli/hook_packages/terraform/hooks/prepare/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ class GatewayAuthorizerToLambdaFunctionLocalVariablesLinkingLimitationException(
"""


class OneGatewayAuthorizerToRestApiLinkingLimitationException(OneResourceLinkingLimitationException):
"""
Exception specific for Gateway Authorizer linking to more than one Rest API
"""


class GatewayAuthorizerToRestApiLocalVariablesLinkingLimitationException(LocalVariablesLinkingLimitationException):
"""
Exception specific for Gateway Authorizer linking to Rest APIs using locals.
"""


class InvalidSamMetadataPropertiesException(UserException):
pass

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from samcli.hook_packages.terraform.hooks.prepare.exceptions import (
FunctionLayerLocalVariablesLinkingLimitationException,
GatewayAuthorizerToLambdaFunctionLocalVariablesLinkingLimitationException,
GatewayAuthorizerToRestApiLocalVariablesLinkingLimitationException,
GatewayResourceToApiGatewayIntegrationLocalVariablesLinkingLimitationException,
GatewayResourceToApiGatewayIntegrationResponseLocalVariablesLinkingLimitationException,
GatewayResourceToApiGatewayMethodLocalVariablesLinkingLimitationException,
Expand All @@ -18,6 +19,7 @@
LambdaFunctionToApiGatewayIntegrationLocalVariablesLinkingLimitationException,
LocalVariablesLinkingLimitationException,
OneGatewayAuthorizerToLambdaFunctionLinkingLimitationException,
OneGatewayAuthorizerToRestApiLinkingLimitationException,
OneGatewayResourceToApiGatewayIntegrationLinkingLimitationException,
OneGatewayResourceToApiGatewayIntegrationResponseLinkingLimitationException,
OneGatewayResourceToApiGatewayMethodLinkingLimitationException,
Expand Down Expand Up @@ -1535,7 +1537,7 @@ def _link_gateway_authorizer_to_lambda_function_call_back(
def _link_gateway_authorizer_to_lambda_function(
authorizer_config_resources: Dict[str, TFResource],
authorizer_cfn_resources: Dict[str, List],
authorizer_tf_resources: Dict[str, Dict],
lamda_function_resources: Dict[str, Dict],
) -> None:
"""
Iterate through all the resources and link the corresponding Authorizer to each Lambda Function
Expand All @@ -1546,8 +1548,8 @@ def _link_gateway_authorizer_to_lambda_function(
Dictionary of configuration Authorizer resources
authorizer_cfn_resources: Dict[str, List]
Dictionary containing resolved configuration address of CFN Authorizer resources
lambda_layers_terraform_resources: Dict[str, Dict]
Dictionary of all actual terraform layers resources (not configuration resources). The dictionary's key is the
lamda_function_resources: Dict[str, Dict]
Dictionary of Terraform Lambda Function resources (not configuration resources). The dictionary's key is the
calculated logical id for each resource
"""
exceptions = ResourcePairExceptions(
Expand All @@ -1557,7 +1559,7 @@ def _link_gateway_authorizer_to_lambda_function(
resource_linking_pair = ResourceLinkingPair(
source_resource_cfn_resource=authorizer_cfn_resources,
source_resource_tf_config=authorizer_config_resources,
destination_resource_tf=authorizer_tf_resources,
destination_resource_tf=lamda_function_resources,
tf_destination_attribute_name="invoke_arn",
terraform_link_field_name="authorizer_uri",
cfn_link_field_name="AuthorizerUri",
Expand All @@ -1566,3 +1568,39 @@ def _link_gateway_authorizer_to_lambda_function(
linking_exceptions=exceptions,
)
ResourceLinker(resource_linking_pair).link_resources()


def _link_gateway_authorizer_to_rest_api(
authorizer_config_resources: Dict[str, TFResource],
authorizer_cfn_resources: Dict[str, List],
rest_api_resource: Dict[str, Dict],
) -> None:
"""
Iterate through all the resources and link the corresponding Authorizer to each Rest Api

Parameters
----------
authorizer_config_resources: Dict[str, TFResource]
Dictionary of configuration Authorizer resources
authorizer_cfn_resources: Dict[str, List]
Dictionary containing resolved configuration address of CFN Authorizer resources
rest_api_resource: Dict[str, Dict]
Dictionary of Terraform Rest Api resources (not configuration resources). The dictionary's key is the
calculated logical id for each resource
"""
exceptions = ResourcePairExceptions(
multiple_resource_linking_exception=OneGatewayAuthorizerToRestApiLinkingLimitationException,
local_variable_linking_exception=GatewayAuthorizerToRestApiLocalVariablesLinkingLimitationException,
)
resource_linking_pair = ResourceLinkingPair(
source_resource_cfn_resource=authorizer_cfn_resources,
source_resource_tf_config=authorizer_config_resources,
destination_resource_tf=rest_api_resource,
tf_destination_attribute_name="id",
terraform_link_field_name="rest_api_id",
cfn_link_field_name="RestApiId",
terraform_resource_type_prefix=API_GATEWAY_REST_API_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=_link_gateway_resource_to_gateway_rest_apis_rest_api_id_call_back,
linking_exceptions=exceptions,
)
ResourceLinker(resource_linking_pair).link_resources()
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from samcli.hook_packages.terraform.hooks.prepare.resource_linking import (
_link_gateway_authorizer_to_lambda_function,
_link_gateway_authorizer_to_rest_api,
_link_gateway_integration_responses_to_gateway_resource,
_link_gateway_integration_responses_to_gateway_rest_apis,
_link_gateway_integrations_to_function_resource,
Expand Down Expand Up @@ -78,4 +79,9 @@
dest=TF_AWS_LAMBDA_FUNCTION,
linking_func=_link_gateway_authorizer_to_lambda_function,
),
LinkingPairCaller(
source=TF_AWS_API_GATEWAY_AUTHORIZER,
dest=TF_AWS_API_GATEWAY_REST_API,
linking_func=_link_gateway_authorizer_to_rest_api,
),
]
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
from parameterized import parameterized
from samcli.hook_packages.terraform.hooks.prepare.exceptions import (
GatewayAuthorizerToLambdaFunctionLocalVariablesLinkingLimitationException,
GatewayAuthorizerToRestApiLocalVariablesLinkingLimitationException,
InvalidResourceLinkingException,
LocalVariablesLinkingLimitationException,
ONE_LAMBDA_LAYER_LINKING_ISSUE_LINK,
LOCAL_VARIABLES_SUPPORT_ISSUE_LINK,
APPLY_WORK_AROUND_MESSAGE,
OneGatewayAuthorizerToLambdaFunctionLinkingLimitationException,
OneGatewayAuthorizerToRestApiLinkingLimitationException,
OneLambdaLayerLinkingLimitationException,
FunctionLayerLocalVariablesLinkingLimitationException,
OneGatewayResourceToApiGatewayMethodLinkingLimitationException,
Expand All @@ -39,6 +41,7 @@
_clean_references_list,
_link_gateway_authorizer_to_lambda_function,
_link_gateway_authorizer_to_lambda_function_call_back,
_link_gateway_authorizer_to_rest_api,
_resolve_module_output,
_resolve_module_variable,
_build_module,
Expand Down Expand Up @@ -2245,3 +2248,41 @@ def test_link_gateway_authorizer_to_lambda_function(
)

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())

@patch(
"samcli.hook_packages.terraform.hooks.prepare.resource_linking._link_gateway_resource_to_gateway_rest_apis_rest_api_id_call_back"
)
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourceLinker")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourceLinkingPair")
@patch("samcli.hook_packages.terraform.hooks.prepare.resource_linking.ResourcePairExceptions")
def test_link_gateway_authorizer_to_rest_api(
self,
mock_resource_linking_exceptions,
mock_resource_linking_pair,
mock_resource_linker,
mock_link_resource_to_rest_api_call_back,
):
authorizer_cfn_resources = Mock()
authorizer_config_resources = Mock()
rest_api_resources = Mock()

_link_gateway_authorizer_to_rest_api(authorizer_config_resources, authorizer_cfn_resources, rest_api_resources)

mock_resource_linking_exceptions.assert_called_once_with(
multiple_resource_linking_exception=OneGatewayAuthorizerToRestApiLinkingLimitationException,
local_variable_linking_exception=GatewayAuthorizerToRestApiLocalVariablesLinkingLimitationException,
)

mock_resource_linking_pair.assert_called_once_with(
source_resource_cfn_resource=authorizer_cfn_resources,
source_resource_tf_config=authorizer_config_resources,
destination_resource_tf=rest_api_resources,
tf_destination_attribute_name="id",
terraform_link_field_name="rest_api_id",
cfn_link_field_name="RestApiId",
terraform_resource_type_prefix=API_GATEWAY_REST_API_RESOURCE_ADDRESS_PREFIX,
cfn_resource_update_call_back_function=mock_link_resource_to_rest_api_call_back,
linking_exceptions=mock_resource_linking_exceptions(),
)

mock_resource_linker.assert_called_once_with(mock_resource_linking_pair())