added create_definitions for bicep
@@ -2,13 +2,20 @@ from __future__ import annotations
import logging
import os
+from typing import Optional
from collections.abc import Collection
from pathlib import Path
+from checkov.common.runners.base_runner import filter_ignored_paths
+from checkov.common.parsers.node import DictNode
+from checkov.runner_filter import RunnerFilter
from pycep import BicepParser
from pycep.typing import BicepJson
+BICEP_POSSIBLE_ENDINGS = [".bicep"]
+
+
class Parser:
def __init__(self) -> None:
self.bicep_parser = BicepParser(add_line_numbers=True)
@@ -46,3 +53,38 @@ class Parser:
logging.info(f"[bicep] successfully parsed {len(definitions)} files")
return definitions, definitions_raw, parsing_errors
+
+ def get_folder_definitions(
+ self, root_folder: str, excluded_paths: Optional[list[str]]
+ ) -> tuple[dict[Path, BicepJson], dict[Path, list[tuple[int, str]]], list[str]]:
+ files_list: set[Path] = set()
+ for root, d_names, f_names in os.walk(root_folder):
+ filter_ignored_paths(root, d_names, excluded_paths)
+ filter_ignored_paths(root, f_names, excluded_paths)
+ for file in f_names:
+ file_ending = os.path.splitext(file)[1]
+ if file_ending in BICEP_POSSIBLE_ENDINGS:
+ full_path = os.path.join(root, file)
+ files_list.add(Path(full_path))
+
+ return self.get_files_definitions(files_list)
+
+ def create_definitions(
+ self, root_folder: str,
+ files: Optional["Collection[Path]"] = None,
+ runner_filter: RunnerFilter = RunnerFilter(),
+ ) -> tuple[dict[str, DictNode], dict[str, list[tuple[int, str]]]]:
+ definitions = {}
+ definitions_raw = {}
+ parsing_errors = []
+ if files:
+ definitions, definitions_raw, parsing_errors = self.get_files_definitions(file_paths=files)
+
+ if root_folder:
+ definitions, definitions_raw, parsing_errors = self.get_folder_definitions(root_folder,
+ runner_filter.excluded_paths)
+
+ if parsing_errors:
+ logging.warning(f"[bicep] found errors while parsing definitions: {parsing_errors}")
+
+ return definitions, definitions_raw
