forked from Yelp/detect-secrets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_performance_tests.py
executable file
·239 lines (196 loc) · 5.6 KB
/
run_performance_tests.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
import argparse
import json
import os
import random
import subprocess
import sys
import tempfile
from enum import Enum
from detect_secrets.util import get_root_directory
class TestCase(Enum):
LONG_FILE = 1
LONG_LINES = 2
def main():
args = parse_args()
# Get data from baseline
if args.baseline:
config = args.baseline['config']
args.mode = config['mode']
args.length = config['length']
mode = None
for case in TestCase:
if case.name == args.mode:
mode = case
break
content = generate_test_content(
mode,
timeout=args.harakiri,
length=args.length,
)
output = scan_content(
content,
timeout=args.harakiri,
baseline=args.baseline,
)
if not args.baseline:
temp = json.loads(output)
temp['config'] = {
'mode': mode.name,
'length': args.length,
}
output = json.dumps(temp)
print(output)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
'--harakiri',
default=30,
type=assert_positive(float),
help=(
'Specifies an upper bound for number of seconds to wait for '
'each test.'
),
)
parser.add_argument(
'--baseline',
type=assert_valid_file,
help=(
'Specifies test config file to run. If this is provided, '
'all config options will be referenced from this file.'
),
)
parser.add_argument(
'-m',
'--mode',
choices=[
value.name
for value in TestCase
],
required=True,
help='Specifies the type of content to generate.',
)
parser.add_argument(
'-L',
'--length',
type=assert_positive(int),
help='Length of test case content.',
)
return parser.parse_args()
def assert_positive(type):
def wrapped(string):
value = type(string)
if value <= 0:
raise argparse.ArgumentTypeError(
'{} must be a positive {}.'.format(
string,
type.__name__,
),
)
return value
return wrapped
def assert_valid_file(string):
if not os.path.isfile(string):
raise argparse.ArgumentTypeError(
'{} must be a valid file.'.format(string),
)
with open(string) as f:
return json.load(f)
def generate_test_content(mode, **kwargs):
"""
:type mode: TestCase
:type length: int
:type timeout: float
"""
if not kwargs['length']:
del kwargs['length']
print('Generating content...', file=sys.stderr)
if mode == TestCase.LONG_FILE:
return generate_long_file(**kwargs)
elif mode == TestCase.LONG_LINES:
return generate_long_lines(**kwargs)
def scan_content(content, timeout, baseline=None):
"""
:type content: str
:type timeout: float
:type baseline: dict|None
"""
args = [
'python',
os.path.join(
get_root_directory(),
'scripts/benchmark.py',
),
'--harakiri', str(timeout),
]
with tempfile.NamedTemporaryFile('w') as f:
f.write(content)
print('Running checks...', file=sys.stderr)
if not baseline:
args.append(f.name)
return subprocess.check_output(
args,
stderr=subprocess.DEVNULL,
).decode('utf-8')
with tempfile.NamedTemporaryFile('w') as b:
b.write(
json.dumps({
'filenames': [f.name],
'timings': baseline['timings'],
}),
)
b.seek(0)
args.append('--baseline')
args.append(b.name)
return subprocess.check_output(
args,
stderr=subprocess.DEVNULL,
).decode('utf-8')
def generate_long_file(length=250000, **kwargs):
return generate_content(
separator='\n',
length=length,
)
def generate_long_lines(length=250000, **kwargs):
return generate_content(
separator=' ',
length=length,
)
def generate_content(separator, length):
"""
:type secret: str
:type separator: str
:type length: int
"""
valid_secrets = {
'AWSKeyDetector': 'AKIATESTTESTTESTTEST',
'ArtifactoryDetector': ':AKCtestTESTte',
'Base64HighEntropyString': 'Y29uZ3JhdHVsYXRpb25zISB0aGlzIGlzIGEgaGlkZGVuIG1lc3NhZ2U=',
'BasicAuthDetector': 'http://username:[email protected]',
'HexHighEntropyString': '123456abcd',
'KeywordDetector': 'api_key = foobar',
'MailchimpDetector': '376a2953ed38c31a43ea46e2b19257db-us2',
'PrivateKeyDetector': 'BEGIN PRIVATE KEY',
'SlackDetector': 'xoxb-1-test',
'StripeDetector': 'rk_live_TESTtestTESTtestTESTtest',
}
with open(
os.path.join(
get_root_directory(),
'test_data/performance/best-songs.txt',
),
) as f:
source_material = f.read().splitlines()
indexes = {}
for key in valid_secrets:
index = random.randint(0, length - 1)
indexes[index] = key
content = []
for line_number in range(length):
if line_number in indexes:
content.append(valid_secrets[indexes[line_number]])
else:
random_line = random.randint(0, len(source_material) - 1)
content.append(source_material[random_line])
return separator.join(content)
if __name__ == '__main__':
main()