Skip to content

Commit 0e45f20

Browse files
authored
fix: Validate Trigger field and test Cognito properties with intrinsic functions (#2092)
* Add headers whenever cors is set * Fix Cognito trigger validation * Make templates deployable * Removed trigger value validation * Fix Python2 string matching test issue * Use PropertyType mechanism to exclude intrinsics * Remove unused import
1 parent 0070a98 commit 0e45f20

File tree

8 files changed

+180
-87
lines changed

8 files changed

+180
-87
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ class Cognito(PushEventSource):
947947

948948
property_types = {
949949
"UserPool": PropertyType(True, is_str()),
950-
"Trigger": PropertyType(True, one_of(is_str(), list_of(is_str()))),
950+
"Trigger": PropertyType(True, one_of(is_str(), list_of(is_str())), False),
951951
}
952952

953953
def resources_to_link(self, resources):

tests/translator/input/cognito_userpool_with_event.yaml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ Resources:
22
UserPool:
33
Type: AWS::Cognito::UserPool
44
Properties:
5-
LambdaConfig:
6-
PreAuthentication: "Test"
5+
UserPoolName: UserPoolName
6+
Policies:
7+
PasswordPolicy:
8+
MinimumLength: 8
9+
UsernameAttributes:
10+
- email
11+
Schema:
12+
- AttributeDataType: String
13+
Name: email
14+
Required: false
715
ImplicitApiFunction:
816
Type: AWS::Serverless::Function
917
Properties:
@@ -14,12 +22,12 @@ Resources:
1422
OneTrigger:
1523
Type: Cognito
1624
Properties:
17-
UserPool:
25+
UserPool:
1826
Ref: UserPool
1927
Trigger: PreSignUp
2028
TwoTrigger:
2129
Type: Cognito
2230
Properties:
23-
UserPool:
31+
UserPool:
2432
Ref: UserPool
25-
Trigger: [Test1, Test2]
33+
Trigger: [PostConfirmation, VerifyAuthChallengeResponse]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Resources:
2+
UserPool:
3+
Type: AWS::Cognito::UserPool
4+
Properties:
5+
UserPoolName: UserPoolName
6+
Policies:
7+
PasswordPolicy:
8+
MinimumLength: 8
9+
UsernameAttributes:
10+
- email
11+
Schema:
12+
- AttributeDataType: String
13+
Name: email
14+
Required: false
15+
ImplicitApiFunction:
16+
Type: AWS::Serverless::Function
17+
Properties:
18+
InlineCode: |
19+
exports.handler = async () => ‘Hello World!'
20+
Handler: index.handler
21+
Runtime: nodejs12.x
22+
Events:
23+
OneTrigger:
24+
Type: Cognito
25+
Properties:
26+
UserPool:
27+
Ref: UserPool
28+
Trigger: !Join [ "", [ "Pre", "Sign", "Up"] ]

tests/translator/output/aws-cn/cognito_userpool_with_event.json

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,67 @@
66
"LambdaConfig": {
77
"PreSignUp": {
88
"Fn::GetAtt": [
9-
"ImplicitApiFunction", "Arn"
9+
"ImplicitApiFunction",
10+
"Arn"
1011
]
1112
},
12-
"PreAuthentication": "Test",
13-
"Test1": {
13+
"PostConfirmation": {
1414
"Fn::GetAtt": [
1515
"ImplicitApiFunction",
1616
"Arn"
1717
]
1818
},
19-
"Test2": {
19+
"VerifyAuthChallengeResponse": {
2020
"Fn::GetAtt": [
2121
"ImplicitApiFunction",
2222
"Arn"
2323
]
2424
}
25-
}
25+
},
26+
"Policies": {
27+
"PasswordPolicy": {
28+
"MinimumLength": 8
29+
}
30+
},
31+
"Schema": [
32+
{
33+
"AttributeDataType": "String",
34+
"Name": "email",
35+
"Required": false
36+
}
37+
],
38+
"UsernameAttributes": [
39+
"email"
40+
],
41+
"UserPoolName": "UserPoolName"
2642
}
2743
},
2844
"ImplicitApiFunction": {
2945
"Type": "AWS::Lambda::Function",
3046
"Properties": {
31-
"Handler": "index.gethtml",
3247
"Code": {
33-
"S3Bucket": "sam-demo-bucket",
48+
"S3Bucket": "sam-demo-bucket",
3449
"S3Key": "member_portal.zip"
35-
},
50+
},
51+
"Handler": "index.gethtml",
3652
"Role": {
3753
"Fn::GetAtt": [
38-
"ImplicitApiFunctionRole",
54+
"ImplicitApiFunctionRole",
3955
"Arn"
4056
]
41-
},
57+
},
4258
"Runtime": "nodejs12.x",
4359
"Tags": [
4460
{
45-
"Value": "SAM",
46-
"Key": "lambda:createdBy"
61+
"Key": "lambda:createdBy",
62+
"Value": "SAM"
4763
}
4864
]
4965
}
5066
},
5167
"ImplicitApiFunctionRole": {
5268
"Type": "AWS::IAM::Role",
5369
"Properties": {
54-
"ManagedPolicyArns": [
55-
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
56-
],
57-
"Tags": [
58-
{
59-
"Value": "SAM",
60-
"Key": "lambda:createdBy"
61-
}
62-
],
6370
"AssumeRolePolicyDocument": {
6471
"Version": "2012-10-17",
6572
"Statement": [
@@ -75,24 +82,33 @@
7582
}
7683
}
7784
]
78-
}
85+
},
86+
"ManagedPolicyArns": [
87+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
88+
],
89+
"Tags": [
90+
{
91+
"Key": "lambda:createdBy",
92+
"Value": "SAM"
93+
}
94+
]
7995
}
80-
},
96+
},
8197
"ImplicitApiFunctionCognitoPermission": {
8298
"Type": "AWS::Lambda::Permission",
8399
"Properties": {
84100
"Action": "lambda:InvokeFunction",
85101
"FunctionName": {
86102
"Ref": "ImplicitApiFunction"
87103
},
104+
"Principal": "cognito-idp.amazonaws.com",
88105
"SourceArn": {
89106
"Fn::GetAtt": [
90107
"UserPool",
91108
"Arn"
92109
]
93-
},
94-
"Principal": "cognito-idp.amazonaws.com"
110+
}
95111
}
96112
}
97113
}
98-
}
114+
}

