|
1
|
|
|
# Copyright 2022, Red Hat, Inc. |
|
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
|
3
|
1 |
|
from ..data_structures import LogicalTest, Platform |
|
4
|
1 |
|
from ..exceptions import ExceptionNoCPEApplicabilityLanguage |
|
5
|
1 |
|
from ..namespaces import NAMESPACES |
|
6
|
1 |
|
from .full_text_parser import FullTextParser |
|
7
|
|
|
|
|
8
|
1 |
|
TEXT_TO_BOOL = {"true": True, "false": False, "": False} |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
1 |
|
class CPEApplicabilityLanguageParser: |
|
12
|
1 |
|
def __init__(self, root, oval_cpe_definitions): |
|
13
|
1 |
|
self.root = root |
|
14
|
1 |
|
self.platform_to_oval_cpe_id = self.get_platform_to_oval_cpe_id_dict() |
|
15
|
1 |
|
self.full_text_parser = FullTextParser({}) |
|
16
|
1 |
|
self.oval_cpe_definitions = oval_cpe_definitions |
|
17
|
|
|
|
|
18
|
1 |
|
def get_platform_to_oval_cpe_id_dict(self): |
|
19
|
1 |
|
cpe_list = self.root.find(".//ds:component/cpe-dict:cpe-list", NAMESPACES) |
|
20
|
1 |
|
out = {} |
|
21
|
1 |
|
if cpe_list is None: |
|
22
|
1 |
|
return out |
|
23
|
1 |
|
for cpe_item in cpe_list: |
|
24
|
1 |
|
name = cpe_item.get("name") |
|
25
|
1 |
|
check = cpe_item.find(".//cpe-dict:check", NAMESPACES) |
|
26
|
1 |
|
oval_id = check.text if check is not None else name |
|
27
|
1 |
|
out[name] = oval_id |
|
28
|
1 |
|
return out |
|
29
|
|
|
|
|
30
|
1 |
|
def _get_cpe_platform_elements(self): |
|
31
|
1 |
|
cpe_platform_elements = {} |
|
32
|
1 |
|
platform_specification = self.root.find('.//cpe-lang:platform-specification', NAMESPACES) |
|
33
|
1 |
|
if platform_specification is None: |
|
34
|
1 |
|
raise ExceptionNoCPEApplicabilityLanguage |
|
35
|
1 |
|
for platform in platform_specification: |
|
36
|
1 |
|
platform_id = platform.get("id") |
|
37
|
1 |
|
cpe_platform_elements[platform_id] = platform |
|
38
|
1 |
|
return cpe_platform_elements |
|
39
|
|
|
|
|
40
|
1 |
|
def _get_oval_cpe_tree(self, platform_name, check_id_ref): |
|
41
|
1 |
|
oval_tree = None |
|
42
|
1 |
|
oval_cpe_id = None |
|
43
|
|
|
|
|
44
|
1 |
|
if platform_name in self.platform_to_oval_cpe_id: |
|
45
|
1 |
|
oval_cpe_id = self.platform_to_oval_cpe_id[platform_name] |
|
46
|
|
|
|
|
47
|
1 |
|
if check_id_ref is not None: |
|
48
|
1 |
|
oval_cpe_id = check_id_ref |
|
49
|
|
|
|
|
50
|
1 |
|
oval_cpe_definition = self.oval_cpe_definitions.get(oval_cpe_id, None) |
|
51
|
1 |
|
oval_tree = oval_cpe_definition.oval_tree if oval_cpe_definition is not None else None |
|
52
|
1 |
|
return oval_tree |
|
53
|
|
|
|
|
54
|
1 |
|
def get_logical_test(self, logical_test_el): |
|
55
|
1 |
|
operator = logical_test_el.get("operator") |
|
56
|
1 |
|
negation = logical_test_el.get("negate", "") |
|
57
|
1 |
|
logical_test = LogicalTest(operator, negation=TEXT_TO_BOOL[negation]) |
|
58
|
1 |
|
for child_logical_test_el in logical_test_el: |
|
59
|
1 |
|
if "fact-ref" in child_logical_test_el.tag: |
|
60
|
1 |
|
platform_name = child_logical_test_el.get("name") |
|
61
|
1 |
|
check_id_ref = child_logical_test_el.get("id-ref") |
|
62
|
1 |
|
logical_test.children.append( |
|
63
|
|
|
LogicalTest( |
|
64
|
|
|
node_type="fact-ref", |
|
65
|
|
|
oval_tree=self._get_oval_cpe_tree(platform_name, check_id_ref)) |
|
66
|
|
|
) |
|
67
|
1 |
|
if child_logical_test_el.get('operator') is not None: |
|
68
|
1 |
|
logical_test.children.append(self.get_logical_test(child_logical_test_el)) |
|
69
|
1 |
|
return logical_test |
|
70
|
|
|
|
|
71
|
1 |
|
def get_cpe_platforms(self): |
|
72
|
1 |
|
out = {} |
|
73
|
1 |
|
for platform, platform_el in self._get_cpe_platform_elements().items(): |
|
74
|
1 |
|
title_el = platform_el.find(".//cpe-lang:title", NAMESPACES) |
|
75
|
1 |
|
title_str = "" |
|
76
|
1 |
|
if title_el is not None: |
|
77
|
|
|
self.full_text_parser.get_full_description(title_el) |
|
78
|
1 |
|
logical_test_el = platform_el.find(".//cpe-lang:logical-test", NAMESPACES) |
|
79
|
|
|
|
|
80
|
1 |
|
out[platform] = Platform( |
|
81
|
|
|
platform_id=platform, |
|
82
|
|
|
logical_test=self.get_logical_test(logical_test_el), |
|
83
|
|
|
title=title_str |
|
84
|
|
|
) |
|
85
|
|
|
return out |
|
86
|
|
|
|