lockettering, uk
grid142 gCO₂/kWh
solar3.8 kW export
last deploy695181c

Real TLS for home services with split-horizon DNS

One Let's Encrypt wildcard for *.lambev.pro, issued on a Pi via Cloudflare DNS-01, a dnsmasq that answers only the names the Pi serves, and a CT-log watchdog that needs a canary to tell the truth.

https://ha.lambev.pro on the LAN, a browser-trusted certificate, no port forward, no inbound firewall rule, and the private key never leaves a Raspberry Pi 4. Caddy issues and renews a Let’s Encrypt wildcard via the Cloudflare DNS-01 challenge; a small dnsmasq makes LAN clients reach the Pi instead of Cloudflare’s edge; a GitHub Action reads Certificate Transparency logs and opens an issue if renewal stops.

Why DNS-01, and why not in CI

HTTP-01 cannot issue wildcards. DNS-01 can, and it only needs an API token that can write a TXT record — scoped to Zone:DNS:Edit + Zone:Zone:Read on lambev.pro and nothing else. Nothing has to be reachable from the internet.

That also rules out issuing in a GitHub Action. A certificate issued in a runner has its private key generated in that runner, which means shipping the key for every home service back over the network. Issuance stays on the Pi. CI gets the job it is actually good at: noticing when something has silently stopped.

Runs where Holds secrets
Issue, renew, serve Caddy on the Pi Cloudflare token, .env, mode 600
Config validation GitHub Actions none
Renewal watchdog GitHub Actions none

One site block, one certificate

Caddy issues a certificate per site block. Listing ha.lambev.pro, git.lambev.pro and friends as separate blocks requests a certificate for each and spends Let’s Encrypt rate limit doing it. Everything lives in one *.lambev.pro block and routes by host:

*.lambev.pro {
	tls {
		dns cloudflare {env.CF_API_TOKEN}
		resolvers 1.1.1.1 8.8.8.8
	}

	@ha host ha.lambev.pro
	handle @ha { reverse_proxy HA_HOST:8123 }

	@git host git.lambev.pro
	handle @git {
		request_body { max_size 1GB }
		reverse_proxy 127.0.0.1:8000
	}

	handle {
		respond "No service is mapped to {host} on this proxy." 404
	}
}

resolvers 1.1.1.1 8.8.8.8 matters once split-horizon DNS exists: Caddy checks the challenge TXT record has propagated before telling Let’s Encrypt to look, and the LAN resolver does not carry that record. Without the line, Caddy asks the wrong server and waits.

A new service is a matcher here plus one DNS line. No new certificate; the wildcard already covers it. caddy reload validates first and keeps the running config if the new one is broken, so a typo can’t take the proxy down.

Split-horizon DNS

Publicly, *.lambev.pro resolves to Cloudflare (104.21.46.201, 172.67.141.173). A LAN client following that answer leaves the house, hits Cloudflare’s edge, and fails. On the LAN those names must resolve to the Pi.

The obvious fix is a public *.lambev.pro A <LAN address> record. Don’t: it publishes internal addressing to anyone who asks, and it buys nothing — DNS-01 never needs the name to resolve anywhere.

The first version of the internal resolver answered the whole domain with one wildcard line. That made the apex, www and this site unreachable from the house, because they are not on the Pi. The current dns/lambev.conf overrides only the hostnames Caddy serves and forwards the rest upstream:

address=/ha.lambev.pro/PI_LAN_IP
address=/git.lambev.pro/PI_LAN_IP

no-resolv
server=1.1.1.1
server=8.8.8.8

domain-needed
bogus-priv
stop-dns-rebind
rebind-domain-ok=/lambev.pro/

no-resolv stops dnsmasq appending /etc/resolv.conf to its upstreams, which becomes a query loop the moment the Pi is pointed at itself. stop-dns-rebind rejects upstream answers that point into private space; rebind-domain-ok exempts the one domain that legitimately does.

dnsmasq runs in a container. The Pi is on Debian 11, which is EOL: security.debian.org now 404s the package and apt-get install dnsmasq fails. A container sidesteps the dead repository and is a separate compose stack from Caddy, so rebuilding the proxy never bounces DNS for the house.

The router is a UniFi Dream Router 7. UniFi Network has no wildcard local records — the config.gateway.json workarounds that circulate are for the USG/UDM generation — but it does have a conditional forwarder:

Settings → Routing → DNS → Create Entry
  Type:   Forward Domain
  Domain: lambev.pro
  Server: PI_LAN_IP

Clients keep the router as their resolver; only lambev.pro queries go to the Pi. If the Pi dies, lambev.pro breaks and nothing else does, which is a better failure than repointing DHCP at the Pi and losing name resolution for the whole house.

Home Assistant returns 400

Behind any proxy, Home Assistant rejects requests with 400 Bad Request until told to trust it:

http:
  use_x_forwarded_for: true
  trusted_proxies:
    - PI_LAN_IP

One address, not a subnet. Anything listed there can set X-Forwarded-For and spoof a client IP to HA. Caddy already preserves Host, sets the X-Forwarded-* headers, and proxies WebSockets, so nothing else is needed on the Caddy side.

The symptom that took longest to place: login succeeds, then the dashboard sits on “Home Assistant is unavailable, retrying in 60 seconds” and recovers by itself a minute or two later. Caddy and trusted_proxies problems don’t heal on a timer. A client that cached the public Cloudflare answer — from before split-horizon, or from roaming off the LAN and back — does: it hangs on the edge until timeout, re-resolves, gets the Pi. nslookup ha.lambev.pro on the affected device while it is stuck settles it. 104.21.x or 172.67.x means DNS cache, and the fix is on the client.

The watchdog needs a canary

Let’s Encrypt publishes every certificate to CT logs within minutes, so a daily Action can check that a fresh one keeps appearing without any access to the LAN. Watching *.lambev.pro would be useless, though. Cloudflare’s Universal SSL independently issues a Let’s Encrypt certificate for *.lambev.pro, lambev.pro and renews it on its own schedule. Same issuer, same SANs. The watchdog would report healthy with Caddy switched off.

Universal SSL does not cover names two labels deep. Caddy also serves canary.tls.lambev.pro, which has no A record and serves nothing; a certificate for it can only have come from the Pi. The Action watches that name and opens an issue (deduplicated by title) when fewer than 21 days remain. Caddy renews at roughly 30, so there are nine days of slack before the alarm and three weeks after it.

It queries two CT indexes, not one. crt.sh was still missing a properly logged canary certificate ten days after issuance while answering every other query normally. “crt.sh is up and has nothing” is therefore not evidence of anything. The script now requires crt.sh and Cert Spotter to both be reachable — checked against letsencrypt.org, which always has certificates — and both to have no unexpired certificate before it reports stale. If either is down it reports unknown and stays quiet.

Net

Two services on real HTTPS, one certificate, no secrets in GitHub, no open ports. Adding a service is two lines. The part that needed the most thought was not issuance but making sure the alarm can’t be satisfied by someone else’s certificate.