|
1
|
|
|
# Copyright 2022, Red Hat, Inc. |
|
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
|
3
|
|
|
|
|
4
|
1 |
|
import re |
|
5
|
|
|
|
|
6
|
1 |
|
from lxml.builder import E |
|
7
|
|
|
|
|
8
|
1 |
|
from ..data_structures import ProfileInfo |
|
9
|
1 |
|
from ..namespaces import NAMESPACES |
|
10
|
1 |
|
from .shared_static_methods_of_parser import SharedStaticMethodsOfParser |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
1 |
|
class ProfileInfoParser: |
|
14
|
1 |
|
def __init__(self, profiles, results_of_cpe_platforms, benchmark_el): |
|
15
|
1 |
|
self.profiles = profiles |
|
16
|
1 |
|
self.results_of_cpe_platforms = results_of_cpe_platforms |
|
17
|
1 |
|
self.benchmark_el = benchmark_el |
|
18
|
|
|
|
|
19
|
1 |
|
def _selected_rules_and_groups_ids(self, selected_elements): |
|
20
|
1 |
|
groups_ids = [] |
|
21
|
1 |
|
rules_ids = [] |
|
22
|
1 |
|
for element in selected_elements: |
|
23
|
1 |
|
if element.get("selected") == "false": |
|
24
|
1 |
|
continue |
|
25
|
1 |
|
id_ = element.get("idref") |
|
26
|
1 |
|
if re.match("xccdf_[^_]+_group_.+", id_): |
|
27
|
1 |
|
groups_ids.append(id_) |
|
28
|
1 |
|
elif re.match("xccdf_[^_]+_rule_.+", id_): |
|
29
|
1 |
|
rules_ids.append(id_) |
|
30
|
1 |
|
return {"selected_rules_ids": rules_ids, "selected_groups_ids": groups_ids} |
|
31
|
|
|
|
|
32
|
1 |
|
def _get_cpe_platforms_for_profile(self): |
|
33
|
1 |
|
out = {} |
|
34
|
1 |
|
if self.benchmark_el is None: |
|
35
|
|
|
return out |
|
36
|
1 |
|
for element in self.benchmark_el.iterchildren(): |
|
37
|
1 |
|
if element.tag.endswith("platform"): |
|
38
|
1 |
|
idref = element.get('idref') |
|
39
|
1 |
|
out[idref] = idref in self.results_of_cpe_platforms |
|
40
|
1 |
|
return out |
|
41
|
|
|
|
|
42
|
1 |
|
def get_profile_info(self, profile_id): |
|
43
|
1 |
|
profile_el = self.profiles.get(profile_id, E.xml("empty")) |
|
44
|
|
|
|
|
45
|
1 |
|
title = profile_el.find('.//xccdf:title', NAMESPACES) |
|
46
|
1 |
|
description = profile_el.find('.//xccdf:description', NAMESPACES) |
|
47
|
1 |
|
profile_info_dict = { |
|
48
|
|
|
"profile_id": profile_id, |
|
49
|
|
|
"title": SharedStaticMethodsOfParser.get_text_of_xml_element(title), |
|
50
|
|
|
"description": SharedStaticMethodsOfParser.get_text_of_xml_element(description), |
|
51
|
|
|
"extends": profile_el.get("extends", ""), |
|
52
|
|
|
"cpe_platforms_for_profile": self._get_cpe_platforms_for_profile() |
|
53
|
|
|
} |
|
54
|
1 |
|
selected_elements = profile_el.findall('.//xccdf:select', NAMESPACES) |
|
55
|
1 |
|
extend_profile_el = self.profiles.get(profile_info_dict["extends"], E.xml("empty")) |
|
56
|
1 |
|
selected_elements.extend(extend_profile_el.findall('.//xccdf:select', NAMESPACES)) |
|
57
|
1 |
|
return ProfileInfo( |
|
58
|
|
|
**profile_info_dict, **self._selected_rules_and_groups_ids(selected_elements) |
|
59
|
|
|
) |
|
60
|
|
|
|