From 87e0a260cc594251ec467c1b74958f01f7b50bc8 Mon Sep 17 00:00:00 2001 From: Mehmet Nuri Deveci <5735811+mndeveci@users.noreply.github.com> Date: Mon, 4 Apr 2022 11:02:05 -0700 Subject: [PATCH 1/2] fix: use formatting instead of concatenating when preparing the exception messages --- samtranslator/model/__init__.py | 4 ++-- samtranslator/model/apigateway.py | 4 ++-- samtranslator/model/apigatewayv2.py | 10 +++++----- samtranslator/plugins/api/implicit_api_plugin.py | 6 ++---- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/samtranslator/model/__init__.py b/samtranslator/model/__init__.py index ca0efc59f2..add5367ef5 100644 --- a/samtranslator/model/__init__.py +++ b/samtranslator/model/__init__.py @@ -327,7 +327,7 @@ def get_runtime_attr(self, attr_name): if attr_name in self.runtime_attrs: return self.runtime_attrs[attr_name](self) else: - raise NotImplementedError(attr_name + " attribute is not implemented for resource " + self.resource_type) + raise NotImplementedError(f"{attr_name} attribute is not implemented for resource {self.resource_type}") def get_passthrough_resource_attributes(self): """ @@ -447,7 +447,7 @@ def _check_tag(self, reserved_tag_name, tags): if reserved_tag_name in tags: raise InvalidResourceException( self.logical_id, - reserved_tag_name + " is a reserved Tag key name and " + f"{reserved_tag_name} is a reserved Tag key name and " "cannot be set on your resource. " "Please change the tag key in the " "input.", diff --git a/samtranslator/model/apigateway.py b/samtranslator/model/apigateway.py index a987e5a999..e0c09a1da3 100644 --- a/samtranslator/model/apigateway.py +++ b/samtranslator/model/apigateway.py @@ -247,13 +247,13 @@ def __init__( if function_payload_type not in ApiGatewayAuthorizer._VALID_FUNCTION_PAYLOAD_TYPES: raise InvalidResourceException( api_logical_id, - name + " Authorizer has invalid " "'FunctionPayloadType': " + function_payload_type + ".", + f"{name} Authorizer has invalid 'FunctionPayloadType': {function_payload_type}.", ) if function_payload_type == "REQUEST" and self._is_missing_identity_source(identity): raise InvalidResourceException( api_logical_id, - name + " Authorizer must specify Identity with at least one " + f"{name} Authorizer must specify Identity with at least one " "of Headers, QueryStrings, StageVariables, or Context.", ) diff --git a/samtranslator/model/apigatewayv2.py b/samtranslator/model/apigatewayv2.py index a9976a1bed..91038934a4 100644 --- a/samtranslator/model/apigatewayv2.py +++ b/samtranslator/model/apigatewayv2.py @@ -151,26 +151,26 @@ def _validate_input_parameters(self): def _validate_jwt_authorizer(self): if not self.jwt_configuration: raise InvalidResourceException( - self.api_logical_id, self.name + " OAuth2 Authorizer must define 'JwtConfiguration'." + self.api_logical_id, f"{self.name} OAuth2 Authorizer must define 'JwtConfiguration'." ) if not self.id_source: raise InvalidResourceException( - self.api_logical_id, self.name + " OAuth2 Authorizer must define 'IdentitySource'." + self.api_logical_id, f"{self.name} OAuth2 Authorizer must define 'IdentitySource'." ) def _validate_lambda_authorizer(self): if not self.function_arn: raise InvalidResourceException( - self.api_logical_id, self.name + " Lambda Authorizer must define 'FunctionArn'." + self.api_logical_id, f"{self.name} Lambda Authorizer must define 'FunctionArn'." ) if not self.authorizer_payload_format_version: raise InvalidResourceException( - self.api_logical_id, self.name + " Lambda Authorizer must define 'AuthorizerPayloadFormatVersion'." + self.api_logical_id, f"{self.name} Lambda Authorizer must define 'AuthorizerPayloadFormatVersion'." ) if self.identity and not isinstance(self.identity, dict): raise InvalidResourceException( - self.api_logical_id, self.name + " Lambda Authorizer property 'identity' is of invalid type." + self.api_logical_id, f"{self.name} Lambda Authorizer property 'identity' is of invalid type." ) def generate_openapi(self): diff --git a/samtranslator/plugins/api/implicit_api_plugin.py b/samtranslator/plugins/api/implicit_api_plugin.py index 99397a9c67..9c14fcbcb7 100644 --- a/samtranslator/plugins/api/implicit_api_plugin.py +++ b/samtranslator/plugins/api/implicit_api_plugin.py @@ -189,10 +189,8 @@ def _add_api_to_swagger(self, event_id, event_properties, template): if isinstance(api_id, dict) or is_referencing_http_from_api_event: raise InvalidEventException( event_id, - self.api_id_property - + " must be a valid reference to an '" - + self._get_api_resource_type_name() - + "' resource in same template.", + f"{self.api_id_property} must be a valid reference to an '{self._get_api_resource_type_name()}'" + " resource in same template.", ) # Make sure Swagger is valid From 3aa0038076f7f1c83409f4621d7d38fe2e838f39 Mon Sep 17 00:00:00 2001 From: Mehmet Nuri Deveci <5735811+mndeveci@users.noreply.github.com> Date: Mon, 4 Apr 2022 12:28:00 -0700 Subject: [PATCH 2/2] fix: use formatting instead of concatenating when preparing the exception messages --- tests/model/test_api.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/model/test_api.py b/tests/model/test_api.py index 708b83a5a5..d718d5f8fe 100644 --- a/tests/model/test_api.py +++ b/tests/model/test_api.py @@ -3,6 +3,7 @@ from samtranslator.model import InvalidResourceException from samtranslator.model.apigateway import ApiGatewayAuthorizer +from samtranslator.utils.py27hash_fix import Py27Dict class TestApiGatewayAuthorizer(TestCase): @@ -25,6 +26,14 @@ def test_create_authorizer_fails_with_missing_identity_values_and_not_cached(sel function_payload_type="REQUEST", ) + def test_create_authorizer_fails_with_invalid_function_payload_type(self): + with self.assertRaises(InvalidResourceException): + ApiGatewayAuthorizer( + api_logical_id="logicalId", + name="authName", + function_payload_type=Py27Dict({"key": "value"}), + ) + def test_create_authorizer_fails_with_empty_identity(self): with pytest.raises(InvalidResourceException): ApiGatewayAuthorizer(