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