Skip to content

Commit 69a2d8a

Browse files
authored
Add Benchmark parsing (#18)
Signed-off-by: Andrew Kofink <akofink@redhat.com>
1 parent 8377e09 commit 69a2d8a

File tree

12 files changed

+167
-58
lines changed

12 files changed

+167
-58
lines changed

lib/openscap_parser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# frozen_string_literal: true
2+
require 'openscap_parser/util'
3+
require 'openscap_parser/benchmarks'
24
require 'openscap_parser/profiles'
35
require 'openscap_parser/profile'
46
require 'openscap_parser/rule'

lib/openscap_parser/benchmark.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require 'openscap_parser/util'
4+
require 'openscap_parser/xml_file'
5+
require 'openscap_parser/rules'
6+
require 'openscap_parser/profiles'
7+
require 'openscap_parser/rule_references'
8+
9+
# Mimics openscap-ruby Benchmark interface
10+
module OpenscapParser
11+
class Benchmark
12+
include OpenscapParser::Util
13+
include OpenscapParser::XmlFile
14+
include OpenscapParser::Rules
15+
include OpenscapParser::RuleReferences
16+
include OpenscapParser::Profiles
17+
18+
def initialize(parsed_xml: nil)
19+
@parsed_xml = parsed_xml
20+
end
21+
22+
def id
23+
@id ||= @parsed_xml['id']
24+
end
25+
26+
def title
27+
@title ||= @parsed_xml.xpath('title') &&
28+
@parsed_xml.xpath('title').text
29+
end
30+
31+
def description
32+
@description ||= newline_to_whitespace(
33+
@parsed_xml.xpath('description') &&
34+
@parsed_xml.xpath('description').text || ''
35+
)
36+
end
37+
38+
def version
39+
@version ||= @parsed_xml.xpath('version') &&
40+
@parsed_xml.xpath('version').text
41+
end
42+
end
43+
end

lib/openscap_parser/benchmarks.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require 'openscap_parser/benchmark'
4+
5+
module OpenscapParser
6+
# Methods related to saving profiles and finding which hosts
7+
# they belong to
8+
module Benchmarks
9+
def self.included(base)
10+
base.class_eval do
11+
def benchmark
12+
@benchmark ||= OpenscapParser::Benchmark.new(parsed_xml: benchmark_node)
13+
end
14+
15+
def benchmark_node(xpath = ".//Benchmark")
16+
xpath_node(xpath)
17+
end
18+
end
19+
end
20+
end
21+
end

lib/openscap_parser/datastream.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# frozen_string_literal: true
22
require 'openscap_parser/xml_file'
3+
require 'openscap_parser/benchmarks'
34

45
module OpenscapParser
56
class Datastream
67
include OpenscapParser::XmlFile
7-
include OpenscapParser::Rules
8-
include OpenscapParser::Profiles
8+
include OpenscapParser::Benchmarks
99

1010
def initialize(report)
1111
parsed_xml report

lib/openscap_parser/profile.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def description
1818
@profile_xml.at_css('description').text
1919
end
2020

21+
def selected_rule_ids
22+
id_refs = @profile_xml.xpath("select[@selected='true']/@idref")
23+
id_refs && id_refs.map(&:text)
24+
end
25+
2126
def to_h
2227
{ :id => id, :title => title, :description => description }
2328
end

lib/openscap_parser/rule.rb

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,63 @@
11
# frozen_string_literal: true
22

33
require 'openscap_parser/rule_identifier'
4-
require 'openscap_parser/rule_reference'
4+
require 'openscap_parser/rule_references'
5+
require 'openscap_parser/xml_file'
56

67
# Mimics openscap-ruby Rule interface
78
module OpenscapParser
89
class Rule
9-
def initialize(rule_xml: nil)
10-
@rule_xml = rule_xml
10+
include OpenscapParser::Util
11+
include OpenscapParser::XmlFile
12+
include OpenscapParser::RuleReferences
13+
14+
def initialize(parsed_xml: nil)
15+
@parsed_xml = parsed_xml
1116
end
1217

1318
def id
14-
@id ||= @rule_xml['id']
19+
@id ||= @parsed_xml['id']
1520
end
1621

1722
def selected
18-
@selected ||= @rule_xml['selected']
23+
@selected ||= @parsed_xml['selected']
1924
end
2025

2126
def severity
22-
@severity ||= @rule_xml['severity']
27+
@severity ||= @parsed_xml['severity']
2328
end
2429

2530
def title
26-
@title ||= @rule_xml.at_css('title') &&
27-
@rule_xml.at_css('title').text
31+
@title ||= @parsed_xml.at_css('title') &&
32+
@parsed_xml.at_css('title').text
2833
end
2934

3035
def description
3136
@description ||= newline_to_whitespace(
32-
@rule_xml.at_css('description') &&
33-
@rule_xml.at_css('description').text || ''
37+
@parsed_xml.at_css('description') &&
38+
@parsed_xml.at_css('description').text || ''
3439
)
3540
end
3641

3742
def rationale
3843
@rationale ||= newline_to_whitespace(
39-
@rule_xml.at_css('rationale') &&
40-
@rule_xml.at_css('rationale').text || ''
44+
@parsed_xml.at_css('rationale') &&
45+
@parsed_xml.at_css('rationale').text || ''
4146
)
4247
end
4348

44-
def references
45-
@references ||= reference_nodes.map do |node|
46-
RuleReference.new(reference_xml: node)
47-
end
48-
end
49-
50-
def reference_nodes
51-
@reference_nodes ||= @rule_xml.xpath('reference')
49+
alias :rule_reference_nodes_old :rule_reference_nodes
50+
def rule_reference_nodes(xpath = "reference")
51+
rule_reference_nodes_old(xpath)
5252
end
5353

54-
def identifier
55-
@identifier ||= RuleIdentifier.new(identifier_xml: @identifier_node)
54+
def rule_identifier
55+
@identifier ||= RuleIdentifier.new(identifier_xml: identifier_node)
5656
end
57+
alias :identifier :rule_identifier
5758

5859
def identifier_node
59-
@identifier_node ||= @rule_xml.at_xpath('ident')
60-
end
61-
62-
private
63-
64-
def newline_to_whitespace(string)
65-
string.gsub(/ *\n+/, " ").strip
60+
@identifier_node ||= @parsed_xml.at_xpath('ident')
6661
end
6762
end
6863
end
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require 'openscap_parser/xml_file'
4+
require 'openscap_parser/rule_reference'
5+
6+
module OpenscapParser
7+
# Methods related to finding and saving rule references
8+
module RuleReferences
9+
def self.included(base)
10+
base.class_eval do
11+
def rule_reference_strings
12+
@rule_reference_strings ||= rule_references.map do |rr|
13+
"#{rr.label}#{rr.href}"
14+
end
15+
end
16+
17+
def rule_references
18+
@rule_references ||= rule_reference_nodes.map do |node|
19+
OpenscapParser::RuleReference.new(reference_xml: node)
20+
end.uniq do |reference|
21+
[reference.label, reference.href]
22+
end
23+
end
24+
alias :references :rule_references
25+
26+
def rule_reference_nodes(xpath = ".//Rule/reference")
27+
xpath_nodes(xpath)
28+
end
29+
end
30+
end
31+
end
32+
end

lib/openscap_parser/rules.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def rule_ids
1111

1212
def rule_objects
1313
@rule_objects ||= rule_nodes.map do |rule_node|
14-
Rule.new(rule_xml: rule_node)
14+
Rule.new(parsed_xml: rule_node)
1515
end
1616
end
1717
alias :rules :rule_objects

lib/openscap_parser/util.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
# Utility functions for OpenscapParser
4+
module OpenscapParser
5+
module Util
6+
def newline_to_whitespace(string)
7+
string.gsub(/ *\n+/, " ").strip
8+
end
9+
end
10+
end

lib/openscap_parser/xml_file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ def parsed_xml(report_contents = '')
1313
end
1414

1515
def xpath_node(xpath)
16-
@parsed_xml.at_xpath(xpath)
16+
@parsed_xml && @parsed_xml.at_xpath(xpath)
1717
end
1818

1919
def xpath_nodes(xpath)
20-
@parsed_xml.xpath(xpath)
20+
@parsed_xml && @parsed_xml.xpath(xpath) || []
2121
end
2222
end
2323
end

0 commit comments

Comments
 (0)