| 1 |
import ssl |
| 2 |
|
| 3 |
from requests import certs |
| 4 |
from requests.adapters import HTTPAdapter, DEFAULT_POOLBLOCK |
| 5 |
from urllib3.util.ssl_ import create_urllib3_context |
| 6 |
|
| 7 |
|
| 8 |
class SSLHTTPAdapter(HTTPAdapter): |
| 9 |
"""Wraps the original HTTP adapter and adds additional SSL context.""" |
| 10 |
|
| 11 |
def __init__(self, *args, **kwargs): |
| 12 |
self._disable_ssl_verification = kwargs.pop('_disable_ssl_verification', None) |
| 13 |
|
| 14 |
super().__init__(*args, **kwargs) |
| 15 |
|
| 16 |
def init_poolmanager(self, connections, maxsize, block=DEFAULT_POOLBLOCK, **pool_kwargs): |
| 17 |
"""Create and use custom SSL configuration.""" |
| 18 |
|
| 19 |
ssl_context = create_urllib3_context() |
| 20 |
# NOTE: https://github.com/psf/requests/pull/6731/files#r1622893724 |
| 21 |
ssl_context.load_verify_locations(certs.where()) |
| 22 |
ssl_context.minimum_version = ssl.TLSVersion.TLSv1_2 |
| 23 |
|
| 24 |
if self._disable_ssl_verification: |
| 25 |
ssl_context.check_hostname = False |
| 26 |
ssl_context.verify_mode = ssl.CERT_NONE |
| 27 |
|
| 28 |
super().init_poolmanager(connections, maxsize, block, ssl_context=ssl_context, **pool_kwargs) |