Skip to content

Commit 7249abd

Browse files
xprazak2akofink
authored andcommitted
Resolve and replace subs with set-values (#27)
* Resolve and replace subs with set-values Fix may have multiple sub elements that reference set-values containing part of the full fix. We need to replace sub with a text node of the corresponding set-value to parse the whole fix script. * Use include instead of prepend
1 parent 4561090 commit 7249abd

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

lib/openscap_parser/fix.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ def strategy
2626
@strategy ||= @parsed_xml['strategy']
2727
end
2828

29+
def full_text(set_values)
30+
full_text_lines(set_values).join('')
31+
end
32+
33+
def full_text_lines(set_values)
34+
map_child_nodes(set_values).map do |text_node|
35+
text_node.respond_to?(:text) ? text_node.text : ''
36+
end
37+
end
38+
39+
def map_child_nodes(set_values = [])
40+
map_sub_nodes @parsed_xml.children, set_values
41+
end
42+
2943
def to_h
3044
{
3145
:id => id,

lib/openscap_parser/subs.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ def subs
1616
def sub_nodes(xpath = './/sub')
1717
@sub_nodes ||= xpath_nodes(xpath)
1818
end
19+
20+
def map_sub_nodes(children, set_values)
21+
children.map do |child|
22+
next child if child.name == 'text'
23+
next replace_sub(Sub.new(parsed_xml: child), set_values) if child.name == 'sub'
24+
child
25+
end
26+
end
27+
28+
private
29+
30+
def replace_sub(sub, set_values)
31+
set_value = set_values.find { |set_value| set_value.id == sub.id }
32+
return unless set_value
33+
set_value.parsed_xml.children.first
34+
end
1935
end
2036
end
2137
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
var_selinux_state="enforcing"
3+
4+
function replace_or_append {
5+
local config_file=$1
6+
local key=$2
7+
local value=$3
8+
local cce=$4
9+
local format=$5
10+
11+
# Check sanity of the input
12+
if [ $# -lt "3" ]
13+
then
14+
echo "Usage: replace_or_append 'config_file_location' 'key_to_search' 'new_value'"
15+
echo
16+
echo "If symlinks need to be taken into account, add yes/no to the last argument"
17+
echo "to allow to 'follow_symlinks'."
18+
echo "Aborting."
19+
exit 1
20+
fi
21+
22+
# Test if the config_file is a symbolic link. If so, use --follow-symlinks with sed.
23+
# Otherwise, regular sed command will do.
24+
if test -L $config_file; then
25+
sed_command="sed -i --follow-symlinks"
26+
else
27+
sed_command="sed -i"
28+
fi
29+
30+
# Test that the cce arg is not empty or does not equal @CCENUM@.
31+
# If @CCENUM@ exists, it means that there is no CCE assigned.
32+
if ! [ "x$cce" = x ] && [ "$cce" != '@CCENUM@' ]; then
33+
cce="CCE-${cce}"
34+
else
35+
cce="CCE"
36+
fi
37+
38+
# Strip any search characters in the key arg so that the key can be replaced without
39+
# adding any search characters to the config file.
40+
stripped_key=$(sed "s/[\^=\$,;+]*//g" <<< $key)
41+
42+
# If there is no print format specified in the last arg, use the default format.
43+
if ! [ "x$format" = x ] ; then
44+
printf -v formatted_output "$format" "$stripped_key" "$value"
45+
else
46+
formatted_output="$stripped_key = $value"
47+
fi
48+
49+
# If the key exists, change it. Otherwise, add it to the config_file.
50+
if `grep -qi $key $config_file` ; then
51+
$sed_command "s/$key.*/$formatted_output/g" $config_file
52+
else
53+
# \n is precaution for case where file ends without trailing newline
54+
echo -e "\n# Per $cce: Set $formatted_output in $config_file" >> $config_file
55+
echo -e "$formatted_output" >> $config_file
56+
fi
57+
58+
}
59+
60+
replace_or_append '/etc/sysconfig/selinux' '^SELINUX=' $var_selinux_state 'CCE-27334-2' '%s=%s'
61+
62+
fixfiles onboot
63+
fixfiles -f relabel

test/openscap_parser/test_result_file_test.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,24 @@ def setup
160160
assert sub.text
161161
assert sub.use
162162
end
163+
164+
test "should resolve set-values for subs" do
165+
set_values = @arf_result_file.test_result.set_values
166+
rule = @arf_result_file.benchmark.rules.find { |rule| rule.id == "xccdf_org.ssgproject.content_rule_selinux_state" }
167+
rule.fixes.first.map_child_nodes(set_values).all? { |node| node.is_a? Nokogiri::XML::Text }
168+
end
169+
170+
test "should parse full fix text lines" do
171+
set_values = @arf_result_file.test_result.set_values
172+
rule = @arf_result_file.benchmark.rules.find { |rule| rule.id == "xccdf_org.ssgproject.content_rule_selinux_state" }
173+
assert_equal 5, rule.fixes.first.full_text_lines(set_values).count
174+
end
175+
176+
test "should compose full fix" do
177+
set_values = @arf_result_file.test_result.set_values
178+
rule = @arf_result_file.benchmark.rules.find { |rule| rule.id == "xccdf_org.ssgproject.content_rule_selinux_state" }
179+
assert_equal file_fixture('selinux_full_fix.sh').read, rule.fixes.first.full_text(set_values)
180+
end
163181
end
164182
end
165183

0 commit comments

Comments
 (0)