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