Skip to content

Commit

Permalink
fix(terraform): Fix external_modules_source_map serialization (#5546)
Browse files Browse the repository at this point in the history
fix external_modules_source_map serialization
  • Loading branch information
ChanochShayner committed Sep 11, 2023
1 parent 7eec797 commit ab05169
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions checkov/terraform/graph_builder/graph_components/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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', '')
Expand All @@ -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:
Expand Down

0 comments on commit ab05169

Please sign in to comment.