diff --git a/checkov/terraform/graph_builder/graph_components/module.py b/checkov/terraform/graph_builder/graph_components/module.py index 162c3a8d6cc..e553ab6c18e 100644 --- a/checkov/terraform/graph_builder/graph_components/module.py +++ b/checkov/terraform/graph_builder/graph_components/module.py @@ -3,6 +3,7 @@ import json import os from typing import List, Dict, Any, Set, Callable, Tuple, TYPE_CHECKING, cast +from ast import literal_eval from checkov.common.typing import TFDefinitionKeyType from checkov.common.util.data_structures_utils import pickle_deepcopy @@ -49,7 +50,7 @@ def __eq__(self, other: object) -> bool: def to_dict(self) -> dict[str, Any]: return { - 'external_modules_source_map': self.external_modules_source_map, + 'external_modules_source_map': self._to_dict_external_modules_source_map(), 'path': self.path, 'customer_name': self.customer_name, 'account_id': self.account_id, @@ -60,10 +61,9 @@ def to_dict(self) -> dict[str, Any]: 'blocks': [block.to_dict() for block in self.blocks] } - @staticmethod - def from_dict(module_dict: dict[str, Any]) -> Module: + def from_dict(self, module_dict: dict[str, Any]) -> Module: module = Module(source_dir=module_dict.get('source_dir', ''), - external_modules_source_map=module_dict.get('external_modules_source_map', {}) + external_modules_source_map=self._from_dict_external_modules_source_map(module_dict) ) module.blocks = [TerraformBlock.from_dict(block) for block in module_dict.get('blocks', [])] module.path = module_dict.get('path', '') @@ -75,6 +75,13 @@ def from_dict(module_dict: dict[str, Any]) -> Module: module.render_dynamic_blocks_env_var = module_dict.get('render_dynamic_blocks_env_var', '') return module + def _to_dict_external_modules_source_map(self) -> dict[str, str]: + return {str(k_tuple): v for k_tuple, v in self.external_modules_source_map.items()} + + @staticmethod + def _from_dict_external_modules_source_map(module_dict: dict[str, Any]) -> dict[tuple[str, str], Any]: + return {literal_eval(k_tuple): v for k_tuple, v in module_dict.get('external_modules_source_map', {}).items()} + def add_blocks( self, block_type: str, blocks: List[Dict[str, Dict[str, Any]]], path: TFDefinitionKeyType, source: str ) -> None: