Skip to content

Commit e8e7394

Browse files
committed
Fix ImportError for old versions of urllib3/requests
Old versions of urllib3/requests don't have a create_urllib3_context method. We work around this by refusing to force TLSv1.2 connections on this setup.
1 parent 19e8a35 commit e8e7394

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

src/cbapi/connection.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
import requests
66
import sys
77
from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK, DEFAULT_RETRIES, DEFAULT_POOLSIZE
8-
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
8+
9+
try:
10+
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
11+
REQUESTS_HAS_URLLIB_SSL_CONTEXT = True
12+
except ImportError:
13+
REQUESTS_HAS_URLLIB_SSL_CONTEXT = False
14+
915
import ssl
1016

1117

@@ -62,10 +68,14 @@ class CbAPISessionAdapter(HTTPAdapter):
6268
def __init__(self, verify_hostname=True, force_tls_1_2=False, max_retries=DEFAULT_RETRIES, **pool_kwargs):
6369
self._cbapi_verify_hostname = verify_hostname
6470
self._cbapi_force_tls_1_2 = force_tls_1_2
71+
72+
if force_tls_1_2 and not REQUESTS_HAS_URLLIB_SSL_CONTEXT:
73+
raise ApiError("Cannot force the use of TLS1.2: Python, urllib3, and requests versions are too old.")
74+
6575
super(CbAPISessionAdapter, self).__init__(max_retries=max_retries, **pool_kwargs)
6676

6777
def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs):
68-
if self._cbapi_force_tls_1_2:
78+
if self._cbapi_force_tls_1_2 and REQUESTS_HAS_URLLIB_SSL_CONTEXT:
6979
# Force the use of TLS v1.2 when talking to this Cb Response server.
7080
context = create_urllib3_context(ciphers=('TLSv1.2:!aNULL:!eNULL:!MD5'))
7181
context.options |= ssl.OP_NO_SSLv2

0 commit comments

Comments
 (0)