|
1
|
|
|
# Copyright 2022, Red Hat, Inc. |
|
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
|
3
|
|
|
|
|
4
|
|
|
from dataclasses import replace |
|
5
|
|
|
|
|
6
|
|
|
try: |
|
7
|
|
|
from functools import cache |
|
8
|
|
|
except ImportError: |
|
9
|
|
|
from functools import lru_cache |
|
10
|
|
|
cache = lru_cache(maxsize=None) |
|
11
|
|
|
|
|
12
|
|
|
from lxml import etree |
|
13
|
|
|
|
|
14
|
|
|
from openscap_report.scap_results_parser import SCAPResultsParser |
|
15
|
|
|
from openscap_report.scap_results_parser.namespaces import NAMESPACES |
|
16
|
|
|
from openscap_report.scap_results_parser.parsers import RuleParser |
|
17
|
|
|
|
|
18
|
|
|
from .constants import (PATH_TO_ARF, |
|
19
|
|
|
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO) |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
@cache |
|
23
|
|
|
def get_xml_data(file_path): |
|
24
|
|
|
with open(file_path, "r", encoding="utf-8") as xml_report: |
|
25
|
|
|
return xml_report.read().encode() |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
def get_parser(file_path): |
|
29
|
|
|
xml_data = get_xml_data(file_path) |
|
30
|
|
|
return SCAPResultsParser(xml_data) |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
BASIC_REPORT = get_parser(PATH_TO_ARF).parse_report() |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
def get_report(file_path=None): |
|
37
|
|
|
if file_path is None: |
|
38
|
|
|
return replace(BASIC_REPORT) |
|
39
|
|
|
return get_parser(file_path).parse_report() |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
REPORT_REPRODUCING_DANGLING_REFERENCE_TO = get_report( |
|
43
|
|
|
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO |
|
44
|
|
|
) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
def get_root(file_path): |
|
48
|
|
|
xml_data = get_xml_data(file_path) |
|
49
|
|
|
return etree.XML(xml_data) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
def get_benchmark(root): |
|
53
|
|
|
benchmark_el = root.find(".//xccdf:Benchmark", NAMESPACES) |
|
54
|
|
|
if "Benchmark" in root.tag: |
|
55
|
|
|
return root |
|
56
|
|
|
return benchmark_el |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def get_test_results(root): |
|
60
|
|
|
return root.find(".//xccdf:TestResult", NAMESPACES) |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
def get_ref_values(root): |
|
64
|
|
|
return { |
|
65
|
|
|
ref_value.get("idref"): ref_value.text |
|
66
|
|
|
for ref_value in root.findall(".//xccdf:set-value", NAMESPACES) |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
DEFAULT_RULES = BASIC_REPORT.rules |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
def get_rules(file_path=None): |
|
74
|
|
|
if file_path is None: |
|
75
|
|
|
return DEFAULT_RULES.copy() |
|
76
|
|
|
root = get_root(file_path) |
|
77
|
|
|
test_results = get_test_results(root) |
|
78
|
|
|
ref_values = get_ref_values(root) |
|
79
|
|
|
rule_parser = RuleParser(root, test_results, ref_values) |
|
80
|
|
|
return rule_parser.get_rules() |
|
81
|
|
|
|