Merge pull request #552 from slingamn/bundle_2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
attempt to use the OS's certificate bundle
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 2f6b5fe, b4eb866 · Browse files · 4 files·+67 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+67 added−5 removed
- tests:+31 added−0 removed
- other:+36 added−5 removed
Provenance
- Commit
1631f0f- Tree
b9d199d- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
2f6b5fe- Parent 2
b4eb866
4 files changed+67 added−5 removed
modified:requests/models.py+8 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)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 488 unmodified lines524 else:525 conn = connectionpool.connection_from_url(url)526 except LocationParseError as e:527 raise InvalidURL(e)527- 528+529 if url.startswith('https') and self.verify:530 531 cert_loc = None532 4 unmodified lines537 # Look for configuration.538 if not cert_loc and self.config.get('trust_env'):539 cert_loc = os.environ.get('REQUESTS_CA_BUNDLE')540 540- # Curl compatiblity.541+ # 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 awesome certifi list.545 if not cert_loc:546- cert_loc = __import__('certifi').where()546+ cert_loc = DEFAULT_CA_BUNDLE_PATH547 548+ if not cert_loc: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:554 conn.cert_reqs = 'CERT_NONE'290 unmodified linesmodified:requests/utils.py+27 added
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 = [36+ # Red Hat, CentOS, Fedora and friends (provided by the ca-certificates package):37+ '/etc/pki/tls/certs/ca-bundle.crt',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 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+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 58 if hasattr(d, 'items'):443 unmodified linesadded:tests/test_requests_https.py+31 added
1+#!/usr/bin/env python2+# -*- coding: utf-8 -*-3+4+import sys, os5+import json6+import unittest7+8+# Path hack.9+sys.path.insert(0, os.path.abspath('..'))10+import requests11+12+class HTTPSTest(unittest.TestCase):13+ """Smoke test for https functionality."""14+15+ smoke_url = "https://github.com"16+17+ def perform_smoke_test(self, verify=False):18+ result = requests.get(self.smoke_url, verify=verify)19+ self.assertEqual(result.status_code, 200)20+21+ def test_smoke(self):22+ """Smoke test without verification."""23+ self.perform_smoke_test(verify=False)24+25+ def test_smoke_verified(self):26+ """Smoke test with SSL verification."""27+ self.perform_smoke_test(verify=True)28+29+30+if __name__ == '__main__':31+ unittest.main()modified:AUTHORS.rst+1 added
93 unmodified lines94 - Steve Pulec95 - Michael Kelly96 - Michael Newman <newmaniese@gmail.com>97 - Jonty Wareing <jonty@jonty.co.uk>98+- Shivaram Lingamneni