-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Description
Version 2.32.0 introduced changes and improvements with SSLContext as specified in the release history:
Improvements - verify=True now reuses a global SSLContext which should improve request time variance between first and subsequent requests. It should also minimize certificate load time on Windows systems when using a Python version built with OpenSSL 3.x. (#6667)
We are facing issue making http requests to webservers which are signed by a local root ca.
The certificate chain is installed correctly on the Windows station and version 2.31.1 is working as expected.
Versions 2.32.x are throwing an error: SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1125)')))
This is a code example that works in 2.31.1 and does not in 2.32.x
from requests.adapters import HTTPAdapter
import requests
from requests.packages.urllib3.util.ssl_ import create_urllib3_context
class SSLContextAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
context = create_urllib3_context()
context.load_default_certs() # this loads the system's CA certificates
kwargs['ssl_context'] = context
return super().init_poolmanager(*args, **kwargs)
SESSION = requests.Session()
SESSION.mount('https://', SSLContextAdapter())
SESSION.get(MY_URL_SIGNED_BY_LOCAL_ROOT_CA, headers = headers, verify = True)
After looking at the lastest changes, if we modifiy our code its working but i dont think that calling the private global _preloaded_ssl_context is the right way:
from requests.adapters import HTTPAdapter, _preloaded_ssl_context
import requests
class SSLContextAdapter(HTTPAdapter):
def init_poolmanager(self, *args, **kwargs):
_preloaded_ssl_context.load_default_certs() # this loads the system's CA certificates
return super().init_poolmanager(*args, **kwargs)
SESSION = requests.Session()
SESSION.mount('https://', SSLContextAdapter())
SESSION.get(MY_URL_SIGNED_BY_LOCAL_ROOT_CA, headers = headers, verify = True)
What is the recommended way for using load_default_certs() with version 2.32.x if the usage has changed or alternatively can you confirm if this is a bug?