1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
|
|
import argparse |
5
|
|
|
import tempfile |
6
|
|
|
from pathlib import Path |
7
|
|
|
|
8
|
|
|
try: |
9
|
|
|
from functools import cache |
10
|
|
|
except ImportError: |
11
|
|
|
from functools import lru_cache |
12
|
|
|
|
13
|
|
|
cache = lru_cache(maxsize=None) |
14
|
|
|
|
15
|
|
|
from lxml import etree |
16
|
|
|
|
17
|
|
|
from openscap_report.dataclasses import replace |
18
|
|
|
from openscap_report.scap_results_parser import SCAPResultsParser |
19
|
|
|
from openscap_report.scap_results_parser.data_structures import OvalDefinition |
20
|
|
|
from openscap_report.scap_results_parser.namespaces import NAMESPACES |
21
|
|
|
from openscap_report.scap_results_parser.parsers import ( |
22
|
|
|
CPEApplicabilityLanguageParser, RuleParser) |
23
|
|
|
|
24
|
|
|
from .constants import (PATH_TO_ARF, |
25
|
|
|
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO) |
26
|
|
|
from .unit_tests.test_oval_tree_eval import OVAL_TREE_TRUE |
27
|
|
|
|
28
|
|
|
PATH_TO_RESULT_FILE = Path(tempfile.gettempdir()) / "oscap-report-tests_result.html" |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
@cache |
32
|
|
|
def get_xml_data(file_path): |
33
|
|
|
with open(file_path, "r", encoding="utf-8") as xml_report: |
34
|
|
|
return xml_report.read().encode() |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
def get_parser(file_path): |
38
|
|
|
xml_data = get_xml_data(file_path) |
39
|
|
|
return SCAPResultsParser(xml_data) |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
BASIC_REPORT = get_parser(PATH_TO_ARF).parse_report() |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def get_report(file_path=None): |
46
|
|
|
if file_path is None: |
47
|
|
|
return replace(BASIC_REPORT) |
48
|
|
|
return get_parser(file_path).parse_report() |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
REPORT_REPRODUCING_DANGLING_REFERENCE_TO = get_report( |
52
|
|
|
PATH_TO_ARF_REPRODUCING_DANGLING_REFERENCE_TO |
53
|
|
|
) |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
def get_root(file_path): |
57
|
|
|
xml_data = get_xml_data(file_path) |
58
|
|
|
return etree.XML(xml_data) |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
def get_benchmark(root): |
62
|
|
|
benchmark_el = root.find(".//xccdf:Benchmark", NAMESPACES) |
63
|
|
|
if "Benchmark" in root.tag: |
64
|
|
|
return root |
65
|
|
|
return benchmark_el |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
def get_test_results(root): |
69
|
|
|
return root.find(".//xccdf:TestResult", NAMESPACES) |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
def get_ref_values(root): |
73
|
|
|
return { |
74
|
|
|
ref_value.get("idref"): ref_value.text |
75
|
|
|
for ref_value in root.findall(".//xccdf:set-value", NAMESPACES) |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
DEFAULT_RULES = BASIC_REPORT.rules |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
def get_rules(file_path=None): |
83
|
|
|
if file_path is None: |
84
|
|
|
return DEFAULT_RULES.copy() |
85
|
|
|
root = get_root(file_path) |
86
|
|
|
test_results = get_test_results(root) |
87
|
|
|
ref_values = get_ref_values(root) |
88
|
|
|
rule_parser = RuleParser(root, test_results, ref_values) |
89
|
|
|
return rule_parser.get_rules() |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
def get_cpe_al_parser(file_path=PATH_TO_ARF): |
93
|
|
|
root = get_root(file_path) |
94
|
|
|
return CPEApplicabilityLanguageParser(root, get_dummy_cpe_oval_definition()) |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
def get_dummy_cpe_oval_definition(): |
98
|
|
|
dummy_oval_definition = OvalDefinition( |
99
|
|
|
definition_id="dummy_oval_def", |
100
|
|
|
title="dummy OVAL definition", |
101
|
|
|
oval_tree=OVAL_TREE_TRUE, |
102
|
|
|
) |
103
|
|
|
return { |
104
|
|
|
"oval:ssg-installed_env_is_a_machine:def:1": dummy_oval_definition, |
105
|
|
|
"oval:ssg-installed_env_has_chrony_package:def:1": dummy_oval_definition, |
106
|
|
|
"oval:ssg-installed_env_has_ntp_package:def:1": dummy_oval_definition, |
107
|
|
|
"oval:ssg-installed_env_has_gdm_package:def:1": dummy_oval_definition, |
108
|
|
|
"oval:ssg-installed_OS_is_fedora:def:1": dummy_oval_definition, |
109
|
|
|
"oval:ssg-installed_env_has_zipl_package:def:1": dummy_oval_definition, |
110
|
|
|
"oval:ssg-system_boot_mode_is_uefi:def:1": dummy_oval_definition, |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
|
114
|
|
|
def get_fake_args(): |
115
|
|
|
# pylint: disable=bad-option-value,R1732 |
116
|
|
|
input_file = open(PATH_TO_ARF, "r", encoding="utf-8") |
117
|
|
|
output_file = open(PATH_TO_RESULT_FILE, "wb") |
118
|
|
|
return argparse.Namespace( |
119
|
|
|
FILE=input_file, output=output_file, |
120
|
|
|
log_file=None, log_level="WARNING", format="HTML", |
121
|
|
|
debug=[""], |
122
|
|
|
) |
123
|
|
|
|