Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(arm): Fix arm graph breadcrumbs #6869

Merged
merged 6 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions checkov/arm/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
from collections.abc import Iterable
from pathlib import Path
from typing import TYPE_CHECKING, Any, cast
from typing_extensions import TypeAlias # noqa[TC002]

Expand All @@ -11,11 +12,12 @@
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
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
from checkov.common.output.extra_resource import ExtraResource
from checkov.common.output.graph_record import GraphRecord
from checkov.common.output.record import Record
from checkov.common.output.report import Report
from checkov.common.bridgecrew.check_type import CheckType
Expand Down Expand Up @@ -263,7 +265,7 @@ def add_graph_check_results(self, report: Report, runner_filter: RunnerFilter) -
for check, check_results in graph_checks_results.items():
for check_result in check_results:
entity = check_result["entity"]
entity_file_path: str = entity[CustomAttributes.FILE_PATH]
entity_file_path = entity[CustomAttributes.FILE_PATH]
start_line = entity[START_LINE] - 1
end_line = entity[END_LINE] - 1

Expand All @@ -272,7 +274,7 @@ def add_graph_check_results(self, report: Report, runner_filter: RunnerFilter) -
check=check,
check_result=check_result,
code_block=self.definitions_raw[entity_file_path][start_line:end_line],
file_path=entity_file_path,
file_path=self.extract_file_path_from_abs_path(clean_file_path(entity_file_path)),
file_abs_path=os.path.abspath(entity_file_path),
file_line_range=[start_line - 1, end_line - 1],
resource_id=entity[CustomAttributes.ID],
Expand Down Expand Up @@ -304,5 +306,12 @@ def build_record(
file_abs_path=file_abs_path,
severity=check.severity,
)
if self.breadcrumbs:
breadcrumb = self.breadcrumbs.get(record.file_path, {}).get(record.resource)
if breadcrumb:
record = GraphRecord(record, breadcrumb)
record.set_guideline(check.guideline)
report.add_record(record=record)

def extract_file_path_from_abs_path(self, path: Path) -> str:
return f"/{os.path.relpath(path, self.root_folder)}"
omriyoffe-panw marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions checkov/arm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,8 @@ def extract_resource_name_from_reference_func(reference: str) -> str:

def clean_string(input: str) -> str:
return input.replace("'", '').replace(" ", "")

def clean_file_path(file_path: Path) -> Path:
path_parts = [part for part in file_path.parts if part not in (".", "..")]

return Path(*path_parts)
Loading