|
1
|
|
|
import argparse |
|
2
|
|
|
import logging |
|
3
|
|
|
from sys import stdin, stdout |
|
4
|
|
|
|
|
5
|
|
|
from . import __version__ |
|
6
|
|
|
from .old_html_report_style.report_generator import ReportGenerator |
|
7
|
|
|
from .scap_results_parser.scap_results_parser import SCAPResultsParser |
|
8
|
|
|
|
|
9
|
|
|
DESCRIPTION = ("Generate a HTML (JSON, PDF?, Printable HTML, etc) document (HTML report)" |
|
10
|
|
|
" from an ARF (or XCCDF file) containing results of oscap scan. Unless" |
|
11
|
|
|
" the --output option is specified it will be written to standard output.") |
|
12
|
|
|
LOG_LEVES_DESCRIPTION = ( |
|
13
|
|
|
"LOG LEVELS:\n" |
|
14
|
|
|
"\tDEBUG - Detailed information, typically of interest only when diagnosing problems.\n" |
|
15
|
|
|
"\tINFO - Confirmation that things are working as expected.\n" |
|
16
|
|
|
"\tWARING - An indication that something unexpected happened, or indicative of" |
|
17
|
|
|
" some problem in the near future. The software is still working as expected.\n" |
|
18
|
|
|
"\tERROR - Due to a more serious problem, the software has not been able to perform " |
|
19
|
|
|
"some function.\n" |
|
20
|
|
|
"\tCRITICAL - A serious error, indicating that the program itself may be unable " |
|
21
|
|
|
"to continue running.\n" |
|
22
|
|
|
) |
|
23
|
|
|
MASSAGE_FORMAT = '%(levelname)s: %(message)s' |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class CommandLineAPI(): |
|
27
|
|
|
def __init__(self): |
|
28
|
|
|
self.arguments = self._parse_arguments() |
|
29
|
|
|
self.log_file = self.arguments.log_file |
|
30
|
|
|
self.log_level = self.arguments.log_level |
|
31
|
|
|
self._setup_logging() |
|
32
|
|
|
logging.debug("Args: %s", self.arguments) |
|
33
|
|
|
self.report_file = self.arguments.FILE |
|
34
|
|
|
self.output_file = self.arguments.output |
|
35
|
|
|
self.output_format = self.arguments.format.upper() |
|
36
|
|
|
|
|
37
|
|
|
def _parse_arguments(self): |
|
38
|
|
|
parser = argparse.ArgumentParser( |
|
39
|
|
|
prog="oscap-report", |
|
40
|
|
|
formatter_class=argparse.RawTextHelpFormatter, |
|
41
|
|
|
description=DESCRIPTION, |
|
42
|
|
|
add_help=False, |
|
43
|
|
|
) |
|
44
|
|
|
self._prepare_arguments(parser) |
|
45
|
|
|
return parser.parse_args() |
|
46
|
|
|
|
|
47
|
|
|
@staticmethod |
|
48
|
|
|
def _prepare_arguments(parser): |
|
49
|
|
|
parser.add_argument( |
|
50
|
|
|
"--version", |
|
51
|
|
|
action="version", |
|
52
|
|
|
version="%(prog)s " + __version__, |
|
53
|
|
|
help="Show program's version number and exit.") |
|
54
|
|
|
parser.add_argument( |
|
55
|
|
|
'-h', |
|
56
|
|
|
'--help', |
|
57
|
|
|
action='help', |
|
58
|
|
|
default=argparse.SUPPRESS, |
|
59
|
|
|
help='Show this help message and exit.') |
|
60
|
|
|
parser.add_argument( |
|
61
|
|
|
'FILE', |
|
62
|
|
|
type=argparse.FileType("r"), |
|
63
|
|
|
nargs='?', |
|
64
|
|
|
default=stdin, |
|
65
|
|
|
help="ARF file, stdin if not provided.") |
|
66
|
|
|
parser.add_argument( |
|
67
|
|
|
"-o", |
|
68
|
|
|
"--output", |
|
69
|
|
|
action="store", |
|
70
|
|
|
type=argparse.FileType("w+"), |
|
71
|
|
|
default=stdout, |
|
72
|
|
|
help="write the report to this file instead of standard output.") |
|
73
|
|
|
parser.add_argument( |
|
74
|
|
|
"--log-file", |
|
75
|
|
|
action="store", |
|
76
|
|
|
default=None, |
|
77
|
|
|
help="if not provided - stderr.") |
|
78
|
|
|
parser.add_argument( |
|
79
|
|
|
"--log-level", |
|
80
|
|
|
action="store", |
|
81
|
|
|
default="WARNING", |
|
82
|
|
|
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"], |
|
83
|
|
|
help=( |
|
84
|
|
|
"creates LOG_FILE file with log information depending on LOG_LEVEL." |
|
85
|
|
|
"\n{}").format(LOG_LEVES_DESCRIPTION) |
|
86
|
|
|
) |
|
87
|
|
|
parser.add_argument( |
|
88
|
|
|
"-f", |
|
89
|
|
|
"--format", |
|
90
|
|
|
action="store", |
|
91
|
|
|
default="HTML", |
|
92
|
|
|
choices=["HTML"], |
|
93
|
|
|
help="FORMAT: %(choices)s (default: %(default)s)." |
|
94
|
|
|
) |
|
95
|
|
|
|
|
96
|
|
|
def _setup_logging(self): |
|
97
|
|
|
logging.basicConfig( |
|
98
|
|
|
format=MASSAGE_FORMAT, |
|
99
|
|
|
filename=self.log_file, |
|
100
|
|
|
filemode='w', |
|
101
|
|
|
level=self.log_level.upper() |
|
102
|
|
|
) |
|
103
|
|
|
|
|
104
|
|
|
@staticmethod |
|
105
|
|
|
def generate_report(report_parser): |
|
106
|
|
|
logging.info("Generate report") |
|
107
|
|
|
report_generator = ReportGenerator(report_parser) |
|
108
|
|
|
return report_generator.generate_html_report() |
|
109
|
|
|
|
|
110
|
|
|
def load_file(self): |
|
111
|
|
|
logging.info("Loading file: %s", self.report_file) |
|
112
|
|
|
return self.report_file.read().encode() |
|
113
|
|
|
|
|
114
|
|
|
def store_file(self, data): |
|
115
|
|
|
logging.info("Store report") |
|
116
|
|
|
self.output_file.write(data) |
|
117
|
|
|
|
|
118
|
|
|
def close_files(self): |
|
119
|
|
|
logging.info("Close files") |
|
120
|
|
|
self.report_file.close() |
|
121
|
|
|
self.output_file.close() |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
def main(): |
|
125
|
|
|
api = CommandLineAPI() |
|
126
|
|
|
arf_report = api.load_file() |
|
127
|
|
|
|
|
128
|
|
|
logging.info("Parse file") |
|
129
|
|
|
parser = SCAPResultsParser(arf_report) |
|
130
|
|
|
|
|
131
|
|
|
report = api.generate_report(parser) |
|
132
|
|
|
|
|
133
|
|
|
api.store_file(report) |
|
134
|
|
|
api.close_files() |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
if __name__ == '__main__': |
|
138
|
|
|
main() |
|
139
|
|
|
|