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