Skip to content

Commit

Permalink
RHICOMPL-2481 Adding Groups to openscap parser (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
marleystipich2 authored Mar 1, 2022
1 parent 3987c65 commit 0cd8828
Show file tree
Hide file tree
Showing 7 changed files with 232,963 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/openscap_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'openscap_parser/test_results'
require 'openscap_parser/profiles'
require 'openscap_parser/rules'
require 'openscap_parser/groups'
require 'openscap_parser/rule_results'
require 'openscap_parser/tailorings'

Expand Down
2 changes: 2 additions & 0 deletions lib/openscap_parser/benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'openscap_parser/rules'
require 'openscap_parser/profiles'
require 'openscap_parser/rule_references'
require 'openscap_parser/groups'

# Mimics openscap-ruby Benchmark interface
module OpenscapParser
Expand All @@ -13,6 +14,7 @@ class Benchmark < XmlNode
include OpenscapParser::Rules
include OpenscapParser::RuleReferences
include OpenscapParser::Profiles
include OpenscapParser::Groups

def id
@id ||= @parsed_xml['id']
Expand Down
73 changes: 73 additions & 0 deletions lib/openscap_parser/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# frozen_string_literal: true
module OpenscapParser
class Group < XmlNode
include OpenscapParser::Util

def id
@id ||= parsed_xml['id']
end

def title
@title ||= parsed_xml.at_css('title') &&
parsed_xml.at_css('title').text
end

def description
@description ||= newline_to_whitespace(
parsed_xml.at_css('description') &&
parsed_xml.at_css('description').text || ''
)
end

def rationale
@rationale ||= newline_to_whitespace(
parsed_xml.at_css('rationale') &&
parsed_xml.at_css('rationale').text || ''
)
end

def requires
@requires ||= parsed_xml.xpath('./requires') &&
parsed_xml.xpath('./requires/@idref').flat_map do |r|
r.to_s&.split
end
end

def conflicts
@conflicts ||= parsed_xml.xpath('./conflicts') &&
parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
c.to_s&.split
end
end

def selected
@selected ||= parsed_xml['selected']
end

def parent_id
@parent_id = parsed_xml.xpath('../@id').to_s
end

def parent_type
if parsed_xml.xpath("name(..)='Group'")
@parent_type = 'Group'
else
@parent_type = 'Benchmark'
end
end

def to_h
{
:id => id,
:title => title,
:description => description,
:requires => requires,
:conflicts => conflicts,
:rationale => rationale,
:selected => selected,
:parent_id => parent_id,
:parent_type => parent_type
}
end
end
end
22 changes: 22 additions & 0 deletions lib/openscap_parser/groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require 'openscap_parser/group'

module OpenscapParser
# Methods related to finding and saving rule references
module Groups
def self.included(base)
base.class_eval do
def groups
@groups ||= group_nodes.map do |group_node|
OpenscapParser::Group.new(parsed_xml: group_node)
end
end

def group_nodes(xpath = './/Group')
xpath_nodes(xpath)
end
end
end
end
end
32 changes: 31 additions & 1 deletion lib/openscap_parser/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@ def title
parsed_xml.at_css('title').text
end

def requires
@requires ||= parsed_xml.xpath('./requires') &&
parsed_xml.xpath('./requires/@idref').flat_map do |r|
r.to_s&.split
end
end

def conflicts
@conflicts ||= parsed_xml.xpath('./conflicts') &&
parsed_xml.xpath('./conflicts/@idref').flat_map do |c|
c.to_s&.split
end
end

def description
@description ||= newline_to_whitespace(
parsed_xml.at_css('description') &&
Expand Down Expand Up @@ -57,15 +71,31 @@ def identifier_node
@identifier_node ||= parsed_xml.at_xpath('ident')
end

def parent_id
parsed_xml.xpath('../@id').to_s
end

def parent_type
if parsed_xml.xpath("name(..)='Group'")
@parent_type = 'Group'
else
@parent_type = 'Benchmark'
end
end

def to_h
{
:id => id,
:selected => selected,
:severity => severity,
:title => title,
:requires => requires,
:conflicts => conflicts,
:description => description,
:rationale => rationale,
:identifier => rule_identifier.to_h
:identifier => rule_identifier.to_h,
:parent_id => parent_id,
:parent_type => parent_type
}
end
end
Expand Down
Loading

0 comments on commit 0cd8828

Please sign in to comment.