1
|
2 |
|
from __future__ import absolute_import |
|
|
|
|
2
|
2 |
|
from __future__ import print_function |
3
|
|
|
|
4
|
2 |
|
import os |
5
|
2 |
|
import re |
6
|
|
|
|
7
|
2 |
|
from .constants import XCCDF11_NS |
8
|
2 |
|
from .oval import parse_affected |
9
|
2 |
|
from .utils import read_file_list |
10
|
|
|
|
11
|
|
|
|
12
|
2 |
|
def get_content_ref_if_exists_and_not_remote(check): |
|
|
|
|
13
|
|
|
""" |
14
|
|
|
Given an OVAL check element, examine the ``xccdf_ns:check-content-ref`` |
15
|
|
|
|
16
|
|
|
If it exists and it isn't remote, pass it as the return value. |
17
|
|
|
Otherwise, return None. |
18
|
|
|
|
19
|
|
|
..see-also:: is_content_href_remote |
20
|
|
|
""" |
21
|
|
|
checkcontentref = check.find("./{%s}check-content-ref" % XCCDF11_NS) |
22
|
|
|
if checkcontentref is None: |
23
|
|
|
return None |
24
|
|
|
if is_content_href_remote(checkcontentref): |
25
|
|
|
return None |
26
|
|
|
return checkcontentref |
27
|
|
|
|
28
|
|
|
|
29
|
2 |
|
def is_content_href_remote(check_content_ref): |
30
|
|
|
""" |
31
|
|
|
Given an OVAL check-content-ref element, examine the 'href' attribute. |
32
|
|
|
|
33
|
|
|
If it starts with 'http://' or 'https://', return True, otherwise |
34
|
|
|
return False. |
35
|
|
|
|
36
|
|
|
Raises RuntimeError if the ``href`` element doesn't exist. |
37
|
|
|
""" |
38
|
|
|
hrefattr = check_content_ref.get("href") |
39
|
|
|
if hrefattr is None: |
40
|
|
|
# @href attribute of <check-content-ref> is required by XCCDF standard |
41
|
|
|
msg = "Invalid OVAL <check-content-ref> detected - missing the " \ |
42
|
|
|
"'href' attribute!" |
43
|
|
|
raise RuntimeError(msg) |
44
|
|
|
|
45
|
|
|
return hrefattr.startswith("http://") or hrefattr.startswith("https://") |
46
|
|
|
|
47
|
|
|
|
48
|
2 |
|
def is_cce_valid(cceid): |
49
|
|
|
""" |
50
|
|
|
IF CCE ID IS IN VALID FORM (either 'CCE-XXXX-X' or 'CCE-XXXXX-X' |
51
|
|
|
where each X is a digit, and the final X is a check-digit) |
52
|
|
|
based on Requirement A17: |
53
|
|
|
|
54
|
|
|
http://people.redhat.com/swells/nist-scap-validation/scap-val-requirements-1.2.html |
55
|
|
|
""" |
56
|
2 |
|
match = re.match(r'^CCE-\d{4,5}-\d$', cceid) |
57
|
2 |
|
return match is not None |
58
|
|
|
|
59
|
|
|
|
60
|
2 |
|
def get_oval_path(rule_obj, oval_id): |
61
|
|
|
""" |
62
|
|
|
For the given oval_id or product, return the full path to the check in the |
63
|
|
|
given rule. |
64
|
|
|
""" |
65
|
2 |
|
if not oval_id.endswith(".xml"): |
66
|
2 |
|
oval_id += ".xml" |
67
|
|
|
|
68
|
2 |
|
if 'dir' not in rule_obj or 'id' not in rule_obj: |
69
|
2 |
|
raise ValueError("Malformed rule_obj") |
70
|
|
|
|
71
|
2 |
|
if 'ovals' not in rule_obj or oval_id not in rule_obj['ovals']: |
72
|
2 |
|
raise ValueError("Unknown oval_id:%s for rule_id" % oval_id) |
73
|
|
|
|
74
|
2 |
|
return os.path.join(rule_obj['dir'], 'oval', oval_id) |
75
|
|
|
|
76
|
|
|
|
77
|
2 |
|
def get_oval_contents(rule_obj, oval_id): |
78
|
|
|
""" |
79
|
|
|
Returns the tuple (path, contents) of the check described by the given |
80
|
|
|
oval_id or product. |
81
|
|
|
""" |
82
|
|
|
|
83
|
|
|
oval_path = get_oval_path(rule_obj, oval_id) |
84
|
|
|
oval_contents = read_file_list(oval_path) |
85
|
|
|
|
86
|
|
|
return oval_path, oval_contents |
87
|
|
|
|
88
|
|
|
|
89
|
2 |
|
def set_applicable_platforms(oval_contents, new_platforms): |
90
|
|
|
""" |
91
|
|
|
Returns a modified contents which updates the platforms to the new |
92
|
|
|
platforms. |
93
|
|
|
""" |
94
|
|
|
|
95
|
|
|
start_affected, end_affected, indent = parse_affected(oval_contents) |
96
|
|
|
|
97
|
|
|
platforms = sorted(new_platforms) |
98
|
|
|
new_platforms_xml = map(lambda x: indent + "<platform>%s</platform>" % x, platforms) |
99
|
|
|
new_platforms_xml = list(new_platforms_xml) |
100
|
|
|
|
101
|
|
|
new_contents = oval_contents[0:start_affected+1] |
102
|
|
|
new_contents.extend(new_platforms_xml) |
103
|
|
|
new_contents.extend(oval_contents[end_affected:]) |
104
|
|
|
|
105
|
|
|
return new_contents |
106
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.