Skip to content

Commit 4babf67

Browse files
akofinkxprazak2
authored andcommitted
Enable more complete datastream parsing
Signed-off-by: Andrew Kofink <akofink@redhat.com>
1 parent 2285213 commit 4babf67

File tree

8 files changed

+71
-47
lines changed

8 files changed

+71
-47
lines changed

lib/openscap_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# frozen_string_literal: true
22
require 'openscap_parser/profiles'
3+
require 'openscap_parser/profile'
34
require 'openscap_parser/rule'
45
require 'openscap_parser/rule_result'
56
require 'openscap_parser/rule_results'

lib/openscap_parser/ds.rb

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,17 @@
44
module OpenscapParser
55
class Ds
66
include OpenscapParser::XmlFile
7+
include OpenscapParser::Rules
8+
include OpenscapParser::Profiles
79

810
def initialize(report)
911
parsed_xml report
1012
end
1113

12-
def profiles
13-
@profiles ||= profile_nodes
14-
end
15-
1614
def valid?
1715
return true if @parsed_xml.root.name == 'data-stream-collection' && namespaces.keys.include?('xmlns:ds')
1816
return true if @parsed_xml.root.name == 'Tailoring' && namespaces.keys.include?('xmlns:xccdf')
1917
false
2018
end
21-
22-
private
23-
24-
def profile_nodes
25-
@parsed_xml.xpath(".//Profile").map do |node|
26-
id_node = node.attribute('id')
27-
id = id_node.value if id_node
28-
title_node = node.at_xpath('./title')
29-
title = title_node.text if title_node
30-
desc_node = node.at_xpath('./description')
31-
description = desc_node.text if desc_node
32-
{ :id => id, :title => title, :description => description }
33-
end
34-
end
3519
end
3620
end

lib/openscap_parser/profiles.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ module Profiles
77
def self.included(base)
88
base.class_eval do
99
def profiles
10-
@profiles ||= profile_nodes.inject({}) do |profiles, profile_node|
11-
profiles[profile_node['id']] = profile_node.at_css('title').text
12-
13-
profiles
10+
@profiles ||= profile_nodes.map do |profile_node|
11+
OpenscapParser::Profile.new(profile_xml: profile_node)
1412
end
1513
end
1614

lib/openscap_parser/rule.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# frozen_string_literal: true
22

3+
require 'openscap_parser/rule_identifier'
4+
require 'openscap_parser/rule_reference'
5+
36
# Mimics openscap-ruby Rule interface
47
module OpenscapParser
58
class Rule
@@ -11,6 +14,10 @@ def id
1114
@id ||= @rule_xml['id']
1215
end
1316

17+
def selected
18+
@selected ||= @rule_xml['selected']
19+
end
20+
1421
def severity
1522
@severity ||= @rule_xml['severity']
1623
end
@@ -35,16 +42,21 @@ def rationale
3542
end
3643

3744
def references
38-
@references ||= @rule_xml.css('reference').map do |node|
39-
{ href: node['href'], label: node.text }
45+
@references ||= reference_nodes.map do |node|
46+
RuleReference.new(reference_xml: node)
4047
end
4148
end
4249

50+
def reference_nodes
51+
@reference_nodes ||= @rule_xml.xpath('reference')
52+
end
53+
4354
def identifier
44-
@identifier ||= {
45-
label: @rule_xml.at_css('ident') && @rule_xml.at_css('ident').text,
46-
system: (ident = @rule_xml.at_css('ident')) && ident['system']
47-
}
55+
@identifier ||= RuleIdentifier.new(identifier_xml: @identifier_node)
56+
end
57+
58+
def identifier_node
59+
@identifier_node ||= @rule_xml.at_xpath('ident')
4860
end
4961

