Skip to content

Commit

Permalink
Validate GetAtts are to a list when being used for a list (#3224)
Browse files Browse the repository at this point in the history
* Validate GetAtts are to a list when being used for a list
* Add in testing for getatt for list
  • Loading branch information
kddejong authored May 9, 2024
1 parent 10b6a8b commit 01a5424
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
20 changes: 20 additions & 0 deletions src/cfnlint/rules/resources/properties/Properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,26 @@ def propertycheck(self, text, proptype, parenttype, resourcename, path, root):
message.format(prop, resourcename),
)
)
elif "Fn::GetAtt" in text[prop]:
getatt = text[prop]["Fn::GetAtt"]
if isinstance(getatt, str):
getatt = getatt.split(".", 1)
valid_getatts = self.cfn.get_valid_getatts()
if getatt[0] in valid_getatts:
if getatt[1] in valid_getatts[getatt[0]]:
getatt_prop = valid_getatts[getatt[0]][
getatt[1]
]
if getatt_prop.get("Type") != "List":
message = "Property {0} should be of type List for resource {1}"
matches.append(
RuleMatch(
proppath,
message.format(
prop, resourcename
),
)
)
else:
if len(text[prop]) == 1:
for k in text[prop].keys():
Expand Down
6 changes: 0 additions & 6 deletions src/cfnlint/template/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,16 +423,10 @@ def build_output_string(resource_type, property_name):
valtype = value["Type"]
if isinstance(valtype, str):
if valtype.startswith(astrik_string_types):
LOGGER.debug(
"Cant build an appropriate getatt list from %s", valtype
)
results[name] = {"*": {"PrimitiveItemType": "String"}}
elif valtype.startswith(astrik_unknown_types) or valtype.endswith(
"::MODULE"
):
LOGGER.debug(
"Cant build an appropriate getatt list from %s", valtype
)
results[name] = {"*": {}}
else:
if value["Type"] in resourcetypes:
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/templates/bad/object_should_be_list.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -677,3 +677,19 @@ Resources:
- - AttributeName: !Ref PartitionKeyName
KeyType: HASH
- "String2"
EC2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: t2.micro
ImageId: XXXXXXXXXXXXXXXXXXXXX
Tags:
- Key: Name
Value: !Ref AWS::StackName
SSMAssociation:
Type: AWS::SSM::Association
Properties:
Name: "SSM Document Name"
ScheduleExpression: rate(2 days)
Targets:
- Key: InstanceIds
Values: !GetAtt EC2Instance.InstanceId
2 changes: 1 addition & 1 deletion test/unit/rules/resources/properties/test_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_file_negative(self):
def test_file_negative_2(self):
"""Failure test"""
self.helper_file_negative(
"test/fixtures/templates/bad/object_should_be_list.yaml", 4
"test/fixtures/templates/bad/object_should_be_list.yaml", 5
)

def test_file_negative_3(self):
Expand Down

0 comments on commit 01a5424

Please sign in to comment.