Passed
Pull Request — master (#59)
by Jan
07:51
created

test_rationale()   A

Complexity

Conditions 1

Size

Total Lines 44
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 44
ccs 5
cts 5
cp 1
rs 9.55
c 0
b 0
f 0
cc 1
nop 2
crap 1
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, result", [
168
    (
169
        "xccdf_org.ssgproject.content_rule_prefer_64bit_os",
170
        (
171
            "Use of a 64-bit operating system offers a few advantages, "
172
            "like a larger address space range for\nAddress Space Layout"
173
            " Randomization (ASLR) and systematic presence of No eXecute"
174
            " and Execute Disable (NX/XD) protection bits."
175
        )
176
    ),
177
    (
178
        "xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled",
179
        (
180
            "A session lock is a temporary action taken when a user stops work and"
181
            " moves away from the immediate physical vicinity\nof the information "
182
            "system but does not want to logout because of the temporary nature of the absense."
183
        )
184
    ),
185
    (
186
        "xccdf_org.ssgproject.content_rule_auditd_data_retention_action_mail_acct",
187
        (
188
            "Email sent to the root account is typically aliased to the\n"
189
            "administrators of the system, who can take appropriate action."
190
        )
191
    ),
192
    (
193
        "xccdf_org.ssgproject.content_rule_sudoers_explicit_command_args",
194
        (
195
            "Any argument can modify quite significantly the behavior of a"
196
            " program, whether regarding the\nrealized operation (read, write, delete, etc.)"
197
            " or accessed resources (path in a file system tree). To\navoid any possibility of"
198
            " misuse of a command by a user, the ambiguities must be removed at the\nlevel of its"
199
            " specification.\n\nFor example, on some systems, the kernel messages are only "
200
            "accessible by root.\nIf a user nevertheless must have the privileges to read them,"
201
            " the argument of the dmesg command has to be restricted\nin order to prevent "
202
            "the user from flushing the buffer through the -c option:\n"
203
            "<pre>\nuser ALL = dmesg \"\"\n</pre>"
204
        )
205
    )
206
])
207 1
def test_rationale(rule, result):
208 1
    parser = get_parser(PATH_TO_ARF)
209 1
    parser.process_groups_or_rules()
210 1
    assert parser.rules[rule].rationale == result
211
212
213 1
@pytest.mark.parametrize("rule, result", [
214
    (
215
        "xccdf_org.ssgproject.content_rule_prefer_64bit_os",
216
        ["There is no remediation besides installing a 64-bit operating system."]
217
    ),
218
    (
219
        "xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled",
220
        []
221
    ),
222
    (
223
        "xccdf_org.ssgproject.content_rule_auditd_data_retention_action_mail_acct",
224
        []
225
    ),
226
    (
227
        "xccdf_org.ssgproject.content_rule_sudoers_explicit_command_args",
228
        [
229
            (
230
                "This rule doesn't come with a remediation, as absence of arguments in"
231
                " the user spec doesn't mean that the command is intended to be executed "
232
                "with no arguments."
233
            ),
234
            (
235
                "The rule can produce false findings when an argument contains a"
236
                " comma - sudoers syntax allows comma escaping using backslash, but"
237
                " the check doesn't support that. For example,"
238
                " <code>root ALL=(ALL) echo 1\\,2</code> allows root to execute"
239
                " <code>echo 1,2</code>, but the check would interpret it as two commands "
240
                "<code>echo 1\\</code> and <code>2</code>."
241
            )
242
        ]
243
    )
244
])
245 1
def test_warnings(rule, result):
246 1
    parser = get_parser(PATH_TO_ARF)
247 1
    parser.process_groups_or_rules()
248 1
    assert parser.rules[rule].warnings == result
249
250
251 1
@pytest.mark.parametrize("rule, remediation_id, scripts", [
252
    (
253
        "xccdf_org.ssgproject.content_rule_prefer_64bit_os",
254
        None,
255
        {}
256
    ),
257
    (
258
        "xccdf_org.ssgproject.content_rule_dconf_gnome_screensaver_lock_enabled",
259
        "dconf_gnome_screensaver_lock_enabled",
260
        {
261
            "urn:xccdf:fix:script:ansible": "dconf_gnome_screensaver_lock_enabled_ansible.txt",
262
            "urn:xccdf:fix:script:sh": "dconf_gnome_screensaver_lock_enabled_sh.txt"
263
        }
264
    ),
265
    (
266
        "xccdf_org.ssgproject.content_rule_auditd_data_retention_action_mail_acct",
267
        "auditd_data_retention_action_mail_acct",
268
        {
269
            "urn:xccdf:fix:script:sh": "auditd_data_retention_action_mail_acct_sh.txt",
270
            "urn:xccdf:fix:script:ansible": "auditd_data_retention_action_mail_acct_ansible.txt"
271
        }
272
    )
273
])
274 1
def test_remediations(rule, remediation_id, scripts):
275 1
    parser = get_parser(PATH_TO_ARF)
276 1
    parser.process_groups_or_rules()
277 1
    for remediation in parser.rules[rule].remediations:
278 1
        assert remediation.remediation_id == remediation_id
279 1
        assert remediation.system in scripts
280 1
        path = PATH_TO_REMEDIATIONS_SCRIPTS / str(scripts[remediation.system])
281 1
        with open(path, "r", encoding="utf-8") as script:
282 1
            data = script.read()
283
            assert data == remediation.fix
284