1
|
|
|
# Copyright 2022, Red Hat, Inc. |
2
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later |
3
|
|
|
|
4
|
1 |
|
import base64 |
5
|
1 |
|
import logging |
6
|
1 |
|
import re |
7
|
1 |
|
from io import BytesIO |
8
|
1 |
|
from pathlib import Path |
9
|
|
|
|
10
|
1 |
|
from jinja2 import Environment, FileSystemLoader |
11
|
|
|
|
12
|
1 |
|
try: |
13
|
1 |
|
from jinja2 import pass_context |
14
|
|
|
except ImportError: |
15
|
|
|
from jinja2 import contextfunction as pass_context |
16
|
|
|
|
17
|
1 |
|
from markupsafe import Markup |
18
|
|
|
|
19
|
1 |
|
from ..scap_results_parser.data_structures import Rule |
20
|
1 |
|
from .exceptions import FilterNotSupportDataStructureException |
21
|
1 |
|
from .report_generator import ReportGenerator |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
class HTMLReportGenerator(ReportGenerator): |
25
|
1 |
|
def __init__(self, parser): # pylint: disable=W0231 |
26
|
1 |
|
self.report = parser.parse_report() |
27
|
1 |
|
self.file_loader = FileSystemLoader(str(Path(__file__).parent / "html_templates")) |
28
|
1 |
|
self.env = Environment(loader=self.file_loader) |
29
|
1 |
|
self.env.globals['include_file_in_base64'] = self.include_file_in_base64 |
30
|
1 |
|
self.env.filters['set_css_for_list'] = self.set_css_for_list |
31
|
1 |
|
self.env.filters['get_selected_rules'] = self.get_selected_rules |
32
|
1 |
|
self.env.trim_blocks = True |
33
|
1 |
|
self.env.lstrip_blocks = True |
34
|
|
|
|
35
|
1 |
|
def generate_report(self, debug_setting): |
36
|
1 |
|
template = self.env.get_template("template_report.html") |
37
|
1 |
|
html_report = template.render(report=self.report, debug_setting=debug_setting) |
38
|
1 |
|
if debug_setting.no_minify: |
39
|
|
|
return BytesIO(html_report.encode()) |
40
|
1 |
|
minified_html_report = re.sub(r'>\s+<', '><', html_report) |
41
|
1 |
|
return BytesIO(minified_html_report.encode()) |
42
|
|
|
|
43
|
1 |
|
@staticmethod |
44
|
1 |
|
@pass_context |
45
|
1 |
|
def include_file_in_base64(context, relative_path): |
46
|
1 |
|
real_path = Path(context.environment.loader.searchpath[0]) / relative_path |
47
|
1 |
|
if "RedHat" in relative_path: |
48
|
1 |
|
real_path = Path(relative_path) |
49
|
1 |
|
if not real_path.exists(): |
50
|
1 |
|
logging.warning("Please, install font: %s", real_path.name) |
51
|
1 |
|
return Markup("NO-FONT-DATA") |
52
|
1 |
|
base64_data = None |
53
|
1 |
|
with open(real_path, "rb") as file_data: |
54
|
1 |
|
base64_data = (base64.b64encode(file_data.read())).decode('utf-8') |
55
|
1 |
|
return Markup(base64_data) |
56
|
|
|
|
57
|
1 |
|
@staticmethod |
58
|
1 |
|
def set_css_for_list(data): |
59
|
1 |
|
out = data.replace("<ul>", "<ul class=\"pf-c-list\">") |
60
|
1 |
|
out = out.replace("<ol>", "<ol class=\"pf-c-list\">") |
61
|
1 |
|
return out |
62
|
|
|
|
63
|
1 |
|
@staticmethod |
64
|
1 |
|
def get_selected_rules(data): |
65
|
1 |
|
out = [] |
66
|
1 |
|
for rule_id, rule in data.items(): |
67
|
1 |
|
if not isinstance(rule, Rule): |
68
|
|
|
raise FilterNotSupportDataStructureException |
69
|
1 |
|
if rule.result != "notselected": |
70
|
1 |
|
out.append((rule_id, rule)) |
71
|
|
|
return out |
72
|
|
|
|