From 64354c537510873366bacee25d25e17eb74df8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20T=C3=BD=C4=8D?= Date: Wed, 27 Jul 2022 18:00:35 +0200 Subject: [PATCH] Refactor autotailor for future exension --- utils/autotailor | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/utils/autotailor b/utils/autotailor index 5bf244c4b4d..205a9122919 100755 --- a/utils/autotailor +++ b/utils/autotailor @@ -51,6 +51,11 @@ class Tailoring: self.rules_to_select = [] self.rules_to_unselect = [] + self.refine_rule = [] + self.refine_value = [] + + self.set_value = [] + def _full_id(self, string, el_type): if is_valid_xccdf_id(string): return string @@ -70,6 +75,23 @@ class Tailoring: def add_value_change(self, varname, value): self.value_changes.append((varname, value)) + def _add_rule_select_operations(self, container_element): + for rule_id in self.rules_to_select: + change = ET.SubElement(container_element, "xccdf-1.2:select") + change.set("idref", self._full_rule_id(rule_id)) + change.set("selected", "true") + + for rule_id in self.rules_to_unselect: + change = ET.SubElement(container_element, "xccdf-1.2:select") + change.set("idref", self._full_rule_id(rule_id)) + change.set("selected", "false") + + def _add_value_selections(self, container_element): + for varname, value in self.value_changes: + change = ET.SubElement(container_element, "xccdf-1.2:set-value") + change.set("idref", self._full_var_id(varname)) + change.text = str(value) + def to_xml(self, location=None): root = ET.Element("xccdf-1.2:Tailoring") root.set("xmlns:xccdf-1.2", ALL_NS["xccdf-1.2"]) @@ -95,20 +117,8 @@ class Tailoring: title.set("override", "false") title.text = self.profile_title - for rule_id in self.rules_to_select: - change = ET.SubElement(profile, "xccdf-1.2:select") - change.set("idref", self._full_rule_id(rule_id)) - change.set("selected", "true") - - for rule_id in self.rules_to_unselect: - change = ET.SubElement(profile, "xccdf-1.2:select") - change.set("idref", self._full_rule_id(rule_id)) - change.set("selected", "false") - - for varname, value in self.value_changes: - change = ET.SubElement(profile, "xccdf-1.2:set-value") - change.set("idref", self._full_var_id(varname)) - change.text = str(value) + self._add_rule_select_operations(container_element)(profile) + self._add_value_selections(profile) if location == "-": location = sys.stdout.buffer