Skip to content

Commit

Permalink
Validate null Condition section (#3169)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddejong authored Apr 24, 2024
1 parent fb55eee commit eaf23ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/cfnlint/rules/conditions/Configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class Configuration(CloudFormationLintRule):
def match(self, cfn):
matches = []

conditions = cfn.template.get("Conditions", {})
if conditions:
if "Conditions" not in cfn.template:
return matches
conditions = cfn.template.get("Conditions", None)
if isinstance(conditions, dict):
for condname, condobj in conditions.items():
if not isinstance(condobj, dict):
message = "Condition {0} has invalid property"
Expand All @@ -52,5 +54,12 @@ def match(self, cfn):
message.format(condname, k),
)
)
else:
matches.append(
RuleMatch(
["Conditions"],
"Condition must be an object",
)
)

return matches
8 changes: 8 additions & 0 deletions test/fixtures/templates/bad/conditions_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
AWSTemplateFormatVersion: "2010-09-09"
Conditions:
Resources:
myTopic:
Type: AWS::SNS::Topic
Properties:
DisplayName: mytopic
TopicName: mytopic
4 changes: 4 additions & 0 deletions test/unit/rules/conditions/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ def test_file_positive(self):
"""Test Positive"""
self.helper_file_positive()

def test_file_negative_type(self):
"""Test failure"""
self.helper_file_negative("test/fixtures/templates/bad/conditions_type.yaml", 1)

def test_file_negative(self):
"""Test failure"""
self.helper_file_negative("test/fixtures/templates/bad/conditions.yaml", 4)

0 comments on commit eaf23ca

Please sign in to comment.