Allow using environment cacert file (#38816)

This commit is contained in:
Bjørn Snoen 2020-09-04 13:54:20 +02:00 committed by GitHub
parent 08d93b8349
commit f207d46390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,5 @@
"""Helper to create SSL contexts."""
from os import environ
import ssl
import certifi
@ -6,9 +7,12 @@ import certifi
def client_context() -> ssl.SSLContext:
"""Return an SSL context for making requests."""
context = ssl.create_default_context(
purpose=ssl.Purpose.SERVER_AUTH, cafile=certifi.where()
)
# Reuse environment variable definition from requests, since it's already a requirement
# If the environment variable has no value, fall back to using certs from certifi package
cafile = environ.get("REQUESTS_CA_BUNDLE", certifi.where())
context = ssl.create_default_context(purpose=ssl.Purpose.SERVER_AUTH, cafile=cafile)
return context