Skip to content

Commit 4a538ba

Browse files
committed
Change template.py of grub2_bootloader_argument to support numeric comparison
- audit_backlog_limit needs >= comparison - Test scenarios now use TEST_CORRECT_VALUE/TEST_WRONG_VALUE instead of hardcoded strings.
1 parent c32f292 commit 4a538ba

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed
Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,56 @@
11
import ssg.utils
22

3+
VALID_OPERATIONS = {
4+
"pattern match",
5+
"greater than or equal",
6+
}
7+
38

49
def preprocess(data, lang):
10+
# arg_value and arg_variable are mutually exclusive
511
if 'arg_value' in data and 'arg_variable' in data:
612
raise RuntimeError(
713
"ERROR: The template should not set both 'arg_value' and 'arg_variable'.\n"
814
"arg_name: {0}\n"
915
"arg_variable: {1}".format(data['arg_value'], data['arg_variable']))
1016

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:
1242
data["arg_name_value"] = data["arg_name"]
1343
else:
14-
if "arg_value" in data:
1544
data["arg_name_value"] = data["arg_name"] + "=" + data["arg_value"]
16-
else:
17-
data["arg_name_value"] = data["arg_name"]
1845

1946
if 'is_substring' not in data:
2047
data["is_substring"] = "false"
2148

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
2855
data["sanitized_arg_name"] = ssg.utils.escape_id(data["arg_name"])
2956
return data

0 commit comments

Comments
 (0)