|
| 1 | +""" |
| 2 | +Site-customization for the macadmins Python framework. |
| 3 | +
|
| 4 | +Why this file exists: |
| 5 | + The python.org Python.framework is built with OpenSSL's default CA-bundle |
| 6 | + path hardcoded to /Library/Frameworks/Python.framework/Versions/<X.Y>/etc/ |
| 7 | + openssl/cert.pem. Our framework installs under /Library/ManagedFrameworks/ |
| 8 | + Python/Python3.framework/..., so that hardcoded path doesn't exist on |
| 9 | + target machines. Stdlib SSL (urllib.request, http.client.HTTPSConnection, |
| 10 | + ssl.SSLContext with default verify paths, etc.) then fails to find a CA |
| 11 | + bundle and certificate validation errors out. |
| 12 | +
|
| 13 | + A regular python.org install ships /Applications/Python 3.X/Install |
| 14 | + Certificates.command that fixes this by symlinking the expected path to |
| 15 | + certifi's bundled cacert.pem. We can't do the equivalent because the |
| 16 | + expected path is outside our framework — touching it would conflict with |
| 17 | + a python.org install if the user has one. |
| 18 | +
|
| 19 | +What it does: |
| 20 | + Sets SSL_CERT_FILE to certifi's bundled cert path during interpreter |
| 21 | + startup. OpenSSL reads SSL_CERT_FILE ahead of its compiled-in path, so |
| 22 | + stdlib SSL operations get a working CA bundle. |
| 23 | +
|
| 24 | + Only sets the variable when it isn't already set, so an explicit user |
| 25 | + override (e.g. `export SSL_CERT_FILE=/path/to/ca.pem`) still wins. |
| 26 | +
|
| 27 | +References: |
| 28 | + macadmins/python#38 |
| 29 | + gregneagle/relocatable-python#13 |
| 30 | +""" |
| 31 | +import os |
| 32 | + |
| 33 | +if "SSL_CERT_FILE" not in os.environ: |
| 34 | + try: |
| 35 | + import certifi |
| 36 | + except ImportError: |
| 37 | + pass |
| 38 | + else: |
| 39 | + os.environ["SSL_CERT_FILE"] = certifi.where() |
0 commit comments