Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCP Update variable filter to consider go_template #11906

Merged
merged 1 commit into from
May 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion build-scripts/build_xccdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import os.path
from collections import namedtuple

import re

import ssg.build_yaml
import ssg.utils
Expand Down Expand Up @@ -114,6 +114,15 @@ def get_linked_xccdf(loader, xccdftree, args):
return oval_linker, xccdftree


def get_variables_from_go_templating(rule, var_ids):
go_templating_pattern = re.compile(r"{{(.*?)}}")
go_templating_var_pattern = re.compile(r"\.([a-zA-Z0-9_]+)")
for ele in rule.itertext():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can go_template be used only in the text part of a rule or can it be present in an XML element attribute?

Copy link
Contributor Author

@Vincent056 Vincent056 May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they will present in the text part of the rule as well as in the remediations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think fix is also part of the rule

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I was curious. Yes, the fix element is a sub-element of the Rule element.

for match in go_templating_pattern.finditer(ele):
for var in go_templating_var_pattern.finditer(match.group(1)):
var_ids.add(var.group(1))


def get_rules_with_variables(xccdftree):
rules = xccdftree.findall(".//{%s}Rule" % ssg.constants.XCCDF12_NS)
out_var_ids = {}
Expand All @@ -129,6 +138,7 @@ def get_rules_with_variables(xccdftree):
var_ids.add(
sub_el.get("idref").replace("xccdf_org.ssgproject.content_value_", "")
)
get_variables_from_go_templating(rule, var_ids)
out_var_ids[
rule.get("id").replace("xccdf_org.ssgproject.content_rule_", "")
] = var_ids
Expand Down