From d6a6c32f8d2679d971142261782cb41a56ce285c Mon Sep 17 00:00:00 2001 From: Vojtech Polasek Date: Fri, 8 Mar 2024 14:41:11 +0100 Subject: [PATCH 1/2] store the "policy" key in complied control files --- ssg/controls.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ssg/controls.py b/ssg/controls.py index 2084f3ea12f..fee298ac4f0 100644 --- a/ssg/controls.py +++ b/ssg/controls.py @@ -222,6 +222,7 @@ def __init__(self, filepath, env_yaml=None): def represent_as_dict(self): data = dict() data["id"] = self.id + data["policy"] = self.policy data["title"] = self.title data["source"] = self.source data["definition_location"] = self.filepath @@ -337,6 +338,7 @@ def load(self): if controls_dir: self.controls_dir = os.path.join(os.path.dirname(self.filepath), controls_dir) self.id = ssg.utils.required_key(yaml_contents, "id") + self.policy = ssg.utils.required_key(yaml_contents, "policy") self.title = ssg.utils.required_key(yaml_contents, "title") self.source = yaml_contents.get("source", "") self.reference_type = yaml_contents.get("reference_type", None) From d7918d2a343df14ae164b30810e422d36cf4ed0c Mon Sep 17 00:00:00 2001 From: Vojtech Polasek Date: Fri, 8 Mar 2024 14:42:21 +0100 Subject: [PATCH 2/2] gen_rendered_policiy_index.py: read compiled control files, not the original ones Original files are not needed. They can contain Jinja and reading Jinja properly would make this script complicated while not bringing any real value. The script needs to read only one key from the file which is not going to be enclosed in Jinja macros in foreseeable future. --- utils/gen_rendered_policies_index.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/utils/gen_rendered_policies_index.py b/utils/gen_rendered_policies_index.py index c7a621bde25..fb0154b6271 100755 --- a/utils/gen_rendered_policies_index.py +++ b/utils/gen_rendered_policies_index.py @@ -23,18 +23,17 @@ def get_rendered_policies_ids(rendered_policies_dir): return policy_ids -def get_policy_names(ssg_root): +def get_policy_names(ssg_root, products): policy_names = dict() - p = pathlib.Path(ssg_root) - for control_file in p.glob("controls/*.yml"): - # only process files, ignore controls directories - if not os.path.isfile(control_file): - continue - policy_id = pathlib.Path(control_file).stem - with open(control_file, "r") as f: - policy_yaml = yaml.full_load(f) - policy_name = policy_yaml["policy"] - policy_names[policy_id] = policy_name + for product in products: + p = pathlib.Path(ssg_root, "build", product.id) + for control_file in p.glob("controls/*.yml"): + policy_id = pathlib.Path(control_file).stem + if policy_id not in policy_names: + with open(control_file, "r") as f: + policy_yaml = yaml.full_load(f) + policy_name = policy_yaml["policy"] + policy_names[policy_id] = policy_name return policy_names @@ -62,7 +61,7 @@ def get_products(ssg_root): def get_data(ssg_root): products = get_products(ssg_root) - policy_names = get_policy_names(ssg_root) + policy_names = get_policy_names(ssg_root, products) data = {"products": products, "policy_names": policy_names} return data