|
1 | 1 | import ssg.utils |
2 | 2 |
|
| 3 | +VALID_OPERATIONS = { |
| 4 | + "pattern match", |
| 5 | + "greater than or equal", |
| 6 | +} |
| 7 | + |
3 | 8 |
|
4 | 9 | def preprocess(data, lang): |
| 10 | + # arg_value and arg_variable are mutually exclusive |
5 | 11 | if 'arg_value' in data and 'arg_variable' in data: |
6 | 12 | raise RuntimeError( |
7 | 13 | "ERROR: The template should not set both 'arg_value' and 'arg_variable'.\n" |
8 | 14 | "arg_name: {0}\n" |
9 | 15 | "arg_variable: {1}".format(data['arg_value'], data['arg_variable'])) |
10 | 16 |
|
11 | | - if 'arg_variable' in data: |
| 17 | + # Fallback to pattern match operation if not set |
| 18 | + if "operation" not in data: |
| 19 | + data["operation"] = "pattern match" |
| 20 | + |
| 21 | + # Placeholder values substituted into tests/*.sh scenarios via |
| 22 | + # TEST_CORRECT_VALUE / TEST_WRONG_VALUE (e.g. grub2_bootloader_argument_remediation calls) |
| 23 | + match data["operation"]: |
| 24 | + case "pattern match": |
| 25 | + data["test_correct_value"] = "correct_value" |
| 26 | + data["test_wrong_value"] = "wrong_value" |
| 27 | + case "greater than or equal": |
| 28 | + data["test_correct_value"] = "200" |
| 29 | + data["test_wrong_value"] = "199" |
| 30 | + case _: |
| 31 | + raise RuntimeError( |
| 32 | + f"ERROR: Invalid operation '{data['operation']}' for rule " |
| 33 | + f"'{data['_rule_id']}'. " |
| 34 | + f"Must be one of: {sorted(VALID_OPERATIONS)}" |
| 35 | + ) |
| 36 | + |
| 37 | + # Build ARG_NAME_VALUE ("name=value") used in oval.template comments/metadata, |
| 38 | + # bash.template remediation, and ansible.template remediation. |
| 39 | + # When arg_variable is set the value comes from an XCCDF variable at eval time. |
| 40 | + |
| 41 | + if 'arg_variable' in data or "arg_value" not in data: |
12 | 42 | data["arg_name_value"] = data["arg_name"] |
13 | 43 | else: |
14 | | - if "arg_value" in data: |
15 | 44 | data["arg_name_value"] = data["arg_name"] + "=" + data["arg_value"] |
16 | | - else: |
17 | | - data["arg_name_value"] = data["arg_name"] |
18 | 45 |
|
19 | 46 | if 'is_substring' not in data: |
20 | 47 | data["is_substring"] = "false" |
21 | 48 |
|
22 | | - if lang == "oval": |
23 | | - # escape dot, this is used in oval regex |
24 | | - data["escaped_arg_name_value"] = data["arg_name_value"].replace(".", "\\.") |
25 | | - data["escaped_arg_name"] = data["arg_name"].replace(".", "\\.") |
26 | | - # replace . with _, this is used in test / object / state ids |
27 | | - |
| 49 | + # OVAL-specific: escape dots for regex patterns in oval.template |
| 50 | + # (ESCAPED_ARG_NAME_VALUE in state subexpressions, ESCAPED_ARG_NAME in object patterns) |
| 51 | + data["escaped_arg_name_value"] = data["arg_name_value"].replace(".", "\\.") |
| 52 | + data["escaped_arg_name"] = data["arg_name"].replace(".", "\\.") |
| 53 | + # SANITIZED_ARG_NAME: used as component of OVAL IDs (test_grub2_<name>_*, |
| 54 | + # obj_grub2_<name>_*, state_grub2_<name>_*) and bash bootc .toml filenames |
28 | 55 | data["sanitized_arg_name"] = ssg.utils.escape_id(data["arg_name"]) |
29 | 56 | return data |
0 commit comments