Skip to content

Commit

Permalink
OCP Update variable filter to consider go_template
Browse files Browse the repository at this point in the history
Update the variable filter to find if a rule is using go-template, if so find any var being used, add them to var list for that rule
  • Loading branch information
Vincent056 committed Apr 29, 2024
1 parent 59013f6 commit b6ef6c2
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions 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,21 +114,32 @@ def get_linked_xccdf(loader, xccdftree, args):
return oval_linker, xccdftree


def get_variables_from_path(rule, path, var_ids):
for path_el in rule.findall(".//{%s}%s" % (ssg.constants.XCCDF12_NS, path)):
var_ids.add(
path_el.get("idref").replace("xccdf_org.ssgproject.content_value_", "")
)


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():
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 = {}
go_templating_pattern = re.compile(r"{{(.*?)}}")
go_templating_var_pattern = re.compile(r"\.([a-zA-Z0-9_]+)")
for rule in rules:
var_ids = set()
check_export_els = rule.findall(".//{%s}check-export" % ssg.constants.XCCDF12_NS)
for check_export_el in check_export_els:
var_ids.add(
check_export_el.get("value-id").replace("xccdf_org.ssgproject.content_value_", "")
)
sub_els = rule.findall(".//{%s}sub" % ssg.constants.XCCDF12_NS)
for sub_el in sub_els:
var_ids.add(
sub_el.get("idref").replace("xccdf_org.ssgproject.content_value_", "")
)
get_variables_from_path(rule, "check-export", var_ids)
get_variables_from_path(rule, "sub", var_ids)
get_variables_from_go_templating(rule, var_ids)
out_var_ids[
rule.get("id").replace("xccdf_org.ssgproject.content_rule_", "")
] = var_ids
Expand Down

0 comments on commit b6ef6c2

Please sign in to comment.