From 0886508b1cc952c93d555f7b02918057a6cb5c2f Mon Sep 17 00:00:00 2001 From: Omri Yoffe Date: Wed, 27 Nov 2024 12:06:27 +0200 Subject: [PATCH] fix file path --- checkov/arm/runner.py | 67 ++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 32 deletions(-) diff --git a/checkov/arm/runner.py b/checkov/arm/runner.py index ee4187d6961..bd5648108fa 100644 --- a/checkov/arm/runner.py +++ b/checkov/arm/runner.py @@ -12,7 +12,8 @@ from checkov.arm.graph_builder.local_graph import ArmLocalGraph from checkov.arm.graph_manager import ArmGraphManager from checkov.arm.registry import arm_resource_registry, arm_parameter_registry -from checkov.arm.utils import get_scannable_file_paths, get_files_definitions, ARM_POSSIBLE_ENDINGS, ArmElements, clean_file_path +from checkov.arm.utils import get_scannable_file_paths, get_files_definitions, ARM_POSSIBLE_ENDINGS, ArmElements, \ + clean_file_path from checkov.common.checks_infra.registry import get_graph_checks_registry from checkov.common.graph.graph_builder import CustomAttributes from checkov.common.graph.graph_builder.consts import GraphSource @@ -41,12 +42,12 @@ class Runner(BaseRunner[_ArmDefinitions, _ArmContext, ArmGraphManager]): check_type = CheckType.ARM # noqa: CCE003 # a static attribute def __init__( - self, - db_connector: LibraryGraphConnector | None = None, - source: str = GraphSource.ARM, - graph_class: type[ArmLocalGraph] = ArmLocalGraph, - graph_manager: ArmGraphManager | None = None, - external_registries: list[BaseRegistry] | None = None, + self, + db_connector: LibraryGraphConnector | None = None, + source: str = GraphSource.ARM, + graph_class: type[ArmLocalGraph] = ArmLocalGraph, + graph_manager: ArmGraphManager | None = None, + external_registries: list[BaseRegistry] | None = None, ) -> None: super().__init__(file_extensions=ARM_POSSIBLE_ENDINGS) @@ -65,18 +66,20 @@ def __init__( self.root_folder: "str | None" = None def run( - self, - root_folder: str | None = None, - external_checks_dir: list[str] | None = None, - files: list[str] | None = None, - runner_filter: RunnerFilter | None = None, - collect_skip_comments: bool = True, + self, + root_folder: str | None = None, + external_checks_dir: list[str] | None = None, + files: list[str] | None = None, + runner_filter: RunnerFilter | None = None, + collect_skip_comments: bool = True, ) -> Report | list[Report]: runner_filter = runner_filter or RunnerFilter() if not runner_filter.show_progress_bar: self.pbar.turn_off_progress_bar() report = Report(self.check_type) + self.root_folder = root_folder + if not self.context or not self.definitions: files_list: "Iterable[str]" = [] if external_checks_dir: @@ -89,10 +92,9 @@ def run( if files: files_list = files.copy() - if root_folder: - self.root_folder = root_folder - - files_list = get_scannable_file_paths(root_folder=root_folder, excluded_paths=runner_filter.excluded_paths) + if self.root_folder: + files_list = get_scannable_file_paths(root_folder=root_folder, + excluded_paths=runner_filter.excluded_paths) self.definitions, self.definitions_raw, parsing_errors = get_files_definitions(files_list) self.context = build_definitions_context(definitions=self.definitions, definitions_raw=self.definitions_raw) @@ -129,7 +131,7 @@ def add_python_check_results(self, report: Report, runner_filter: RunnerFilter, for arm_file in self.definitions.keys(): self.pbar.set_additional_data({"Current File Scanned": os.path.relpath(arm_file, root_folder)}) - file_abs_path = os.path.abspath(arm_file) + file_abs_path = Path(arm_file).absolute() if isinstance(self.definitions[arm_file], dict): arm_context_parser = ContextParser(arm_file, self.definitions[arm_file], self.definitions_raw[arm_file]) @@ -194,7 +196,7 @@ def add_python_check_results(self, report: Report, runner_filter: RunnerFilter, resource=resource_id, evaluations=variable_evaluations, check_class=check.__class__.__module__, - file_abs_path=file_abs_path, + file_abs_path=str(file_abs_path), severity=check.severity, ) record.set_guideline(check.guideline) @@ -203,7 +205,7 @@ def add_python_check_results(self, report: Report, runner_filter: RunnerFilter, # resources without checks, but not existing ones report.extra_resources.add( ExtraResource( - file_abs_path=file_abs_path, + file_abs_path=str(file_abs_path), file_path=self.extract_file_path_from_abs_path(cleaned_path), resource=resource_id, ) @@ -241,7 +243,7 @@ def add_python_check_results(self, report: Report, runner_filter: RunnerFilter, check_result=check_result, code_block=censored_code_lines, file_path=self.extract_file_path_from_abs_path(cleaned_path), - file_abs_path=file_abs_path, + file_abs_path=str(file_abs_path), file_line_range=entity_lines_range, resource_id=resource_id, evaluations=variable_evaluations, @@ -259,6 +261,7 @@ def add_graph_check_results(self, report: Report, runner_filter: RunnerFilter) - for check_result in check_results: entity = check_result["entity"] entity_file_path = entity[CustomAttributes.FILE_PATH] + file_abs_path = Path(entity_file_path).absolute() start_line = entity[START_LINE] - 1 end_line = entity[END_LINE] - 1 @@ -268,22 +271,22 @@ def add_graph_check_results(self, report: Report, runner_filter: RunnerFilter) - check_result=check_result, code_block=self.definitions_raw[entity_file_path][start_line:end_line], file_path=self.extract_file_path_from_abs_path(clean_file_path(Path(entity_file_path))), - file_abs_path=os.path.abspath(entity_file_path), + file_abs_path=str(file_abs_path), file_line_range=[start_line - 1, end_line - 1], resource_id=entity[CustomAttributes.ID], ) def build_record( - self, - report: Report, - check: BaseCheck | BaseGraphCheck, - check_result: _CheckResult, - code_block: list[tuple[int, str]], - file_path: str, - file_abs_path: str, - file_line_range: list[int], - resource_id: str, - evaluations: dict[str, Any] | None = None, + self, + report: Report, + check: BaseCheck | BaseGraphCheck, + check_result: _CheckResult, + code_block: list[tuple[int, str]], + file_path: str, + file_abs_path: str, + file_line_range: list[int], + resource_id: str, + evaluations: dict[str, Any] | None = None, ) -> None: record = Record( check_id=check.id,