From aa6e248dfa68f1fc46f25e7e0cc4860fd2ae431f Mon Sep 17 00:00:00 2001 From: omriyoffe-panw Date: Wed, 18 Dec 2024 14:54:09 +0200 Subject: [PATCH] feat(serverless): Serverless graph integration (#6911) * add definitions functions for platform --- checkov/serverless/graph_manager.py | 2 +- checkov/serverless/runner.py | 3 +++ checkov/serverless/utils.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/checkov/serverless/graph_manager.py b/checkov/serverless/graph_manager.py index e3e20fc8581..0b0ac63f947 100644 --- a/checkov/serverless/graph_manager.py +++ b/checkov/serverless/graph_manager.py @@ -12,7 +12,7 @@ class ServerlessGraphManager(GraphManager[ServerlessLocalGraph, "dict[str, dict[str, Any]]"]): - def __init__(self, db_connector: LibraryGraphConnector, source: str = GraphSource.ARM) -> None: + def __init__(self, db_connector: LibraryGraphConnector, source: str = GraphSource.SERVERLESS) -> None: super().__init__(db_connector=db_connector, parser=None, source=source) def build_graph_from_source_directory( diff --git a/checkov/serverless/runner.py b/checkov/serverless/runner.py index b50e70318c3..fa0ba938f11 100644 --- a/checkov/serverless/runner.py +++ b/checkov/serverless/runner.py @@ -355,3 +355,6 @@ def cfn_resources_checks(self, def extract_file_path_from_abs_path(self, path: Path) -> str: return f"{os.path.sep}{os.path.relpath(path, self.root_folder)}" + + def set_definitions_raw(self, definitions_raw: dict[str, list[tuple[int, str]]]) -> None: + self.definitions_raw = definitions_raw diff --git a/checkov/serverless/utils.py b/checkov/serverless/utils.py index 17e1dbfdd83..f79a5bf2cfe 100644 --- a/checkov/serverless/utils.py +++ b/checkov/serverless/utils.py @@ -1,10 +1,13 @@ from __future__ import annotations import os +from collections.abc import Collection from enum import Enum from typing import Callable, Any +from pathlib import Path from checkov.common.parallelizer.parallel_runner import parallel_runner +from checkov.runner_filter import RunnerFilter from checkov.serverless.parsers.parser import parse from checkov.common.runners.base_runner import filter_ignored_paths @@ -28,6 +31,22 @@ def __str__(self) -> str: return self.value +def create_definitions( + root_folder: str, + files: Collection[Path] | None = None, + runner_filter: RunnerFilter | None = None, +) -> tuple[dict[str, dict[str, Any]], dict[str, list[tuple[int, str]]]]: + definitions: dict[str, dict[str, Any]] = {} + definitions_raw: dict[str, list[tuple[int, str]]] = {} + runner_filter = runner_filter or RunnerFilter() + + if root_folder: + file_paths = get_scannable_file_paths(root_folder, runner_filter.excluded_paths) + definitions, definitions_raw = get_files_definitions(files=file_paths) + + return definitions, definitions_raw + + def get_scannable_file_paths(root_folder: str | None = None, excluded_paths: list[str] | None = None) -> list[str]: files_list: list[str] = []