1
|
2 |
|
from __future__ import absolute_import |
|
|
|
|
2
|
2 |
|
from __future__ import print_function |
3
|
|
|
|
4
|
2 |
|
from .xml import ElementTree |
5
|
2 |
|
from .constants import oval_namespace as oval_ns |
6
|
2 |
|
from .constants import ocil_namespace as ocil_ns |
7
|
2 |
|
from .constants import OVALTAG_TO_ABBREV, OCILTAG_TO_ABBREV |
8
|
2 |
|
from .constants import OVALREFATTR_TO_TAG, OCILREFATTR_TO_TAG |
9
|
|
|
|
10
|
|
|
|
11
|
2 |
|
def _split_namespace(tag): |
12
|
|
|
"""returns a tuple of (namespace, tag), |
13
|
|
|
removing any fragment id from namespace |
14
|
|
|
""" |
15
|
|
|
|
16
|
2 |
|
if tag[0] == "{": |
17
|
2 |
|
namespace, name = tag[1:].split("}", 1) |
18
|
2 |
|
return (namespace.split("#", 1)[0], name) |
19
|
|
|
|
20
|
2 |
|
return (None, tag) |
21
|
|
|
|
22
|
|
|
|
23
|
2 |
|
def _namespace_to_prefix(tag): |
24
|
|
|
namespace, _ = _split_namespace(tag) |
25
|
|
|
if namespace == ocil_ns: |
26
|
|
|
return "ocil" |
27
|
|
|
if namespace == oval_ns: |
28
|
|
|
return "oval" |
29
|
|
|
|
30
|
|
|
raise RuntimeError( |
31
|
|
|
"Error: unknown checksystem referenced in tag : %s" % tag |
32
|
|
|
) |
33
|
|
|
|
34
|
|
|
|
35
|
2 |
|
def _tagname_to_abbrev(tag): |
36
|
|
|
namespace, tag = _split_namespace(tag) |
37
|
|
|
if tag == "extend_definition": |
38
|
|
|
return tag |
39
|
|
|
# grab the last part of the tag name to determine its type |
40
|
|
|
tag = tag.rsplit("_", 1)[-1] |
41
|
|
|
if namespace == ocil_ns: |
42
|
|
|
return OCILTAG_TO_ABBREV[tag] |
43
|
|
|
if namespace == oval_ns: |
44
|
|
|
return OVALTAG_TO_ABBREV[tag] |
45
|
|
|
|
46
|
|
|
raise RuntimeError( |
47
|
|
|
"Error: unknown checksystem referenced in tag : %s" % tag |
48
|
|
|
) |
49
|
|
|
|
50
|
|
|
|
51
|
2 |
|
class IDTranslator(object): |
|
|
|
|
52
|
|
|
"""This class is designed to handle the mapping of meaningful, human-readable |
53
|
|
|
names to IDs in the formats required by the SCAP checking systems, such as |
54
|
|
|
OVAL and OCIL.""" |
55
|
|
|
|
56
|
2 |
|
def __init__(self, content_id): |
57
|
|
|
self.content_id = content_id |
58
|
|
|
|
59
|
2 |
|
def generate_id(self, tagname, name): |
|
|
|
|
60
|
|
|
return "%s:%s-%s:%s:1" % ( |
61
|
|
|
_namespace_to_prefix(tagname), |
62
|
|
|
self.content_id, name, |
63
|
|
|
_tagname_to_abbrev(tagname) |
64
|
|
|
) |
65
|
|
|
|
66
|
2 |
|
def translate(self, tree, store_defname=False): |
|
|
|
|
67
|
|
|
for element in tree.getiterator(): |
68
|
|
|
idname = element.get("id") |
69
|
|
|
if idname: |
70
|
|
|
# store the old name if requested (for OVAL definitions) |
71
|
|
|
if store_defname and \ |
72
|
|
|
element.tag == "{%s}definition" % oval_ns: |
73
|
|
|
metadata = element.find("{%s}metadata" % oval_ns) |
74
|
|
|
if metadata is None: |
75
|
|
|
metadata = ElementTree.SubElement(element, "metadata") |
76
|
|
|
defnam = ElementTree.Element( |
77
|
|
|
"reference", ref_id=idname, source=self.content_id) |
78
|
|
|
metadata.append(defnam) |
79
|
|
|
|
80
|
|
|
# set the element to the new identifier |
81
|
|
|
element.set("id", self.generate_id(element.tag, idname)) |
82
|
|
|
# continue |
83
|
|
|
if element.tag == "{%s}filter" % oval_ns: |
84
|
|
|
element.text = self.generate_id("{%s}state" % oval_ns, |
85
|
|
|
element.text) |
86
|
|
|
continue |
87
|
|
|
if element.tag == "{%s#independent}var_ref" % oval_ns: |
88
|
|
|
element.text = self.generate_id("{%s}variable" % oval_ns, |
89
|
|
|
element.text) |
90
|
|
|
continue |
91
|
|
|
for attr in element.keys(): |
92
|
|
|
if attr in OVALREFATTR_TO_TAG.keys(): |
93
|
|
|
element.set(attr, self.generate_id( |
94
|
|
|
"{%s}%s" % (oval_ns, OVALREFATTR_TO_TAG[attr]), |
95
|
|
|
element.get(attr))) |
96
|
|
|
if attr in OCILREFATTR_TO_TAG.keys(): |
97
|
|
|
element.set(attr, self.generate_id( |
98
|
|
|
"{%s}%s" % (ocil_ns, OCILREFATTR_TO_TAG[attr]), |
99
|
|
|
element.get(attr))) |
100
|
|
|
if element.tag == "{%s}test_action_ref" % ocil_ns: |
101
|
|
|
element.text = self.generate_id("{%s}action" % ocil_ns, |
102
|
|
|
element.text) |
103
|
|
|
|
104
|
|
|
return tree |
105
|
|
|
|
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.