Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudformation): only parse valid tag key-pairs in CloudFormation #3835

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions checkov/cloudformation/cfn_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from checkov.common.models.consts import YAML_COMMENT_MARK

CF_POSSIBLE_ENDINGS = frozenset([".yml", ".yaml", ".json", ".template"])
TAG_FIELD_NAMES = ("Key", "Value")


def get_resource_tags(entity: Dict[StrNode, DictNode], registry: Registry = cfn_registry) -> Optional[Dict[str, str]]:
Expand All @@ -43,8 +44,12 @@ def get_resource_tags(entity: Dict[StrNode, DictNode], registry: Registry = cfn_


def parse_entity_tags(tags: Union[ListNode, Dict[str, Any]]) -> Optional[Dict[str, str]]:
if isinstance(tags, ListNode):
tag_dict = {get_entity_value_as_string(tag["Key"]): get_entity_value_as_string(tag["Value"]) for tag in tags}
if isinstance(tags, list):
tag_dict = {
get_entity_value_as_string(tag["Key"]): get_entity_value_as_string(tag["Value"])
for tag in tags
if all(field in tag for field in TAG_FIELD_NAMES)
}
return tag_dict
elif isinstance(tags, dict):
tag_dict = {
Expand Down
21 changes: 13 additions & 8 deletions tests/cloudformation/runner/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,25 @@ def test_get_tags(self):
entity = {resource_name: resource}
entity_tags = cfn_utils.get_resource_tags(entity)

self.assertIsNone(entity_tags)
self.assertDictEqual(
entity_tags,
{
"Name": "TF-FulfillmentServer",
"terraform-server-tag-key": "terraform-server-tag-value",
}
)

resource_name = 'EKSClusterNodegroup'
resource = definitions['Resources'][resource_name]
entity = {resource_name: resource}
entity_tags = cfn_utils.get_resource_tags(entity)

self.assertEqual(len(entity_tags), 1)
tags = {
'Name': '{\'Ref\': \'ClusterName\'}-EKS-{\'Ref\': \'NodeGroupName\'}'
}

for name, value in tags.items():
self.assertEqual(entity_tags[name], value)
self.assertDictEqual(
entity_tags,
{
'Name': '{\'Ref\': \'ClusterName\'}-EKS-{\'Ref\': \'NodeGroupName\'}',
}
)

def test_wrong_check_imports(self):
wrong_imports = ["arm", "dockerfile", "helm", "kubernetes", "serverless", "terraform"]
Expand Down