|
1
|
2 |
|
from __future__ import absolute_import |
|
2
|
2 |
|
from __future__ import print_function |
|
3
|
|
|
|
|
4
|
2 |
|
import platform |
|
5
|
2 |
|
import re |
|
6
|
|
|
|
|
7
|
2 |
|
from .constants import xml_version, oval_header, timestamp, PREFIX_TO_NS |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
2 |
|
try: |
|
11
|
2 |
|
from xml.etree import cElementTree as ElementTree |
|
12
|
|
|
except ImportError: |
|
13
|
|
|
from xml.etree import ElementTree as ElementTree |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
2 |
|
def oval_generated_header(product_name, schema_version, ssg_version): |
|
17
|
2 |
|
return xml_version + oval_header + \ |
|
18
|
|
|
""" |
|
19
|
|
|
<generator> |
|
20
|
|
|
<oval:product_name>%s from SCAP Security Guide</oval:product_name> |
|
21
|
|
|
<oval:product_version>ssg: %s, python: %s</oval:product_version> |
|
22
|
|
|
<oval:schema_version>%s</oval:schema_version> |
|
23
|
|
|
<oval:timestamp>%s</oval:timestamp> |
|
24
|
|
|
</generator>""" % (product_name, ssg_version, platform.python_version(), |
|
25
|
|
|
schema_version, timestamp) |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
2 |
|
def register_namespaces(): |
|
29
|
|
|
""" |
|
30
|
|
|
Register all possible namespaces |
|
31
|
|
|
""" |
|
32
|
|
|
try: |
|
33
|
|
|
for prefix, uri in PREFIX_TO_NS.items(): |
|
34
|
|
|
ElementTree.register_namespace(prefix, uri) |
|
35
|
|
|
except Exception: |
|
36
|
|
|
# Probably an old version of Python |
|
37
|
|
|
# Doesn't matter, as this is non-essential. |
|
38
|
|
|
pass |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
2 |
|
def open_xml(filename): |
|
42
|
|
|
""" |
|
43
|
|
|
Given a filename, register all possible namespaces, and return the XML tree. |
|
44
|
|
|
""" |
|
45
|
|
|
register_namespaces() |
|
46
|
|
|
return ElementTree.parse(filename) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
2 |
|
def parse_file(filename): |
|
50
|
|
|
""" |
|
51
|
|
|
Given a filename, return the root of the ElementTree |
|
52
|
|
|
""" |
|
53
|
|
|
tree = open_xml(filename) |
|
54
|
|
|
return tree.getroot() |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
2 |
|
def map_elements_to_their_ids(tree, xpath_expr): |
|
58
|
|
|
""" |
|
59
|
|
|
Given an ElementTree and an XPath expression, |
|
60
|
|
|
iterate through matching elements and create 1:1 id->element mapping. |
|
61
|
|
|
|
|
62
|
|
|
Raises AssertionError if a matching element doesn't have the ``id`` |
|
63
|
|
|
attribute. |
|
64
|
|
|
|
|
65
|
|
|
Returns mapping as a dictionary |
|
66
|
|
|
""" |
|
67
|
|
|
aggregated = {} |
|
68
|
|
|
for element in tree.findall(xpath_expr): |
|
69
|
|
|
element_id = element.get("id") |
|
70
|
|
|
assert element_id is not None |
|
71
|
|
|
aggregated[element_id] = element |
|
72
|
|
|
return aggregated |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
2 |
|
SSG_XHTML_TAGS = [ |
|
76
|
|
|
'table', 'tr', 'th', 'td', 'ul', 'li', 'ol', |
|
77
|
|
|
'p', 'code', 'strong', 'b', 'em', 'i', 'pre', 'br', 'hr', 'small', |
|
78
|
|
|
] |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
2 |
|
def add_xhtml_namespace(data): |
|
82
|
|
|
""" |
|
83
|
|
|
Given a xml blob, adds the xhtml namespace to all relevant tags. |
|
84
|
|
|
""" |
|
85
|
|
|
# Transform <tt> in <code> |
|
86
|
|
|
data = re.sub(r'<(\/)?tt(\/)?>', r'<\1code\2>', data) |
|
87
|
|
|
# Adds xhtml prefix to elements: <tag>, </tag>, <tag/> |
|
88
|
|
|
return re.sub(r'<(\/)?((?:%s).*?)(\/)?>' % "|".join(SSG_XHTML_TAGS), r'<\1xhtml:\2\3>', data) |
|
89
|
|
|
|