Class: OpenscapParser::Rule

Inherits:
XmlNode
  • Object
show all
Includes:
Fixes, RuleReferences, Util
Defined in:
lib/openscap_parser/rule.rb

Instance Attribute Summary

Attributes inherited from XmlNode

#namespaces

Instance Method Summary collapse

Methods included from Fixes

included

Methods included from RuleReferences

included

Methods included from Util

#newline_to_whitespace

Methods inherited from XmlNode

#initialize, #parsed_xml, #text, #xpath_node, #xpath_nodes

Constructor Details

This class inherits a constructor from OpenscapParser::XmlNode

Instance Method Details

#conflictsObject



39
40
41
42
43
44
# File 'lib/openscap_parser/rule.rb', line 39

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

#descriptionObject



46
47
48
49
50
51
# File 'lib/openscap_parser/rule.rb', line 46

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

#idObject



15
16
17
# File 'lib/openscap_parser/rule.rb', line 15

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

#identifier_nodeObject



70
71
72
# File 'lib/openscap_parser/rule.rb', line 70

def identifier_node
  @identifier_node ||= parsed_xml.at_xpath('ident')
end

#parent_idObject



74
75
76
# File 'lib/openscap_parser/rule.rb', line 74

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

#parent_typeObject



78
79
80
81
82
83
84
# File 'lib/openscap_parser/rule.rb', line 78

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

#rationaleObject



53
54
55
56
57
58
# File 'lib/openscap_parser/rule.rb', line 53

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

#requiresObject



32
33
34
35
36
37
# File 'lib/openscap_parser/rule.rb', line 32

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

#rule_identifierObject Also known as: identifier



65
66
67
# File 'lib/openscap_parser/rule.rb', line 65

def rule_identifier
  @identifier ||= identifier_node && RuleIdentifier.new(parsed_xml: identifier_node)
end

#rule_reference_nodes(xpath = "reference") ⇒ Object



61
62
63
# File 'lib/openscap_parser/rule.rb', line 61

def rule_reference_nodes(xpath = "reference")
  rule_reference_nodes_old(xpath)
end

#rule_reference_nodes_oldObject



60
# File 'lib/openscap_parser/rule.rb', line 60

alias :rule_reference_nodes_old :rule_reference_nodes

#selectedObject



19
20
21
# File 'lib/openscap_parser/rule.rb', line 19

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

#severityObject



23
24
25
# File 'lib/openscap_parser/rule.rb', line 23

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

#titleObject



27
28
29
30
# File 'lib/openscap_parser/rule.rb', line 27

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

#to_hObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/openscap_parser/rule.rb', line 90

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

#valuesObject



86
87
88
# File 'lib/openscap_parser/rule.rb', line 86

def values
  parsed_xml.xpath("check/check-export").map { |r| r.at_xpath('@value-id')&.text }
end