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(terraform): Fix dynamic blocks nested module #3890

Merged
merged 3 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 9 additions & 4 deletions checkov/terraform/graph_builder/variable_rendering/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ def _render_dynamic_blocks(self) -> None:
if dynamic_blocks:
try:
rendered_blocks = self._process_dynamic_blocks(dynamic_blocks)
except Exception:
except Exception as e:
logging.info(f'Failed to process dynamic blocks in file {vertex.path} of resource {vertex.name}'
f' for blocks: {dynamic_blocks}')
f' for blocks: {dynamic_blocks}, error: {e}')
ChanochShayner marked this conversation as resolved.
Show resolved Hide resolved
continue
changed_attributes = []

Expand All @@ -327,7 +327,7 @@ def _extract_dynamic_arguments(block_name: str, block_content: Dict[str, Any], d
@staticmethod
def _process_dynamic_blocks(dynamic_blocks: list[dict[str, Any]] | dict[str, Any]) -> dict[
str, list[dict[str, Any]]]:
rendered_blocks: dict[str, list[dict[str, Any]]] = {}
rendered_blocks: dict[str, list[dict[str, Any]] | dict[str, Any]] = {}

if not isinstance(dynamic_blocks, list) and not isinstance(dynamic_blocks, dict):
logging.info(f"Dynamic blocks found, but of type {type(dynamic_blocks)}")
Expand Down Expand Up @@ -379,7 +379,12 @@ def _process_dynamic_blocks(dynamic_blocks: list[dict[str, Any]] | dict[str, Any
except (StopIteration, AttributeError):
continue
block_content[DYNAMIC_STRING][next_key]['for_each'] = dynamic_values
rendered_blocks.update(TerraformVariableRenderer._process_dynamic_blocks(block_content[DYNAMIC_STRING]))

flatten_key = next(iter(rendered_blocks.keys()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 🥇

if next_key in rendered_blocks[flatten_key]:
rendered_blocks[flatten_key].update(TerraformVariableRenderer._process_dynamic_blocks(block_content[DYNAMIC_STRING]))
else:
rendered_blocks.update(TerraformVariableRenderer._process_dynamic_blocks(block_content[DYNAMIC_STRING]))

return rendered_blocks

Expand Down
7 changes: 3 additions & 4 deletions tests/terraform/graph/variable_rendering/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,9 @@ def test_dynamic_blocks_with_nested_map(self):
local_graph, _ = graph_manager.build_graph_from_source_directory(path, render_variables=True)
resources_vertex = list(filter(lambda v: v.block_type == BlockType.RESOURCE, local_graph.vertices))
assert len(resources_vertex[0].attributes.get('required_resource_access')) == 2
# TODO support nested with dict.
# assert resources_vertex[0].attributes.get('required_resource_access') == \
# {'resource_app_id': '00000003-0000-0000-c000-000000000000',
# 'resource_access': {'id': '7ab1d382-f21e-4acd-a863-ba3e13f7da61', 'type': 'Role'}}
assert resources_vertex[0].attributes.get('required_resource_access') == \
{'resource_app_id': '00000003-0000-0000-c000-000000000000',
'resource_access': {'id': '7ab1d382-f21e-4acd-a863-ba3e13f7da61', 'type': 'Role'}}

def test_dynamic_example_for_security_rule(self):
graph_manager = TerraformGraphManager('m', ['m'])
Expand Down