Skip to content

Commit a92c9a6

Browse files
authored
Fix an issue with conditions in E1024 (#2780)
1 parent 582258a commit a92c9a6

File tree

2 files changed

+38
-16
lines changed
  • src/cfnlint/rules/functions
  • test/fixtures/templates/good/functions

2 files changed

+38
-16
lines changed

src/cfnlint/rules/functions/Cidr.py

+21-15
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ def check_ip_block(self, value, path):
4949
)
5050
)
5151
else:
52-
message = "Cidr ipBlock should be Cidr Range, Ref, GetAtt, Sub or Select for {0}"
52+
message = (
53+
"Cidr ipBlock should be Cidr Range, Ref, GetAtt, Sub or"
54+
" Select for {0}"
55+
)
5356
matches.append(
5457
RuleMatch(
5558
path, message.format("/".join(map(str, value)))
@@ -78,18 +81,16 @@ def check_count(self, value, path):
7881
if len(value.get("Fn::If")) == 3 and isinstance(
7982
value.get("Fn::If"), list
8083
):
81-
matches.extend(
82-
self.check_count(
83-
value.get("Fn::If")[1],
84-
path=path[:] + [index_key, 1],
85-
)
86-
)
87-
matches.extend(
88-
self.check_count(
89-
value.get("Fn::If")[2],
90-
path=path[:] + [index_key, 2],
84+
for i in [1, 2]:
85+
(
86+
new_count_parameters,
87+
new_matches,
88+
) = self.check_count(
89+
value.get("Fn::If")[i],
90+
path=path[:] + [index_key, i],
9191
)
92-
)
92+
count_parameters.extend(new_count_parameters)
93+
matches.extend(new_matches)
9394
else:
9495
message = "Cidr count should be Int, Ref, or Select for {0}"
9596
matches.append(
@@ -168,15 +169,21 @@ def check_parameter_count(self, cfn, parameter_name):
168169
max_value = parameter_obj.get("MaxValue")
169170
min_value = parameter_obj.get("MinValue")
170171
if (not min_value) or min_value < 1 or min_value > 256:
171-
message = "Parameter for Cidr count have MinValue between 1 and 256 at {0}"
172+
message = (
173+
"Parameter for Cidr count have MinValue between 1 and 256"
174+
" at {0}"
175+
)
172176
matches.append(
173177
RuleMatch(
174178
tree + ["MinValue"],
175179
message.format("/".join(map(str, tree + ["MinValue"]))),
176180
)
177181
)
178182
if (not max_value) or max_value < 1 or max_value > 256:
179-
message = "Parameter for Cidr count have MaxValue between 1 and 256 at {0}"
183+
message = (
184+
"Parameter for Cidr count have MaxValue between 1 and 256"
185+
" at {0}"
186+
)
180187
matches.append(
181188
RuleMatch(
182189
tree + ["MaxValue"],
@@ -258,7 +265,6 @@ def match(self, cfn):
258265
)
259266
count_parameters.extend(new_count_parameters)
260267
matches.extend(new_matches)
261-
262268
new_size_mask_parameters, new_matches = self.check_size_mask(
263269
size_mask_obj, tree[:] + [2]
264270
)

test/fixtures/templates/good/functions/cidr.yaml

+17-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Parameters:
1313
MaxValue: 6
1414
Size:
1515
Type: String
16+
FirstTierSubnet1CIDR:
17+
Type: String
1618
Mappings:
1719
CidrBitsMap:
1820
"2":
@@ -55,7 +57,7 @@ Resources:
5557
- 8
5658
- Fn::Select:
5759
- 15
58-
- Fn::Cidr:
60+
- Fn::Cidr:
5961
- Fn::GetAtt: MyVPC.CidrBlock
6062
- 16
6163
- 8
@@ -64,3 +66,17 @@ Resources:
6466
- 2
6567
- 4
6668
VpcId: 'vpc-123456'
69+
PublicSubnet1:
70+
Type: AWS::EC2::Subnet
71+
Properties:
72+
VpcId: !Ref MyVPC
73+
AvailabilityZone: !Select [0, !GetAZs ""]
74+
CidrBlock: !If
75+
- IsProd
76+
- !Ref FirstTierSubnet1CIDR
77+
- !Select
78+
- 0
79+
- !Cidr
80+
- 10.0.0.0/24
81+
- !If [IsProd, 16, 8]
82+
- 4

0 commit comments

Comments
 (0)