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

fix: Raise correct exception when Api authorizer identity is not a dict #2603

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 23 additions & 13 deletions samtranslator/model/apigateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,24 @@ def __init__( # type: ignore[no-untyped-def]
):
if authorization_scopes is None:
authorization_scopes = []

self.api_logical_id = api_logical_id
self.name = name
self.user_pool_arn = user_pool_arn
self.function_arn = function_arn
self.identity = identity
self.function_payload_type = function_payload_type
self.function_invoke_role = function_invoke_role
self.is_aws_iam_authorizer = is_aws_iam_authorizer
self.authorization_scopes = authorization_scopes

if function_payload_type not in ApiGatewayAuthorizer._VALID_FUNCTION_PAYLOAD_TYPES:
raise InvalidResourceException(
api_logical_id,
f"{name} Authorizer has invalid 'FunctionPayloadType': {function_payload_type}.",
)

if function_payload_type == "REQUEST" and self._is_missing_identity_source(identity): # type: ignore[no-untyped-call]
if function_payload_type == "REQUEST" and self._is_missing_identity_source(identity):
raise InvalidResourceException(
api_logical_id,
f"{name} Authorizer must specify Identity with at least one "
Expand All @@ -269,20 +280,17 @@ def __init__( # type: ignore[no-untyped-def]
if authorization_scopes is not None and not isinstance(authorization_scopes, list):
raise InvalidResourceException(api_logical_id, "AuthorizationScopes must be a list.")

self.api_logical_id = api_logical_id
self.name = name
self.user_pool_arn = user_pool_arn
self.function_arn = function_arn
self.identity = identity
self.function_payload_type = function_payload_type
self.function_invoke_role = function_invoke_role
self.is_aws_iam_authorizer = is_aws_iam_authorizer
self.authorization_scopes = authorization_scopes

def _is_missing_identity_source(self, identity): # type: ignore[no-untyped-def]
def _is_missing_identity_source(self, identity: Dict[str, Any]) -> bool:
if not identity:
return True

if not isinstance(identity, dict):
# TODO: we should have a more centralized validation approach.
raise InvalidResourceException(
self.api_logical_id,
f"Invalid type for {self.name} Authorizer's Identity. It must be a dictionary.",
)

headers = identity.get("Headers")
query_strings = identity.get("QueryStrings")
stage_variables = identity.get("StageVariables")
Expand All @@ -291,9 +299,11 @@ def _is_missing_identity_source(self, identity): # type: ignore[no-untyped-def]

required_properties_missing = not headers and not query_strings and not stage_variables and not context

if ttl is None:
return required_properties_missing
try:
ttl_int = int(ttl)
# this will catch if ttl is None and not convertable to an int
# this will catch if and not convertable to an int
aahung marked this conversation as resolved.
Show resolved Hide resolved
except (TypeError, ValueError):
# previous behavior before trying to read ttl
return required_properties_missing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ Resources:
Identity: LambdaAuthorizationIdentity
AuthorizerPayloadFormatVersion: 1.0
DefaultAuthorizer: MyLambdaAuthUpdated

MyRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: Stage name
Auth:
Authorizers:
LambdaRequestIdentityNotObject:
FunctionArn: Function.Arn
FunctionPayloadType: REQUEST
Identity: This should not be a string
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApi] is invalid. MyLambdaAuthUpdated Lambda Authorizer property 'identity' is of invalid type.",
"errors": [
{
"errorMessage": "Resource with id [MyApi] is invalid. MyLambdaAuthUpdated Lambda Authorizer property 'identity' is of invalid type."
}
]
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 2. Resource with id [MyApi] is invalid. MyLambdaAuthUpdated Lambda Authorizer property 'identity' is of invalid type. Resource with id [MyRestApi] is invalid. Invalid type for LambdaRequestIdentityNotObject Authorizer's Identity. It must be a dictionary."
}