Commit 1c3ff048 by PLN (Algolia)

devname: degrade to http when mkcert is missing instead of crashing

parent a46c84a7
......@@ -74,7 +74,7 @@ PLIST = os.path.join(HOME, "Library", "LaunchAgents", LABEL + ".plist")
LOGDIR = os.path.join(HOME, "Library", "Logs", "devname")
POOL = range(17000, 18000)
CADDY = shutil.which("caddy") or "/opt/homebrew/bin/caddy"
MKCERT = shutil.which("mkcert") or "/opt/homebrew/bin/mkcert"
MKCERT = shutil.which("mkcert") or ("/opt/homebrew/bin/mkcert" if os.path.exists("/opt/homebrew/bin/mkcert") else None)
# no JDK keystore here: mkcert's optional Java step exits 1 and would block issuing the cert (found 2026-11-03)
MKCERT_ENV = dict(os.environ, TRUST_STORES="system,nss")
SUFFIX = ".localhost"
......@@ -500,6 +500,16 @@ def lan_gate_open():
return False
def mkcert_caroot():
"""mkcert's CA directory, or "" when mkcert is missing or fails: no CA means http only, never a crash."""
if not MKCERT:
return ""
try:
return subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True, timeout=10).stdout.strip()
except (OSError, subprocess.TimeoutExpired):
return ""
def ensure_cert(reg):
"""One SAN cert over every registered name, loopback and LAN faces both (wildcards cannot span two
labels). Issued whether or not the LAN gate is open: the cert must be ready when the gate opens.
......@@ -507,8 +517,8 @@ def ensure_cert(reg):
names = sorted(set(reg["entries"])
| {h[:-len(SUFFIX)] + LAN_SUFFIX for h in reg["entries"]}
| {INDEX_HOST, GATEWAY_HOST, "localhost", "127.0.0.1", "::1"})
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
if not os.path.exists(os.path.join(root, "rootCA.pem")):
root = mkcert_caroot()
if not root or not os.path.exists(os.path.join(root, "rootCA.pem")):
return False
want = "\n".join(names)
have = open(SANS).read() if os.path.exists(SANS) else None
......@@ -947,7 +957,7 @@ class IndexHandler(http.server.BaseHTTPRequestHandler):
data = ctype = None
code = 200
if path in ("/rootCA.pem", "/CA"): # served over plain http on the LAN face: a phone that trusts no CA yet
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
root = mkcert_caroot()
try:
with open(os.path.join(root, "rootCA.pem"), "rb") as fh:
data = fh.read()
......@@ -1031,8 +1041,9 @@ def cmd_doctor(a):
except OSError:
pass
line(code == 200, f"index page on {INDEX_HOST} (got {code})")
root = subprocess.run([MKCERT, "-CAROOT"], capture_output=True, text=True).stdout.strip()
line(os.path.exists(os.path.join(root, "rootCA.pem")), "mkcert root CA exists (https; trust it with `mkcert -install`)")
root = mkcert_caroot()
line(bool(root) and os.path.exists(os.path.join(root, "rootCA.pem")),
"mkcert root CA exists (https; trust it with `mkcert -install`)")
line(have_https(), "https cert issued for the registered names")
try:
with open(LAN_GATE) as fh:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment