19
19
import pytest
20
20
import requests
21
21
22
+ from warehouse .integrations import verifier
22
23
from warehouse .integrations .github import tasks , utils
23
24
24
25
@@ -81,55 +82,27 @@ def test_token_leak_disclosure_request_from_api_record():
81
82
assert request .public_url == "http://example.com"
82
83
83
84
84
- class TestCache :
85
- def test_set (self ):
86
- cache = utils .PublicKeysCache (cache_time = 10 )
87
- cache .set (now = 1 , value = "foo" )
88
-
89
- assert cache .cached_at == 1
90
- assert cache .cache == "foo"
91
-
92
- def test_get_no_cache (self ):
93
- cache = utils .PublicKeysCache (cache_time = 10 )
94
-
95
- with pytest .raises (utils .CacheMiss ):
96
- cache .get (now = 1 )
97
-
98
- def test_get_old_cache (self ):
99
- cache = utils .PublicKeysCache (cache_time = 10 )
100
- cache .set (now = 5 , value = "foo" )
101
-
102
- with pytest .raises (utils .CacheMiss ):
103
- cache .get (now = 20 )
104
-
105
- def test_get_valid (self ):
106
- cache = utils .PublicKeysCache (cache_time = 10 )
107
- cache .set (now = 5 , value = "foo" )
108
-
109
- assert cache .get (now = 10 ) == "foo"
110
-
111
-
112
85
class TestGitHubTokenScanningPayloadVerifier :
113
86
def test_init (self ):
114
87
metrics = pretend .stub ()
115
88
session = pretend .stub ()
116
89
token = "api_token"
117
90
url = "http://foo"
118
- cache = utils .PublicKeysCache (cache_time = 12 )
91
+ cache = verifier .PublicKeysCache (cache_time = 12 )
119
92
120
- verifier = utils .GitHubTokenScanningPayloadVerifier (
121
- api_url = url ,
93
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
94
+ api_url = "http://foo" ,
122
95
session = session ,
123
96
metrics = metrics ,
124
97
api_token = token ,
125
98
public_keys_cache = cache ,
126
99
)
127
100
128
- assert verifier ._session is session
129
- assert verifier ._metrics is metrics
130
- assert verifier ._api_token == token
131
- assert verifier ._api_url == url
132
- assert verifier ._public_keys_cache is cache
101
+ assert github_verifier ._session is session
102
+ assert github_verifier ._metrics is metrics
103
+ assert github_verifier ._api_token == token
104
+ assert github_verifier ._api_url == url
105
+ assert github_verifier ._public_keys_cache is cache
133
106
134
107
def test_verify_cache_miss (self ):
135
108
# Example taken from
@@ -152,8 +125,8 @@ def test_verify_cache_miss(self):
152
125
)
153
126
session = pretend .stub (get = lambda * a , ** k : response )
154
127
metrics = pretend .stub (increment = pretend .call_recorder (lambda str : None ))
155
- cache = utils .PublicKeysCache (cache_time = 12 )
156
- verifier = utils .GitHubTokenScanningPayloadVerifier (
128
+ cache = verifier .PublicKeysCache (cache_time = 12 )
129
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
157
130
api_url = "http://foo" ,
158
131
session = session ,
159
132
metrics = metrics ,
@@ -172,7 +145,8 @@ def test_verify_cache_miss(self):
172
145
b'b0dd59c0b500650cacd4551ca5989a6194001b10/production.env"}]'
173
146
)
174
147
assert (
175
- verifier .verify (payload = payload , key_id = key_id , signature = signature ) is True
148
+ github_verifier .verify (payload = payload , key_id = key_id , signature = signature )
149
+ is True
176
150
)
177
151
178
152
assert metrics .increment .calls == [
@@ -183,7 +157,7 @@ def test_verify_cache_miss(self):
183
157
def test_verify_cache_hit (self ):
184
158
session = pretend .stub ()
185
159
metrics = pretend .stub (increment = pretend .call_recorder (lambda str : None ))
186
- cache = utils .PublicKeysCache (cache_time = 12 )
160
+ cache = verifier .PublicKeysCache (cache_time = 12 )
187
161
cache .cached_at = time .time ()
188
162
cache .cache = [
189
163
{
@@ -195,7 +169,7 @@ def test_verify_cache_hit(self):
195
169
"-----END PUBLIC KEY-----" ,
196
170
}
197
171
]
198
- verifier = utils .GitHubTokenScanningPayloadVerifier (
172
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
199
173
api_url = "http://foo" ,
200
174
session = session ,
201
175
metrics = metrics ,
@@ -215,7 +189,8 @@ def test_verify_cache_hit(self):
215
189
b'b0dd59c0b500650cacd4551ca5989a6194001b10/production.env"}]'
216
190
)
217
191
assert (
218
- verifier .verify (payload = payload , key_id = key_id , signature = signature ) is True
192
+ github_verifier .verify (payload = payload , key_id = key_id , signature = signature )
193
+ is True
219
194
)
220
195
221
196
assert metrics .increment .calls == [
@@ -225,19 +200,19 @@ def test_verify_cache_hit(self):
225
200
226
201
def test_verify_error (self ):
227
202
metrics = pretend .stub (increment = pretend .call_recorder (lambda str : None ))
228
- cache = utils .PublicKeysCache (cache_time = 12 )
229
- verifier = utils .GitHubTokenScanningPayloadVerifier (
203
+ cache = verifier .PublicKeysCache (cache_time = 12 )
204
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
230
205
api_url = "http://foo" ,
231
206
session = pretend .stub (),
232
207
metrics = metrics ,
233
208
api_token = "api-token" ,
234
209
public_keys_cache = cache ,
235
210
)
236
- verifier . _retrieve_public_key_payload = pretend .raiser (
237
- utils . InvalidTokenLeakRequest ("Bla" , "bla" )
211
+ github_verifier . retrieve_public_key_payload = pretend .raiser (
212
+ verifier . InvalidPayloadSignature ("Bla" , "bla" )
238
213
)
239
214
240
- assert verifier .verify (payload = {}, key_id = "a" , signature = "a" ) is False
215
+ assert github_verifier .verify (payload = {}, key_id = "a" , signature = "a" ) is False
241
216
242
217
assert metrics .increment .calls == [
243
218
pretend .call ("warehouse.token_leak.github.auth.cache.miss" ),
@@ -284,14 +259,14 @@ def test_retrieve_public_key_payload(self):
284
259
session = pretend .stub (get = pretend .call_recorder (lambda * a , ** k : response ))
285
260
metrics = pretend .stub (increment = pretend .call_recorder (lambda str : None ))
286
261
287
- verifier = utils .GitHubTokenScanningPayloadVerifier (
262
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
288
263
api_url = "http://foo" ,
289
264
session = session ,
290
265
metrics = metrics ,
291
266
api_token = "api-token" ,
292
267
public_keys_cache = pretend .stub (),
293
268
)
294
- assert verifier . _retrieve_public_key_payload () == meta_payload
269
+ assert github_verifier . retrieve_public_key_payload () == meta_payload
295
270
assert session .get .calls == [
296
271
pretend .call (
297
272
"http://foo" ,
@@ -302,33 +277,33 @@ def test_retrieve_public_key_payload(self):
302
277
def test_get_cached_public_key_cache_hit (self ):
303
278
metrics = pretend .stub ()
304
279
session = pretend .stub ()
305
- cache = utils .PublicKeysCache (cache_time = 12 )
280
+ cache = verifier .PublicKeysCache (cache_time = 12 )
306
281
cache_value = pretend .stub ()
307
282
cache .set (now = time .time (), value = cache_value )
308
283
309
- verifier = utils .GitHubTokenScanningPayloadVerifier (
284
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
310
285
api_url = "http://foo" ,
311
286
session = session ,
312
287
metrics = metrics ,
313
288
public_keys_cache = cache ,
314
289
)
315
290
316
- assert verifier ._get_cached_public_keys () is cache_value
291
+ assert github_verifier ._get_cached_public_keys () is cache_value
317
292
318
293
def test_get_cached_public_key_cache_miss_no_cache (self ):
319
294
metrics = pretend .stub ()
320
295
session = pretend .stub ()
321
- cache = utils .PublicKeysCache (cache_time = 12 )
296
+ cache = verifier .PublicKeysCache (cache_time = 12 )
322
297
323
- verifier = utils .GitHubTokenScanningPayloadVerifier (
298
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
324
299
api_url = "http://foo" ,
325
300
session = session ,
326
301
metrics = metrics ,
327
302
public_keys_cache = cache ,
328
303
)
329
304
330
- with pytest .raises (utils .CacheMiss ):
331
- verifier ._get_cached_public_keys ()
305
+ with pytest .raises (verifier .CacheMiss ):
306
+ github_verifier ._get_cached_public_keys ()
332
307
333
308
def test_retrieve_public_key_payload_http_error (self ):
334
309
response = pretend .stub (
@@ -339,14 +314,14 @@ def test_retrieve_public_key_payload_http_error(self):
339
314
session = pretend .stub (
340
315
get = lambda * a , ** k : response ,
341
316
)
342
- verifier = utils .GitHubTokenScanningPayloadVerifier (
317
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
343
318
api_url = "http://foo" ,
344
319
session = session ,
345
320
metrics = pretend .stub (),
346
321
public_keys_cache = pretend .stub (),
347
322
)
348
323
with pytest .raises (utils .GitHubPublicKeyMetaAPIError ) as exc :
349
- verifier . _retrieve_public_key_payload ()
324
+ github_verifier . retrieve_public_key_payload ()
350
325
351
326
assert str (exc .value ) == "Invalid response code 418: I'm a teapot"
352
327
assert exc .value .reason == "public_key_api.status.418"
@@ -358,30 +333,30 @@ def test_retrieve_public_key_payload_json_error(self):
358
333
raise_for_status = lambda : None ,
359
334
)
360
335
session = pretend .stub (get = lambda * a , ** k : response )
361
- verifier = utils .GitHubTokenScanningPayloadVerifier (
336
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
362
337
api_url = "http://foo" ,
363
338
session = session ,
364
339
metrics = pretend .stub (),
365
340
public_keys_cache = pretend .stub (),
366
341
)
367
342
with pytest .raises (utils .GitHubPublicKeyMetaAPIError ) as exc :
368
- verifier . _retrieve_public_key_payload ()
343
+ github_verifier . retrieve_public_key_payload ()
369
344
370
345
assert str (exc .value ) == "Non-JSON response received: Still a non-json teapot"
371
346
assert exc .value .reason == "public_key_api.invalid_json"
372
347
373
348
def test_retrieve_public_key_payload_connection_error (self ):
374
349
session = pretend .stub (get = pretend .raiser (requests .ConnectionError ))
375
350
376
- verifier = utils .GitHubTokenScanningPayloadVerifier (
351
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
377
352
api_url = "http://foo" ,
378
353
session = session ,
379
354
metrics = pretend .stub (),
380
355
public_keys_cache = pretend .stub (),
381
356
)
382
357
383
358
with pytest .raises (utils .GitHubPublicKeyMetaAPIError ) as exc :
384
- verifier . _retrieve_public_key_payload ()
359
+ github_verifier . retrieve_public_key_payload ()
385
360
386
361
assert str (exc .value ) == "Could not connect to GitHub"
387
362
assert exc .value .reason == "public_key_api.network_error"
@@ -400,15 +375,15 @@ def test_extract_public_keys(self):
400
375
}
401
376
]
402
377
}
403
- cache = utils .PublicKeysCache (cache_time = 12 )
404
- verifier = utils .GitHubTokenScanningPayloadVerifier (
378
+ cache = verifier .PublicKeysCache (cache_time = 12 )
379
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
405
380
api_url = "http://foo" ,
406
381
session = pretend .stub (),
407
382
metrics = pretend .stub (),
408
383
public_keys_cache = cache ,
409
384
)
410
385
411
- keys = verifier . _extract_public_keys (pubkey_api_data = meta_payload )
386
+ keys = github_verifier . extract_public_keys (pubkey_api_data = meta_payload )
412
387
413
388
assert keys == [
414
389
{
@@ -443,23 +418,23 @@ def test_extract_public_keys(self):
443
418
],
444
419
)
445
420
def test_extract_public_keys_error (self , payload , expected ):
446
- cache = utils .PublicKeysCache (cache_time = 12 )
447
- verifier = utils .GitHubTokenScanningPayloadVerifier (
421
+ cache = verifier .PublicKeysCache (cache_time = 12 )
422
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
448
423
api_url = "http://foo" ,
449
424
session = pretend .stub (),
450
425
metrics = pretend .stub (),
451
426
public_keys_cache = cache ,
452
427
)
453
428
454
429
with pytest .raises (utils .GitHubPublicKeyMetaAPIError ) as exc :
455
- list (verifier . _extract_public_keys (pubkey_api_data = payload ))
430
+ list (github_verifier . extract_public_keys (pubkey_api_data = payload ))
456
431
457
432
assert exc .value .reason == "public_key_api.format_error"
458
433
assert str (exc .value ) == expected
459
434
assert cache .cache is None
460
435
461
436
def test_check_public_key (self ):
462
- verifier = utils .GitHubTokenScanningPayloadVerifier (
437
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
463
438
api_url = "http://foo" ,
464
439
session = pretend .stub (),
465
440
metrics = pretend .stub (),
@@ -470,24 +445,24 @@ def test_check_public_key(self):
470
445
{"key_id" : "a" , "key" : "b" },
471
446
{"key_id" : "c" , "key" : "d" },
472
447
]
473
- assert verifier ._check_public_key (github_public_keys = keys , key_id = "c" ) == "d"
448
+ assert github_verifier ._check_public_key (public_keys = keys , key_id = "c" ) == "d"
474
449
475
450
def test_check_public_key_error (self ):
476
- verifier = utils .GitHubTokenScanningPayloadVerifier (
451
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
477
452
api_url = "http://foo" ,
478
453
session = pretend .stub (),
479
454
metrics = pretend .stub (),
480
455
public_keys_cache = pretend .stub (),
481
456
)
482
457
483
- with pytest .raises (utils . InvalidTokenLeakRequest ) as exc :
484
- verifier ._check_public_key (github_public_keys = [], key_id = "c" )
458
+ with pytest .raises (verifier . InvalidPayloadSignature ) as exc :
459
+ github_verifier ._check_public_key (public_keys = [], key_id = "c" )
485
460
486
- assert str (exc .value ) == "Key c not found in github public keys"
461
+ assert str (exc .value ) == "Key c not found in public keys"
487
462
assert exc .value .reason == "wrong_key_id"
488
463
489
464
def test_check_signature (self ):
490
- verifier = utils .GitHubTokenScanningPayloadVerifier (
465
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
491
466
api_url = "http://foo" ,
492
467
session = pretend .stub (),
493
468
metrics = pretend .stub (),
@@ -510,14 +485,14 @@ def test_check_signature(self):
510
485
b'b0dd59c0b500650cacd4551ca5989a6194001b10/production.env"}]'
511
486
)
512
487
assert (
513
- verifier ._check_signature (
488
+ github_verifier ._check_signature (
514
489
payload = payload , public_key = public_key , signature = signature
515
490
)
516
491
is None
517
492
)
518
493
519
494
def test_check_signature_invalid_signature (self ):
520
- verifier = utils .GitHubTokenScanningPayloadVerifier (
495
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
521
496
api_url = "http://foo" ,
522
497
session = pretend .stub (),
523
498
metrics = pretend .stub (),
@@ -540,16 +515,16 @@ def test_check_signature_invalid_signature(self):
540
515
b'f43808034d7f5","url":" https://github.com/github/faketestrepo/blob/'
541
516
b'b0dd59c0b500650cacd4551ca5989a6194001b10/production.env"}]'
542
517
)
543
- with pytest .raises (utils . InvalidTokenLeakRequest ) as exc :
544
- verifier ._check_signature (
518
+ with pytest .raises (verifier . InvalidPayloadSignature ) as exc :
519
+ github_verifier ._check_signature (
545
520
payload = payload , public_key = public_key , signature = signature
546
521
)
547
522
548
523
assert str (exc .value ) == "Invalid signature"
549
524
assert exc .value .reason == "invalid_signature"
550
525
551
526
def test_check_signature_invalid_crypto (self ):
552
- verifier = utils .GitHubTokenScanningPayloadVerifier (
527
+ github_verifier = utils .GitHubTokenScanningPayloadVerifier (
553
528
api_url = "http://foo" ,
554
529
session = pretend .stub (),
555
530
metrics = pretend .stub (),
@@ -560,8 +535,8 @@ def test_check_signature_invalid_crypto(self):
560
535
561
536
payload = "yeah, nope, that won't pass"
562
537
563
- with pytest .raises (utils . InvalidTokenLeakRequest ) as exc :
564
- verifier ._check_signature (
538
+ with pytest .raises (verifier . InvalidPayloadSignature ) as exc :
539
+ github_verifier ._check_signature (
565
540
payload = payload , public_key = public_key , signature = signature
566
541
)
567
542
0 commit comments