Skip to content

Commit 01a5424

Browse files
authored
Validate GetAtts are to a list when being used for a list (#3224)
* Validate GetAtts are to a list when being used for a list * Add in testing for getatt for list
1 parent 10b6a8b commit 01a5424

File tree

4 files changed

+37
-7
lines changed

4 files changed

+37
-7
lines changed

src/cfnlint/rules/resources/properties/Properties.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,26 @@ def propertycheck(self, text, proptype, parenttype, resourcename, path, root):
493493
message.format(prop, resourcename),
494494
)
495495
)
496+
elif "Fn::GetAtt" in text[prop]:
497+
getatt = text[prop]["Fn::GetAtt"]
498+
if isinstance(getatt, str):
499+
getatt = getatt.split(".", 1)
500+
valid_getatts = self.cfn.get_valid_getatts()
501+
if getatt[0] in valid_getatts:
502+
if getatt[1] in valid_getatts[getatt[0]]:
503+
getatt_prop = valid_getatts[getatt[0]][
504+
getatt[1]
505+
]
506+
if getatt_prop.get("Type") != "List":
507+
message = "Property {0} should be of type List for resource {1}"
508+
matches.append(
509+
RuleMatch(
510+
proppath,
511+
message.format(
512+
prop, resourcename
513+
),
514+
)
515+
)
496516
else:
497517
if len(text[prop]) == 1:
498518
for k in text[prop].keys():

src/cfnlint/template/template.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -423,16 +423,10 @@ def build_output_string(resource_type, property_name):
423423
valtype = value["Type"]
424424
if isinstance(valtype, str):
425425
if valtype.startswith(astrik_string_types):
426-
LOGGER.debug(
427-
"Cant build an appropriate getatt list from %s", valtype
428-
)
429426
results[name] = {"*": {"PrimitiveItemType": "String"}}
430427
elif valtype.startswith(astrik_unknown_types) or valtype.endswith(
431428
"::MODULE"
432429
):
433-
LOGGER.debug(
434-
"Cant build an appropriate getatt list from %s", valtype
435-
)
436430
results[name] = {"*": {}}
437431
else:
438432
if value["Type"] in resourcetypes:

test/fixtures/templates/bad/object_should_be_list.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,19 @@ Resources:
677677
- - AttributeName: !Ref PartitionKeyName
678678
KeyType: HASH
679679
- "String2"
680+
EC2Instance:
681+
Type: AWS::EC2::Instance
682+
Properties:
683+
InstanceType: t2.micro
684+
ImageId: XXXXXXXXXXXXXXXXXXXXX
685+
Tags:
686+
- Key: Name
687+
Value: !Ref AWS::StackName
688+
SSMAssociation:
689+
Type: AWS::SSM::Association
690+
Properties:
691+
Name: "SSM Document Name"
692+
ScheduleExpression: rate(2 days)
693+
Targets:
694+
- Key: InstanceIds
695+
Values: !GetAtt EC2Instance.InstanceId

test/unit/rules/resources/properties/test_properties.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def test_file_negative(self):
3737
def test_file_negative_2(self):
3838
"""Failure test"""
3939
self.helper_file_negative(
40-
"test/fixtures/templates/bad/object_should_be_list.yaml", 4
40+
"test/fixtures/templates/bad/object_should_be_list.yaml", 5
4141
)
4242

4343
def test_file_negative_3(self):

0 commit comments

Comments
 (0)