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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(terraform_json): support locals block in CDKTF output #6452

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
44 changes: 27 additions & 17 deletions checkov/terraform_json/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,37 @@ def prepare_definition(definition: dict[str, Any]) -> dict[str, Any]:
if block_type == COMMENT_FIELD_NAME or block_type in LINE_FIELD_NAMES:
continue

definition_new[block_type] = []
for block_name, config in blocks.items():
if block_name == COMMENT_FIELD_NAME or block_name in LINE_FIELD_NAMES:
continue

if block_type in (BlockType.RESOURCE, BlockType.DATA):
# data/resource have an extra nested level resource_type -> resource_name -> resource_config
for resource_name, resource_config in config.items():
if resource_name in IGNORE_FILED_NAMES:
continue
definition_new[block_type].append({block_name: {resource_name: hclify(obj=resource_config)}})
elif block_type == BlockType.PROVIDER:
# provider are stored as a list, which we need to move one level higher to add the name
for provider_config in config:
definition_new[block_type].append({block_name: hclify(obj=provider_config)})
else:
definition_new[block_type].append({block_name: hclify(obj=config)})
definition_new[block_type] = handle_block_type(block_type=block_type, blocks=blocks)

return definition_new


def handle_block_type(block_type: str, blocks: dict[str, Any]) -> list[dict[str, Any]]:
result: list[dict[str, Any]] = []

for block_name, config in blocks.items():
if block_name == COMMENT_FIELD_NAME or block_name in LINE_FIELD_NAMES:
continue

if block_type in (BlockType.RESOURCE, BlockType.DATA):
# data/resource have an extra nested level resource_type -> resource_name -> resource_config
for resource_name, resource_config in config.items():
if resource_name in IGNORE_FILED_NAMES:
continue
result.append({block_name: {resource_name: hclify(obj=resource_config)}})
elif block_type == BlockType.PROVIDER:
# provider are stored as a list, which we need to move one level higher to add the name
for provider_config in config:
result.append({block_name: hclify(obj=provider_config)})
elif block_type == BlockType.LOCALS:
# a local block is stored as single dict
return [hclify(obj=blocks)]
else:
result.append({block_name: hclify(obj=config)})

return result


def hclify(
obj: dict[str, Any],
conf: dict[str, Any] | None = None,
Expand Down
28 changes: 27 additions & 1 deletion tests/terraform_json/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from checkov.terraform_json.parser import hclify
from checkov.terraform_json.parser import hclify, prepare_definition


def test_hclify():
Expand Down Expand Up @@ -34,3 +34,29 @@ def test_hclify():
}
],
}


def test_prepare_definition_locals():
cdk_definition = {
"locals": {
"bucket_name": "example",
"http_endpoint": "disabled",
"__startline__": 1,
"__endline__": 2,
}
}

# when
tf_definition = prepare_definition(cdk_definition)

# then
assert tf_definition == {
"locals": [
{
"bucket_name": ["example"],
"http_endpoint": ["disabled"],
"__startline__": 1,
"__endline__": 2,
}
]
}
Loading