Securely connecting to a TLS endpoint that uses self signed SSL certificate
Author(s):
Muhammad Akbar
Publish date: Jun 19, 2019
Publish date: Jun 19, 2019
If you are using requests directly to make the connection, the following code shows how to verify SSL thumbprint for self signed certificates.
import requests
from requests.packages.urllib3.poolmanager import PoolManager
class _FingerprintAdapter(requests.adapters.HTTPAdapter):
def __init__(self, fingerprint=None, **kwargs):
self.fingerprint = str(fingerprint)
super(_FingerprintAdapter, self).__init__(**kwargs)
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
assert_fingerprint=self.fingerprint)
remote_url = 'https://10.0.0.123'
SSL_thumbprint = '33:66:B6:79:B8:3A:A7:4A:EE:7C:2B:C3:41:B0:82:3F:D6:00:94:45'
session = requests.Session()
fingerprint_adapter = _FingerprintAdapter(SSL_thumbprint)
session.mount("https://", fingerprint_adapter)
r = session.request(method='GET', url=remote_url, verify=False)
print("Success")