|
1
|
|
|
# Copyright 2022, Red Hat, Inc. |
|
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
|
3
|
|
|
|
|
4
|
1 |
|
from ..data_structures import OvalDefinition, OvalReference |
|
5
|
1 |
|
from ..exceptions import MissingOVALResult |
|
6
|
1 |
|
from ..namespaces import NAMESPACES |
|
7
|
1 |
|
from .oval_result_parser import OVALResultParser |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
1 |
|
class OVALDefinitionParser: |
|
11
|
1 |
|
def __init__(self, root, oval_var_id_to_value_id, ref_values): |
|
12
|
1 |
|
self.root = root |
|
13
|
1 |
|
self.oval_result_parser = OVALResultParser(self.root, oval_var_id_to_value_id, ref_values) |
|
14
|
1 |
|
self.oval_trees_by_oval_reports = self.oval_result_parser.get_oval_trees_by_oval_reports() |
|
15
|
|
|
|
|
16
|
1 |
|
self.oval_reports = self.oval_result_parser.oval_reports |
|
17
|
1 |
|
self.oval_definitions = self._get_xml_elements_of_oval_definitions() |
|
18
|
|
|
|
|
19
|
1 |
|
def _get_xml_elements_of_oval_definitions(self): |
|
20
|
1 |
|
out = {} |
|
21
|
1 |
|
for oval, oval_report in self.oval_reports.items(): |
|
22
|
1 |
|
out[oval] = oval_report.oval_report_element.find( |
|
23
|
|
|
'.//oval-definitions:oval_definitions/oval-definitions:definitions', NAMESPACES) |
|
24
|
1 |
|
return out |
|
25
|
|
|
|
|
26
|
1 |
|
def _get_references(self, definition): |
|
27
|
1 |
|
references = [] |
|
28
|
1 |
|
for ref in definition.findall('.//oval-definitions:reference', NAMESPACES): |
|
29
|
1 |
|
references.append(OvalReference(ref.get("source"), ref.get("ref_id"))) |
|
30
|
1 |
|
return references |
|
31
|
|
|
|
|
32
|
1 |
|
def parse_oval_definition(self, definition_id, definition): |
|
33
|
1 |
|
oval_definition_dict = { |
|
34
|
|
|
"definition_id": definition_id, |
|
35
|
|
|
"title": definition.find('.//oval-definitions:title', NAMESPACES).text, |
|
36
|
|
|
"description": definition.find('.//oval-definitions:description', NAMESPACES).text, |
|
37
|
|
|
"version": definition.get("version"), |
|
38
|
|
|
"references": self._get_references(definition), |
|
39
|
|
|
} |
|
40
|
1 |
|
return OvalDefinition(**oval_definition_dict) |
|
41
|
|
|
|
|
42
|
1 |
|
def _get_oval_definitions(self, oval): |
|
43
|
1 |
|
if oval not in self.oval_definitions: |
|
44
|
|
|
raise MissingOVALResult(oval) |
|
45
|
|
|
|
|
46
|
1 |
|
definitions = {} |
|
47
|
1 |
|
dict_of_criteria = {} |
|
48
|
1 |
|
for definition in self.oval_definitions[oval]: |
|
49
|
1 |
|
definition_id = definition.get("id") |
|
50
|
1 |
|
oval_definition = self.parse_oval_definition( |
|
51
|
|
|
definition_id, |
|
52
|
|
|
definition |
|
53
|
|
|
) |
|
54
|
1 |
|
criteria = definition.find('.//oval-definitions:criteria', NAMESPACES) |
|
55
|
1 |
|
dict_of_criteria[definition_id] = self._create_dict_from_criteria(criteria) |
|
56
|
1 |
|
definitions[definition_id] = oval_definition |
|
57
|
|
|
|
|
58
|
1 |
|
self._add_comments_to_oval_tree(dict_of_criteria, oval) |
|
59
|
1 |
|
self._add_oval_tree_to_definition(definitions, oval) |
|
60
|
1 |
|
return definitions |
|
61
|
|
|
|
|
62
|
1 |
|
def get_oval_definitions(self): |
|
63
|
1 |
|
oval_definitions_by_reports = {} |
|
64
|
1 |
|
for report_id in self.oval_trees_by_oval_reports: |
|
65
|
1 |
|
oval_definitions_by_reports[report_id] = self._get_oval_definitions(report_id) |
|
66
|
1 |
|
return oval_definitions_by_reports |
|
67
|
|
|
|
|
68
|
1 |
|
def _get_test_criteria(self, criterion): |
|
69
|
1 |
|
out = {"comment": criterion.get("comment")} |
|
70
|
1 |
|
if criterion.get('definition_ref'): |
|
71
|
1 |
|
out['extend_definition'] = criterion.get('definition_ref') |
|
72
|
|
|
else: |
|
73
|
1 |
|
out['value_id'] = criterion.get('test_ref') |
|
74
|
1 |
|
return out |
|
75
|
|
|
|
|
76
|
1 |
|
def _create_dict_from_criteria(self, criteria): |
|
77
|
1 |
|
criteria_dict = { |
|
78
|
|
|
"operator": criteria.get("operator", "AND"), |
|
79
|
|
|
"comment": criteria.get("comment"), |
|
80
|
|
|
"child_criteria": [], |
|
81
|
|
|
} |
|
82
|
1 |
|
for criterion in criteria: |
|
83
|
1 |
|
if criterion.get("operator") or "criteria" in criterion.tag: |
|
84
|
1 |
|
criteria_dict["child_criteria"].append(self._create_dict_from_criteria(criterion)) |
|
85
|
|
|
else: |
|
86
|
1 |
|
criteria_dict["child_criteria"].append(self._get_test_criteria(criterion)) |
|
87
|
1 |
|
return criteria_dict |
|
88
|
|
|
|
|
89
|
1 |
|
def _add_oval_tree_to_definition(self, definitions, oval_report_id): |
|
90
|
1 |
|
oval_tree_source = self.oval_trees_by_oval_reports.get(oval_report_id, {}) |
|
91
|
1 |
|
for definition_id in definitions: |
|
92
|
1 |
|
self._set_oval_tree_to_definition(definitions, definition_id, oval_tree_source) |
|
93
|
|
|
|
|
94
|
1 |
|
def _set_oval_tree_to_definition(self, definitions, definition_id, oval_tree_source): |
|
95
|
1 |
|
if definition_id in oval_tree_source: |
|
96
|
1 |
|
oval_tree_source[definition_id].comment = definitions[definition_id].description |
|
97
|
1 |
|
definitions[definition_id].oval_tree = oval_tree_source[definition_id] |
|
98
|
|
|
|
|
99
|
1 |
|
@staticmethod |
|
100
|
1 |
|
def _get_criteria(criteria_id, criteria, criteria_dict): |
|
101
|
1 |
|
if criteria is None and criteria_id is not None: |
|
102
|
1 |
|
criteria = criteria_dict[criteria_id] |
|
103
|
1 |
|
return criteria |
|
104
|
|
|
|
|
105
|
1 |
|
@staticmethod |
|
106
|
1 |
|
def _set_comment_to_oval_tree(oval_tree, criteria): |
|
107
|
1 |
|
if not oval_tree.comment: |
|
108
|
1 |
|
oval_tree.comment = criteria.get("comment") |
|
109
|
|
|
|
|
110
|
1 |
|
def _fill_oval_tree_with_comments(self, oval_tree, criteria_id, criteria_dict, criteria=None): |
|
111
|
1 |
|
criteria = self._get_criteria(criteria_id, criteria, criteria_dict) |
|
112
|
1 |
|
self._set_comment_to_oval_tree(oval_tree, criteria) |
|
113
|
|
|
|
|
114
|
1 |
|
for criterion, oval_node in zip(criteria["child_criteria"], oval_tree.children): |
|
115
|
1 |
|
oval_node.comment = criterion.get("comment") |
|
116
|
1 |
|
if criterion.get('operator'): |
|
117
|
1 |
|
self._fill_oval_tree_with_comments( |
|
118
|
|
|
oval_node, None, criteria_dict, criterion) |
|
119
|
1 |
|
if criterion.get("extend_definition"): |
|
120
|
1 |
|
self._fill_oval_tree_with_comments( |
|
121
|
|
|
oval_node, criterion.get("extend_definition"), criteria_dict) |
|
122
|
|
|
|
|
123
|
1 |
|
def _add_comments_to_oval_tree(self, dict_of_criteria, oval_report_id): |
|
124
|
1 |
|
oval_tree_source = self.oval_trees_by_oval_reports.get(oval_report_id, {}) |
|
125
|
1 |
|
for id_ in dict_of_criteria: |
|
126
|
|
|
|
|
127
|
1 |
|
if id_ not in oval_tree_source: |
|
128
|
|
|
continue |
|
129
|
|
|
|
|
130
|
1 |
|
oval_tree = oval_tree_source[id_] |
|
131
|
|
|
self._fill_oval_tree_with_comments(oval_tree, id_, dict_of_criteria) |
|
132
|
|
|
|