|
1
|
|
|
# |
|
2
|
|
|
# Copyright (C) 2021 Red Hat, Inc. |
|
3
|
|
|
# |
|
4
|
|
|
# This copyrighted material is made available to anyone wishing to use, |
|
5
|
|
|
# modify, copy, or redistribute it subject to the terms and conditions of |
|
6
|
|
|
# the GNU General Public License v.2, or (at your option) any later version. |
|
7
|
|
|
# This program is distributed in the hope that it will be useful, but WITHOUT |
|
8
|
|
|
# ANY WARRANTY expressed or implied, including the implied warranties of |
|
9
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|
10
|
|
|
# Public License for more details. You should have received a copy of the |
|
11
|
|
|
# GNU General Public License along with this program; if not, write to the |
|
12
|
|
|
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
13
|
|
|
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the |
|
14
|
|
|
# source code or documentation are not subject to the GNU General Public |
|
15
|
|
|
# License and may only be used or replicated with the express permission of |
|
16
|
|
|
# Red Hat, Inc. |
|
17
|
|
|
# |
|
18
|
|
|
# Red Hat Author(s): Jan Černý <[email protected]> |
|
19
|
|
|
# |
|
20
|
|
|
|
|
21
|
|
|
from collections import namedtuple |
|
22
|
|
|
import os |
|
23
|
|
|
import re |
|
24
|
|
|
import xml.etree.ElementTree as ET |
|
25
|
|
|
|
|
26
|
|
|
from org_fedora_oscap.content_handling import parse_HTML_from_content |
|
27
|
|
|
|
|
28
|
|
|
# namedtuple class (not a constant, pylint!) for info about a XCCDF profile |
|
29
|
|
|
# pylint: disable-msg=C0103 |
|
30
|
|
|
ProfileInfo = namedtuple("ProfileInfo", ["id", "title", "description"]) |
|
31
|
|
|
|
|
32
|
|
|
ns = { |
|
33
|
|
|
"ds": "http://scap.nist.gov/schema/scap/source/1.2", |
|
34
|
|
|
"xccdf-1.1": "http://checklists.nist.gov/xccdf/1.1", |
|
35
|
|
|
"xccdf-1.2": "http://checklists.nist.gov/xccdf/1.2", |
|
36
|
|
|
"xlink": "http://www.w3.org/1999/xlink" |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
class SCAPContentHandlerError(Exception): |
|
41
|
|
|
"""Exception class for errors related to SCAP content handling.""" |
|
42
|
|
|
pass |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
class SCAPContentHandler: |
|
46
|
|
|
def __init__(self, file_path, tailoring_file_path=None): |
|
47
|
|
|
""" |
|
48
|
|
|
Constructor for the SCAPContentHandler class. |
|
49
|
|
|
|
|
50
|
|
|
:param file_path: path to an SCAP file (only SCAP source data streams, |
|
51
|
|
|
XCCDF files and tailoring files are supported) |
|
52
|
|
|
:type file_path: str |
|
53
|
|
|
:param tailoring_file_path: path to the tailoring file, can be None if |
|
54
|
|
|
no tailoring exists |
|
55
|
|
|
:type tailoring_file_path: str |
|
56
|
|
|
""" |
|
57
|
|
|
self.file_path = file_path |
|
58
|
|
|
tree = ET.parse(file_path) |
|
59
|
|
|
self.root = tree.getroot() |
|
60
|
|
|
if not tailoring_file_path: |
|
61
|
|
|
self.tailoring = None |
|
62
|
|
|
else: |
|
63
|
|
|
self.tailoring = ET.parse(tailoring_file_path).getroot() |
|
64
|
|
|
self.scap_type = self._get_scap_type(self.root) |
|
65
|
|
|
self._data_stream_id = None |
|
66
|
|
|
self._checklist_id = None |
|
67
|
|
|
|
|
68
|
|
|
def _get_scap_type(self, root): |
|
69
|
|
|
if root.tag == f"{{{ns['ds']}}}data-stream-collection": |
|
70
|
|
|
return "SCAP_SOURCE_DATA_STREAM" |
|
71
|
|
|
elif (root.tag == f"{{{ns['xccdf-1.1']}}}Benchmark" or |
|
72
|
|
|
root.tag == f"{{{ns['xccdf-1.2']}}}Benchmark"): |
|
73
|
|
|
return "XCCDF" |
|
74
|
|
|
elif (root.tag == f"{{{ns['xccdf-1.1']}}}Tailoring" or |
|
75
|
|
|
root.tag == f"{{{ns['xccdf-1.2']}}}Tailoring"): |
|
76
|
|
|
return "TAILORING" |
|
77
|
|
|
else: |
|
78
|
|
|
msg = f"Unsupported SCAP content type {root.tag}" |
|
79
|
|
|
raise SCAPContentHandlerError(msg) |
|
80
|
|
|
|
|
81
|
|
|
def get_data_streams_checklists(self): |
|
82
|
|
|
""" |
|
83
|
|
|
Method to get data streams and their checklists found in the SCAP |
|
84
|
|
|
source data stream represented by the SCAPContentHandler. |
|
85
|
|
|
|
|
86
|
|
|
:return: a dictionary consisting of the IDs of the data streams as keys |
|
87
|
|
|
and lists of their checklists' IDs as values |
|
88
|
|
|
None if the file isn't a SCAP source data stream |
|
89
|
|
|
:rtype: dict(str -> list of strings) |
|
90
|
|
|
""" |
|
91
|
|
|
if self.scap_type != "SCAP_SOURCE_DATA_STREAM": |
|
92
|
|
|
return None |
|
93
|
|
|
checklists = {} |
|
94
|
|
|
for data_stream in self.root.findall("ds:data-stream", ns): |
|
95
|
|
|
data_stream_id = data_stream.get("id") |
|
96
|
|
|
crefs = [] |
|
97
|
|
|
for cref in data_stream.findall( |
|
98
|
|
|
"ds:checklists/ds:component-ref", ns): |
|
99
|
|
|
cref_id = cref.get("id") |
|
100
|
|
|
crefs.append(cref_id) |
|
101
|
|
|
checklists[data_stream_id] = crefs |
|
102
|
|
|
return checklists |
|
103
|
|
|
|
|
104
|
|
|
def _parse_profiles_from_xccdf(self, benchmark): |
|
105
|
|
|
if benchmark is None: |
|
106
|
|
|
return [] |
|
107
|
|
|
|
|
108
|
|
|
# Find out the namespace of the benchmark element |
|
109
|
|
|
match = re.match(r"^\{([^}]+)\}", benchmark.tag) |
|
110
|
|
|
if match is None: |
|
111
|
|
|
raise SCAPContentHandlerError("The document has no namespace.") |
|
112
|
|
|
root_element_ns = match.groups()[0] |
|
113
|
|
|
for prefix, uri in ns.items(): |
|
114
|
|
|
if uri == root_element_ns: |
|
115
|
|
|
xccdf_ns_prefix = prefix |
|
116
|
|
|
break |
|
117
|
|
|
else: |
|
118
|
|
|
raise SCAPContentHandlerError( |
|
119
|
|
|
f"Unsupported XML namespace {root_element_ns}") |
|
120
|
|
|
|
|
121
|
|
|
profiles = [] |
|
122
|
|
|
for profile in benchmark.findall(f"{xccdf_ns_prefix}:Profile", ns): |
|
123
|
|
|
profile_id = profile.get("id") |
|
124
|
|
|
title = profile.find(f"{xccdf_ns_prefix}:title", ns) |
|
125
|
|
|
description = profile.find(f"{xccdf_ns_prefix}:description", ns) |
|
126
|
|
|
if description is None: |
|
127
|
|
|
description_text = "" |
|
128
|
|
|
else: |
|
129
|
|
|
description_text = parse_HTML_from_content(description.text) |
|
130
|
|
|
profile_info = ProfileInfo( |
|
131
|
|
|
profile_id, title.text, description_text) |
|
132
|
|
|
profiles.append(profile_info) |
|
133
|
|
|
# if there are no profiles we would like to prevent empty profile |
|
134
|
|
|
# selection list in the GUI so we create the default profile |
|
135
|
|
|
if len(profiles) == 0: |
|
136
|
|
|
default_profile = ProfileInfo( |
|
137
|
|
|
"default", |
|
138
|
|
|
"Default", |
|
139
|
|
|
"The implicit XCCDF profile. Usually, the default profile " |
|
140
|
|
|
"contains no rules.") |
|
141
|
|
|
profiles.append(default_profile) |
|
142
|
|
|
return profiles |
|
143
|
|
|
|
|
144
|
|
|
def select_checklist(self, data_stream_id, checklist_id): |
|
145
|
|
|
""" |
|
146
|
|
|
Method to select a specific XCCDF Benchmark using |
|
147
|
|
|
:param data_stream_id: value of ds:data-stream/@id |
|
148
|
|
|
:type data_stream_id: str |
|
149
|
|
|
:param checklist_id: value of ds:component-ref/@id pointing to |
|
150
|
|
|
an xccdf:Benchmark |
|
151
|
|
|
:type checklist_id: str |
|
152
|
|
|
:return: None |
|
153
|
|
|
|
|
154
|
|
|
""" |
|
155
|
|
|
self._data_stream_id = data_stream_id |
|
156
|
|
|
self._checklist_id = checklist_id |
|
157
|
|
|
|
|
158
|
|
|
def _find_benchmark_in_source_data_stream(self): |
|
159
|
|
|
cref_xpath = f"ds:data-stream[@id='{self._data_stream_id}']/" \ |
|
160
|
|
|
f"ds:checklists/ds:component-ref[@id='{self._checklist_id}']" |
|
161
|
|
|
cref = self.root.find(cref_xpath, ns) |
|
162
|
|
|
if cref is None: |
|
163
|
|
|
msg = f"Can't find ds:component-ref " \ |
|
164
|
|
|
f"with id='{self._checklist_id}' " \ |
|
165
|
|
|
f"in ds:datastream with id='{self._data_stream_id}'" |
|
166
|
|
|
raise SCAPContentHandlerError(msg) |
|
167
|
|
|
cref_href = cref.get(f"{{{ns['xlink']}}}href") |
|
168
|
|
|
if cref_href is None: |
|
169
|
|
|
msg = f"The ds:component-ref with id='{self._checklist_id} '" \ |
|
170
|
|
|
f"in ds:datastream with id='{self._data_stream_id}' " \ |
|
171
|
|
|
f"doesn't have a xlink:href attribute." |
|
172
|
|
|
raise SCAPContentHandlerError(msg) |
|
173
|
|
|
if not cref_href.startswith("#"): |
|
174
|
|
|
msg = f"The component {cref_href} isn't local." |
|
175
|
|
|
raise SCAPContentHandlerError(msg) |
|
176
|
|
|
component_id = cref_href[1:] |
|
177
|
|
|
component = self.root.find( |
|
178
|
|
|
f"ds:component[@id='{component_id}']", ns) |
|
179
|
|
|
if component is None: |
|
180
|
|
|
msg = f"Can't find component {component_id}" |
|
181
|
|
|
raise SCAPContentHandlerError(msg) |
|
182
|
|
|
benchmark = component.find("xccdf-1.1:Benchmark", ns) |
|
183
|
|
|
if benchmark is None: |
|
184
|
|
|
benchmark = component.find("xccdf-1.2:Benchmark", ns) |
|
185
|
|
|
if benchmark is None: |
|
186
|
|
|
msg = f"The component {cref_href} doesn't contain an XCCDF " \ |
|
187
|
|
|
"Benchmark." |
|
188
|
|
|
raise SCAPContentHandlerError(msg) |
|
189
|
|
|
return benchmark |
|
190
|
|
|
|
|
191
|
|
|
def get_profiles(self): |
|
192
|
|
|
""" |
|
193
|
|
|
Method to get a list of profiles defined in the currently selected |
|
194
|
|
|
checklist that is defined in the currently selected data stream. |
|
195
|
|
|
|
|
196
|
|
|
:return: list of profiles found in the checklist |
|
197
|
|
|
:rtype: list of ProfileInfo instances |
|
198
|
|
|
|
|
199
|
|
|
""" |
|
200
|
|
|
if self.scap_type not in ("XCCDF", "SCAP_SOURCE_DATA_STREAM"): |
|
201
|
|
|
msg = f"Unsupported SCAP content type '{self.scap_type}'." |
|
202
|
|
|
raise SCAPContentHandlerError(msg) |
|
203
|
|
|
if self.scap_type == "XCCDF" and ( |
|
204
|
|
|
self._data_stream_id is not None or |
|
205
|
|
|
self._checklist_id is not None): |
|
206
|
|
|
msg = "For XCCDF documents, the data_stream_id and checklist_id " \ |
|
207
|
|
|
"must be both None." |
|
208
|
|
|
raise SCAPContentHandlerError(msg) |
|
209
|
|
|
if self.scap_type == "SCAP_SOURCE_DATA_STREAM" and ( |
|
210
|
|
|
self._data_stream_id is None or self._checklist_id is None): |
|
211
|
|
|
msg = "For SCAP source data streams, data_stream_id and " \ |
|
212
|
|
|
"checklist_id must be both different than None" |
|
213
|
|
|
raise SCAPContentHandlerError(msg) |
|
214
|
|
|
|
|
215
|
|
|
if self.scap_type == "SCAP_SOURCE_DATA_STREAM": |
|
216
|
|
|
benchmark = self._find_benchmark_in_source_data_stream() |
|
217
|
|
|
else: |
|
218
|
|
|
benchmark = self.root |
|
219
|
|
|
benchmark_profiles = self._parse_profiles_from_xccdf(benchmark) |
|
220
|
|
|
tailoring_profiles = self._parse_profiles_from_xccdf(self.tailoring) |
|
221
|
|
|
return benchmark_profiles + tailoring_profiles |
|
222
|
|
|
|