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.\n" |
31
|
|
|
"\tBUTTON-SHOW-ALL-RULES - Adds the button that shows all rules." |
32
|
|
|
) |
33
|
|
|
|
34
|
1 |
|
MASSAGE_FORMAT = '%(levelname)s: %(message)s' |
35
|
1 |
|
EXPECTED_ERRORS = (XMLSyntaxError, ) |
36
|
1 |
|
EXIT_FAILURE_CODE = 1 |
37
|
1 |
|
EXIT_SUCCESS_CODE = 0 |
38
|
|
|
|
39
|
|
|
|
40
|
1 |
|
class CommandLineAPI(): # pylint: disable=R0902 |
41
|
1 |
|
def __init__(self): |
42
|
1 |
|
self.arguments = self._parse_arguments() |
43
|
1 |
|
self.log_file = self.arguments.log_file |
44
|
1 |
|
self.log_level = self.arguments.log_level |
45
|
1 |
|
self.debug_flags = self.arguments.debug |
46
|
1 |
|
self._setup_logging() |
47
|
1 |
|
logging.debug("Args: %s", self.arguments) |
48
|
1 |
|
self.report_file = self.arguments.FILE |
49
|
1 |
|
self.output_file = self.arguments.output |
50
|
1 |
|
self.output_format = self.arguments.format.upper() |
51
|
1 |
|
self.debug_setting = DebugSetting() |
52
|
|
|
|
53
|
1 |
|
def _parse_arguments(self): |
54
|
1 |
|
parser = argparse.ArgumentParser( |
55
|
|
|
prog="oscap-report", |
56
|
|
|
formatter_class=argparse.RawTextHelpFormatter, |
57
|
|
|
description=DESCRIPTION, |
58
|
|
|
add_help=False, |
59
|
|
|
) |
60
|
1 |
|
self._prepare_arguments(parser) |
61
|
1 |
|
return parser.parse_args() |
62
|
|
|
|
63
|
1 |
|
@staticmethod |
64
|
1 |
|
def _prepare_arguments(parser): |
65
|
1 |
|
parser.add_argument( |
66
|
|
|
"--version", |
67
|
|
|
action="version", |
68
|
|
|
version="%(prog)s " + __version__, |
69
|
|
|
help="Show program's version number and exit.") |
70
|
1 |
|
parser.add_argument( |
71
|
|
|
'-h', |
72
|
|
|
'--help', |
73
|
|
|
action='help', |
74
|
|
|
default=argparse.SUPPRESS, |
75
|
|
|
help='Show this help message and exit.') |
76
|
1 |
|
parser.add_argument( |
77
|
|
|
'FILE', |
78
|
|
|
type=argparse.FileType("r"), |
79
|
|
|
nargs='?', |
80
|
|
|
default=stdin, |
81
|
|
|
help="ARF file, stdin if not provided.") |
82
|
1 |
|
parser.add_argument( |
83
|
|
|
"-o", |
84
|
|
|
"--output", |
85
|
|
|
action="store", |
86
|
|
|
type=argparse.FileType("wb+", 0), |
87
|
|
|
default=stdout, |
88
|
|
|
help="write the report to this file instead of standard output.") |
89
|
1 |
|
parser.add_argument( |
90
|
|
|
"--log-file", |
91
|
|
|
action="store", |
92
|
|
|
default=None, |
93
|
|
|
help="if not provided - stderr.") |
94
|
1 |
|
parser.add_argument( |
95
|
|
|
"--log-level", |
96
|
|
|
action="store", |
97
|
|
|
default="WARNING", |
98
|
|
|
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
99
|
|
|
help=( |
100
|
|
|
"creates LOG_FILE file with log information depending on LOG_LEVEL." |
101
|
|
|
f"\n{LOG_LEVES_DESCRIPTION}") |
102
|
|
|
) |
103
|
1 |
|
parser.add_argument( |
104
|
|
|
"-f", |
105
|
|
|
"--format", |
106
|
|
|
action="store", |
107
|
|
|
default="HTML", |
108
|
|
|
choices=["HTML", "OLD-STYLE-HTML"], |
109
|
|
|
help="FORMAT: %(choices)s (default: %(default)s)." |
110
|
|
|
) |
111
|
1 |
|
parser.add_argument( |
112
|
|
|
"-d", |
113
|
|
|
"--debug", |
114
|
|
|
action="store", |
115
|
|
|
nargs='+', |
116
|
|
|
default=[""], |
117
|
|
|
choices=["NO-MINIFY", "BUTTON-SHOW-ALL-RULES"], |
118
|
|
|
help=f"{DEBUG_FLAGS_DESCRIPTION}" |
119
|
|
|
) |
120
|
|
|
|
121
|
1 |
|
def _setup_logging(self): |
122
|
1 |
|
logging.basicConfig( |
123
|
|
|
format=MASSAGE_FORMAT, |
124
|
|
|
filename=self.log_file, |
125
|
|
|
filemode='w', |
126
|
|
|
level=self.log_level.upper() |
127
|
|
|
) |
128
|
|
|
|
129
|
1 |
|
def generate_report(self, report_parser): |
130
|
1 |
|
logging.info("Generate report") |
131
|
1 |
|
if self.output_format == "OLD-STYLE-HTML": |
132
|
|
|
report_generator = OldOSCAPReportGenerator(report_parser) |
133
|
|
|
return report_generator.generate_html_report() |
134
|
1 |
|
report_generator = ReportGenerator(report_parser) |
135
|
|
|
|
136
|
1 |
|
self.debug_setting.update_settings_with_debug_flags(self.debug_flags) |
137
|
|
|
|
138
|
1 |
|
return report_generator.generate_html_report(self.debug_setting) |
139
|
|
|
|
140
|
1 |
|
def load_file(self): |
141
|
1 |
|
logging.info("Loading file: %s", self.report_file) |
142
|
1 |
|
return self.report_file.read().encode() |
143
|
|
|
|
144
|
1 |
|
def store_file(self, data): |
145
|
1 |
|
logging.info("Store report") |
146
|
1 |
|
if self.output_file.name == "<stdout>": |
147
|
|
|
logging.info("Output is stdout, converting bytes output to str") |
148
|
|
|
data = data.read().decode("utf-8") |
149
|
1 |
|
self.output_file.writelines(data) |
150
|
|
|
|
151
|
1 |
|
def close_files(self): |
152
|
1 |
|
logging.info("Close files") |
153
|
1 |
|
self.report_file.close() |
154
|
1 |
|
self.output_file.close() |
155
|
|
|
|
156
|
|
|
|
157
|
1 |
|
@dataclass |
158
|
1 |
|
class DebugSetting(): |
159
|
1 |
|
no_minify: bool = False |
160
|
1 |
|
options_require_debug_script: tuple = ("BUTTON-SHOW-ALL-RULES") |
161
|
1 |
|
include_debug_script: bool = False |
162
|
1 |
|
button_show_all_rules: bool = False |
163
|
|
|
|
164
|
1 |
|
def update_settings_with_debug_flags(self, debug_flags): |
165
|
1 |
|
for flag in debug_flags: |
166
|
1 |
|
if flag in self.options_require_debug_script: |
167
|
1 |
|
self.include_debug_script = True |
168
|
1 |
|
if flag == "NO-MINIFY": |
169
|
|
|
self.no_minify = True |
170
|
1 |
|
if flag == "BUTTON-SHOW-ALL-RULES": |
171
|
|
|
self.button_show_all_rules = True |
172
|
|
|
|
173
|
|
|
|
174
|
1 |
|
def main(): |
175
|
|
|
exit_code = EXIT_SUCCESS_CODE |
176
|
|
|
api = CommandLineAPI() |
177
|
|
|
arf_report = api.load_file() |
178
|
|
|
|
179
|
|
|
logging.info("Parse file") |
180
|
|
|
try: |
181
|
|
|
parser = SCAPResultsParser(arf_report) |
182
|
|
|
|
183
|
|
|
report = api.generate_report(parser) |
184
|
|
|
|
185
|
|
|
api.store_file(report) |
186
|
|
|
except EXPECTED_ERRORS as error: |
187
|
|
|
logging.fatal("%s", error) |
188
|
|
|
exit_code = EXIT_FAILURE_CODE |
189
|
|
|
api.close_files() |
190
|
|
|
sys_exit(exit_code) |
191
|
|
|
|
192
|
|
|
|
193
|
1 |
|
if __name__ == '__main__': |
194
|
|
|
main() |
195
|
|
|
|