prefer certifi's bundle to the OS bundle
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Shivaram Lingamneni · committed by Shivaram Lingamneni · parent e1528ce · Browse files · 2 files·+18 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+18 added−9 removed
- other:+18 added−9 removed
Provenance
- Commit
b4eb866- Tree
b9d199d- Author
Shivaram Lingamneni <slingamn@cs.stanford.edu>- Committer
Shivaram Lingamneni <slingamn@cs.stanford.edu>- Parent
e1528ce
2 files changed+18 added−9 removed
modified:requests/models.py+4 added-5 removed
25 unmodified lines26 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,27 URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)28 from .utils import (29 get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,30- dict_from_string, stream_decode_response_unicode, get_netrc_auth, CA_BUNDLE_PATH)30+ dict_from_string, stream_decode_response_unicode, get_netrc_auth,31+ DEFAULT_CA_BUNDLE_PATH)32 from .compat import (33 urlparse, urlunparse, urljoin, urlsplit, urlencode, str, bytes,34 SimpleCookie, is_py2)35 505 unmodified lines541 # Curl compatibility.542 if not cert_loc and self.config.get('trust_env'):543 cert_loc = os.environ.get('CURL_CA_BUNDLE')544 544- # Use the operating system's bundle, if it can be found.545 if not cert_loc:546- cert_loc = CA_BUNDLE_PATH546+ cert_loc = DEFAULT_CA_BUNDLE_PATH547 548- # Use the awesome certifi list.548 if not cert_loc:550- cert_loc = __import__('certifi').where()549+ raise Exception("Could not find a suitable SSL CA certificate bundle.")550 551 conn.cert_reqs = 'CERT_REQUIRED'552 conn.ca_certs = cert_loc553 else:291 unmodified linesmodified:requests/utils.py+14 added-4 removed
19 unmodified lines20 from .compat import parse_http_list as _parse_list_header21 from .compat import quote, cookielib, SimpleCookie, is_py2, urlparse22 from .compat import basestring, bytes, str23 24+CERTIFI_BUNDLE_PATH = None25+try:26+ # see if requests's own CA certificate bundle is installed27+ import certifi28+ CERTIFI_BUNDLE_PATH = certifi.where()29+except ImportError:30+ pass31 32 NETRC_FILES = ('.netrc', '_netrc')33 34 # common paths for the OS's CA certificate bundle35 POSSIBLE_CA_BUNDLE_PATHS = [29- # Red Hat, CentOS, Fedora and friends:36+ # Red Hat, CentOS, Fedora and friends (provided by the ca-certificates package):37 '/etc/pki/tls/certs/ca-bundle.crt',31- # Ubuntu and friends:38+ # Ubuntu, Debian, and friends (provided by the ca-certificates package):39 '/etc/ssl/certs/ca-certificates.crt',40 # FreeBSD (provided by the ca_root_nss package):41 '/usr/local/share/certs/ca-root-nss.crt',42 ]43 37-def get_ca_bundle_path():44+def get_os_ca_bundle_path():45 """Try to pick an available CA certificate bundle provided by the OS."""46 for path in POSSIBLE_CA_BUNDLE_PATHS:47 if os.path.exists(path):48 return path49+ return None50 43-CA_BUNDLE_PATH = get_ca_bundle_path()51+# if certifi is installed, use its CA bundle;52+# otherwise, try and use the OS bundle53+DEFAULT_CA_BUNDLE_PATH = CERTIFI_BUNDLE_PATH or get_os_ca_bundle_path()54 55 def dict_to_sequence(d):56 """Returns an internal sequence dictionary update."""57 444 unmodified lines