Skip to content

Commit

Permalink
fix: Fix two places that could cause internal errors (#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahung authored Mar 13, 2023
1 parent c1d1001 commit 7ae661a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
12 changes: 6 additions & 6 deletions samtranslator/model/eventsources/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,26 +1199,27 @@ def to_cloudformation(self, **kwargs): # type: ignore[no-untyped-def]
lambda_permission.set_resource_attribute(attribute, value)
resources.append(lambda_permission)

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

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

# TODO can these be conditional?

properties = userpool.get("Properties", None)
properties = userpool.get("Properties")
if properties is None:
properties = {}
userpool["Properties"] = properties

lambda_config = properties.get("LambdaConfig", None)
lambda_config = properties.get("LambdaConfig")
if lambda_config is None:
lambda_config = {}
properties["LambdaConfig"] = lambda_config
sam_expect(lambda_config, userpool_id, "LambdaConfig").to_be_a_map()

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


class HttpApi(PushEventSource):
Expand Down
9 changes: 5 additions & 4 deletions samtranslator/swagger/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SwaggerEditor(BaseEditor):
_X_APIGW_REQUEST_VALIDATOR = "x-amazon-apigateway-request-validator"
_X_ENDPOINT_CONFIG = "x-amazon-apigateway-endpoint-configuration"
_CACHE_KEY_PARAMETERS = "cacheKeyParameters"
_SECURITY_DEFINITIONS = "securityDefinitions"
# https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
_EXCLUDED_PATHS_FIELDS = ["summary", "description", "parameters"]
_POLICY_TYPE_IAM = "Iam"
Expand All @@ -65,9 +66,9 @@ def __init__(self, doc: Optional[Dict[str, Any]]) -> None:

self._doc = _deepcopy(doc)
self.paths = self._doc["paths"]
self.security_definitions = self._doc.get("securityDefinitions", Py27Dict())
self.gateway_responses = self._doc.get(self._X_APIGW_GATEWAY_RESPONSES, Py27Dict())
self.resource_policy = self._doc.get(self._X_APIGW_POLICY, Py27Dict())
self.security_definitions = self._doc.get(self._SECURITY_DEFINITIONS) or Py27Dict()
self.gateway_responses = self._doc.get(self._X_APIGW_GATEWAY_RESPONSES) or Py27Dict()
self.resource_policy = self._doc.get(self._X_APIGW_POLICY) or Py27Dict()
self.definitions = self._doc.get("definitions", Py27Dict())

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

if self.security_definitions:
self._doc["securityDefinitions"] = self.security_definitions
self._doc[self._SECURITY_DEFINITIONS] = self.security_definitions
if self.gateway_responses:
self._doc[self._X_APIGW_GATEWAY_RESPONSES] = self.gateway_responses
if self.definitions:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Resources:
UserPool:
Type: AWS::Cognito::UserPool
Properties:
LambdaConfig:
- this: should not be a list

Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: s3://sam-demo-bucket/member_portal.zip
Handler: index.gethtml
Runtime: nodejs12.x
Events:
OneTrigger:
Type: Cognito
Properties:
UserPool:
Ref: UserPool
Trigger: PreSignUp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"_autoGeneratedBreakdownErrorMessage": [
"Invalid Serverless Application Specification document. ",
"Number of errors found: 1. ",
"Resource with id [UserPool] is invalid. ",
"Property 'LambdaConfig' should be a map."
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [UserPool] is invalid. Property 'LambdaConfig' should be a map."
}

0 comments on commit 7ae661a

Please sign in to comment.