Skip to content

Commit 3bdb171

Browse files
committed
Do not drop empty lines from Ansible remediations
We are removing empty lines from generated Ansible Playbook. It looks nice, but there is a catch. Ansible tasks are a YAML snippets. So they contain YAML block scalars, indicated by a pipe (|). Within these block scalars, the newlines should be preserved. By removing the empty lines from the scalars we are actually changing the meaning of the scalars. Unfortunately, OpenSCAP doesn't understand YAML, but only reads the Ansible fix line-by-line as s string. That means it doesn't know when it reads a block scalar. Therefore we need to stop removing the empty lines anywhere. This commit also adds a small test. Resolves: RHBZ#1795563
1 parent f6fbafe commit 3bdb171

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

src/XCCDF_POLICY/xccdf_policy_remediate.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ static int _write_remediation_to_fd_and_free(int output_fd, const char* template
139139
free(text);
140140
return 1;
141141
}
142-
143-
if (_write_text_to_fd(output_fd, "\n") != 0) {
144-
free(text);
145-
return 1;
146-
}
142+
}
143+
if (_write_text_to_fd(output_fd, "\n") != 0) {
144+
free(text);
145+
return 1;
147146
}
148147

149148
if (next_delim != NULL) {

tests/API/XCCDF/unittests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ if(PYTHONINTERP_FOUND)
1818
add_oscap_test("all_python.sh")
1919
endif()
2020

21+
add_oscap_test("test_ansible_yaml_block_scalar.sh")
2122
add_oscap_test("test_xccdf_shall_pass1.sh")
2223
add_oscap_test("test_xccdf_shall_pass2.sh")
2324
add_oscap_test("test_xccdf_shall_pass3.sh")
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
3+
4+
- hosts: all
5+
vars:
6+
tasks:
7+
- name: Make sure contents of /etc/audit/rules.d/10-base-config.rules are as expected
8+
copy:
9+
dest: /etc/audit/rules.d/10-base-config.rules
10+
content: |+
11+
## First rule - delete all
12+
-D
13+
14+
## Increase the buffers to survive stress events.
15+
## Make this bigger for busy systems
16+
-b 8192
17+
18+
## This determine how long to wait in burst of events
19+
--backlog_wait_time 60000
20+
21+
## Set failure mode to syslog
22+
-f 1
23+
24+
25+
force: true
26+
when: ansible_virtualization_role != "guest" or ansible_virtualization_type != "docker"
27+
tags:
28+
- audit_basic_configuration
29+
- medium_severity
30+
- restrict_strategy
31+
- low_complexity
32+
- low_disruption
33+
- no_reboot_needed
34+
- CCE-82462-3
35+
- NIST-800-53-AU-2(a)
36+
37+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
. $builddir/tests/test_common.sh
3+
4+
set -e
5+
set -o pipefail
6+
7+
profile="xccdf_moc.elpmaxe.www_profile_standard"
8+
9+
name=$(basename $0 .sh)
10+
stderr=$(mktemp -t ${name}.err.XXXXXX)
11+
playbook=$(mktemp -t ${name}.yml.XXXXXX)
12+
playbook_without_header=$(mktemp -t ${name}.yml.XXXXXX)
13+
14+
# Generate an Ansible playbook from a profile in SDS file
15+
$OSCAP xccdf generate fix --profile $profile --fix-type ansible "$srcdir/$name.xccdf.xml" >$playbook 2>$stderr
16+
sed '/^#/d' $playbook > $playbook_without_header
17+
diff -u $playbook_without_header $srcdir/$name.playbook.yml
18+
[ -f $stderr ]; [ ! -s $stderr ]; rm $stderr
19+
20+
rm $playbook
21+
rm $playbook_without_header
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Benchmark xmlns="http://checklists.nist.gov/xccdf/1.2" id="xccdf_moc.elpmaxe.www_benchmark_test">
3+
<status>incomplete</status>
4+
<title>Security Benchmark</title>
5+
<description xml:lang="en-US">A sample benchmark</description>
6+
<version>1.0</version>
7+
<Profile id="xccdf_moc.elpmaxe.www_profile_standard">
8+
<title xml:lang="en-US">Standard System Security Profile</title>
9+
<description xml:lang="en-US">This profile contains rules to ensure standard security baseline of your system.</description>
10+
<select idref="xccdf_moc.elpmaxe.www_rule_1" selected="true"/>
11+
</Profile>
12+
<Rule selected="false" id="xccdf_moc.elpmaxe.www_rule_1">
13+
<title>Passing rule</title>
14+
<fix id="ansible_fix_for_passing_rule" system="urn:xccdf:fix:script:ansible">- name: Make sure contents of /etc/audit/rules.d/10-base-config.rules are as expected
15+
copy:
16+
dest: /etc/audit/rules.d/10-base-config.rules
17+
content: |+
18+
## First rule - delete all
19+
-D
20+
21+
## Increase the buffers to survive stress events.
22+
## Make this bigger for busy systems
23+
-b 8192
24+
25+
## This determine how long to wait in burst of events
26+
--backlog_wait_time 60000
27+
28+
## Set failure mode to syslog
29+
-f 1
30+
31+
32+
force: true
33+
when: ansible_virtualization_role != "guest" or ansible_virtualization_type != "docker"
34+
tags:
35+
- audit_basic_configuration
36+
- medium_severity
37+
- restrict_strategy
38+
- low_complexity
39+
- low_disruption
40+
- no_reboot_needed
41+
- CCE-82462-3
42+
- NIST-800-53-AU-2(a)
43+
</fix>
44+
<check system="http://oval.mitre.org/XMLSchema/oval-definitions-5">
45+
<check-content-ref href="oval/pass/oval.xml" name="oval:moc.elpmaxe.www:def:1"/>
46+
</check>
47+
</Rule>
48+
</Benchmark>

0 commit comments

Comments
 (0)