-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathusage.py
354 lines (285 loc) · 10.3 KB
/
usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from __future__ import absolute_import
import argparse
from collections import namedtuple
from detect_secrets import VERSION
class ParserBuilder(object):
def __init__(self):
self.parser = argparse.ArgumentParser()
self.add_default_arguments()
def add_default_arguments(self):
self._add_verbosity_argument()\
._add_version_argument()
return self
def add_pre_commit_arguments(self):
self._add_filenames_argument()\
._add_set_baseline_argument()
PluginOptions(self.parser).add_arguments()
return self
def add_console_use_arguments(self):
subparser = self.parser.add_subparsers(
dest='action',
)
for action_parser in [ScanOptions, AuditOptions]:
action_parser(subparser).add_arguments()
return self
def parse_args(self, argv):
output = self.parser.parse_args(argv)
PluginOptions.consolidate_args(output)
return output
def _add_version_argument(self):
self.parser.add_argument(
'--version',
action='version',
version=VERSION,
help='Display version information.',
)
return self
def _add_verbosity_argument(self):
self.parser.add_argument(
'-v',
'--verbose',
action='count',
help='Verbose mode.',
)
return self
def _add_filenames_argument(self):
self.parser.add_argument('filenames', nargs='*', help='Filenames to check')
return self
def _add_set_baseline_argument(self):
self.parser.add_argument(
'--baseline',
nargs=1,
default=[''],
help='Sets a baseline for explicitly ignored secrets, generated by `--scan`.',
)
return self
class ScanOptions(object):
def __init__(self, subparser):
self.parser = subparser.add_parser(
'scan',
)
def add_arguments(self):
self._add_initialize_baseline_argument()\
._add_adhoc_scanning_argument()
PluginOptions(self.parser).add_arguments()
return self
def _add_initialize_baseline_argument(self):
self.parser.add_argument(
'path',
nargs='?',
default='.',
help=(
'Scans the entire codebase and outputs a snapshot of '
'currently identified secrets.'
),
)
# Pairing `--exclude` with `--scan` because it's only used for the initialization.
# The pre-commit hook framework already has an `exclude` option that can be used instead.
self.parser.add_argument(
'--exclude',
nargs=1,
help='Pass in regex to specify ignored paths during initialization scan.',
)
# Pairing `--update` with `--scan` because it's only used for initialization.
self.parser.add_argument(
'--update',
nargs=1,
metavar='OLD_BASELINE_FILE',
help='Update existing baseline by importing settings from it.',
dest='import_filename',
)
self.parser.add_argument(
'--all-files',
action='store_true',
help='Scan all files recursively (as compared to only scanning git tracked files).',
)
return self
def _add_adhoc_scanning_argument(self):
self.parser.add_argument(
'--string',
nargs='?',
const=True,
help=(
'Scans an individual string, and displays configured '
'plugins\' verdict.'
),
)
return self
class AuditOptions(object):
def __init__(self, subparser):
self.parser = subparser.add_parser(
'audit',
)
def add_arguments(self):
self.parser.add_argument(
'filename',
nargs=1,
help=(
'Audit a given baseline file to distinguish the difference '
'between false and true positives.'
),
)
return self
class PluginDescriptor(namedtuple(
'PluginDescriptor',
[
# Classname of plugin; used for initialization
'classname',
# Flag to disable plugin. e.g. `--no-hex-string-scan`
'disable_flag_text',
# Description for disable flag.
'disable_help_text',
# type: list
# Allows the bundling of all related command line provided
# arguments together, under one plugin name.
# Assumes there is no shared related arg.
#
# Furthermore, each related arg can have its own default
# value (paired together, with a tuple). This allows us to
# distinguish the difference between a default value, and
# whether a user has entered the same value as a default value.
# Therefore, only populate the default value upon consolidation
# (rather than relying on argparse default).
'related_args',
],
)):
def __new__(cls, related_args=None, **kwargs):
if not related_args:
related_args = []
return super(PluginDescriptor, cls).__new__(
cls,
related_args=related_args,
**kwargs
)
class PluginOptions(object):
all_plugins = [
PluginDescriptor(
classname='HexHighEntropyString',
disable_flag_text='--no-hex-string-scan',
disable_help_text='Disables scanning for hex high entropy strings',
related_args=[
('--hex-limit', 3,),
],
),
PluginDescriptor(
classname='Base64HighEntropyString',
disable_flag_text='--no-base64-string-scan',
disable_help_text='Disables scanning for base64 high entropy strings',
related_args=[
('--base64-limit', 4.5,),
],
),
PluginDescriptor(
classname='PrivateKeyDetector',
disable_flag_text='--no-private-key-scan',
disable_help_text='Disables scanning for private keys.',
),
PluginDescriptor(
classname='BasicAuthDetector',
disable_flag_text='--no-basic-auth-scan',
disable_help_text='Disables scanning for Basic Auth formatted URIs.',
),
PluginDescriptor(
classname='KeywordDetector',
disable_flag_text='--no-keyword-scan',
disable_help_text='Disables scanning for secret keywords.',
),
]
def __init__(self, parser):
self.parser = parser.add_argument_group(
title='plugins',
description=(
'Configure settings for each secret scanning '
'ruleset. By default, all plugins are enabled '
'unless explicitly disabled.'
),
)
def add_arguments(self):
self._add_custom_limits()
self._add_opt_out_options()
return self
@staticmethod
def consolidate_args(args):
"""There are many argument fields related to configuring plugins.
This function consolidates all of them, and saves the consolidated
information in args.plugins.
Note that we're deferring initialization of those plugins, because
plugins may have various initialization values, referenced in
different places.
:param args: output of `argparse.ArgumentParser.parse_args`
"""
# Using `--hex-limit` as a canary to identify whether this
# consolidation is appropriate.
if not hasattr(args, 'hex_limit'):
return
active_plugins = {}
for plugin in PluginOptions.all_plugins:
arg_name = PluginOptions._convert_flag_text_to_argument_name(
plugin.disable_flag_text,
)
# Remove disabled plugins
is_disabled = getattr(args, arg_name)
delattr(args, arg_name)
if is_disabled:
continue
# Consolidate related args
related_args = {}
for related_arg_tuple in plugin.related_args:
try:
flag_name, default_value = related_arg_tuple
except ValueError:
flag_name = related_arg_tuple
default_value = None
arg_name = PluginOptions._convert_flag_text_to_argument_name(
flag_name,
)
related_args[arg_name] = getattr(args, arg_name)
delattr(args, arg_name)
if default_value and related_args[arg_name] is None:
related_args[arg_name] = default_value
active_plugins.update({
plugin.classname: related_args,
})
args.plugins = active_plugins
def _add_custom_limits(self):
high_entropy_help_text = (
'Sets the entropy limit for high entropy strings. '
'Value must be between 0.0 and 8.0, '
)
self.parser.add_argument(
'--base64-limit',
type=self._argparse_minmax_type,
nargs='?',
help=high_entropy_help_text + 'defaults to 4.5.',
)
self.parser.add_argument(
'--hex-limit',
type=self._argparse_minmax_type,
nargs='?',
help=high_entropy_help_text + 'defaults to 3.0.',
)
return self
def _add_opt_out_options(self):
for plugin in self.all_plugins:
self.parser.add_argument(
plugin.disable_flag_text,
action='store_true',
help=plugin.disable_help_text,
)
return self
def _argparse_minmax_type(self, string):
"""Custom type for argparse to enforce value limits"""
value = float(string)
if value < 0 or value > 8:
raise argparse.ArgumentTypeError(
'%s must be between 0.0 and 8.0' % string,
)
return value
@staticmethod
def _convert_flag_text_to_argument_name(flag_text):
"""This just emulates argparse's underlying logic.
:type flag_text: str
:param flag_text: eg. `--no-hex-string-scan`
:return: `no_hex_string_scan`
"""
return flag_text[2:].replace('-', '_')