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: Validate Trigger field and test Cognito properties with intrinsic functions #2092

Merged
merged 8 commits into from Jul 23, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions samtranslator/model/eventsources/push.py
@@ -1,4 +1,5 @@
import copy
import json
import re
from six import string_types
from samtranslator.model import ResourceMacro, PropertyType
Expand Down Expand Up @@ -994,6 +995,16 @@ def to_cloudformation(self, **kwargs):

def _inject_lambda_config(self, function, userpool):
event_triggers = self.Trigger

if isinstance(event_triggers, dict):
mgrandis marked this conversation as resolved.
Show resolved Hide resolved
err_string = json.dumps(event_triggers)
raise InvalidEventException(
self.relative_id,
'Invalid type for trigger "{trigger}". Trigger must be of type list or string.'.format(
trigger=err_string
),
)

if isinstance(self.Trigger, string_types):
event_triggers = [self.Trigger]

Expand Down
18 changes: 13 additions & 5 deletions tests/translator/input/cognito_userpool_with_event.yaml
Expand Up @@ -2,8 +2,16 @@ Resources:
UserPool:
Type: AWS::Cognito::UserPool
Properties:
LambdaConfig:
PreAuthentication: "Test"
UserPoolName: UserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
ImplicitApiFunction:
Type: AWS::Serverless::Function
Properties:
Expand All @@ -14,12 +22,12 @@ Resources:
OneTrigger:
Type: Cognito
Properties:
UserPool:
UserPool:
Ref: UserPool
Trigger: PreSignUp
TwoTrigger:
Type: Cognito
Properties:
UserPool:
UserPool:
Ref: UserPool
Trigger: [Test1, Test2]
Trigger: [PostConfirmation, VerifyAuthChallengeResponse]
28 changes: 28 additions & 0 deletions tests/translator/input/error_cognito_trigger_invalid_type.yaml
@@ -0,0 +1,28 @@
Resources:
UserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: UserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
ImplicitApiFunction:
Type: AWS::Serverless::Function
Properties:
InlineCode: |
exports.handler = async () => ‘Hello World!'
Handler: index.handler
Runtime: nodejs12.x
Events:
OneTrigger:
Type: Cognito
Properties:
UserPool:
Ref: UserPool
Trigger: !Join [ "", [ "Pre", "Sign", "Up"] ]
68 changes: 42 additions & 26 deletions tests/translator/output/aws-cn/cognito_userpool_with_event.json
Expand Up @@ -6,60 +6,67 @@
"LambdaConfig": {
"PreSignUp": {
"Fn::GetAtt": [
"ImplicitApiFunction", "Arn"
"ImplicitApiFunction",
"Arn"
]
},
"PreAuthentication": "Test",
"Test1": {
"PostConfirmation": {
"Fn::GetAtt": [
"ImplicitApiFunction",
"Arn"
]
},
"Test2": {
"VerifyAuthChallengeResponse": {
"Fn::GetAtt": [
"ImplicitApiFunction",
"Arn"
]
}
}
},
"Policies": {
"PasswordPolicy": {
"MinimumLength": 8
}
},
"Schema": [
{
"AttributeDataType": "String",
"Name": "email",
"Required": false
}
],
"UsernameAttributes": [
"email"
],
"UserPoolName": "UserPoolName"
}
},
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.gethtml",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"ImplicitApiFunctionRole",
"ImplicitApiFunctionRole",
"Arn"
]
},
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ImplicitApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -75,24 +82,33 @@
}
}
]
}
},
"ManagedPolicyArns": [
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
},
"ImplicitApiFunctionCognitoPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ImplicitApiFunction"
},
"Principal": "cognito-idp.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"UserPool",
"Arn"
]
},
"Principal": "cognito-idp.amazonaws.com"
}
}
}
}
}
}
Expand Up @@ -6,60 +6,67 @@
"LambdaConfig": {
"PreSignUp": {
"Fn::GetAtt": [
"ImplicitApiFunction", "Arn"
"ImplicitApiFunction",
"Arn"
]
},
"PreAuthentication": "Test",
"Test1": {
"PostConfirmation": {
"Fn::GetAtt": [
"ImplicitApiFunction",
"Arn"
]
},
"Test2": {
"VerifyAuthChallengeResponse": {
"Fn::GetAtt": [
"ImplicitApiFunction",
"Arn"
]
}
}
},
"Policies": {
"PasswordPolicy": {
"MinimumLength": 8
}
},
"Schema": [
{
"AttributeDataType": "String",
"Name": "email",
"Required": false
}
],
"UsernameAttributes": [
"email"
],
"UserPoolName": "UserPoolName"
}
},
"ImplicitApiFunction": {
"Type": "AWS::Lambda::Function",
"Type": "AWS::Lambda::Function",
"Properties": {
"Handler": "index.gethtml",
"Code": {
"S3Bucket": "sam-demo-bucket",
"S3Bucket": "sam-demo-bucket",
"S3Key": "member_portal.zip"
},
},
"Handler": "index.gethtml",
"Role": {
"Fn::GetAtt": [
"ImplicitApiFunctionRole",
"ImplicitApiFunctionRole",
"Arn"
]
},
},
"Runtime": "nodejs12.x",
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
"ImplicitApiFunctionRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Value": "SAM",
"Key": "lambda:createdBy"
}
],
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
Expand All @@ -75,24 +82,33 @@
}
}
]
}
},
"ManagedPolicyArns": [
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
],
"Tags": [
{
"Key": "lambda:createdBy",
"Value": "SAM"
}
]
}
},
},
"ImplicitApiFunctionCognitoPermission": {
"Type": "AWS::Lambda::Permission",
"Properties": {
"Action": "lambda:InvokeFunction",
"FunctionName": {
"Ref": "ImplicitApiFunction"
},
"Principal": "cognito-idp.amazonaws.com",
"SourceArn": {
"Fn::GetAtt": [
"UserPool",
"Arn"
]
},
"Principal": "cognito-idp.amazonaws.com"
}
}
}
}
}
}