Python throwing an SSL certificate validation error w/o consulting system CA bundles? -
with 2 largely identical systems (both running fedora 25, both similar package versions installed), 1 system failing ssl certicate verification error while not. is, if run:
import requests r = requests.get('https://insidehost.corp.example.com') one 1 system works, while on other fails:
requests.exceptions.sslerror: ("bad handshake: error([('ssl routines', 'ssl3_get_server_certificate', 'certificate verify failed')],)",) at first figure missing necessary ca certificates, running python under strace reveals on failing system, python never attempting open ca bundle. is, on system works:
strace -e trace=open,stat python testscript.py |& grep /etc/pki yields only:
open("/etc/pki/tls/legacy-settings", o_rdonly) = -1 enoent (no such file or directory) stat("/etc/pki/tls/certs/ca-bundle.crt", {st_mode=s_ifreg|0444, st_size=257079, ...}) = 0 open("/etc/pki/tls/certs/ca-bundle.crt", o_rdonly) = 4 but on failing system yields:
open("/etc/pki/tls/legacy-settings", o_rdonly) = -1 enoent (no such file or directory) furthermore, running same test script python3 on failing system...works!
in both cases, python /usr/bin/python python-2.7.13-1.fc25.x86_64. neither system setting *_ca_bundle environment variable.
after additional investigation i've figured out, , thought post solution here because it's not obvious.
the requests module includes own certificate bundle, , fall on if can't find 1 use. way looks certificate bundle this, in requests/certs.py:
try: certifi import except importerror: def where(): """return preferred certificate bundle.""" # vendored bundle inside requests return os.path.join(os.path.dirname(__file__), 'cacert.pem') you can see result of running:
$ python -m requests.certs /etc/pki/tls/certs/ca-bundle.crt as can see above code, requests uses certifi module locate appropriate bundle. on failing system, certifi module had been installed via pip rather using system package, meant lacking appropriate configuration.
the solution yum install python2-certifi.
Comments
Post a Comment