diff --git a/checkov/terraform/graph_builder/foreach/abstract_handler.py b/checkov/terraform/graph_builder/foreach/abstract_handler.py index b4500931a6c..20fc51b85af 100644 --- a/checkov/terraform/graph_builder/foreach/abstract_handler.py +++ b/checkov/terraform/graph_builder/foreach/abstract_handler.py @@ -67,23 +67,6 @@ def _build_sub_graph(self, blocks_to_render: list[int]) -> TerraformLocalGraph: sub_graph.out_edges = self.local_graph.out_edges return sub_graph - @staticmethod - def _get_module_with_only_relevant_foreach_idx(original_foreach_or_count_key: int | str, - original_module_key: TFModule, - tf_moudle: TFModule | None) -> TFModule | None: - if tf_moudle is None: - return None - if tf_moudle == original_module_key: - return TFModule(name=tf_moudle.name, path=tf_moudle.path, - nested_tf_module=tf_moudle.nested_tf_module, - foreach_idx=original_foreach_or_count_key) - nested_module = tf_moudle.nested_tf_module - updated_module = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx( - original_foreach_or_count_key, original_module_key, nested_module) - return TFModule(name=tf_moudle.name, path=tf_moudle.path, - nested_tf_module=updated_module, - foreach_idx=tf_moudle.foreach_idx) - @staticmethod def _pop_foreach_attrs(attrs: dict[str, Any]) -> None: attrs.pop(COUNT_STRING, None) diff --git a/checkov/terraform/graph_builder/foreach/module_handler.py b/checkov/terraform/graph_builder/foreach/module_handler.py index 7bcb313f7de..9aafd411d72 100644 --- a/checkov/terraform/graph_builder/foreach/module_handler.py +++ b/checkov/terraform/graph_builder/foreach/module_handler.py @@ -281,10 +281,27 @@ 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] - tf_source_modules = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx( + 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, ) config[RESOLVED_MODULE_ENTRY_NAME][0] = TFDefinitionKey(file_path=original_definition_key.file_path, tf_source_modules=tf_source_modules) + + @staticmethod + def _get_module_with_only_relevant_foreach_idx(original_foreach_or_count_key: int | str, + original_module_key: TFModule, + tf_moudle: TFModule | None) -> TFModule | None: + if tf_moudle is None: + return None + if tf_moudle == original_module_key: + return TFModule(name=tf_moudle.name, path=tf_moudle.path, + nested_tf_module=tf_moudle.nested_tf_module, + foreach_idx=original_foreach_or_count_key) + nested_module = tf_moudle.nested_tf_module + updated_module = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx( + original_foreach_or_count_key, original_module_key, nested_module) + return TFModule(name=tf_moudle.name, path=tf_moudle.path, + nested_tf_module=updated_module, + foreach_idx=tf_moudle.foreach_idx) \ No newline at end of file diff --git a/tests/terraform/graph/variable_rendering/test_foreach_renderer.py b/tests/terraform/graph/variable_rendering/test_foreach_renderer.py index ee250e7202b..3bdd01a9140 100644 --- a/tests/terraform/graph/variable_rendering/test_foreach_renderer.py +++ b/tests/terraform/graph/variable_rendering/test_foreach_renderer.py @@ -433,7 +433,7 @@ def test__get_module_with_only_relevant_foreach_idx(): ) original_key = TFModule(name='2', path='2', foreach_idx='2', nested_tf_module=TFModule(name='3', path='3', foreach_idx='3', nested_tf_module=None)) - result = ForeachAbstractHandler._get_module_with_only_relevant_foreach_idx('test', original_key, module) + result = ForeachModuleHandler._get_module_with_only_relevant_foreach_idx('test', original_key, module) assert result == TFModule(name='1', path='1', foreach_idx='1', nested_tf_module=TFModule(name='2', path='2', foreach_idx='test', nested_tf_module=TFModule(name='3', path='3', foreach_idx='3',