1 | #!/usr/bin/python3 |
||
2 | |||
3 | import argparse |
||
4 | import json |
||
5 | import os |
||
6 | import re |
||
7 | import sys |
||
8 | |||
9 | try: |
||
10 | from ssg.build_cpe import ProductCPEs |
||
11 | import ssg.build_profile |
||
12 | import ssg.controls |
||
13 | import ssg.environment |
||
14 | import ssg.products |
||
15 | except (ModuleNotFoundError, ImportError): |
||
16 | sys.stderr.write("Unable to load ssg python modules.\n") |
||
17 | sys.stderr.write("Hint: run source ./.pyenv.sh\n") |
||
18 | exit(6) |
||
19 | |||
20 | SSG_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) |
||
21 | RULES_JSON = os.path.join(SSG_ROOT, "build", "rule_dirs.json") |
||
22 | BUILD_CONFIG = os.path.join(SSG_ROOT, "build", "build_config.yml") |
||
23 | CONTROLS_DIR = os.path.join(SSG_ROOT, "controls") |
||
24 | |||
25 | |||
26 | View Code Duplication | def parse_args() -> argparse.Namespace: |
|
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
27 | parser = argparse.ArgumentParser(description="Ensures that Control and rule files are in sync") |
||
28 | parser.add_argument("-j", "--json", type=str, action="store", |
||
29 | default=RULES_JSON, help="File to read " |
||
30 | "json output of rule_dir_json from (defaults to " |
||
31 | f"{RULES_JSON}") |
||
32 | parser.add_argument("-c", "--build-config-yaml", default=BUILD_CONFIG, |
||
33 | help="YAML file with information about the build configuration. " |
||
34 | f"Defaults to {BUILD_CONFIG}") |
||
35 | parser.add_argument("--controls", default=CONTROLS_DIR, |
||
36 | help="Directory that contains control files with policy controls.") |
||
37 | parser.add_argument("product", type=str, help="Product to check has required references") |
||
38 | parser.add_argument("control", type=str, help="Control to iterate over") |
||
39 | parser.add_argument("reference", type=str, help="Required reference system to check for") |
||
40 | return parser.parse_args() |
||
41 | |||
42 | |||
43 | def check_product(product: str) -> None: |
||
44 | linux_products, other_products = ssg.products.get_all(SSG_ROOT) |
||
45 | all_products = linux_products.union(other_products) |
||
46 | if product not in all_products: |
||
47 | sys.stderr.write(f"{product} is not a valid product\n") |
||
48 | exit(2) |
||
49 | return None |
||
50 | |||
51 | |||
52 | def check_files(json_path: str, controls_dir: str) -> None: |
||
53 | if not os.path.exists(json_path): |
||
54 | sys.stderr.write(f"JSON at {json_path} was not found.\n") |
||
55 | sys.stderr.write("Run ./utils/rule_dir_json.py to create.") |
||
56 | exit(3) |
||
57 | if not os.path.exists(controls_dir): |
||
58 | sys.stderr.write(f"Controls directory {controls_dir} was not found.\n") |
||
59 | exit(4) |
||
60 | if not os.path.isdir(controls_dir): |
||
61 | sys.stderr.write(f"Given controls directory {controls_dir} is not a directory.\n") |
||
62 | exit(5) |
||
63 | |||
64 | |||
65 | def get_rule_object(all_rules, args, control_rule, env_yaml) -> ssg.build_yaml.Rule: |
||
66 | rule_dict = all_rules.get(control_rule) |
||
67 | rule_path = os.path.join(rule_dict['dir'], 'rule.yml') |
||
68 | rule_obj = ssg.build_yaml.Rule.from_yaml(rule_path, env_yaml=env_yaml) |
||
69 | rule_obj.normalize(args.product) |
||
70 | return rule_obj |
||
71 | |||
72 | |||
73 | def get_controls_env(args): |
||
74 | product_base = os.path.join(SSG_ROOT, "products", args.product) |
||
75 | product_yaml = os.path.join(product_base, "product.yml") |
||
76 | env_yaml = ssg.environment.open_environment( |
||
77 | args.build_config_yaml, product_yaml, os.path.join(SSG_ROOT, "product_properties")) |
||
78 | controls_manager = ssg.controls.ControlsManager(args.controls, env_yaml) |
||
79 | controls_manager.load() |
||
80 | return controls_manager, env_yaml |
||
81 | |||
82 | |||
83 | def check_cis(reference: str, control_id: str) -> bool: |
||
84 | return reference == 'cis' and not re.match(r"\d(\.\d+){0,3}", control_id) |
||
85 | |||
86 | |||
87 | def should_rule_be_checked(reference: str, control_id: str) -> bool: |
||
88 | if check_cis(reference, control_id): |
||
89 | print(f'Skipping {control_id} as it does not match a CIS id.') |
||
90 | return False |
||
91 | return True |
||
92 | |||
93 | |||
94 | def does_rule_exist(all_rules: dict, control_rule: str) -> bool: |
||
95 | if all_rules.get(control_rule) is None: |
||
96 | print(f'{control_rule} was not found in the project.') |
||
97 | return False |
||
98 | return True |
||
99 | |||
100 | |||
101 | def check_reference(reference: str, rule_object: ssg.build_yaml.Rule, control_id: str, |
||
102 | product: str) -> bool: |
||
103 | if reference in rule_object.references and control_id \ |
||
104 | not in rule_object.references[reference].split(','): |
||
105 | print(f"{rule_object.id_} {reference}@{product} " |
||
106 | f"{rule_object.references[reference]} does not match the control id " |
||
107 | f"{control_id}") |
||
108 | return False |
||
109 | return True |
||
110 | |||
111 | |||
112 | def downgrade_bool(current: bool, target: bool) -> bool: |
||
113 | if not current or not target: |
||
114 | return False |
||
115 | return True |
||
116 | |||
117 | |||
118 | def main(): |
||
119 | args = parse_args() |
||
120 | |||
121 | check_product(args.product) |
||
122 | |||
123 | rule_dir_json = open(args.json, 'r') |
||
124 | all_rules = json.load(rule_dir_json) |
||
125 | |||
126 | controls_manager, env_yaml = get_controls_env(args) |
||
127 | |||
128 | ok = True |
||
129 | for control in controls_manager.get_all_controls(args.control): |
||
130 | for control_rule in control.selected: |
||
131 | control_id = str(control.id) |
||
132 | exists = does_rule_exist(all_rules, control_rule) |
||
133 | ok = downgrade_bool(ok, exists) |
||
134 | if should_rule_be_checked(args.reference, control_id): |
||
135 | rule_object = get_rule_object(all_rules, args, control_rule, env_yaml) |
||
136 | check_ok = check_reference(args.reference, rule_object, control_id, args.product) |
||
137 | ok = downgrade_bool(ok, check_ok) |
||
138 | |||
139 | if not ok: |
||
140 | exit(1) |
||
141 | |||
142 | |||
143 | if __name__ == '__main__': |
||
144 | main() |
||
145 |