1
|
1 |
|
import re |
2
|
1 |
|
import argparse |
3
|
1 |
|
import tempfile |
4
|
1 |
|
import os |
5
|
1 |
|
import webbrowser |
6
|
1 |
|
import json |
7
|
1 |
|
import shutil |
8
|
1 |
|
from datetime import datetime |
9
|
1 |
|
import sys |
10
|
|
|
|
11
|
1 |
|
from .xml_parser import XmlParser |
12
|
|
|
|
13
|
|
|
|
14
|
1 |
|
class Client(): |
15
|
1 |
|
def __init__(self, args): |
16
|
1 |
|
self.parser = None |
17
|
1 |
|
self.MESSAGES = self._get_message() |
18
|
1 |
|
self.arg = self.parse_arguments(args) |
19
|
1 |
|
self.hide_passing_tests = self.arg.hide_passing_tests |
20
|
1 |
|
self.source_filename = self.arg.source_filename |
21
|
1 |
|
self.rule_name = self.arg.rule_id |
22
|
1 |
|
self.out = self.arg.output |
23
|
1 |
|
self.all_rules = self.arg.all |
24
|
1 |
|
self.isatty = sys.stdout.isatty() |
25
|
1 |
|
self.show_failed_rules = False |
26
|
1 |
|
self.show_not_selected_rules = False |
27
|
1 |
|
self.xml_parser = XmlParser( |
28
|
|
|
self.source_filename) |
29
|
1 |
|
self.parts = self.get_src('parts') |
30
|
|
|
|
31
|
1 |
|
def _get_message(self): |
32
|
1 |
|
MESSAGES = { |
33
|
|
|
'description': '', |
34
|
|
|
'--output': '', |
35
|
|
|
'source_filename': '', |
36
|
|
|
} |
37
|
1 |
|
return MESSAGES |
38
|
|
|
|
39
|
1 |
|
def print_red_text(self, text): |
40
|
|
|
CRED = '\033[91m' |
41
|
|
|
CEND = '\033[0m' |
42
|
|
|
print(CRED + str(text) + CEND) |
43
|
|
|
|
44
|
1 |
|
def run_gui_and_return_answers(self): |
45
|
1 |
|
if self.isatty: |
46
|
1 |
|
if self.all_rules: |
47
|
|
|
return self._get_rules() |
48
|
|
|
else: |
49
|
1 |
|
try: |
50
|
1 |
|
import inquirer |
51
|
1 |
|
return inquirer.prompt(self.get_questions()) |
52
|
1 |
|
except ImportError: |
53
|
1 |
|
print(self.get_selection_rules()) |
54
|
1 |
|
return None |
55
|
|
|
else: |
56
|
1 |
|
return self._get_rules() |
57
|
|
|
|
58
|
1 |
|
def _get_rules(self): |
59
|
1 |
|
if self.show_failed_rules: |
60
|
1 |
|
return {'rules': self._get_only_fail_rule(self.search_rules_id())} |
61
|
|
|
else: |
62
|
1 |
|
return {'rules': self.search_rules_id()} |
63
|
|
|
|
64
|
1 |
|
def get_list_of_matched_rules(self): |
65
|
1 |
|
rules = self.search_rules_id() |
66
|
1 |
|
if self.show_failed_rules: |
67
|
1 |
|
rules = self._get_only_fail_rule(rules) |
68
|
1 |
|
return rules |
69
|
|
|
|
70
|
1 |
|
def get_list_of_lines(self): |
71
|
1 |
|
lines = ['== The Rule IDs =='] |
72
|
1 |
|
for rule in self.get_list_of_matched_rules(): |
73
|
1 |
|
lines.append("'" + rule + r'\b' + "'") |
74
|
1 |
|
if self.show_not_selected_rules: |
75
|
1 |
|
for line in self.get_lines_of_wanted_not_selected_rules(): |
76
|
1 |
|
lines.append(line) |
77
|
1 |
|
lines.append( |
78
|
|
|
"You haven't got installed inquirer lib. " |
79
|
|
|
"Please copy id rule with you want use and put it in command") |
80
|
1 |
|
return lines |
81
|
|
|
|
82
|
1 |
|
def get_selection_rules(self): |
83
|
1 |
|
return "\n".join(self.get_list_of_lines()) |
84
|
|
|
|
85
|
1 |
|
def get_lines_of_wanted_not_selected_rules(self): |
86
|
1 |
|
out = [] |
87
|
1 |
|
out.append('== The not selected rule IDs ==') |
88
|
1 |
|
for rule in self._get_wanted_rules_from_array_of_IDs( |
89
|
|
|
self.xml_parser.notselected_rules): |
90
|
1 |
|
out.append(rule + '(Not selected)') |
91
|
1 |
|
return out |
92
|
|
|
|
93
|
1 |
|
def get_choices(self): |
94
|
1 |
|
rules = self.search_rules_id() |
95
|
1 |
|
if self.show_failed_rules: |
96
|
1 |
|
rules = self._get_only_fail_rule(rules) |
97
|
1 |
|
choices = rules |
98
|
1 |
|
if self.show_not_selected_rules: |
99
|
1 |
|
print("\n".join(self.get_lines_of_wanted_not_selected_rules())) |
100
|
1 |
|
return choices |
101
|
|
|
|
102
|
1 |
|
def get_questions(self): |
103
|
1 |
|
choices = self.get_choices() |
104
|
1 |
|
from inquirer.questions import Checkbox as checkbox |
105
|
1 |
|
questions = [ |
106
|
|
|
checkbox( |
107
|
|
|
'rules', |
108
|
|
|
message=( |
109
|
|
|
"= The Rules IDs = (move - UP and DOWN arrows," |
110
|
|
|
" select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"), |
111
|
|
|
choices=choices, |
112
|
|
|
), |
113
|
|
|
] |
114
|
1 |
|
return questions |
115
|
|
|
|
116
|
1 |
|
def _get_only_fail_rule(self, rules): |
117
|
1 |
|
return list( |
118
|
|
|
filter( |
119
|
|
|
lambda rule: self.xml_parser.used_rules[rule]['result'] == 'fail', |
120
|
|
|
rules)) |
121
|
|
|
|
122
|
1 |
|
def _get_wanted_rules_from_array_of_IDs(self, rules): |
123
|
1 |
|
return [ |
124
|
|
|
x for x in rules if re.search( |
125
|
|
|
self.rule_name, x)] |
126
|
|
|
|
127
|
1 |
|
def search_rules_id(self): |
128
|
1 |
|
rules = self._get_wanted_rules_from_array_of_IDs( |
129
|
|
|
self.xml_parser.used_rules.keys()) |
130
|
1 |
|
notselected_rules = self._get_wanted_rules_from_array_of_IDs( |
131
|
|
|
self.xml_parser.notselected_rules) |
132
|
1 |
|
return self._check_rules_id(rules, notselected_rules) |
133
|
|
|
|
134
|
1 |
|
def _check_rules_id(self, rules, notselected_rules): |
135
|
1 |
|
if len(notselected_rules) and not rules: |
136
|
1 |
|
raise ValueError( |
137
|
|
|
('Rule(s) "{}" was not selected, ' |
138
|
|
|
"so there are no results. The rule is" |
139
|
|
|
' "notselected" because it' |
140
|
|
|
" wasn't a part of the executed profile" |
141
|
|
|
" and therefore it wasn't evaluated " |
142
|
|
|
"during the scan.") |
143
|
|
|
.format(notselected_rules)) |
144
|
1 |
|
elif not notselected_rules and not rules: |
145
|
1 |
|
raise ValueError('404 rule "{}" not found!'.format(self.rule_name)) |
146
|
|
|
else: |
147
|
1 |
|
return rules |
148
|
|
|
|
149
|
1 |
|
def save_html_and_open_html(self, oval_tree_dict, src, rule, out): |
150
|
1 |
|
self.save_html_report(oval_tree_dict, src) |
151
|
1 |
|
print('Rule "{}" done!'.format(rule)) |
152
|
1 |
|
out.append(src) |
153
|
1 |
|
self.open_web_browser(src) |
154
|
|
|
|
155
|
1 |
|
def open_web_browser(self, src): |
156
|
1 |
|
if not self.off_webbrowser: |
157
|
|
|
try: |
158
|
|
|
webbrowser.get('firefox').open_new_tab(src) |
159
|
|
|
except BaseException: |
160
|
|
|
webbrowser.open_new_tab(src) |
161
|
|
|
|
162
|
1 |
|
def get_src(self, src): |
163
|
1 |
|
_dir = os.path.dirname(os.path.realpath(__file__)) |
164
|
1 |
|
FIXTURE_DIR = os.path.join(_dir, src) |
165
|
1 |
|
return str(FIXTURE_DIR) |
166
|
|
|
|
167
|
1 |
|
def get_save_src(self, rule): |
168
|
1 |
|
date = str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
169
|
1 |
|
if self.out is not None: |
170
|
1 |
|
os.makedirs(self.out, exist_ok=True) |
171
|
1 |
|
return os.path.join( |
172
|
|
|
self.out, |
173
|
|
|
'graph-of-' + rule + date + '.html') |
174
|
1 |
|
return os.path.join( |
175
|
|
|
os.getcwd(), |
176
|
|
|
'graph-of-' + rule + date + '.html') |
177
|
|
|
|
178
|
1 |
|
def _get_part(self, part): |
179
|
1 |
|
with open(os.path.join(self.parts, part), "r") as data_file: |
180
|
1 |
|
return data_file.readlines() |
181
|
|
|
|
182
|
1 |
|
def _merge_report_parts(self, data): |
183
|
1 |
|
head = self._get_part('head.html') |
184
|
1 |
|
body = self._get_part('body.html') |
185
|
1 |
|
script = self._get_part('script.js') |
186
|
1 |
|
footer = ['</script>', '</body>', '</html>'] |
187
|
1 |
|
return [*head, data, *body, *script, *footer] |
188
|
|
|
|
189
|
1 |
|
def save_html_report(self, dict_, src): |
190
|
1 |
|
data = "var data_of_tree =" + str( |
191
|
|
|
json.dumps(dict_, sort_keys=False, indent=4) + ";") |
192
|
1 |
|
with open(src, "w+") as data_file: |
193
|
1 |
|
data_file.writelines(self._merge_report_parts(data)) |
194
|
|
|
|
195
|
1 |
|
def parse_arguments(self, args): |
196
|
1 |
|
self.prepare_parser() |
197
|
1 |
|
args = self.parser.parse_args(args) |
198
|
1 |
|
return args |
199
|
|
|
|
200
|
1 |
|
def prepare_parser(self): |
201
|
1 |
|
self.parser = argparse.ArgumentParser( |
202
|
|
|
description=self.MESSAGES.get('description')) |
203
|
1 |
|
self.parser.add_argument( |
204
|
|
|
'--all', |
205
|
|
|
action="store_true", |
206
|
|
|
default=False, |
207
|
|
|
help="Process all matched rules.") |
208
|
1 |
|
self.parser.add_argument( |
209
|
|
|
'--hide-passing-tests', |
210
|
|
|
action="store_true", |
211
|
|
|
default=False, |
212
|
|
|
help=( |
213
|
|
|
"Do not display passing tests for better orientation in" |
214
|
|
|
" graphs that contain a large amount of nodes.(Not implemented)")) |
215
|
1 |
|
self.parser.add_argument( |
216
|
|
|
'-o', |
217
|
|
|
'--output', |
218
|
|
|
action="store", |
219
|
|
|
default=None, |
220
|
|
|
help=self.MESSAGES.get('--output')) |
221
|
1 |
|
self.parser.add_argument( |
222
|
|
|
"source_filename", |
223
|
|
|
help=self.MESSAGES.get('source_filename')) |
224
|
1 |
|
self.parser.add_argument( |
225
|
|
|
"rule_id", help=( |
226
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
227
|
|
|
" a part of the ID or a regular expression can be used." |
228
|
|
|
" If brackets are used in the regular expression " |
229
|
|
|
"the regular expression must be quoted.")) |
230
|
|
|
|