1
|
1 |
|
import argparse |
2
|
1 |
|
import logging |
3
|
1 |
|
from dataclasses import dataclass |
4
|
1 |
|
from sys import exit as sys_exit |
5
|
1 |
|
from sys import stdin, stdout |
6
|
|
|
|
7
|
1 |
|
from lxml.etree import XMLSyntaxError |
8
|
|
|
|
9
|
1 |
|
from . import __version__ |
10
|
1 |
|
from .html_report import ReportGenerator |
11
|
1 |
|
from .old_html_report_style import OldOSCAPReportGenerator |
12
|
1 |
|
from .scap_results_parser import SCAPResultsParser |
13
|
|
|
|
14
|
1 |
|
DESCRIPTION = ("Generate a HTML (JSON, PDF?, Printable HTML, etc) document (HTML report)" |
15
|
|
|
" from an ARF (or XCCDF file) containing results of oscap scan. Unless" |
16
|
|
|
" the --output option is specified it will be written to standard output.") |
17
|
1 |
|
LOG_LEVES_DESCRIPTION = ( |
18
|
|
|
"LOG LEVELS:\n" |
19
|
|
|
"\tDEBUG - Detailed information, typically of interest only when diagnosing problems.\n" |
20
|
|
|
"\tINFO - Confirmation that things are working as expected.\n" |
21
|
|
|
"\tWARING - An indication that something unexpected happened, or indicative of" |
22
|
|
|
" some problem in the near future. The software is still working as expected.\n" |
23
|
|
|
"\tERROR - Due to a more serious problem, the software has not been able to perform " |
24
|
|
|
"some function.\n" |
25
|
|
|
"\tCRITICAL - A serious error, indicating that the program itself may be unable " |
26
|
|
|
"to continue running.\n" |
27
|
|
|
) |
28
|
1 |
|
DEBUG_FLAGS_DESCRIPTION = ( |
29
|
|
|
"DEBUG FLAGS:\n" |
30
|
|
|
"\tNO-MINIFY - The HTML report is not minified." |
31
|
|
|
) |
32
|
|
|
|
33
|
1 |
|
MASSAGE_FORMAT = '%(levelname)s: %(message)s' |
34
|
1 |
|
EXPECTED_ERRORS = (XMLSyntaxError, ) |
35
|
1 |
|
EXIT_FAILURE_CODE = 1 |
36
|
1 |
|
EXIT_SUCCESS_CODE = 0 |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
class CommandLineAPI(): # pylint: disable=R0902 |
40
|
1 |
|
def __init__(self): |
41
|
1 |
|
self.arguments = self._parse_arguments() |
42
|
1 |
|
self.log_file = self.arguments.log_file |
43
|
1 |
|
self.log_level = self.arguments.log_level |
44
|
1 |
|
self.debug_flags = self.arguments.debug |
45
|
1 |
|
self._setup_logging() |
46
|
1 |
|
logging.debug("Args: %s", self.arguments) |
47
|
1 |
|
self.report_file = self.arguments.FILE |
48
|
1 |
|
self.output_file = self.arguments.output |
49
|
1 |
|
self.output_format = self.arguments.format.upper() |
50
|
1 |
|
self.debug_setting = DebugSetting() |
51
|
|
|
|
52
|
1 |
|
def _parse_arguments(self): |
53
|
1 |
|
parser = argparse.ArgumentParser( |
54
|
|
|
prog="oscap-report", |
55
|
|
|
formatter_class=argparse.RawTextHelpFormatter, |
56
|
|
|
description=DESCRIPTION, |
57
|
|
|
add_help=False, |
58
|
|
|
) |
59
|
1 |
|
self._prepare_arguments(parser) |
60
|
1 |
|
return parser.parse_args() |
61
|
|
|
|
62
|
1 |
|
@staticmethod |
63
|
1 |
|
def _prepare_arguments(parser): |
64
|
1 |
|
parser.add_argument( |
65
|
|
|
"--version", |
66
|
|
|
action="version", |
67
|
|
|
version="%(prog)s " + __version__, |
68
|
|
|
help="Show program's version number and exit.") |
69
|
1 |
|
parser.add_argument( |
70
|
|
|
'-h', |
71
|
|
|
'--help', |
72
|
|
|
action='help', |
73
|
|
|
default=argparse.SUPPRESS, |
74
|
|
|
help='Show this help message and exit.') |
75
|
1 |
|
parser.add_argument( |
76
|
|
|
'FILE', |
77
|
|
|
type=argparse.FileType("r"), |
78
|
|
|
nargs='?', |
79
|
|
|
default=stdin, |
80
|
|
|
help="ARF file, stdin if not provided.") |
81
|
1 |
|
parser.add_argument( |
82
|
|
|
"-o", |
83
|
|
|
"--output", |
84
|
|
|
action="store", |
85
|
|
|
type=argparse.FileType("wb+", 0), |
86
|
|
|
default=stdout, |
87
|
|
|
help="write the report to this file instead of standard output.") |
88
|
1 |
|
parser.add_argument( |
89
|
|
|
"--log-file", |
90
|
|
|
action="store", |
91
|
|
|
default=None, |
92
|
|
|
help="if not provided - stderr.") |
93
|
1 |
|
parser.add_argument( |
94
|
|
|
"--log-level", |
95
|
|
|
action="store", |
96
|
|
|
default="WARNING", |
97
|
|
|
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
98
|
|
|
help=( |
99
|
|
|
"creates LOG_FILE file with log information depending on LOG_LEVEL." |
100
|
|
|
f"\n{LOG_LEVES_DESCRIPTION}") |
101
|
|
|
) |
102
|
1 |
|
parser.add_argument( |
103
|
|
|
"-f", |
104
|
|
|
"--format", |
105
|
|
|
action="store", |
106
|
|
|
default="HTML", |
107
|
|
|
choices=["HTML", "OLD-STYLE-HTML"], |
108
|
|
|
help="FORMAT: %(choices)s (default: %(default)s)." |
109
|
|
|
) |
110
|
1 |
|
parser.add_argument( |
111
|
|
|
"-d", |
112
|
|
|
"--debug", |
113
|
|
|
action="store", |
114
|
|
|
nargs='+', |
115
|
|
|
default=[""], |
116
|
|
|
choices=["NO-MINIFY", "BUTTON-SHOW-ALL-RULES"], |
117
|
|
|
help=f"{DEBUG_FLAGS_DESCRIPTION}" |
118
|
|
|
) |
119
|
|
|
|
120
|
1 |
|
def _setup_logging(self): |
121
|
1 |
|
logging.basicConfig( |
122
|
|
|
format=MASSAGE_FORMAT, |
123
|
|
|
filename=self.log_file, |
124
|
|
|
filemode='w', |
125
|
|
|
level=self.log_level.upper() |
126
|
|
|
) |
127
|
|
|
|
128
|
1 |
|
def generate_report(self, report_parser): |
129
|
1 |
|
logging.info("Generate report") |
130
|
1 |
|
if self.output_format == "OLD-STYLE-HTML": |
131
|
|
|
report_generator = OldOSCAPReportGenerator(report_parser) |
132
|
|
|
return report_generator.generate_html_report() |
133
|
1 |
|
report_generator = ReportGenerator(report_parser) |
134
|
|
|
|
135
|
1 |
|
self.debug_setting.update_settings_with_debug_flags(self.debug_flags) |
136
|
|
|
|
137
|
1 |
|
return report_generator.generate_html_report(self.debug_setting) |
138
|
|
|
|
139
|
1 |
|
def load_file(self): |
140
|
1 |
|
logging.info("Loading file: %s", self.report_file) |
141
|
1 |
|
return self.report_file.read().encode() |
142
|
|
|
|
143
|
1 |
|
def store_file(self, data): |
144
|
1 |
|
logging.info("Store report") |
145
|
1 |
|
if self.output_file.name == "<stdout>": |
146
|
|
|
logging.info("Output is stdout, converting bytes output to str") |
147
|
|
|
data = data.read().decode("utf-8") |
148
|
1 |
|
self.output_file.writelines(data) |
149
|
|
|
|
150
|
1 |
|
def close_files(self): |
151
|
1 |
|
logging.info("Close files") |
152
|
1 |
|
self.report_file.close() |
153
|
1 |
|
self.output_file.close() |
154
|
|
|
|
155
|
|
|
|
156
|
1 |
|
@dataclass |
157
|
1 |
|
class DebugSetting(): |
158
|
1 |
|
no_minify: bool = False |
159
|
1 |
|
options_require_debug_script: tuple = ("BUTTON-SHOW-ALL-RULES") |
160
|
1 |
|
include_debug_script: bool = False |
161
|
1 |
|
button_show_all_rules: bool = False |
162
|
|
|
|
163
|
1 |
|
def update_settings_with_debug_flags(self, debug_flags): |
164
|
1 |
|
for flag in debug_flags: |
165
|
1 |
|
if flag in self.options_require_debug_script: |
166
|
1 |
|
self.include_debug_script = True |
167
|
1 |
|
if flag == "NO-MINIFY": |
168
|
|
|
self.no_minify = True |
169
|
1 |
|
if flag == "BUTTON-SHOW-ALL-RULES": |
170
|
|
|
self.button_show_all_rules = True |
171
|
|
|
|
172
|
|
|
|
173
|
1 |
|
def main(): |
174
|
|
|
exit_code = EXIT_SUCCESS_CODE |
175
|
|
|
api = CommandLineAPI() |
176
|
|
|
arf_report = api.load_file() |
177
|
|
|
|
178
|
|
|
logging.info("Parse file") |
179
|
|
|
try: |
180
|
|
|
parser = SCAPResultsParser(arf_report) |
181
|
|
|
|
182
|
|
|
report = api.generate_report(parser) |
183
|
|
|
|
184
|
|
|
api.store_file(report) |
185
|
|
|
except EXPECTED_ERRORS as error: |
186
|
|
|
logging.fatal("%s", error) |
187
|
|
|
exit_code = EXIT_FAILURE_CODE |
188
|
|
|
api.close_files() |
189
|
|
|
sys_exit(exit_code) |
190
|
|
|
|
191
|
|
|
|
192
|
1 |
|
if __name__ == '__main__': |
193
|
|
|
main() |
194
|
|
|
|