Skip to content

Commit

Permalink
fix file path
Browse files Browse the repository at this point in the history
  • Loading branch information
omriyoffe-panw committed Nov 27, 2024
1 parent 6f66d38 commit 0886508
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions checkov/arm/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
Expand All @@ -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,
)
Expand Down Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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,
Expand Down

0 comments on commit 0886508

Please sign in to comment.