-
Notifications
You must be signed in to change notification settings - Fork 482
/
Copy pathmain.py
236 lines (188 loc) · 6.82 KB
/
main.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
import json
import sys
from detect_secrets.core import audit
from detect_secrets.core import baseline
from detect_secrets.core.common import write_baseline_to_file
from detect_secrets.core.log import log
from detect_secrets.core.secrets_collection import SecretsCollection
from detect_secrets.core.usage import ParserBuilder
from detect_secrets.plugins.common import initialize
from detect_secrets.util import build_automaton
def parse_args(argv):
return ParserBuilder()\
.add_console_use_arguments()\
.parse_args(argv)
def main(argv=sys.argv[1:]):
if len(sys.argv) == 1: # pragma: no cover
sys.argv.append('--help')
args = parse_args(argv)
if args.verbose: # pragma: no cover
log.set_debug_level(args.verbose)
if args.action == 'scan':
automaton = None
word_list_hash = None
if args.word_list_file:
automaton, word_list_hash = build_automaton(args.word_list_file)
# Plugins are *always* rescanned with fresh settings, because
# we want to get the latest updates.
plugins = initialize.from_parser_builder(
plugins_dict=args.plugins,
custom_plugin_paths=args.custom_plugin_paths,
exclude_lines_regex=args.exclude_lines,
automaton=automaton,
should_verify_secrets=not args.no_verify,
)
if args.string:
line = args.string
if isinstance(args.string, bool):
line = sys.stdin.read().splitlines()[0]
_scan_string(line, plugins)
else:
baseline_dict = _perform_scan(
args,
plugins,
automaton,
word_list_hash,
)
if args.import_filename:
write_baseline_to_file(
filename=args.import_filename[0],
data=baseline_dict,
)
else:
print(
baseline.format_baseline_for_output(
baseline_dict,
),
)
elif args.action == 'audit':
if not args.diff and not args.display_results:
audit.audit_baseline(args.filename[0])
return 0
if args.display_results:
audit.print_audit_results(args.filename[0])
return 0
if len(args.filename) != 2:
print(
'Must specify two files to compare!',
file=sys.stderr,
)
return 1
try:
audit.compare_baselines(args.filename[0], args.filename[1])
except audit.RedundantComparisonError:
print(
'No difference, because it\'s the same file!',
file=sys.stderr,
)
return 0
def _get_plugins_from_baseline(old_baseline):
plugins = []
if old_baseline and 'plugins_used' in old_baseline:
secrets_collection = SecretsCollection.load_baseline_from_dict(old_baseline)
plugins = secrets_collection.plugins
return plugins
def _scan_string(line, plugins):
longest_plugin_name_length = max(
map(
lambda x: len(x.__class__.__name__),
plugins,
),
)
output = [
('{:%d}: {}' % longest_plugin_name_length).format(
plugin.__class__.__name__,
plugin.adhoc_scan(line),
)
for plugin in plugins
]
print('\n'.join(sorted(output)))
def _perform_scan(args, plugins, automaton, word_list_hash):
"""
:param args: output of `argparse.ArgumentParser.parse_args`
:param plugins: tuple of initialized plugins
:type automaton: ahocorasick.Automaton|None
:param automaton: optional automaton for ignoring certain words.
:type word_list_hash: str|None
:param word_list_hash: optional iterated sha1 hash of the words in the word list.
:rtype: dict
"""
old_baseline = _get_existing_baseline(args.import_filename)
if old_baseline:
plugins = initialize.merge_plugins_from_baseline(
_get_plugins_from_baseline(old_baseline),
args,
automaton=automaton,
)
# Favors CLI arguments over existing baseline configuration
if old_baseline:
if not args.exclude_files:
args.exclude_files = _get_exclude_files(old_baseline)
if (
not args.exclude_lines
and old_baseline.get('exclude')
):
args.exclude_lines = old_baseline['exclude']['lines']
if (
not args.word_list_file
and old_baseline.get('word_list')
):
args.word_list_file = old_baseline['word_list']['file']
if (
not args.custom_plugin_paths
and old_baseline.get('custom_plugin_paths')
):
args.custom_plugin_paths = old_baseline['custom_plugin_paths']
# If we have knowledge of an existing baseline file, we should use
# that knowledge and add it to our exclude_files regex.
if args.import_filename:
_add_baseline_to_exclude_files(args)
new_baseline = baseline.initialize(
path=args.path,
plugins=plugins,
custom_plugin_paths=args.custom_plugin_paths,
exclude_files_regex=args.exclude_files,
exclude_lines_regex=args.exclude_lines,
word_list_file=args.word_list_file,
word_list_hash=word_list_hash,
should_scan_all_files=args.all_files,
).format_for_baseline_output()
if old_baseline:
new_baseline = baseline.merge_baseline(
old_baseline,
new_baseline,
)
return new_baseline
def _get_existing_baseline(import_filename):
# Favors --update argument over stdin.
if import_filename:
return _read_from_file(import_filename[0])
if not sys.stdin.isatty():
stdin = sys.stdin.read().strip()
if stdin:
return json.loads(stdin)
def _read_from_file(filename): # pragma: no cover
"""Used for mocking."""
with open(filename) as f:
return json.loads(f.read())
def _get_exclude_files(old_baseline):
"""
Older versions of detect-secrets always had an `exclude_regex` key,
this was replaced by the `files` key under an `exclude` key in v0.12.0
:rtype: str|None
"""
if old_baseline.get('exclude'):
return old_baseline['exclude']['files']
if old_baseline.get('exclude_regex'):
return old_baseline['exclude_regex']
def _add_baseline_to_exclude_files(args):
"""
Modifies args.exclude_files in-place.
"""
baseline_name_regex = r'^{}$'.format(args.import_filename[0])
if not args.exclude_files:
args.exclude_files = baseline_name_regex
elif baseline_name_regex not in args.exclude_files:
args.exclude_files += r'|{}'.format(baseline_name_regex)
if __name__ == '__main__':
sys.exit(main())