5062
private
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# RuleIdentifier interface as an object
4+
module OpenscapParser
5+
class RuleIdentifier
6+
def initialize(identifier_xml: nil)
7+
@identifier_xml = identifier_xml
8+
end
9+
10+
def label
11+
@label ||= @identifier_xml && @identifier_xml.text
12+
end
13+
14+
def system
15+
@system ||= @identifier_xml && @identifier_xml['system']
16+
end
17+
end
18+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
# RuleReference interface as an object
4+
module OpenscapParser
5+
class RuleReference
6+
def initialize(reference_xml: nil)
7+
@reference_xml = reference_xml
8+
end
9+
10+
def href
11+
@href ||= @reference_xml && @reference_xml['href']
12+
end
13+
14+
def label
15+
@label ||= @reference_xml && @reference_xml.text
16+
end
17+
end
18+
end

test/ds_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class DsTest < MiniTest::Test
1818
"Red Hat Corporate Profile for Certified Cloud Providers (RH CCP)",
1919
"PCI-DSS v3 Control Baseline for Red Hat Enterprise Linux 7"
2020
]
21-
assert_equal(profile_titles, parser.profiles.map { |profile| profile[:title] })
21+
assert_equal(profile_titles, parser.profiles.map(&:title))
2222
end
2323
end
2424

@@ -29,7 +29,7 @@ class DsTest < MiniTest::Test
2929
"Standard System Security Profile [CUSTOMIZED]",
3030
"Common Profile for General-Purpose Systems [CUSTOMIZED]"
3131
]
32-
assert_equal(profile_titles, parser.profiles.map { |profile| profile[:title] })
32+
assert_equal(profile_titles, parser.profiles.map(&:title))
3333
end
3434
end
3535

test/openscap_parser_test.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
# frozen_string_literal: true
22

33
require 'test_helper'
4+
require 'openscap_parser/rule_reference'
45

56
class OpenscapParserTest < Minitest::Test
67
def setup
78
fake_report = file_fixture('xccdf_report.xml').read
8-
@profile = {
9-
'xccdf_org.ssgproject.content_profile_standard' =>
10-
'Standard System Security Profile for Fedora'
11-
}
9+
@profile_ref_id = 'xccdf_org.ssgproject.content_profile_standard'
1210
@report_parser = ::OpenscapParser::Base.new(fake_report)
1311
end
1412

1513
context 'profile' do
1614
should 'be able to parse it' do
17-
assert_equal(@profile, @report_parser.profiles)
15+
assert_equal(@profile_ref_id, @report_parser.profiles.first.id)
1816
end
1917

2018
should 'not save more than one profile when there are no test results' do
2119
fake_report = file_fixture('rhel-xccdf-report.xml').read
22-
@profile = {
23-
'xccdf_org.ssgproject.content_profile_rht-ccp' =>
24-
'Red Hat Corporate Profile for Certified Cloud Providers (RH CCP)'
25-
}
20+
@profile_ref_id = 'xccdf_org.ssgproject.content_profile_rht-ccp'
2621
@report_parser = ::OpenscapParser::Base.new(fake_report)
2722
assert_equal 1, @report_parser.test_result_profiles.count
2823
end
@@ -55,19 +50,17 @@ def setup
5550
end
5651

5752
should 'parse rule references' do
58-
rule = @report_parser.rule_objects.find do |r|
59-
r.title == 'Disable At Service (atd)'
53+
rule = @report_parser.rules.find do |r|
54+
r.id == 'xccdf_org.ssgproject.content_rule_service_atd_disabled'
6055
end
6156

6257
references = [
63-
{:href=>"http://iase.disa.mil/stigs/cci/Pages/index.aspx",
64-
:label=>"CCI-000381"},
65-
{:href=>'http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.'\
66-
'800-53r4.pdf',
67-
:label=>"CM-7"}
58+
["http://iase.disa.mil/stigs/cci/Pages/index.aspx", "CCI-000381"],
59+
['http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.'\
60+
'800-53r4.pdf', "CM-7"]
6861
]
6962

70-
assert_equal references, rule.references
63+
assert_equal references, rule.references.map { |rr| [rr.href, rr.label] }
7164
end
7265

7366
should 'parse rule description without newlines' do

0 commit comments

Comments
 (0)