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 remove_empty_array(group_dict): |
24
|
1 |
|
if not group_dict["platforms"]: |
25
|
1 |
|
group_dict["platforms"] = None |
26
|
1 |
|
if not group_dict["rules_ids"]: |
27
|
1 |
|
group_dict["rules_ids"] = None |
28
|
1 |
|
if not group_dict["sub_groups"]: |
29
|
1 |
|
group_dict["sub_groups"] = None |
30
|
|
|
|
31
|
1 |
|
@staticmethod |
32
|
1 |
|
def _set_title_to_group_dict(group_dict, item): |
33
|
1 |
|
group_dict["title"] = item.text |
34
|
|
|
|
35
|
1 |
|
def _set_description_to_group_dict(self, group_dict, item): |
36
|
1 |
|
group_dict["description"] = self.description_parser.get_full_description(item) |
37
|
|
|
|
38
|
1 |
|
@staticmethod |
39
|
1 |
|
def _append_platform_to_group_dict(group_dict, item): |
40
|
1 |
|
group_dict["platforms"].append(item.get("idref")) |
41
|
|
|
|
42
|
1 |
|
def _append_sub_group_to_group_dict(self, group_dict, item): |
43
|
1 |
|
group_dict["sub_groups"].append(self.get_group(item, group_dict.get("platforms"))) |
44
|
|
|
|
45
|
1 |
|
def _append_rule_id_to_group_dict(self, group_dict, item): |
46
|
1 |
|
group_dict["rules_ids"].append(item.get("id")) |
47
|
1 |
|
self.rule_to_grup_id[item.get("id")] = group_dict.get("group_id") |
48
|
|
|
|
49
|
1 |
|
def get_group(self, group, platforms=None): |
50
|
1 |
|
if platforms is None: |
51
|
1 |
|
platforms = [] |
52
|
1 |
|
group_dict = { |
53
|
|
|
"platforms": [], |
54
|
|
|
"rules_ids": [], |
55
|
|
|
"sub_groups": [], |
56
|
|
|
"group_id": group.get("id"), |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
tag_to_function = { |
60
|
|
|
"title": self._set_title_to_group_dict, |
61
|
|
|
"description": self._set_description_to_group_dict, |
62
|
|
|
"platform": self._append_platform_to_group_dict, |
63
|
|
|
"Group": self._append_sub_group_to_group_dict, |
64
|
|
|
"Rule": self._append_rule_id_to_group_dict, |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
for item in group.iterchildren(): |
68
|
1 |
|
tag_without_namespace_prefix = re.sub(r"{(.*?)}", "", item.tag) |
69
|
1 |
|
if tag_without_namespace_prefix in tag_to_function: |
70
|
1 |
|
tag_to_function[tag_without_namespace_prefix](group_dict, item) |
71
|
|
|
|
72
|
1 |
|
self.insert_to_dict_group_to_platforms(group_dict, platforms) |
73
|
1 |
|
self.remove_empty_array(group_dict) |
74
|
1 |
|
return Group(**group_dict) |
75
|
|
|
|
76
|
1 |
|
def get_groups(self): |
77
|
1 |
|
groups = {} |
78
|
1 |
|
group = self.root.find(".//xccdf:Group", NAMESPACES) |
79
|
1 |
|
benchmark = self.root.find(".//xccdf:Benchmark", NAMESPACES) |
80
|
1 |
|
if group is None or benchmark is None: |
81
|
1 |
|
return None |
82
|
|
|
|
83
|
1 |
|
for item in benchmark: |
84
|
1 |
|
if "Group" in item.tag: |
85
|
1 |
|
groups[item.get("id")] = self.get_group(item) |
86
|
|
|
return groups |
87
|
|
|
|