Skip to content

Commit 489aae7

Browse files
committed
add a sitecustomize
1 parent 58b420c commit 489aae7

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

build_python_framework_pkgs.zsh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ build_framework() {
139139
"$TOOLSDIR/$TYPE/payload${FRAMEWORKDIR}/Python3.framework"
140140
}
141141

142+
install_sitecustomize() {
143+
# Drop a sitecustomize.py that points OpenSSL at certifi's CA bundle via
144+
# SSL_CERT_FILE at interpreter startup. Works around macadmins/python#38:
145+
# python.org's framework has the OpenSSL CA path compiled in to
146+
# /Library/Frameworks/... which doesn't match our relocated install.
147+
local site_packages="$TOOLSDIR/$TYPE/payload${FRAMEWORKDIR}/Python3.framework/Versions/${PYTHON_BIN_VERSION}/lib/python${PYTHON_BIN_VERSION}/site-packages"
148+
/bin/cp "${TOOLSDIR}/managed_python_sitecustomize.py" "$site_packages/sitecustomize.py"
149+
}
150+
142151
codesign_framework() {
143152
local identity="${APPLICATION_ID:--}" # `-` means ad-hoc
144153
local framework_root="$TOOLSDIR/$TYPE/payload${FRAMEWORKDIR}/Python3.framework"
@@ -274,6 +283,7 @@ download_tool munki-pkg "$MP_SHA" \
274283
"https://github.com/munki/munki-pkg/archive/${MP_SHA}.zip" \
275284
"$MP_ZIP" "$MP_BINDIR"
276285
build_framework
286+
install_sitecustomize
277287
codesign_framework
278288
build_pkg
279289
notarize_and_staple

managed_python_sitecustomize.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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

Comments
 (0)