Skip to content

Commit 0c0c10f

Browse files
marleystipich2skateman
authored andcommitted
RHICOMPL-3348: Add Values to openscap parser
1 parent d6ca5df commit 0c0c10f

File tree

6 files changed

+130
-11
lines changed

6 files changed

+130
-11
lines changed

lib/openscap_parser.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
require 'openscap_parser/profiles'
88
require 'openscap_parser/rules'
99
require 'openscap_parser/groups'
10+
require 'openscap_parser/value_definitions'
1011
require 'openscap_parser/rule_results'
1112
require 'openscap_parser/tailorings'
1213

lib/openscap_parser/benchmark.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
require 'openscap_parser/profiles'
77
require 'openscap_parser/rule_references'
88
require 'openscap_parser/groups'
9+
require 'openscap_parser/value_definitions'
910

1011
# Mimics openscap-ruby Benchmark interface
1112
module OpenscapParser
@@ -15,6 +16,7 @@ class Benchmark < XmlNode
1516
include OpenscapParser::RuleReferences
1617
include OpenscapParser::Profiles
1718
include OpenscapParser::Groups
19+
include OpenscapParser::ValueDefinitions
1820

1921
def id
2022
@id ||= @parsed_xml['id']
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
module OpenscapParser
3+
class ValueDefinition < XmlNode
4+
include OpenscapParser::Util
5+
6+
def id
7+
@id ||= parsed_xml['id']
8+
end
9+
10+
def description
11+
@description ||= newline_to_whitespace(parsed_xml.at_css('description')&.text)
12+
end
13+
14+
def title
15+
@title ||= parsed_xml.at_css('title')&.text
16+
end
17+
18+
def type
19+
@type ||= parsed_xml['type'] || 'string'
20+
end
21+
22+
def lower_bound
23+
@lower_bound ||= begin
24+
lower_bound_element = parsed_xml.at_xpath("lower-bound[@selector='']") || parsed_xml.at_xpath('lower-bound[not(@selector)]')
25+
lower_bound_element&.text
26+
end
27+
end
28+
29+
def upper_bound
30+
@upper_bound ||= begin
31+
upper_bound_element = parsed_xml.at_xpath("upper-bound[@selector='']") || parsed_xml.at_xpath('upper-bound[not(@selector)]')
32+
upper_bound_element&.text
33+
end
34+
end
35+
36+
def default_value
37+
# The default value is the value element with a empty or absent @selector
38+
# If there is no value element with an empty or absent @selector, the first value in
39+
# the top down processing shall be the default element
40+
@default_value ||= begin
41+
value_element = parsed_xml.at_xpath("value[@selector='']") || parsed_xml.at_xpath('value[not(@selector)]') || parsed_xml.xpath("value")[0]
42+
value_element&.text
43+
end
44+
end
45+
46+
def to_h
47+
{
48+
:id => id,
49+
:title => title,
50+
:description => description,
51+
:type => type,
52+
:lower_bound => lower_bound,
53+
:upper_bound => upper_bound,
54+
:default_value => default_value
55+
}
56+
end
57+
end
58+
end
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
require 'openscap_parser/value_definition'
4+
5+
module OpenscapParser
6+
# Methods related to parsing values
7+
module ValueDefinitions
8+
def self.included(base)
9+
base.class_eval do
10+
def value_definitions
11+
@value_definitions ||= value_definition_nodes.map do |vdn|
12+
ValueDefinition.new(parsed_xml: vdn)
13+
end
14+
end
15+
16+
def value_definition_nodes(xpath = ".//Value")
17+
xpath_nodes(xpath)
18+
end
19+
end
20+
end
21+
end
22+
end

