diff --git a/ssg/entities/profile_base.py b/ssg/entities/profile_base.py index dde548dd7af..f36fcb9343d 100644 --- a/ssg/entities/profile_base.py +++ b/ssg/entities/profile_base.py @@ -89,9 +89,28 @@ def _add_selects(self, element, selections, prefix, selected): select.set("selected", selected) element.append(select) + def _setup_defined_rules(self, element): + for value_id, selector in self.variables.items(): + refine_value = ET.Element("{%s}refine-value" % XCCDF12_NS) + refine_value.set("idref", OSCAP_VALUE + value_id) + refine_value.set("selector", selector) + element.append(refine_value) + for refined_rule, refinement_list in self.refine_rules.items(): + refine_rule = ET.Element("{%s}refine-rule" % XCCDF12_NS) + refine_rule.set("idref", OSCAP_RULE + refined_rule) + for refinement in refinement_list: + refine_rule.set(refinement[0], refinement[1]) + element.append(refine_rule) + + def _should_have_version(self): + return self.metadata and 'version' in self.metadata and self.metadata[ + 'version'] is not None + def to_xml_element(self): element = ET.Element('{%s}Profile' % XCCDF12_NS) element.set("id", OSCAP_PROFILE + self.id_) + if self._should_have_version(): + add_sub_element(element, "version", XCCDF12_NS, str(self.metadata["version"])) if self.extends: element.set("extends", self.extends) title = add_sub_element(element, "title", XCCDF12_NS, self.title) @@ -112,18 +131,7 @@ def to_xml_element(self): self._add_selects(element, self.unselected, OSCAP_RULE, "false") self._add_selects(element, self.unselected_groups, OSCAP_GROUP, "false") - for value_id, selector in self.variables.items(): - refine_value = ET.Element("{%s}refine-value" % XCCDF12_NS) - refine_value.set("idref", OSCAP_VALUE + value_id) - refine_value.set("selector", selector) - element.append(refine_value) - - for refined_rule, refinement_list in self.refine_rules.items(): - refine_rule = ET.Element("{%s}refine-rule" % XCCDF12_NS) - refine_rule.set("idref", OSCAP_RULE + refined_rule) - for refinement in refinement_list: - refine_rule.set(refinement[0], refinement[1]) - element.append(refine_rule) + self._setup_defined_rules(element) return element diff --git a/tests/unit/ssg-module/data/ospp.xml b/tests/unit/ssg-module/data/ospp.xml index 187d043b459..b39d8af9870 100644 --- a/tests/unit/ssg-module/data/ospp.xml +++ b/tests/unit/ssg-module/data/ospp.xml @@ -1,5 +1,6 @@ + 4.2.1 Protection Profile for General Purpose Operating Systems This profile is part of Red Hat Enterprise Linux 9 Common Criteria Guidance documentation for Target of Evaluation based on Protection Profile for diff --git a/tests/unit/ssg-module/test_build_yaml.py b/tests/unit/ssg-module/test_build_yaml.py index dd0718d65f5..4a36b6b9c19 100644 --- a/tests/unit/ssg-module/test_build_yaml.py +++ b/tests/unit/ssg-module/test_build_yaml.py @@ -533,3 +533,25 @@ def test_rule_to_ocil(rule_accounts_tmout): expected_file_path = os.path.join(DATADIR, expected_filename) diff = xmldiff_main.diff_files(real_file_path, expected_file_path) assert diff == [] + + +@pytest.fixture() +def profile_without_version(profile_ospp): + profile_ospp.metadata.pop('version', None) + return profile_ospp + + +def test_profile_without_version(profile_without_version): + profile_el = profile_without_version.to_xml_element() + assert profile_el.find("{%s}version" % XCCDF12_NS) is None + + +@pytest.fixture() +def profile_with_version(profile_ospp): + profile_ospp.metadata["version"] = "3.2.1" + return profile_ospp + + +def test_profile_with_version(profile_with_version): + profile_el = profile_with_version.to_xml_element() + assert profile_el.find("{%s}version" % XCCDF12_NS).text == "3.2.1"