1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
1 |
|
from ..data_structures import Group |
5
|
1 |
|
from ..namespaces import NAMESPACES |
6
|
1 |
|
from .full_text_parser import FullTextParser |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class GroupParser: |
10
|
1 |
|
def __init__(self, root, ref_values): |
11
|
1 |
|
self.root = root |
12
|
1 |
|
self.description_parser = FullTextParser(ref_values) |
13
|
1 |
|
self.rule_to_grup_id = {} |
14
|
1 |
|
self.group_to_platforms = {} |
15
|
|
|
|
16
|
1 |
|
def insert_to_dict_group_to_platforms(self, group_dict, platforms): |
17
|
1 |
|
platforms_of_group = list(set(group_dict.get("platforms")) | set(platforms)) |
18
|
1 |
|
self.group_to_platforms[group_dict.get("group_id")] = platforms_of_group |
19
|
|
|
|
20
|
1 |
|
@staticmethod |
21
|
1 |
|
def remove_empty_array(group_dict): |
22
|
1 |
|
if not group_dict["platforms"]: |
23
|
1 |
|
group_dict["platforms"] = None |
24
|
1 |
|
if not group_dict["rules_ids"]: |
25
|
1 |
|
group_dict["rules_ids"] = None |
26
|
1 |
|
if not group_dict["sub_groups"]: |
27
|
1 |
|
group_dict["sub_groups"] = None |
28
|
|
|
|
29
|
1 |
|
@staticmethod |
30
|
1 |
|
def _set_title_to_group_dict(group_dict, item): |
31
|
|
|
group_dict["title"] = item.text |
32
|
|
|
|
33
|
1 |
|
def _set_description_to_group_dict(self, group_dict, item): |
34
|
|
|
group_dict["description"] = self.description_parser.get_full_description(item) |
35
|
|
|
|
36
|
1 |
|
@staticmethod |
37
|
1 |
|
def _append_platform_to_group_dict(group_dict, item): |
38
|
|
|
group_dict["platforms"].append(item.get("idref")) |
39
|
|
|
|
40
|
1 |
|
def _append_sub_group_to_group_dict(self, group_dict, item): |
41
|
|
|
group_dict["sub_groups"].append(self.get_group(item, group_dict.get("platforms"))) |
42
|
|
|
|
43
|
1 |
|
def get_group(self, group, platforms=None): |
44
|
1 |
|
if platforms is None: |
45
|
1 |
|
platforms = [] |
46
|
1 |
|
group_dict = { |
47
|
|
|
"platforms": [], |
48
|
|
|
"rules_ids": [], |
49
|
|
|
"sub_groups": [], |
50
|
|
|
"group_id": group.get("id"), |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
tag_to_function = { |
54
|
|
|
"title": self._set_title_to_group_dict, |
55
|
|
|
"description": self._set_description_to_group_dict, |
56
|
|
|
"platforms": self._append_platform_to_group_dict, |
57
|
|
|
"sub_groups": self._append_sub_group_to_group_dict, |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
for item in group.iterchildren(): |
61
|
1 |
|
if item.tag in tag_to_function: |
62
|
|
|
tag_to_function[item.tag](group_dict, item) |
63
|
|
|
|
64
|
1 |
|
self.insert_to_dict_group_to_platforms(group_dict, platforms) |
65
|
1 |
|
self.remove_empty_array(group_dict) |
66
|
1 |
|
return Group(**group_dict) |
67
|
|
|
|
68
|
1 |
|
def get_groups(self): |
69
|
1 |
|
groups = {} |
70
|
1 |
|
group = self.root.find(".//xccdf:Group", NAMESPACES) |
71
|
1 |
|
benchmark = self.root.find(".//xccdf:Benchmark", NAMESPACES) |
72
|
1 |
|
if group is None or benchmark is None: |
73
|
1 |
|
return None |
74
|
|
|
|
75
|
1 |
|
for item in benchmark: |
76
|
1 |
|
if "Group" in item.tag: |
77
|
1 |
|
groups[item.get("id")] = self.get_group(item) |
78
|
|
|
return groups |
79
|
|
|
|