Repository navigation
Expand file tree
/
Copy pathtest_ocsp.py
More file actions
407 lines (324 loc) · 12.7 KB
/
Copy pathtest_ocsp.py
File metadata and controls
407 lines (324 loc) · 12.7 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2019 Snowflake Computing Inc. All right reserved.
#
import codecs
import json
import logging
import tempfile
import time
import os
import platform
from os import path
from os import environ
import pytest
from snowflake.connector import OperationalError
from snowflake.connector.compat import PY2
from snowflake.connector.errorcode import (ER_SERVER_CERTIFICATE_REVOKED, ER_INVALID_OCSP_RESPONSE_CODE)
from snowflake.connector.ocsp_snowflake import SnowflakeOCSP
from snowflake.connector.errors import RevocationCheckError
if PY2:
from snowflake.connector.ocsp_pyasn1 import (
SnowflakeOCSPPyasn1 as SFOCSP
)
else:
from snowflake.connector.ocsp_asn1crypto import (
SnowflakeOCSPAsn1Crypto as SFOCSP
)
from snowflake.connector.ocsp_snowflake import OCSPCache
from snowflake.connector.ssl_wrap_socket import _openssl_connect
for logger_name in ['test', 'snowflake.connector', 'botocore']:
logger = logging.getLogger(logger_name)
logger.setLevel(logging.DEBUG)
ch = logging.FileHandler(
path.join(tempfile.gettempdir(), 'python_connector.log'))
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter(
'%(asctime)s - %(threadName)s %(filename)s:%(lineno)d - '
'%(funcName)s() - %(levelname)s - %(message)s'))
logger.addHandler(ch)
TARGET_HOSTS = [
'ocspssd.us-east-1.snowflakecomputing.com',
'sqs.us-west-2.amazonaws.com',
'sfcsupport.us-east-1.snowflakecomputing.com',
'sfcsupport.eu-central-1.snowflakecomputing.com',
'sfc-dev1-regression.s3.amazonaws.com',
'sfctest0.snowflakecomputing.com',
'sfc-ds2-customer-stage.s3.amazonaws.com',
'snowflake.okta.com',
'sfcdev1.blob.core.windows.net',
'sfc-aus-ds1-customer-stage.s3-ap-southeast-2.amazonaws.com',
]
THIS_DIR = path.dirname(path.realpath(__file__))
CACHE_ROOT_DIR = os.getenv('SF_OCSP_RESPONSE_CACHE_DIR') or \
os.path.expanduser("~") or tempfile.gettempdir()
if platform.system() == 'Windows':
CACHE_DIR = path.join(CACHE_ROOT_DIR, 'AppData', 'Local', 'Snowflake',
'Caches')
elif platform.system() == 'Darwin':
CACHE_DIR = path.join(CACHE_ROOT_DIR, 'Library', 'Caches', 'Snowflake')
else:
CACHE_DIR = path.join(CACHE_ROOT_DIR, '.cache', 'snowflake')
CACHE_LOCATION = path.join(CACHE_DIR, "ocsp_response_cache.json")
def test_ocsp():
"""
OCSP tests
"""
# reset the memory cache
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP()
for url in TARGET_HOSTS:
connection = _openssl_connect(url)
assert ocsp.validate(url, connection), \
'Failed to validate: {0}'.format(url)
def test_ocsp_wo_cache_server():
"""
OCSP Tests with Cache Server Disabled
"""
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP(use_ocsp_cache_server=False)
for url in TARGET_HOSTS:
connection = _openssl_connect(url)
assert ocsp.validate(url, connection),\
'Failed to validate: {0}'.format(url)
def test_ocsp_fail_open_w_single_endpoint():
SnowflakeOCSP.clear_cache()
if os.path.exists(CACHE_LOCATION):
os.remove(CACHE_LOCATION)
environ["SF_OCSP_TEST_MODE"] = "true"
environ["SF_TEST_OCSP_URL"] = "http://httpbin.org/delay/10"
environ["SF_TEST_CA_OCSP_RESPONDER_CONNECTION_TIMEOUT"] = "5"
ocsp = SFOCSP(use_ocsp_cache_server=False)
connection = _openssl_connect("snowflake.okta.com")
try:
assert ocsp.validate("snowflake.okta.com", connection), \
'Failed to validate: {0}'.format("snowflake.okta.com")
finally:
del environ['SF_OCSP_TEST_MODE']
del environ['SF_TEST_OCSP_URL']
del environ['SF_TEST_CA_OCSP_RESPONDER_CONNECTION_TIMEOUT']
def test_ocsp_fail_close_w_single_endpoint():
SnowflakeOCSP.clear_cache()
environ["SF_OCSP_TEST_MODE"] = "true"
environ["SF_TEST_OCSP_URL"] = "http://httpbin.org/delay/10"
environ["SF_TEST_CA_OCSP_RESPONDER_CONNECTION_TIMEOUT"] = "5"
if os.path.exists(CACHE_LOCATION):
os.remove(CACHE_LOCATION)
ocsp = SFOCSP(use_ocsp_cache_server=False, use_fail_open=False)
connection = _openssl_connect("snowflake.okta.com")
with pytest.raises(RevocationCheckError) as ex:
ocsp.validate("snowflake.okta.com", connection)
try:
assert ex.value.errno == ER_INVALID_OCSP_RESPONSE_CODE, "Connection should have failed"
finally:
del environ['SF_OCSP_TEST_MODE']
del environ['SF_TEST_OCSP_URL']
del environ['SF_TEST_CA_OCSP_RESPONDER_CONNECTION_TIMEOUT']
def test_ocsp_bad_validity():
SnowflakeOCSP.clear_cache()
environ["SF_OCSP_TEST_MODE"] = "true"
environ["SF_TEST_OCSP_FORCE_BAD_RESPONSE_VALIDITY"] = "true"
if os.path.exists(CACHE_LOCATION):
os.remove(CACHE_LOCATION)
ocsp = SFOCSP(use_ocsp_cache_server=False)
connection = _openssl_connect("snowflake.okta.com")
assert ocsp.validate("snowflake.okta.com", connection), "Connection should have passed with fail open"
del environ['SF_OCSP_TEST_MODE']
del environ['SF_TEST_OCSP_FORCE_BAD_RESPONSE_VALIDITY']
def test_ocsp_single_endpoint():
environ['SF_OCSP_ACTIVATE_NEW_ENDPOINT'] = 'True'
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP()
ocsp.OCSP_CACHE_SERVER.NEW_DEFAULT_CACHE_SERVER_BASE_URL = \
"https://snowflake.preprod2.us-west-2-dev.external-zone.snowflakecomputing.com:8085/ocsp/"
connection = _openssl_connect("snowflake.okta.com")
assert ocsp.validate("snowflake.okta.com", connection), \
'Failed to validate: {0}'.format("snowflake.okta.com")
del environ['SF_OCSP_ACTIVATE_NEW_ENDPOINT']
def test_ocsp_by_post_method():
"""
OCSP tests
"""
# reset the memory cache
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP(use_post_method=True)
for url in TARGET_HOSTS:
connection = _openssl_connect(url)
assert ocsp.validate(url, connection), \
'Failed to validate: {0}'.format(url)
def test_ocsp_with_file_cache(tmpdir):
"""
OCSP tests and the cache server and file
"""
tmp_dir = str(tmpdir.mkdir('ocsp_response_cache'))
cache_file_name = path.join(tmp_dir, 'cache_file.txt')
# reset the memory cache
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
for url in TARGET_HOSTS:
connection = _openssl_connect(url)
assert ocsp.validate(url, connection), \
'Failed to validate: {0}'.format(url)
def test_ocsp_with_bogus_cache_files(tmpdir):
"""
Attempt to use bogus OCSP response data
"""
cache_file_name, target_hosts = _store_cache_in_file(tmpdir)
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
OCSPCache.read_ocsp_response_cache_file(ocsp, cache_file_name)
cache_data = OCSPCache.CACHE
assert cache_data, "more than one cache entries should be stored."
# setting bogus data
current_time = int(time.time())
for k, v in cache_data.items():
cache_data[k] = (current_time, b'bogus')
# write back the cache file
OCSPCache.CACHE = cache_data
OCSPCache.write_ocsp_response_cache_file(ocsp, cache_file_name)
# forces to use the bogus cache file but it should raise errors
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
for hostname in target_hosts:
connection = _openssl_connect(hostname)
assert ocsp.validate(hostname, connection), \
'Failed to validate: {0}'.format(hostname)
def test_ocsp_with_outdated_cache(tmpdir):
"""
Attempt to use outdated OCSP response cache file
"""
cache_file_name, target_hosts = _store_cache_in_file(tmpdir)
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
# reading cache file
OCSPCache.read_ocsp_response_cache_file(ocsp, cache_file_name)
cache_data = OCSPCache.CACHE
assert cache_data, "more than one cache entries should be stored."
# setting outdated data
current_time = int(time.time())
for k, v in cache_data.items():
cache_data[k] = (current_time - 48 * 60 * 60, v[1])
# write back the cache file
OCSPCache.CACHE = cache_data
OCSPCache.write_ocsp_response_cache_file(ocsp, cache_file_name)
# forces to use the bogus cache file but it should raise errors
SnowflakeOCSP.clear_cache() # reset the memory cache
SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
assert SnowflakeOCSP.cache_size() == 0, \
'must be empty. outdated cache should not be loaded'
def _store_cache_in_file(
tmpdir, target_hosts=None, filename=None):
if target_hosts is None:
target_hosts = TARGET_HOSTS
if filename is None:
filename = path.join(str(tmpdir), 'cache_file.txt')
# cache OCSP response
SnowflakeOCSP.clear_cache()
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + filename,
use_ocsp_cache_server=False)
for hostname in target_hosts:
connection = _openssl_connect(hostname)
assert ocsp.validate(hostname, connection), \
'Failed to validate: {0}'.format(hostname)
assert path.exists(filename), "OCSP response cache file"
return filename, target_hosts
def test_ocsp_with_invalid_cache_file():
"""
OCSP tests with an invalid cache file
"""
SnowflakeOCSP.clear_cache() # reset the memory cache
ocsp = SFOCSP(ocsp_response_cache_uri="NEVER_EXISTS")
for url in TARGET_HOSTS[0:1]:
connection = _openssl_connect(url)
assert ocsp.validate(url, connection), \
'Failed to validate: {0}'.format(url)
def test_concurrent_ocsp_requests(tmpdir):
"""
Run OCSP revocation checks in parallel. The memory and file caches are
deleted randomly.
"""
from multiprocessing.pool import ThreadPool
cache_file_name = path.join(str(tmpdir), 'cache_file.txt')
SnowflakeOCSP.clear_cache() # reset the memory cache
target_hosts = TARGET_HOSTS * 5
pool = ThreadPool(len(target_hosts))
for hostname in target_hosts:
pool.apply_async(_validate_certs_using_ocsp,
[hostname, cache_file_name])
pool.close()
pool.join()
def _validate_certs_using_ocsp(url, cache_file_name):
"""
Validate OCSP response. Deleting memory cache and file cache randomly
"""
logger = logging.getLogger('test')
import time
import random
time.sleep(random.randint(0, 3))
if random.random() < 0.2:
logger.info('clearing up cache: OCSP_VALIDATION_CACHE')
SnowflakeOCSP.clear_cache()
if random.random() < 0.05:
logger.info('deleting a cache file: %s', cache_file_name)
SnowflakeOCSP.delete_cache_file()
connection = _openssl_connect(url)
ocsp = SFOCSP(
ocsp_response_cache_uri='file://' + cache_file_name)
ocsp.validate(url, connection)
def test_ocsp_revoked_certificate():
"""
Test Revoked certificate.
"""
revoked_cert = path.join(
THIS_DIR, 'data', 'cert_tests', 'revoked_certs.pem')
SnowflakeOCSP.clear_cache() # reset the memory cache
ocsp = SFOCSP()
with pytest.raises(OperationalError) as ex:
ocsp.validate_certfile(revoked_cert)
assert ex.value.errno == ex.value.errno == ER_SERVER_CERTIFICATE_REVOKED
def test_ocsp_incomplete_chain():
"""
Test incomplete chained certificate
"""
incomplete_chain_cert = path.join(
THIS_DIR, 'data', 'cert_tests', 'incomplete-chain.pem')
SnowflakeOCSP.clear_cache() # reset the memory cache
ocsp = SFOCSP()
with pytest.raises(OperationalError) as ex:
ocsp.validate_certfile(incomplete_chain_cert)
assert 'CA certificate is NOT found' in ex.value.msg
def test_ocsp_cache_merge(tmpdir):
"""
Merge two OCSP response cache files
"""
previous_cache_filename = path.join(str(tmpdir), 'cache_file1.txt')
_store_cache_in_file(
tmpdir,
target_hosts=TARGET_HOSTS[0:3],
filename=previous_cache_filename)
current_cache_filename = path.join(str(tmpdir), 'cache_file2.txt')
_store_cache_in_file(
tmpdir,
target_hosts=TARGET_HOSTS[4:],
filename=current_cache_filename)
latest_cache_filename = path.join(str(tmpdir), 'cache_file.txt')
SnowflakeOCSP.clear_cache() # reset the memory cache
ocsp = SFOCSP()
OCSPCache.merge_cache(
ocsp,
previous_cache_filename,
current_cache_filename,
latest_cache_filename)
with codecs.open(previous_cache_filename) as f:
prev = json.load(f)
with codecs.open(current_cache_filename) as f:
curr = json.load(f)
with codecs.open(latest_cache_filename) as f:
latest = json.load(f)
assert len(latest) > len(prev)
assert len(latest) > len(curr)