Skip to content

Commit

Permalink
fix(terraform): Add source_module_object to blocks from_dict func (#5806
Browse files Browse the repository at this point in the history
)

* small fixes to foreach handler

* mypy

* mypy
  • Loading branch information
ChanochShayner authored Nov 28, 2023
1 parent a0a343c commit 7c0eb2b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion checkov/terraform/graph_builder/foreach/module_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import typing
from collections import defaultdict
from typing import Any
import json

from checkov.common.util.consts import RESOLVED_MODULE_ENTRY_NAME
from checkov.common.util.data_structures_utils import pickle_deepcopy
Expand Down Expand Up @@ -281,10 +282,13 @@ def _update_resolved_entry_for_tf_definition(child: TerraformBlock, original_for
resolved_module_name = config.get(RESOLVED_MODULE_ENTRY_NAME)
if resolved_module_name is not None and len(resolved_module_name) > 0:
original_definition_key = config[RESOLVED_MODULE_ENTRY_NAME][0]
if isinstance(original_definition_key, str):
original_definition_key = TFDefinitionKey.from_json(json.loads(original_definition_key))
resolved_tf_source_module = TFDefinitionKey.from_json(json.loads(resolved_module_name[0])) if isinstance(resolved_module_name[0], str) else resolved_module_name[0]
tf_source_modules = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx(
original_foreach_or_count_key,
original_module_key,
resolved_module_name[0].tf_source_modules,
resolved_tf_source_module.tf_source_modules,
)
config[RESOLVED_MODULE_ENTRY_NAME][0] = TFDefinitionKey(file_path=original_definition_key.file_path,
tf_source_modules=tf_source_modules)
Expand Down
10 changes: 5 additions & 5 deletions checkov/terraform/graph_builder/graph_components/blocks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Union, Dict, Any, List, Optional, Set, TYPE_CHECKING, cast
from typing import Union, Dict, Any, List, Optional, Set, cast
import dpath
import re

Expand All @@ -11,9 +11,7 @@
from checkov.common.graph.graph_builder.graph_components.blocks import Block
from checkov.common.util.consts import RESOLVED_MODULE_ENTRY_NAME
from checkov.terraform.graph_builder.graph_components.block_types import BlockType

if TYPE_CHECKING:
from checkov.terraform import TFModule
from checkov.terraform.modules.module_objects import TFModule


class TerraformBlock(Block):
Expand Down Expand Up @@ -252,7 +250,8 @@ def to_dict(self) -> dict[str, Any]:
'name': self.name,
'path': self.path,
'source': self.source,
'source_module': list(self.source_module)
'source_module': list(self.source_module),
'source_module_object': self.source_module_object
}

@staticmethod
Expand All @@ -266,4 +265,5 @@ def from_dict(data: dict[str, Any]) -> TerraformBlock:
tf_block.breadcrumbs = data.get('breadcrumbs', {})
tf_block.module_connections = data.get('module_connections', {})
tf_block.source_module = data.get('source_module', set())
tf_block.source_module_object = TFModule.from_json(data.get('source_module_object'))
return tf_block
2 changes: 1 addition & 1 deletion checkov/terraform/modules/module_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __str__(self) -> str:
return json.dumps(dict(self), cls=CustomJSONEncoder)

@staticmethod
def from_json(json_dct: dict[str, Any]) -> TFModule | None:
def from_json(json_dct: dict[str, Any] | None) -> TFModule | None:
return TFModule(path=json_dct['path'], name=json_dct['name'], foreach_idx=json_dct['foreach_idx'],
nested_tf_module=TFModule.from_json(json_dct['nested_tf_module']) if json_dct.get(
'nested_tf_module') else None) if json_dct else None
Expand Down

0 comments on commit 7c0eb2b

Please sign in to comment.