1
|
1 |
|
import argparse |
|
|
|
|
2
|
1 |
|
import re |
3
|
1 |
|
import sys |
4
|
1 |
|
from datetime import datetime |
5
|
|
|
|
6
|
1 |
|
from .. import __version__ |
7
|
|
|
|
8
|
|
|
|
9
|
1 |
|
class Client(): |
|
|
|
|
10
|
1 |
|
def __init__(self, args): |
11
|
1 |
|
self.arg = self.parse_arguments(args) |
12
|
|
|
|
13
|
1 |
|
self.source_filename = self.arg.source_filename |
14
|
1 |
|
self.rule_name = None |
15
|
1 |
|
if hasattr(self.arg, 'rule_id'): |
16
|
1 |
|
self.rule_name = self.arg.rule_id |
17
|
|
|
|
18
|
1 |
|
self.isatty = sys.stdout.isatty() |
19
|
|
|
|
20
|
1 |
|
self.all_rules = None |
21
|
1 |
|
if hasattr(self.arg, 'all'): |
22
|
1 |
|
self.all_rules = self.arg.all |
23
|
1 |
|
self.show_failed_rules = False |
24
|
1 |
|
self.show_not_selected_rules = False |
25
|
|
|
|
26
|
1 |
|
@staticmethod |
27
|
|
|
def _get_message(): |
28
|
1 |
|
return { |
29
|
|
|
'description': '', |
30
|
|
|
'source_filename': '', |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
@staticmethod |
34
|
|
|
def _get_date(): |
35
|
1 |
|
return str(datetime.now().strftime("-%d_%m_%Y-%H_%M_%S")) |
36
|
|
|
|
37
|
|
|
# Functions for selection of rules |
38
|
|
|
|
39
|
1 |
|
def search_rules_id(self): |
40
|
|
|
""" |
41
|
|
|
Function retunes array of all matched IDs of rules in selected file. |
42
|
|
|
""" |
43
|
1 |
|
raise NotImplementedError |
44
|
|
|
|
45
|
1 |
|
def get_only_fail_rule(self, rules): |
46
|
|
|
""" |
47
|
|
|
Function processes array of matched IDs of rules in selected file. |
48
|
|
|
Function retunes array of failed matched IDs of rules in selected file. |
49
|
|
|
""" |
50
|
1 |
|
raise NotImplementedError |
51
|
|
|
|
52
|
1 |
|
def _get_rows_of_unselected_rules(self): |
53
|
|
|
""" |
54
|
|
|
Function retunes array of rows where is not selected IDs of rules in selected file. |
55
|
|
|
""" |
56
|
1 |
|
raise NotImplementedError |
57
|
|
|
|
58
|
1 |
|
def run_gui_and_return_answers(self): |
|
|
|
|
59
|
1 |
|
if self.isatty: |
60
|
1 |
|
if self.all_rules: |
61
|
1 |
|
return self._get_rules() |
62
|
|
|
|
63
|
1 |
|
try: |
64
|
1 |
|
import inquirer |
|
|
|
|
65
|
1 |
|
return inquirer.prompt(self.get_questions()) |
66
|
1 |
|
except ImportError: |
67
|
1 |
|
print(self.get_selection_rules()) |
68
|
1 |
|
return None |
69
|
1 |
|
return self._get_rules() |
70
|
|
|
|
71
|
1 |
|
def _get_rules(self): |
72
|
1 |
|
if self.show_failed_rules: |
73
|
1 |
|
return {'rules': self.get_only_fail_rule(self.search_rules_id())} |
74
|
1 |
|
return {'rules': self.search_rules_id()} |
75
|
|
|
|
76
|
1 |
|
def _get_list_of_matched_rules(self): |
77
|
1 |
|
if self.show_failed_rules: |
78
|
1 |
|
return self.get_only_fail_rule(self.search_rules_id()) |
79
|
1 |
|
return self.search_rules_id() |
80
|
|
|
|
81
|
1 |
|
def _get_list_of_lines(self): |
82
|
1 |
|
lines = ['== The Rule ID regular expressions =='] |
83
|
1 |
|
for rule in self._get_list_of_matched_rules(): |
84
|
1 |
|
lines.append("^" + rule + "$") |
85
|
1 |
|
if self.show_not_selected_rules: |
86
|
1 |
|
for line in self._get_rows_of_unselected_rules(): |
87
|
1 |
|
lines.append(line) |
88
|
1 |
|
lines.append( |
89
|
|
|
"Interactive rule selection is not available," |
90
|
|
|
" because inquirer is not installed." |
91
|
|
|
" Copy id of the rule you want to visualize and" |
92
|
|
|
" paste it into a command with regular" |
93
|
|
|
" expression characters(^$).\n" |
94
|
|
|
"Alternatively, use the --all or --all-in-one arguments.") |
95
|
1 |
|
return lines |
96
|
|
|
|
97
|
1 |
|
def get_selection_rules(self): |
|
|
|
|
98
|
1 |
|
return "\n".join(self._get_list_of_lines()) |
99
|
|
|
|
100
|
1 |
|
def _get_choices(self): |
101
|
1 |
|
if self.show_not_selected_rules: |
102
|
1 |
|
print("\n".join(self._get_rows_of_unselected_rules())) |
103
|
1 |
|
return self._get_list_of_matched_rules() |
104
|
|
|
|
105
|
1 |
|
def get_questions(self): |
|
|
|
|
106
|
1 |
|
from inquirer.questions import Checkbox as checkbox |
|
|
|
|
107
|
1 |
|
choices = self._get_choices() |
108
|
1 |
|
questions = [ |
109
|
|
|
checkbox( |
110
|
|
|
'rules', |
111
|
|
|
message=( |
112
|
|
|
"= The Rules IDs = (move - UP and DOWN arrows," |
113
|
|
|
" select - SPACE or LEFT and RIGHT arrows, submit - ENTER)"), |
114
|
|
|
choices=choices, |
115
|
|
|
), |
116
|
|
|
] |
117
|
1 |
|
return questions |
118
|
|
|
|
119
|
1 |
|
def _get_wanted_rules(self, rules): |
120
|
1 |
|
return [ |
121
|
|
|
x for x in rules if re.search( |
122
|
|
|
self.rule_name, x)] |
123
|
|
|
|
124
|
1 |
|
def _check_rules_id(self, rules, notselected_rules): |
125
|
1 |
|
if notselected_rules and not rules: |
126
|
1 |
|
raise ValueError( |
127
|
|
|
('Rule(s) "{}" was not selected, ' |
128
|
|
|
"so there are no results. The rule is" |
129
|
|
|
' "notselected" because it' |
130
|
|
|
" wasn't a part of the executed profile" |
131
|
|
|
" and therefore it wasn't evaluated " |
132
|
|
|
"during the scan.") |
133
|
|
|
.format(notselected_rules)) |
134
|
1 |
|
if not notselected_rules and not rules: |
135
|
1 |
|
raise ValueError('404 rule "{}" not found!'.format(self.rule_name)) |
136
|
1 |
|
return rules |
137
|
|
|
|
138
|
|
|
# Function for setting arguments |
139
|
|
|
|
140
|
1 |
|
def parse_arguments(self, args): |
|
|
|
|
141
|
1 |
|
parser = argparse.ArgumentParser( |
142
|
|
|
prog='oval-graph', |
143
|
|
|
description=self._get_message().get('description')) |
144
|
1 |
|
self.prepare_parser(parser) |
145
|
1 |
|
if args is None: |
146
|
|
|
return parser.parse_args() |
147
|
1 |
|
return parser.parse_args(args) |
148
|
|
|
|
149
|
1 |
|
@staticmethod |
150
|
|
|
def prepare_args_when_user_can_list_in_rules(parser): |
|
|
|
|
151
|
1 |
|
parser.add_argument( |
152
|
|
|
'--show-failed-rules', |
153
|
|
|
action="store_true", |
154
|
|
|
default=False, |
155
|
|
|
help="Show only FAILED rules") |
156
|
1 |
|
parser.add_argument( |
157
|
|
|
'--show-not-selected-rules', |
158
|
|
|
action="store_true", |
159
|
|
|
default=False, |
160
|
|
|
help="Show notselected rules. These rules will not be visualized.") |
161
|
|
|
|
162
|
1 |
|
@staticmethod |
163
|
|
|
def prepare_args_options_all_hide_passing_tests(parser): |
|
|
|
|
164
|
1 |
|
parser.add_argument( |
165
|
|
|
'-a', |
166
|
|
|
'--all', |
167
|
|
|
action="store_true", |
168
|
|
|
default=False, |
169
|
|
|
help="Process all matched rules.") |
170
|
1 |
|
parser.add_argument( |
171
|
|
|
'--hide-passing-tests', |
172
|
|
|
action="store_true", |
173
|
|
|
default=False, |
174
|
|
|
help=( |
175
|
|
|
"Do not display passing tests for better orientation in" |
176
|
|
|
" graphs that contain a large amount of nodes.")) |
177
|
|
|
|
178
|
1 |
|
def prepare_args_source_file(self, parser): |
|
|
|
|
179
|
1 |
|
parser.add_argument( |
180
|
|
|
"source_filename", |
181
|
|
|
help=self._get_message().get('source_filename')) |
182
|
|
|
|
183
|
1 |
|
@staticmethod |
184
|
|
|
def prepare_args_rule_id(parser): |
|
|
|
|
185
|
1 |
|
parser.add_argument( |
186
|
|
|
"rule_id", help=( |
187
|
|
|
"Rule ID to be visualized. A part from the full rule ID" |
188
|
|
|
" a part of the ID or a regular expression can be used." |
189
|
|
|
" If brackets are used in the regular expression " |
190
|
|
|
"the regular expression must be quoted.")) |
191
|
|
|
|
192
|
1 |
|
@staticmethod |
193
|
|
|
def prepare_args_basic_functions(parser): |
|
|
|
|
194
|
1 |
|
parser.add_argument( |
195
|
|
|
'--version', |
196
|
|
|
action='version', |
197
|
|
|
version='%(prog)s ' + __version__) |
198
|
1 |
|
parser.add_argument( |
199
|
|
|
'-v', |
200
|
|
|
'--verbose', |
201
|
|
|
action="store_true", |
202
|
|
|
default=False, |
203
|
|
|
help="Displays details about the results of the running command.") |
204
|
1 |
|
parser.add_argument( |
205
|
|
|
'-o', |
206
|
|
|
'--output', |
207
|
|
|
action="store", |
208
|
|
|
default=None, |
209
|
|
|
help='The file where to save output.') |
210
|
|
|
|
211
|
1 |
|
def prepare_parser(self, parser): |
|
|
|
|
212
|
1 |
|
self.prepare_args_basic_functions(parser) |
213
|
1 |
|
self.prepare_args_options_all_hide_passing_tests(parser) |
214
|
1 |
|
self.prepare_args_source_file(parser) |
215
|
|
|
self.prepare_args_rule_id(parser) |
216
|
|
|
|