|
1
|
|
|
""" |
|
2
|
|
|
A couple generic XCCDF utilities used by build-all-guides.py and |
|
3
|
|
|
build-all-remediation-roles.py |
|
4
|
|
|
|
|
5
|
|
|
Author: Martin Preisler <[email protected]> |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
import re |
|
9
|
|
|
|
|
10
|
|
|
from ssg._constants import XCCDF11_NS, XCCDF12_NS |
|
11
|
|
|
|
|
12
|
|
|
# if a profile ID ends with a string listed here we skip it |
|
13
|
|
|
PROFILE_ID_BLACKLIST = ["test", "index", "default"] |
|
14
|
|
|
# filler XCCDF 1.2 prefix which we will strip to avoid very long filenames |
|
15
|
|
|
PROFILE_ID_PREFIX = ("^xccdf_org.*content_profile_") |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
def get_benchmark_ids_titles_for_input(input_tree): |
|
|
|
|
|
|
19
|
|
|
ret = {} |
|
20
|
|
|
|
|
21
|
|
|
def scrape_benchmarks(root_element, namespace, dest): |
|
|
|
|
|
|
22
|
|
|
candidates = \ |
|
23
|
|
|
list(root_element.findall(".//{%s}Benchmark" % (namespace))) |
|
24
|
|
|
if root_element.tag == "{%s}Benchmark" % (namespace): |
|
25
|
|
|
candidates.append(root_element) |
|
26
|
|
|
|
|
27
|
|
|
for elem in candidates: |
|
28
|
|
|
id_ = elem.get("id") |
|
29
|
|
|
if id_ is None: |
|
30
|
|
|
continue |
|
31
|
|
|
|
|
32
|
|
|
title = "<unknown>" |
|
33
|
|
|
for element in elem.findall("{%s}title" % (namespace)): |
|
34
|
|
|
title = element.text |
|
35
|
|
|
break |
|
36
|
|
|
|
|
37
|
|
|
dest[id_] = title |
|
38
|
|
|
|
|
39
|
|
|
input_root = input_tree.getroot() |
|
40
|
|
|
|
|
41
|
|
|
scrape_benchmarks( |
|
42
|
|
|
input_root, XCCDF11_NS, ret |
|
43
|
|
|
) |
|
44
|
|
|
scrape_benchmarks( |
|
45
|
|
|
input_root, XCCDF12_NS, ret |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
return ret |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
def get_profile_choices_for_input(input_tree, benchmark_id, tailoring_tree): |
|
52
|
|
|
"""Returns a dictionary that maps profile_ids to their respective titles. |
|
53
|
|
|
""" |
|
54
|
|
|
|
|
55
|
|
|
# Ideally oscap would have a command line to do this, but as of now it |
|
56
|
|
|
# doesn't so we have to implement it ourselves. Importing openscap Python |
|
57
|
|
|
# bindings is nasty and overkill for this. |
|
58
|
|
|
|
|
59
|
|
|
ret = {} |
|
60
|
|
|
|
|
61
|
|
|
def scrape_profiles(root_element, namespace, dest): |
|
|
|
|
|
|
62
|
|
|
candidates = \ |
|
63
|
|
|
list(root_element.findall(".//{%s}Benchmark" % (namespace))) |
|
64
|
|
|
if root_element.tag == "{%s}Benchmark" % (namespace): |
|
65
|
|
|
candidates.append(root_element) |
|
66
|
|
|
|
|
67
|
|
|
for benchmark in candidates: |
|
68
|
|
|
if benchmark.get("id") != benchmark_id: |
|
69
|
|
|
continue |
|
70
|
|
|
|
|
71
|
|
|
for elem in benchmark.findall(".//{%s}Profile" % (namespace)): |
|
72
|
|
|
id_ = elem.get("id") |
|
73
|
|
|
if id_ is None: |
|
74
|
|
|
continue |
|
75
|
|
|
|
|
76
|
|
|
title = "<unknown>" |
|
77
|
|
|
for element in elem.findall("{%s}title" % (namespace)): |
|
78
|
|
|
title = element.text |
|
79
|
|
|
break |
|
80
|
|
|
|
|
81
|
|
|
dest[id_] = title |
|
82
|
|
|
|
|
83
|
|
|
input_root = input_tree.getroot() |
|
84
|
|
|
|
|
85
|
|
|
scrape_profiles( |
|
86
|
|
|
input_root, XCCDF11_NS, ret |
|
87
|
|
|
) |
|
88
|
|
|
scrape_profiles( |
|
89
|
|
|
input_root, XCCDF12_NS, ret |
|
90
|
|
|
) |
|
91
|
|
|
|
|
92
|
|
|
if tailoring_tree is not None: |
|
93
|
|
|
tailoring_root = tailoring_tree.getroot() |
|
94
|
|
|
|
|
95
|
|
|
scrape_profiles( |
|
96
|
|
|
tailoring_root, XCCDF11_NS, ret |
|
97
|
|
|
) |
|
98
|
|
|
scrape_profiles( |
|
99
|
|
|
tailoring_root, XCCDF12_NS, ret |
|
100
|
|
|
) |
|
101
|
|
|
|
|
102
|
|
|
return ret |
|
103
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
def get_profile_short_id(long_id): |
|
106
|
|
|
"""If given profile ID is the XCCDF 1.2 long ID this function shortens it |
|
107
|
|
|
""" |
|
108
|
|
|
|
|
109
|
|
|
if re.search(PROFILE_ID_PREFIX, long_id): |
|
110
|
|
|
return long_id[re.search(PROFILE_ID_PREFIX, long_id).end():] |
|
111
|
|
|
|
|
112
|
|
|
return long_id |
|
113
|
|
|
|
This check looks for invalid names for a range of different identifiers.
You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.
If your project includes a Pylint configuration file, the settings contained in that file take precedence.
To find out more about Pylint, please refer to their site.