|
1
|
|
|
import os |
|
2
|
|
|
import tempfile |
|
3
|
|
|
|
|
4
|
|
|
import yaml |
|
5
|
|
|
import pytest |
|
6
|
|
|
import xml.etree.ElementTree as ET |
|
7
|
|
|
from ssg.build_cpe import ProductCPEs |
|
8
|
|
|
|
|
9
|
|
|
import ssg.build_yaml |
|
10
|
|
|
from ssg.constants import cpe_language_namespace |
|
11
|
|
|
from ssg.yaml import open_raw |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), "..", "..", "..", ) |
|
15
|
|
|
DATADIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "data")) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def test_serialize_rule(): |
|
19
|
|
|
filename = PROJECT_ROOT + "/linux_os/guide/system/accounts/accounts-restrictions/password_storage/no_empty_passwords/rule.yml" |
|
20
|
|
|
rule_ds = ssg.build_yaml.Rule.from_yaml(filename) |
|
21
|
|
|
rule_as_dict = rule_ds.represent_as_dict() |
|
22
|
|
|
|
|
23
|
|
|
with tempfile.NamedTemporaryFile("w+", delete=True) as f: |
|
24
|
|
|
yaml.dump(rule_as_dict, f) |
|
25
|
|
|
rule_ds_reloaded = ssg.build_yaml.Rule.from_yaml(f.name) |
|
26
|
|
|
|
|
27
|
|
|
reloaded_dict = rule_ds_reloaded.represent_as_dict() |
|
28
|
|
|
|
|
29
|
|
|
# Those two should be really equal if there are no jinja macros in the rule def. |
|
30
|
|
|
assert rule_as_dict == reloaded_dict |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
TEST_TEMPLATE_DICT = { |
|
34
|
|
|
"backends": { |
|
35
|
|
|
"anaconda": True, |
|
36
|
|
|
"anaconda@rhel7": False, |
|
37
|
|
|
}, |
|
38
|
|
|
"vars": { |
|
39
|
|
|
"filesystem": "tmpfs", |
|
40
|
|
|
"filesystem@rhel7": "" |
|
41
|
|
|
}, |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
def test_make_items_product_specific(): |
|
46
|
|
|
rule = ssg.build_yaml.Rule("something") |
|
47
|
|
|
|
|
48
|
|
|
rule.identifiers = { |
|
49
|
|
|
"cce@rhel7": "CCE-27445-6", |
|
50
|
|
|
"cce@rhel8": "CCE-80901-2", |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
rule.template = TEST_TEMPLATE_DICT.copy() |
|
54
|
|
|
|
|
55
|
|
|
rule.normalize("rhel7") |
|
56
|
|
|
assert "cce@rhel7" not in rule.identifiers |
|
57
|
|
|
assert "cce@rhel8" not in rule.identifiers |
|
58
|
|
|
assert rule.identifiers["cce"] == "CCE-27445-6" |
|
59
|
|
|
|
|
60
|
|
|
assert "filesystem@rhel7" not in rule.template["vars"] |
|
61
|
|
|
assert rule.template["vars"]["filesystem"] == "" |
|
62
|
|
|
assert "anaconda@rhel7" not in rule.template["backends"] |
|
63
|
|
|
assert not rule.template["backends"]["anaconda"] |
|
64
|
|
|
|
|
65
|
|
|
rule.identifiers = { |
|
66
|
|
|
"cce": "CCE-27100-7", |
|
67
|
|
|
"cce@rhel7": "CCE-27445-6", |
|
68
|
|
|
} |
|
69
|
|
|
with pytest.raises(Exception) as exc: |
|
70
|
|
|
rule.normalize("rhel7") |
|
71
|
|
|
assert "'cce'" in str(exc) |
|
72
|
|
|
assert "identifiers" in str(exc) |
|
73
|
|
|
|
|
74
|
|
|
rule.identifiers = { |
|
75
|
|
|
"cce@rhel7": "CCE-27445-6", |
|
76
|
|
|
"cce": "CCE-27445-6", |
|
77
|
|
|
} |
|
78
|
|
|
rule.normalize("rhel7") |
|
79
|
|
|
assert "cce@rhel7" not in rule.identifiers |
|
80
|
|
|
assert rule.identifiers["cce"] == "CCE-27445-6" |
|
81
|
|
|
|
|
82
|
|
|
rule.references = { |
|
83
|
|
|
"stigid@rhel7": "RHEL-07-040370", |
|
84
|
|
|
"stigid": "tralala", |
|
85
|
|
|
} |
|
86
|
|
|
with pytest.raises(ValueError) as exc: |
|
87
|
|
|
rule.make_refs_and_identifiers_product_specific("rhel7") |
|
88
|
|
|
assert "stigid" in str(exc) |
|
89
|
|
|
|
|
90
|
|
|
rule.references = { |
|
91
|
|
|
"stigid@rhel7": "RHEL-07-040370", |
|
92
|
|
|
} |
|
93
|
|
|
rule.normalize("rhel7") |
|
94
|
|
|
assert rule.references["stigid"] == "RHEL-07-040370" |
|
95
|
|
|
|
|
96
|
|
|
rule.references = { |
|
97
|
|
|
"stigid@rhel7": "RHEL-07-040370", |
|
98
|
|
|
} |
|
99
|
|
|
rule.template = TEST_TEMPLATE_DICT.copy() |
|
100
|
|
|
|
|
101
|
|
|
assert "filesystem@rhel8" not in rule.template["vars"] |
|
102
|
|
|
assert rule.template["vars"]["filesystem"] == "tmpfs" |
|
103
|
|
|
assert "anaconda@rhel8" not in rule.template["backends"] |
|
104
|
|
|
assert rule.template["backends"]["anaconda"] |
|
105
|
|
|
|
|
106
|
|
|
rule.references = { |
|
107
|
|
|
"stigid@rhel7": "RHEL-07-040370,RHEL-07-057364", |
|
108
|
|
|
} |
|
109
|
|
|
with pytest.raises(ValueError, match="Rules can not have multiple STIG IDs."): |
|
110
|
|
|
rule.make_refs_and_identifiers_product_specific("rhel7") |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
def test_priority_ordering(): |
|
114
|
|
|
ORDER = ["ga", "be", "al"] |
|
115
|
|
|
to_order = ["alpha", "beta", "gamma"] |
|
116
|
|
|
ordered = ssg.build_yaml.reorder_according_to_ordering(to_order, ORDER) |
|
117
|
|
|
assert ordered == ["gamma", "beta", "alpha"] |
|
118
|
|
|
|
|
119
|
|
|
to_order = ["alpha", "beta", "gamma", "epsilon"] |
|
120
|
|
|
ordered = ssg.build_yaml.reorder_according_to_ordering(to_order, ORDER) |
|
121
|
|
|
assert ordered == ["gamma", "beta", "alpha", "epsilon"] |
|
122
|
|
|
|
|
123
|
|
|
to_order = ["alpha"] |
|
124
|
|
|
ordered = ssg.build_yaml.reorder_according_to_ordering(to_order, ORDER) |
|
125
|
|
|
assert ordered == ["alpha"] |
|
126
|
|
|
|
|
127
|
|
|
to_order = ["x"] |
|
128
|
|
|
ordered = ssg.build_yaml.reorder_according_to_ordering(to_order, ORDER) |
|
129
|
|
|
assert ordered == ["x"] |
|
130
|
|
|
|
|
131
|
|
|
to_order = ["alpha", "beta", "alnum", "gaha"] |
|
132
|
|
|
ordered = ssg.build_yaml.reorder_according_to_ordering( |
|
133
|
|
|
to_order, ORDER + ["gaha"], regex=".*ha") |
|
134
|
|
|
assert ordered[:2] == ["gaha", "alpha"] |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
@pytest.fixture |
|
138
|
|
|
def product_cpes(): |
|
139
|
|
|
product_yaml_path = os.path.join(DATADIR, "product.yml") |
|
140
|
|
|
product_yaml = open_raw(product_yaml_path) |
|
141
|
|
|
product_yaml["product_dir"] = os.path.dirname(product_yaml_path) |
|
142
|
|
|
return ProductCPEs(product_yaml) |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
def test_platform_from_text_unknown_platform(product_cpes): |
|
146
|
|
|
with pytest.raises(ssg.build_cpe.CPEDoesNotExist): |
|
147
|
|
|
ssg.build_yaml.Platform.from_text("something_bogus", product_cpes) |
|
148
|
|
|
|
|
149
|
|
|
|
|
150
|
|
View Code Duplication |
def test_platform_from_text_simple(product_cpes): |
|
|
|
|
|
|
151
|
|
|
platform = ssg.build_yaml.Platform.from_text("machine", product_cpes) |
|
152
|
|
|
assert platform.to_ansible_conditional() == \ |
|
153
|
|
|
"ansible_virtualization_type not in [\"docker\", \"lxc\", \"openvz\", \"podman\", \"container\"]" |
|
154
|
|
|
assert platform.to_bash_conditional() == \ |
|
155
|
|
|
"[ ! -f /.dockerenv ] && [ ! -f /run/.containerenv ]" |
|
156
|
|
|
platform_el = ET.fromstring(platform.to_xml_element()) |
|
157
|
|
|
assert platform_el.tag == "{%s}platform" % cpe_language_namespace |
|
158
|
|
|
assert platform_el.get("id") == "machine" |
|
159
|
|
|
logical_tests = platform_el.findall( |
|
160
|
|
|
"{%s}logical-test" % cpe_language_namespace) |
|
161
|
|
|
assert len(logical_tests) == 1 |
|
162
|
|
|
assert logical_tests[0].get("operator") == "AND" |
|
163
|
|
|
assert logical_tests[0].get("negate") == "false" |
|
164
|
|
|
fact_refs = logical_tests[0].findall( |
|
165
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
166
|
|
|
assert len(fact_refs) == 1 |
|
167
|
|
|
assert fact_refs[0].get("name") == "cpe:/a:machine" |
|
168
|
|
|
|
|
169
|
|
|
|
|
170
|
|
View Code Duplication |
def test_platform_from_text_simple_product_cpe(product_cpes): |
|
|
|
|
|
|
171
|
|
|
platform = ssg.build_yaml.Platform.from_text("rhel7-workstation", product_cpes) |
|
172
|
|
|
assert platform.to_bash_conditional() == "" |
|
173
|
|
|
assert platform.to_ansible_conditional() == "" |
|
174
|
|
|
platform_el = ET.fromstring(platform.to_xml_element()) |
|
175
|
|
|
assert platform_el.tag == "{%s}platform" % cpe_language_namespace |
|
176
|
|
|
assert platform_el.get("id") == "rhel7-workstation" |
|
177
|
|
|
logical_tests = platform_el.findall( |
|
178
|
|
|
"{%s}logical-test" % cpe_language_namespace) |
|
179
|
|
|
assert len(logical_tests) == 1 |
|
180
|
|
|
assert logical_tests[0].get("operator") == "AND" |
|
181
|
|
|
assert logical_tests[0].get("negate") == "false" |
|
182
|
|
|
fact_refs = logical_tests[0].findall( |
|
183
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
184
|
|
|
assert len(fact_refs) == 1 |
|
185
|
|
|
assert fact_refs[0].get("name") == \ |
|
186
|
|
|
"cpe:/o:redhat:enterprise_linux:7::workstation" |
|
187
|
|
|
|
|
188
|
|
|
|
|
189
|
|
View Code Duplication |
def test_platform_from_text_or(product_cpes): |
|
|
|
|
|
|
190
|
|
|
platform = ssg.build_yaml.Platform.from_text("ntp or chrony", product_cpes) |
|
191
|
|
|
assert platform.to_bash_conditional() == "( rpm --quiet -q chrony || rpm --quiet -q ntp )" |
|
192
|
|
|
assert platform.to_ansible_conditional() == \ |
|
193
|
|
|
"( \"chrony\" in ansible_facts.packages or \"ntp\" in ansible_facts.packages )" |
|
194
|
|
|
platform_el = ET.fromstring(platform.to_xml_element()) |
|
195
|
|
|
assert platform_el.tag == "{%s}platform" % cpe_language_namespace |
|
196
|
|
|
assert platform_el.get("id") == "chrony_or_ntp" |
|
197
|
|
|
logical_tests = platform_el.findall( |
|
198
|
|
|
"{%s}logical-test" % cpe_language_namespace) |
|
199
|
|
|
assert len(logical_tests) == 1 |
|
200
|
|
|
assert logical_tests[0].get("operator") == "OR" |
|
201
|
|
|
assert logical_tests[0].get("negate") == "false" |
|
202
|
|
|
fact_refs = logical_tests[0].findall( |
|
203
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
204
|
|
|
assert len(fact_refs) == 2 |
|
205
|
|
|
assert fact_refs[0].get("name") == "cpe:/a:chrony" |
|
206
|
|
|
assert fact_refs[1].get("name") == "cpe:/a:ntp" |
|
207
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
def test_platform_from_text_complex_expression(product_cpes): |
|
210
|
|
|
platform = ssg.build_yaml.Platform.from_text( |
|
211
|
|
|
"systemd and !yum and (ntp or chrony)", product_cpes) |
|
212
|
|
|
assert platform.to_bash_conditional() == "( rpm --quiet -q systemd && ( rpm --quiet -q chrony || rpm --quiet -q ntp ) && ! ( rpm --quiet -q yum ) )" |
|
213
|
|
|
assert platform.to_ansible_conditional() == "( \"systemd\" in ansible_facts.packages and ( \"chrony\" in ansible_facts.packages or \"ntp\" in ansible_facts.packages ) and not ( \"yum\" in ansible_facts.packages ) )" |
|
214
|
|
|
platform_el = ET.fromstring(platform.to_xml_element()) |
|
215
|
|
|
assert platform_el.tag == "{%s}platform" % cpe_language_namespace |
|
216
|
|
|
assert platform_el.get("id") == "systemd_and_chrony_or_ntp_and_not_yum" |
|
217
|
|
|
logical_tests = platform_el.findall( |
|
218
|
|
|
"{%s}logical-test" % cpe_language_namespace) |
|
219
|
|
|
assert len(logical_tests) == 1 |
|
220
|
|
|
assert logical_tests[0].get("operator") == "AND" |
|
221
|
|
|
assert logical_tests[0].get("negate") == "false" |
|
222
|
|
|
logical_tests_2 = logical_tests[0].findall( |
|
223
|
|
|
"{%s}logical-test" % cpe_language_namespace) |
|
224
|
|
|
assert len(logical_tests_2) == 2 |
|
225
|
|
|
assert logical_tests_2[0].get("operator") == "OR" |
|
226
|
|
|
assert logical_tests_2[0].get("negate") == "false" |
|
227
|
|
|
fact_refs = logical_tests_2[0].findall( |
|
228
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
229
|
|
|
assert len(fact_refs) == 2 |
|
230
|
|
|
assert fact_refs[0].get("name") == "cpe:/a:chrony" |
|
231
|
|
|
assert fact_refs[1].get("name") == "cpe:/a:ntp" |
|
232
|
|
|
assert logical_tests_2[1].get("operator") == "AND" |
|
233
|
|
|
assert logical_tests_2[1].get("negate") == "true" |
|
234
|
|
|
fact_refs_2 = logical_tests_2[1].findall( |
|
235
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
236
|
|
|
assert len(fact_refs_2) == 1 |
|
237
|
|
|
assert fact_refs_2[0].get("name") == "cpe:/a:yum" |
|
238
|
|
|
fact_refs_3 = logical_tests[0].findall( |
|
239
|
|
|
"{%s}fact-ref" % cpe_language_namespace) |
|
240
|
|
|
assert len(fact_refs_3) == 1 |
|
241
|
|
|
assert fact_refs_3[0].get("name") == "cpe:/a:systemd" |
|
242
|
|
|
|
|
243
|
|
|
|
|
244
|
|
|
def test_platform_equality(product_cpes): |
|
245
|
|
|
platform1 = ssg.build_yaml.Platform.from_text("ntp or chrony", product_cpes) |
|
246
|
|
|
platform2 = ssg.build_yaml.Platform.from_text("chrony or ntp", product_cpes) |
|
247
|
|
|
assert platform1 == platform2 |
|
248
|
|
|
platform3 = ssg.build_yaml.Platform.from_text("(chrony and ntp)", product_cpes) |
|
249
|
|
|
platform4 = ssg.build_yaml.Platform.from_text("chrony and ntp", product_cpes) |
|
250
|
|
|
assert platform3 == platform4 |
|
251
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
def test_platform_as_dict(product_cpes): |
|
254
|
|
|
pl = ssg.build_yaml.Platform.from_text("chrony and rhel7", product_cpes) |
|
255
|
|
|
# represent_as_dict is used during dump_yaml |
|
256
|
|
|
d = pl.represent_as_dict() |
|
257
|
|
|
assert d["name"] == "chrony_and_rhel7" |
|
258
|
|
|
# the "rhel7" platform doesn't have any conditionals |
|
259
|
|
|
# therefore the final conditional doesn't use it |
|
260
|
|
|
assert d["ansible_conditional"] == "( \"chrony\" in ansible_facts.packages )" |
|
261
|
|
|
assert d["bash_conditional"] == "( rpm --quiet -q chrony )" |
|
262
|
|
|
assert "xml_content" in d |
|
263
|
|
|
|