tests/translator/output/aws-us-gov/cognito_userpool_with_event.json

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,67 @@
66
"LambdaConfig": {
77
"PreSignUp": {
88
"Fn::GetAtt": [
9-
"ImplicitApiFunction", "Arn"
9+
"ImplicitApiFunction",
10+
"Arn"
1011
]
1112
},
12-
"PreAuthentication": "Test",
13-
"Test1": {
13+
"PostConfirmation": {
1414
"Fn::GetAtt": [
1515
"ImplicitApiFunction",
1616
"Arn"
1717
]
1818
},
19-
"Test2": {
19+
"VerifyAuthChallengeResponse": {
2020
"Fn::GetAtt": [
2121
"ImplicitApiFunction",
2222
"Arn"
2323
]
2424
}
25-
}
25+
},
26+
"Policies": {
27+
"PasswordPolicy": {
28+
"MinimumLength": 8
29+
}
30+
},
31+
"Schema": [
32+
{
33+
"AttributeDataType": "String",
34+
"Name": "email",
35+
"Required": false
36+
}
37+
],
38+
"UsernameAttributes": [
39+
"email"
40+
],
41+
"UserPoolName": "UserPoolName"
2642
}
2743
},
2844
"ImplicitApiFunction": {
29-
"Type": "AWS::Lambda::Function",
45+
"Type": "AWS::Lambda::Function",
3046
"Properties": {
31-
"Handler": "index.gethtml",
3247
"Code": {
33-
"S3Bucket": "sam-demo-bucket",
48+
"S3Bucket": "sam-demo-bucket",
3449
"S3Key": "member_portal.zip"
35-
},
50+
},
51+
"Handler": "index.gethtml",
3652
"Role": {
3753
"Fn::GetAtt": [
38-
"ImplicitApiFunctionRole",
54+
"ImplicitApiFunctionRole",
3955
"Arn"
4056
]
41-
},
57+
},
4258
"Runtime": "nodejs12.x",
4359
"Tags": [
4460
{
45-
"Value": "SAM",
46-
"Key": "lambda:createdBy"
61+
"Key": "lambda:createdBy",
62+
"Value": "SAM"
4763
}
4864
]
4965
}
5066
},
5167
"ImplicitApiFunctionRole": {
5268
"Type": "AWS::IAM::Role",
5369
"Properties": {
54-
"ManagedPolicyArns": [
55-
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
56-
],
57-
"Tags": [
58-
{
59-
"Value": "SAM",
60-
"Key": "lambda:createdBy"
61-
}
62-
],
6370
"AssumeRolePolicyDocument": {
6471
"Version": "2012-10-17",
6572
"Statement": [
@@ -75,24 +82,33 @@
7582
}
7683
}
7784
]
78-
}
85+
},
86+
"ManagedPolicyArns": [
87+
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
88+
],
89+
"Tags": [
90+
{
91+
"Key": "lambda:createdBy",
92+
"Value": "SAM"
93+
}
94+
]
7995
}
80-
},
96+
},
8197
"ImplicitApiFunctionCognitoPermission": {
8298
"Type": "AWS::Lambda::Permission",
8399
"Properties": {
84100
"Action": "lambda:InvokeFunction",
85101
"FunctionName": {
86102
"Ref": "ImplicitApiFunction"
87103
},
104+
"Principal": "cognito-idp.amazonaws.com",
88105
"SourceArn": {
89106
"Fn::GetAtt": [
90107
"UserPool",
91108
"Arn"
92109
]
93-
},
94-
"Principal": "cognito-idp.amazonaws.com"
110+
}
95111
}
96112
}
97113
}
98-
}
114+
}

0 commit comments

Comments
 (0)