Skip to content

Commit

Permalink
Apply suggestions from review in #11916.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mab879 committed Apr 30, 2024
1 parent 7a72e25 commit 0f5cc9d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
5 changes: 2 additions & 3 deletions docs/manual/developer/05_tools_and_utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,14 @@ Example

## Generating Controls From a Reference
When converting profile to use a control file this script can be helpful in creating the skeleton control.
The output of this script will also need to be adjusted to add things title's to the controls.
Default output path is `build/reference_control.yml`.
The output of this script will need to be adjusted to add other keys such as title or description to the controls.
This script does require that `./utils/rule_dir_json.py` be run before this script is used.
See `./utils/build_control_from_reference.py --help` for the full set options the script provides.


Example
```bash
$ ./utils/build_control_from_reference.py --product rhel10 --reference ospp
$ ./utils/build_control_from_reference.py --product rhel10 --reference ospp --output controls/ospp.yml
```

## Generating login banner regular expressions
Expand Down
23 changes: 11 additions & 12 deletions utils/build_control_from_reference.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
#!/usr/bin/python3

import argparse
from collections import defaultdict
import os
import json
import sys
from typing import List, Dict
import yaml


import ssg.environment
import ssg.yaml

SSG_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
BUILD_ROOT = os.path.join(SSG_ROOT, "build")
RULES_JSON = os.path.join(BUILD_ROOT, "rule_dirs.json")
BUILD_CONFIG = os.path.join(BUILD_ROOT, "build_config.yml")
OUTPUT_PATH = os.path.join(BUILD_ROOT, "reference_control.yml")


def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Given a reference this script will create an control file.")
description="Given a reference this script will create a control file.")
parser.add_argument("-j", "--json", type=str,
help=f"Path to the rule_dirs.json file. (Defaults to {RULES_JSON})",
default=RULES_JSON)
parser.add_argument("-p", "--product", type=str, help="Product to build the control with",
required=True)
parser.add_argument("-r", "--root", type=str,
help=f"Path to the root of the project. (Defaults to {SSG_ROOT}.",
help=f"Path to the root of the project. (Defaults to {SSG_ROOT}).",
default=SSG_ROOT)
parser.add_argument("-ref", "--reference", type=str,
help="Reference to use for the profile. Example: ospp", required=True)
parser.add_argument("-c", "--build-config-yaml", default=BUILD_CONFIG,
help=f"YAML file with information about the build configuration."
f"Defaults to (BUILD_CONFIG)")
parser.add_argument("-o", "--output", type=str,
help=f"Path to output the control file. (Defaults to {OUTPUT_PATH})",
default=OUTPUT_PATH)
help=f"YAML file with information about the build configuration. "
f"Defaults to {BUILD_CONFIG}")
parser.add_argument("-o", "--output", type=str, required=True,
help=f"Path to output the control file.")
return parser.parse_args()


Expand All @@ -47,6 +47,7 @@ def _get_rule_dirs(json_path: str) -> Dict[str, str]:
def _check_rule_dirs_path(json: str):
if not os.path.exists(json):
print(f"Path {json} does not exist.", file=sys.stderr)
print("Hint: run ./utils/rule_dirs.py first.", file=sys.stderr)
raise SystemExit(1)


Expand All @@ -60,7 +61,7 @@ def _get_env_yaml(root: str, product: str, build_config_yaml: str) -> str:

def _get_id_mapping(env_yaml, reference, json_path: str) -> Dict:
rule_dir_json: Dict = _get_rule_dirs(json_path)
id_mapping: Dict[str, list[str]] = {}
id_mapping: Dict[str, list[str]] = defaultdict(list)
for rule_id, rule_obj in rule_dir_json.items():
rule_yaml = os.path.join(rule_obj["dir"], "rule.yml")
rule = ssg.yaml.open_and_macro_expand(rule_yaml, env_yaml)
Expand All @@ -71,8 +72,6 @@ def _get_id_mapping(env_yaml, reference, json_path: str) -> Dict:
continue
ids: List[str] = ref_id.split(",")
for _id in ids:
if _id not in id_mapping:
id_mapping[_id] = list()
id_mapping[_id].append(rule_id)
return id_mapping

Expand All @@ -90,7 +89,7 @@ def main() -> int:
control = dict()
control["id"] = _id
control["levels"] = ["base"]
control["rules"] = rules
control["rules"] = sorted(rules)
control["status"] = "automated"
output["controls"].append(control)

Expand Down

0 comments on commit 0f5cc9d

Please sign in to comment.