|
1
|
1 |
|
import pytest |
|
2
|
|
|
|
|
3
|
1 |
|
from openscap_report.scap_results_parser.data_structures.data_structures import ( |
|
4
|
|
|
Report, Rule) |
|
5
|
1 |
|
from openscap_report.scap_results_parser.scap_results_parser import \ |
|
6
|
|
|
SCAPResultsParser |
|
7
|
|
|
|
|
8
|
1 |
|
from ..constants import (PATH_TO_ARF, PATH_TO_ARF_SCANNED_ON_CONTAINER, |
|
9
|
|
|
PATH_TO_ARF_WITH_MULTI_CHECK, |
|
10
|
|
|
PATH_TO_ARF_WITH_OS_CPE_CHECK, |
|
11
|
|
|
PATH_TO_ARF_WITHOUT_INFO, |
|
12
|
|
|
PATH_TO_ARF_WITHOUT_SYSTEM_DATA, |
|
13
|
|
|
PATH_TO_EMPTY_XML_FILE, PATH_TO_REMEDIATIONS_SCRIPTS, |
|
14
|
|
|
PATH_TO_RULE_AND_CPE_CHECK_ARF, |
|
15
|
|
|
PATH_TO_RULE_AND_CPE_CHECK_XCCDF, |
|
16
|
|
|
PATH_TO_SIMPLE_RULE_FAIL_ARF, |
|
17
|
|
|
PATH_TO_SIMPLE_RULE_FAIL_XCCDF, |
|
18
|
|
|
PATH_TO_SIMPLE_RULE_PASS_ARF, |
|
19
|
|
|
PATH_TO_SIMPLE_RULE_PASS_XCCDF, PATH_TO_XCCDF, |
|
20
|
|
|
PATH_TO_XCCDF_WITH_MULTI_CHECK, |
|
21
|
|
|
PATH_TO_XCCDF_WITHOUT_INFO, |
|
22
|
|
|
PATH_TO_XCCDF_WITHOUT_SYSTEM_DATA) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
1 |
|
def get_parser(file_path): |
|
26
|
1 |
|
xml_data = None |
|
27
|
1 |
|
with open(file_path, "r", encoding="utf-8") as xml_report: |
|
28
|
1 |
|
xml_data = xml_report.read().encode() |
|
29
|
1 |
|
return SCAPResultsParser(xml_data) |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
1 |
|
@pytest.mark.parametrize("file_path, result", [ |
|
33
|
|
|
(PATH_TO_ARF, True), |
|
34
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_ARF, True), |
|
35
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_ARF, True), |
|
36
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_ARF, True), |
|
37
|
|
|
(PATH_TO_ARF_WITHOUT_INFO, True), |
|
38
|
|
|
(PATH_TO_ARF_WITHOUT_SYSTEM_DATA, True), |
|
39
|
|
|
(PATH_TO_ARF_SCANNED_ON_CONTAINER, True), |
|
40
|
|
|
(PATH_TO_ARF_WITH_OS_CPE_CHECK, True), |
|
41
|
|
|
(PATH_TO_XCCDF, False), |
|
42
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_XCCDF, False), |
|
43
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_XCCDF, False), |
|
44
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_XCCDF, False), |
|
45
|
|
|
(PATH_TO_XCCDF_WITHOUT_INFO, False), |
|
46
|
|
|
(PATH_TO_XCCDF_WITHOUT_SYSTEM_DATA, False), |
|
47
|
|
|
(PATH_TO_EMPTY_XML_FILE, False), |
|
48
|
|
|
]) |
|
49
|
1 |
|
def test_validation(file_path, result): |
|
50
|
1 |
|
parser = get_parser(file_path) |
|
51
|
1 |
|
assert parser.validate(parser.arf_schemas_path) == result |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
1 |
|
@pytest.mark.parametrize("file_path, number_of_cpe_platforms, os_cpe_platform", [ |
|
55
|
|
|
(PATH_TO_ARF, 13, "cpe:/o:fedoraproject:fedora:32"), |
|
56
|
|
|
(PATH_TO_XCCDF, 13, "cpe:/o:fedoraproject:fedora:32"), |
|
57
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_ARF, 0, ""), |
|
58
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_ARF, 0, ""), |
|
59
|
|
|
(PATH_TO_ARF_WITHOUT_INFO, 0, ""), |
|
60
|
|
|
(PATH_TO_ARF_WITHOUT_SYSTEM_DATA, 0, ""), |
|
61
|
|
|
(PATH_TO_ARF_SCANNED_ON_CONTAINER, 6, "cpe:/o:fedoraproject:fedora:35"), |
|
62
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_ARF, 1, "cpe:/o:example:applicable:5"), |
|
63
|
|
|
(PATH_TO_ARF_WITH_OS_CPE_CHECK, 0, "cpe:/o:fedoraproject:fedora:1"), |
|
64
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_XCCDF, 0, ""), |
|
65
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_XCCDF, 0, ""), |
|
66
|
|
|
(PATH_TO_XCCDF_WITHOUT_INFO, 0, ""), |
|
67
|
|
|
(PATH_TO_XCCDF_WITHOUT_SYSTEM_DATA, 0, ""), |
|
68
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_XCCDF, 1, "cpe:/o:example:applicable:5"), |
|
69
|
|
|
]) |
|
70
|
1 |
|
def test_get_profile_info(file_path, number_of_cpe_platforms, os_cpe_platform): |
|
71
|
1 |
|
parser = get_parser(file_path) |
|
72
|
1 |
|
report = parser.get_profile_info() |
|
73
|
1 |
|
assert len(report.cpe_platforms) == number_of_cpe_platforms |
|
74
|
1 |
|
assert report.platform == os_cpe_platform |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
1 |
|
@pytest.mark.parametrize("file_path, number_of_rules", [ |
|
78
|
|
|
(PATH_TO_ARF, 714), |
|
79
|
|
|
(PATH_TO_XCCDF, 714), |
|
80
|
|
|
(PATH_TO_ARF_SCANNED_ON_CONTAINER, 121), |
|
81
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_ARF, 1), |
|
82
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_ARF, 1), |
|
83
|
|
|
(PATH_TO_ARF_WITHOUT_INFO, 1), |
|
84
|
|
|
(PATH_TO_ARF_WITHOUT_SYSTEM_DATA, 1), |
|
85
|
|
|
(PATH_TO_ARF_WITH_OS_CPE_CHECK, 1), |
|
86
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_ARF, 3), |
|
87
|
|
|
(PATH_TO_SIMPLE_RULE_PASS_XCCDF, 1), |
|
88
|
|
|
(PATH_TO_SIMPLE_RULE_FAIL_XCCDF, 1), |
|
89
|
|
|
(PATH_TO_XCCDF_WITHOUT_INFO, 1), |
|
90
|
|
|
(PATH_TO_XCCDF_WITHOUT_SYSTEM_DATA, 1), |
|
91
|
|
|
(PATH_TO_RULE_AND_CPE_CHECK_XCCDF, 3), |
|
92
|
|
|
]) |
|
93
|
1 |
|
def test_get_info_about_rules_in_profile(file_path, number_of_rules): |
|
94
|
1 |
|
parser = get_parser(file_path) |
|
95
|
1 |
|
parser.process_groups_or_rules() |
|
96
|
1 |
|
assert len(parser.rules.keys()) == number_of_rules |
|
97
|
1 |
|
for rule in parser.rules.values(): |
|
98
|
1 |
|
assert isinstance(rule, Rule) |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
1 |
|
@pytest.mark.parametrize("file_path, contains_oval_tree", [ |
|
102
|
|
|
(PATH_TO_ARF, True), |
|
103
|
|
|
(PATH_TO_XCCDF, False), |
|
104
|
|
|
]) |
|
105
|
1 |
|
def test_parse_report(file_path, contains_oval_tree): |
|
106
|
1 |
|
parser = get_parser(file_path) |
|
107
|
1 |
|
report = parser.parse_report() |
|
108
|
1 |
|
assert isinstance(report, Report) |
|
109
|
1 |
|
assert report.profile_name is not None |
|
110
|
1 |
|
assert report.rules is not None |
|
111
|
1 |
|
rule_id = "xccdf_org.ssgproject.content_rule_accounts_passwords_pam_faillock_deny" |
|
112
|
1 |
|
assert isinstance(report.rules[rule_id], Rule) |
|
113
|
1 |
|
assert (report.rules[rule_id].oval_tree is not None) == contains_oval_tree |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
1 |
|
@pytest.mark.parametrize("file_path, contains_rules_some_multi_check_rule", [ |
|
117
|
|
|
(PATH_TO_ARF, False), |
|
118
|
|
|
(PATH_TO_XCCDF, False), |
|
119
|
|
|
(PATH_TO_XCCDF_WITH_MULTI_CHECK, True), |
|
120
|
|
|
(PATH_TO_ARF_WITH_MULTI_CHECK, True), |
|
121
|
|
|
]) |
|
122
|
1 |
|
def test_multi_check(file_path, contains_rules_some_multi_check_rule): |
|
123
|
1 |
|
parser = get_parser(file_path) |
|
124
|
1 |
|
parser.process_groups_or_rules() |
|
125
|
1 |
|
result = False |
|
126
|
1 |
|
for rule in parser.rules.values(): |
|
127
|
1 |
|
if rule.multi_check: |
|
128
|
1 |
|
result = True |
|
129
|
1 |
|
assert result == contains_rules_some_multi_check_rule |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
1 |
|
@pytest.mark.parametrize("rule, result", [ |
|
133
|
|
|
( |
|
134
|
|
|
"xccdf_org.ssgproject.content_rule_prefer_64bit_os", |
|
135
|
|
|
"Prefer installation of 64-bit operating systems when the CPU supports it." |
|
136
|
|
|
), |
|
137
|
|
|
( |
|
138
|
|
|
"xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled", |
|
139
|
|
|
( |
|
140
|
|
|
"\nTo activate locking of the screensaver in the GNOME3 desktop" |
|
141
|
|
|
" when it is activated,\nadd or set <code>lock-enabled</code>" |
|
142
|
|
|
" to <code>true</code> in\n<code>/etc/dconf/db/local.d/00-security-settings</code>." |
|
143
|
|
|
" For example:\n<pre>[org/gnome/desktop/screensaver]\nlock-enabled=true\n</pre>\n" |
|
144
|
|
|
"Once the settings have been added, add a lock to\n" |
|
145
|
|
|
"<code>/etc/dconf/db/local.d/locks/00-security-settings-lock</code> " |
|
146
|
|
|
"to prevent user modification.\nFor example:\n" |
|
147
|
|
|
"<pre>/org/gnome/desktop/screensaver/lock-enabled</pre>\n" |
|
148
|
|
|
"After the settings have been set, run <code>dconf update</code>." |
|
149
|
|
|
) |
|
150
|
|
|
), |
|
151
|
|
|
( |
|
152
|
|
|
"xccdf_org.ssgproject.content_rule_auditd_data_retention_action_mail_acct", |
|
153
|
|
|
( |
|
154
|
|
|
"The <code>auditd</code> service can be configured to send email to\n" |
|
155
|
|
|
"a designated account in certain situations. Add or correct the following line\n" |
|
156
|
|
|
"in <code>/etc/audit/auditd.conf</code> to ensure that administrators are notified\n" |
|
157
|
|
|
"via email for those situations:\n<pre>action_mail_acct = root</pre>" |
|
158
|
|
|
) |
|
159
|
|
|
) |
|
160
|
|
|
]) |
|
161
|
1 |
|
def test_description(rule, result): |
|
162
|
1 |
|
parser = get_parser(PATH_TO_ARF) |
|
163
|
1 |
|
parser.process_groups_or_rules() |
|
164
|
1 |
|
assert parser.rules[rule].description == result |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
1 |
|
@pytest.mark.parametrize("rule, remediation_id, scripts", [ |
|
168
|
|
|
( |
|
169
|
|
|
"xccdf_org.ssgproject.content_rule_prefer_64bit_os", |
|
170
|
|
|
None, |
|
171
|
|
|
{} |
|
172
|
|
|
), |
|
173
|
|
|
( |
|
174
|
|
|
"xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled", |
|
175
|
|
|
"dconf_gnome_screensaver_lock_enabled", |
|
176
|
|
|
{ |
|
177
|
|
|
"urn:xccdf:fix:script:ansible": "dconf_gnome_screensaver_lock_enabled_ansible.txt", |
|
178
|
|
|
"urn:xccdf:fix:script:sh": "dconf_gnome_screensaver_lock_enabled_sh.txt" |
|
179
|
|
|
} |
|
180
|
|
|
), |
|
181
|
|
|
( |
|
182
|
|
|
"xccdf_org.ssgproject.content_rule_auditd_data_retention_action_mail_acct", |
|
183
|
|
|
"auditd_data_retention_action_mail_acct", |
|
184
|
|
|
{ |
|
185
|
|
|
"urn:xccdf:fix:script:sh": "auditd_data_retention_action_mail_acct_sh.txt", |
|
186
|
|
|
"urn:xccdf:fix:script:ansible": "auditd_data_retention_action_mail_acct_ansible.txt" |
|
187
|
|
|
} |
|
188
|
|
|
) |
|
189
|
|
|
]) |
|
190
|
1 |
|
def test_remediations(rule, remediation_id, scripts): |
|
191
|
1 |
|
parser = get_parser(PATH_TO_ARF) |
|
192
|
1 |
|
parser.process_groups_or_rules() |
|
193
|
1 |
|
for remediation in parser.rules[rule].remediations: |
|
194
|
1 |
|
assert remediation.remediation_id == remediation_id |
|
195
|
1 |
|
assert remediation.system in scripts |
|
196
|
1 |
|
path = PATH_TO_REMEDIATIONS_SCRIPTS / str(scripts[remediation.system]) |
|
197
|
1 |
|
with open(path, "r", encoding="utf-8") as script: |
|
198
|
1 |
|
data = script.read() |
|
199
|
|
|
assert data == remediation.fix |
|
200
|
|
|
|