Skip to content

Commit f5e1979

Browse files
prenx4xMufaddal Makati
andauthored
Fix: DefaultAuth not a string exception (#1774)
* Fix: DefaultAuth not a string exception * fix: userpool ref not a string Co-authored-by: Mufaddal Makati <[email protected]>
1 parent 6f505ec commit f5e1979

7 files changed

+85
-0
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,12 @@ def _set_default_authorizer(
930930
if not default_authorizer:
931931
return
932932

933+
if not isinstance(default_authorizer, string_types):
934+
raise InvalidResourceException(
935+
self.logical_id,
936+
"DefaultAuthorizer is not a string.",
937+
)
938+
933939
if not authorizers.get(default_authorizer) and default_authorizer != "AWS_IAM":
934940
raise InvalidResourceException(
935941
self.logical_id,

samtranslator/model/eventsources/push.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,11 @@ class Cognito(PushEventSource):
889889
def resources_to_link(self, resources):
890890
if isinstance(self.UserPool, dict) and "Ref" in self.UserPool:
891891
userpool_id = self.UserPool["Ref"]
892+
if not isinstance(userpool_id, string_types):
893+
raise InvalidEventException(
894+
self.logical_id,
895+
"Ref in Userpool is not a string.",
896+
)
892897
if userpool_id in resources:
893898
return {"userpool": resources[userpool_id], "userpool_id": userpool_id}
894899
raise InvalidEventException(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Resources:
2+
UserPool:
3+
Type: AWS::Cognito::UserPool
4+
5+
ImplicitApiFunction:
6+
Type: AWS::Serverless::Function
7+
Properties:
8+
CodeUri: s3://sam-demo-bucket/member_portal.zip
9+
Handler: index.gethtml
10+
Runtime: nodejs12.x
11+
Events:
12+
OneTrigger:
13+
Type: Cognito
14+
Properties:
15+
UserPool:
16+
Ref:
17+
- NotAString
18+
Trigger: PreSignUp
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Resources:
2+
MyApi:
3+
Type: "AWS::Serverless::Api"
4+
Properties:
5+
StageName: Prod
6+
Auth:
7+
DefaultAuthorizer:
8+
- NotAString
9+
ApiKeyRequired: true
10+
Authorizers:
11+
MyLambdaTokenAuth:
12+
FunctionPayloadType: TOKEN
13+
FunctionArn: arn:aws
14+
FunctionInvokeRole: arn:aws:iam::123456789012:role/S3Access
15+
Identity:
16+
Header: MyCustomAuthHeader
17+
ValidationExpression: mycustomauthexpression
18+
ReauthorizeEvery: 20
19+
20+
StateMachine:
21+
Type: AWS::Serverless::StateMachine
22+
Properties:
23+
Name: MyStateMachine
24+
Type: STANDARD
25+
Definition:
26+
Comment: A Hello World example of the Amazon States Language using Pass states
27+
StartAt: Hello
28+
States:
29+
Hello:
30+
Type: Pass
31+
Result: Hello
32+
Next: World
33+
World:
34+
Type: Pass
35+
Result: World
36+
End: true
37+
Policies:
38+
- Version: "2012-10-17"
39+
Statement:
40+
- Effect: Deny
41+
Action: "*"
42+
Resource: "*"
43+
Events:
44+
WithNoAuthorizer:
45+
Type: Api
46+
Properties:
47+
Path: /startNoAuth
48+
Method: post
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [ImplicitApiFunction] is invalid. Event with id [ImplicitApiFunctionOneTrigger] is invalid. Ref in Userpool is not a string."
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApi] is invalid. DefaultAuthorizer is not a string."
3+
}

tests/translator/test_translator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,9 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
568568
"error_state_machine_with_api_auth_none",
569569
"error_state_machine_with_no_api_authorizers",
570570
"error_state_machine_with_undefined_api_authorizer",
571+
"error_state_machine_with_invalid_default_authorizer",
571572
"error_cognito_userpool_duplicate_trigger",
573+
"error_cognito_userpool_not_string",
572574
"error_api_duplicate_methods_same_path",
573575
"error_api_gateway_responses_nonnumeric_status_code",
574576
"error_api_gateway_responses_unknown_responseparameter",

0 commit comments

Comments
 (0)