test/fixtures/files/xccdf_report_with_conflicts_and_requires.xml

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9199,7 +9199,10 @@ files installed on the system.</description>
91999199
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Integrity Scan Notification Email Address</title>
92009200
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Specify the email address for designated personnel if baseline
92019201
configurations are changed in an unauthorized manner.</description>
9202-
<value>root@localhost</value>
9202+
<value selector='5345'>51882M</value>
9203+
<value selector="512M">212M</value>
9204+
<value selector="5435">1G</value>
9205+
<value selector='5345'>512M</value>
92039206
</Value>
92049207
<Group id="xccdf_org.ssgproject.content_group_rpm_verification">
92059208
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Verify Integrity with RPM</title>
@@ -11840,15 +11843,15 @@ Currently the supported backends are:
1184011843
<html:ul xmlns:html="http://www.w3.org/1999/xhtml"><html:li>GnuTLS library</html:li><html:li>OpenSSL library</html:li><html:li>NSS library</html:li><html:li>OpenJDK</html:li><html:li>Libkrb5</html:li><html:li>BIND</html:li><html:li>OpenSSH</html:li></html:ul>
1184111844
Applications and languages which rely on any of these backends will follow the
1184211845
system policies as well. Examples are apache httpd, nginx, php, and others.</description>
11843-
<Value id="xccdf_org.ssgproject.content_value_var_ssh_client_rekey_limit_size" type="string" interactive="true">
11846+
<Value id="xccdf_org.ssgproject.content_value_var_ssh_client_rekey_limit_size" interactive="true">
1184411847
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">SSH client RekeyLimit - size</title>
1184511848
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Specify the size component of the rekey limit. This limit signifies amount
1184611849
of data. After this amount of data is transferred through the connection,
1184711850
the session key is renegotiated. The number is followed by K, M or G for
1184811851
kilobytes, megabytes or gigabytes. Note that the RekeyLimit can be also
1184911852
configured according to elapsed time.</description>
1185011853
<value>512M</value>
11851-
<value selector="512M">512M</value>
11854+
<value selector="512M">513M</value>
1185211855
<value selector="1G">1G</value>
1185311856
</Value>
1185411857
<Value id="xccdf_org.ssgproject.content_value_var_ssh_client_rekey_limit_time" type="string" interactive="true">
@@ -11857,18 +11860,18 @@ configured according to elapsed time.</description>
1185711860
renegotiated after the defined amount of time passes. The number is followed
1185811861
by units such as H or M for hours or minutes. Note that the RekeyLimit can
1185911862
be also configured according to amount of transfered data.</description>
11860-
<value>1h</value>
11863+
<value selector="3hour">3h</value>
1186111864
<value selector="1hour">1h</value>
1186211865
</Value>
1186311866
<Value id="xccdf_org.ssgproject.content_value_var_system_crypto_policy" type="string">
1186411867
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">The system-provided crypto policies</title>
1186511868
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Specify the crypto policy for the system.</description>
11866-
<value>DEFAULT</value>
11867-
<value selector="default_policy">DEFAULT</value>
11869+
<value selector="default_policy">DEFAULT2</value>
1186811870
<value selector="default_nosha1">DEFAULT:NO-SHA1</value>
1186911871
<value selector="fips">FIPS</value>
1187011872
<value selector="fips_ospp">FIPS:OSPP</value>
1187111873
<value selector="legacy">LEGACY</value>
11874+
<value selector="">DEFAULT</value>
1187211875
<value selector="future">FUTURE</value>
1187311876
<value selector="next">NEXT</value>
1187411877
</Value>
@@ -12227,10 +12230,14 @@ VirusScan Enterprise for Linux (VSEL) is required to be installed on all systems
1222712230
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">The age of McAfee defintion file before requiring updating</title>
1222812231
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Specify the amount of time (in seconds) before McAfee definition files need to be
1222912232
updated.</description>
12230-
<value>2592000</value>
12231-
<value selector="1_day">86400</value>
12233+
<value selector="1_day">2592000</value>
12234+
<value selector="2_day">86400</value>
1223212235
<value selector="1_week">604800</value>
12233-
<value selector="30_days">2592000</value>
12236+
<value selector="30_days">2592001</value>
12237+
<lower-bound>0</lower-bound>
12238+
<lower-bound selector="1_day">1</lower-bound>
12239+
<upper-bound>40000000</upper-bound>
12240+
<upper-bound selector="1_day">70000000</upper-bound>
1223412241
</Value>
1223512242
<Rule id="xccdf_org.ssgproject.content_rule_service_nails_enabled" selected="false" role="full" severity="medium">
1223612243
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Enable nails Service</title>
@@ -16249,10 +16256,9 @@ the man page <html:code xmlns:html="http://www.w3.org/1999/xhtml">dconf(1)</html
1624916256
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Screensaver Inactivity timeout</title>
1625016257
<description xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Choose allowed duration (in seconds) of inactive graphical sessions</description>
1625116258
<value selector="10_minutes">600</value>
16252-
<value selector="15_minutes">900</value>
16259+
<value selector="15_minutes">901</value>
1625316260
<value selector="30_minutes">1800</value>
1625416261
<value selector="5_minutes">300</value>
16255-
<value>900</value>
1625616262
</Value>
1625716263
<Value id="xccdf_org.ssgproject.content_value_var_screensaver_lock_delay" type="number">
1625816264
<title xmlns:xhtml="http://www.w3.org/1999/xhtml" xml:lang="en-US">Screensaver Lock Delay</title>

test/openscap_parser/test_result_file_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,36 @@ def setup
182182
end
183183
end
184184

185+
context 'value_definitions' do
186+
test 'value_description' do
187+
assert_match(/^Specify the email address for designated personnel if baseline configurations are changed in an unauthorized manner./,
188+
@test_result_file2.benchmark.value_definitions.first.description)
189+
end
190+
191+
test 'type' do
192+
assert_equal("string", @test_result_file2.benchmark.value_definitions[0].type)
193+
assert_equal("string", @test_result_file2.benchmark.value_definitions[1].type)
194+
assert_equal("number", @test_result_file2.benchmark.value_definitions[4].type)
195+
end
196+
197+
test 'lower bound' do
198+
assert_equal(nil, @test_result_file2.benchmark.value_definitions[0].lower_bound)
199+
assert_equal("0", @test_result_file2.benchmark.value_definitions[4].lower_bound)
200+
end
201+
202+
test 'upper bound' do
203+
assert_equal(nil, @test_result_file2.benchmark.value_definitions[0].upper_bound)
204+
assert_equal("40000000", @test_result_file2.benchmark.value_definitions[4].upper_bound)
205+
end
206+
207+
test 'default value' do
208+
assert_equal("51882M", @test_result_file2.benchmark.value_definitions[0].default_value)
209+
assert_equal("512M", @test_result_file2.benchmark.value_definitions[1].default_value)
210+
assert_equal("3h", @test_result_file2.benchmark.value_definitions[2].default_value)
211+
assert_equal("DEFAULT", @test_result_file2.benchmark.value_definitions[3].default_value)
212+
end
213+
end
214+
185215
context 'rule_references' do
186216
test 'rule references' do
187217
rule = @test_result_file.benchmark.rules.find do |r|

0 commit comments

Comments
 (0)