From 5dcde8430062ed009548b3e5e0701d72f4218575 Mon Sep 17 00:00:00 2001 From: Marley Stipich Date: Thu, 24 Mar 2022 16:08:07 -0500 Subject: [PATCH] RHICOMPL-2833: Parse selected groups and selected rules separately in Profile --- lib/openscap_parser/profile.rb | 24 ++++++++++- lib/openscap_parser/regex_handler.rb | 9 ++++ ...cdf_report_with_conflicts_and_requires.xml | 23 ++++++---- test/openscap_parser/test_result_file_test.rb | 42 +++++++++++++++++++ 4 files changed, 90 insertions(+), 8 deletions(-) create mode 100644 lib/openscap_parser/regex_handler.rb diff --git a/lib/openscap_parser/profile.rb b/lib/openscap_parser/profile.rb index a448096..4e87f8f 100644 --- a/lib/openscap_parser/profile.rb +++ b/lib/openscap_parser/profile.rb @@ -1,3 +1,5 @@ +require 'openscap_parser/regex_handler' + module OpenscapParser class Profile < XmlNode def id @@ -20,7 +22,27 @@ def description end def selected_rule_ids - @selected_rule_ids ||= @parsed_xml.xpath("select[@selected='true']/@idref") && + # Look for selected rule ids where the idref contains '_rule_' that is not preceded by 'group' + @selected_rule_ids ||= @parsed_xml.xpath("select[@selected='true'] + [regex(@idref, '^((?!_group_).)*?(_rule_).*$')] + /@idref", RegexHandler) && + @parsed_xml.xpath("select[@selected='true'] + [regex(@idref, '^((?!_group_).)*?(_rule_).*$')] + /@idref", RegexHandler).map(&:text) + end + + def selected_group_ids + # Look for selected group ids where the idref contains '_group_' that is not preceded by 'rule' + @selected_group_ids ||= @parsed_xml.xpath("select[@selected='true'] + [regex(@idref, '^((?!_rule_).)*?(_group_).*$')] + /@idref", RegexHandler) && + @parsed_xml.xpath("select[@selected='true'] + [regex(@idref, '^((?!_rule_).)*?(_group_).*$')] + /@idref", RegexHandler).map(&:text) + end + + def selected_entity_ids + @selected_entity_ids ||= @parsed_xml.xpath("select[@selected='true']/@idref") && @parsed_xml.xpath("select[@selected='true']/@idref").map(&:text) end diff --git a/lib/openscap_parser/regex_handler.rb b/lib/openscap_parser/regex_handler.rb new file mode 100644 index 0000000..d694ada --- /dev/null +++ b/lib/openscap_parser/regex_handler.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module OpenscapParser + class RegexHandler < XmlNode + def self.regex node_set, regex + node_set.find_all { |node| node.to_s =~ /#{regex}/ } + end + end +end diff --git a/test/fixtures/files/xccdf_report_with_conflicts_and_requires.xml b/test/fixtures/files/xccdf_report_with_conflicts_and_requires.xml index b44af0f..a6fed81 100644 --- a/test/fixtures/files/xccdf_report_with_conflicts_and_requires.xml +++ b/test/fixtures/files/xccdf_report_with_conflicts_and_requires.xml @@ -298,9 +298,13 @@ CIS' terms and conditions, specifically Restrictions(8), note there is no representation or claim that the C2S profile will ensure a system is in compliance or consistency with the CIS baseline. + + + @@ -528,15 +532,20 @@ baseline. + + + + - - - + +