Skip to content

Commit 7ae661a

Browse files
authored
fix: Fix two places that could cause internal errors (#3023)
1 parent c1d1001 commit 7ae661a

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,26 +1199,27 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
11991199
lambda_permission.set_resource_attribute(attribute, value)
12001200
resources.append(lambda_permission)
12011201

1202-
self._inject_lambda_config(function, userpool) # type: ignore[no-untyped-call]
1203-
resources.append(CognitoUserPool.from_dict(userpool_id, userpool))
1202+
self._inject_lambda_config(function, userpool, userpool_id)
1203+
resources.append(CognitoUserPool.from_dict(userpool_id, userpool, userpool_id))
12041204
return resources
12051205

1206-
def _inject_lambda_config(self, function, userpool): # type: ignore[no-untyped-def]
1206+
def _inject_lambda_config(self, function: Any, userpool: Dict[str, Any], userpool_id: str) -> None:
12071207
event_triggers = self.Trigger
12081208
if isinstance(self.Trigger, str):
12091209
event_triggers = [self.Trigger]
12101210

12111211
# TODO can these be conditional?
12121212

1213-
properties = userpool.get("Properties", None)
1213+
properties = userpool.get("Properties")
12141214
if properties is None:
12151215
properties = {}
12161216
userpool["Properties"] = properties
12171217

1218-
lambda_config = properties.get("LambdaConfig", None)
1218+
lambda_config = properties.get("LambdaConfig")
12191219
if lambda_config is None:
12201220
lambda_config = {}
12211221
properties["LambdaConfig"] = lambda_config
1222+
sam_expect(lambda_config, userpool_id, "LambdaConfig").to_be_a_map()
12221223

12231224
for event_trigger in event_triggers:
12241225
if event_trigger not in lambda_config:
@@ -1227,7 +1228,6 @@ def _inject_lambda_config(self, function, userpool): # type: ignore[no-untyped-
12271228
raise InvalidEventException(
12281229
self.relative_id, f'Cognito trigger "{self.Trigger}" defined multiple times.'
12291230
)
1230-
return userpool
12311231

12321232

12331233
class HttpApi(PushEventSource):

samtranslator/swagger/swagger.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SwaggerEditor(BaseEditor):
4141
_X_APIGW_REQUEST_VALIDATOR = "x-amazon-apigateway-request-validator"
4242
_X_ENDPOINT_CONFIG = "x-amazon-apigateway-endpoint-configuration"
4343
_CACHE_KEY_PARAMETERS = "cacheKeyParameters"
44+
_SECURITY_DEFINITIONS = "securityDefinitions"
4445
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
4546
_EXCLUDED_PATHS_FIELDS = ["summary", "description", "parameters"]
4647
_POLICY_TYPE_IAM = "Iam"
@@ -65,9 +66,9 @@ def __init__(self, doc: Optional[Dict[str, Any]]) -> None:
6566

6667
self._doc = _deepcopy(doc)
6768
self.paths = self._doc["paths"]
68-
self.security_definitions = self._doc.get("securityDefinitions", Py27Dict())
69-
self.gateway_responses = self._doc.get(self._X_APIGW_GATEWAY_RESPONSES, Py27Dict())
70-
self.resource_policy = self._doc.get(self._X_APIGW_POLICY, Py27Dict())
69+
self.security_definitions = self._doc.get(self._SECURITY_DEFINITIONS) or Py27Dict()
70+
self.gateway_responses = self._doc.get(self._X_APIGW_GATEWAY_RESPONSES) or Py27Dict()
71+
self.resource_policy = self._doc.get(self._X_APIGW_POLICY) or Py27Dict()
7172
self.definitions = self._doc.get("definitions", Py27Dict())
7273

7374
# https://swagger.io/specification/#path-item-object
@@ -1208,7 +1209,7 @@ def swagger(self) -> Dict[str, Any]:
12081209
self._doc[key] = self.paths
12091210

12101211
if self.security_definitions:
1211-
self._doc["securityDefinitions"] = self.security_definitions
1212+
self._doc[self._SECURITY_DEFINITIONS] = self.security_definitions
12121213
if self.gateway_responses:
12131214
self._doc[self._X_APIGW_GATEWAY_RESPONSES] = self.gateway_responses
12141215
if self.definitions:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Resources:
2+
UserPool:
3+
Type: AWS::Cognito::UserPool
4+
Properties:
5+
LambdaConfig:
6+
- this: should not be a list
7+
8+
Function:
9+
Type: AWS::Serverless::Function
10+
Properties:
11+
CodeUri: s3://sam-demo-bucket/member_portal.zip
12+
Handler: index.gethtml
13+
Runtime: nodejs12.x
14+
Events:
15+
OneTrigger:
16+
Type: Cognito
17+
Properties:
18+
UserPool:
19+
Ref: UserPool
20+
Trigger: PreSignUp
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"_autoGeneratedBreakdownErrorMessage": [
3+
"Invalid Serverless Application Specification document. ",
4+
"Number of errors found: 1. ",
5+
"Resource with id [UserPool] is invalid. ",
6+
"Property 'LambdaConfig' should be a map."
7+
],
8+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [UserPool] is invalid. Property 'LambdaConfig' should be a map."
9+
}

0 commit comments

Comments
 (0)