Skip to content

Commit c365e22

Browse files
authored
Validate the parameter AllowedValue exists before we remove it (#2649)
1 parent 7192944 commit c365e22

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

src/cfnlint/conditions/conditions.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,15 @@ def _build_cnf(
115115
if param.hash not in allowed_values:
116116
continue
117117
if isinstance(equal_1.left, str):
118-
allowed_values[param.hash].remove(get_hash(equal_1.left))
118+
if get_hash(equal_1.left) in allowed_values[param.hash]:
119+
allowed_values[param.hash].remove(get_hash(equal_1.left))
120+
else:
121+
equal_vars[equal_1.hash] = BooleanFalse()
119122
if isinstance(equal_1.right, str):
120-
allowed_values[param.hash].remove(get_hash(equal_1.right))
123+
if get_hash(equal_1.right) in allowed_values[param.hash]:
124+
allowed_values[param.hash].remove(get_hash(equal_1.right))
125+
else:
126+
equal_vars[equal_1.hash] = BooleanFalse()
121127

122128
# iteration 2 builds the cnf formulas to make sure any empty lists
123129
# are now full not equals
@@ -157,10 +163,10 @@ def build_scenarios(self, condition_names: List[str]) -> Iterator[Dict[str, bool
157163
return
158164

159165
# if only one condition we will assume its True/False
160-
if len(condition_names) == 1:
161-
yield {condition_names[0]: True}
162-
yield {condition_names[0]: False}
163-
return
166+
# if len(condition_names) == 1:
167+
# yield {condition_names[0]: True}
168+
# yield {condition_names[0]: False}
169+
# return
164170

165171
try:
166172
# build a large matric of True/False options based on the provided conditions

test/unit/module/conditions/test_conditions.py

+30-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_check_never_false(self):
127127
],
128128
)
129129

130-
def test_check_can_be_salfe(self):
130+
def test_check_can_be_false(self):
131131
"""With allowed values two conditions can both be false"""
132132
template = decode_str(
133133
"""
@@ -151,3 +151,32 @@ def test_check_can_be_salfe(self):
151151
{"IsProd": False, "IsDev": False},
152152
],
153153
)
154+
155+
def test_check_can_be_good_when_condition_value(self):
156+
"""Some times a condition Equals doesn't match to allowed values"""
157+
template = decode_str(
158+
"""
159+
Parameters:
160+
Environment:
161+
Type: String
162+
AllowedValues: ["prod", "dev", "stage"]
163+
Conditions:
164+
IsGamma: !Equals [!Ref Environment, "gamma"]
165+
IsBeta: !Equals ["beta", !Ref Environment]
166+
"""
167+
)[0]
168+
169+
cfn = Template("", template)
170+
self.assertEqual(len(cfn.conditions._conditions), 2)
171+
self.assertListEqual(
172+
list(cfn.conditions.build_scenarios(["IsGamma", "IsBeta"])),
173+
[
174+
{"IsBeta": False, "IsGamma": False},
175+
],
176+
)
177+
self.assertListEqual(
178+
list(cfn.conditions.build_scenarios(["IsGamma"])),
179+
[
180+
{"IsGamma": False},
181+
],
182+
)

0 commit comments

Comments
 (0)