What the job was supposed to do

An always-on local device at my house serves two small web applications over a mesh VPN. The VPN issues each device a public certificate, ninety days at a time, through a command on its client. A weekly job, run by the system scheduler as root at four on Monday mornings, asked for a fresh certificate, put it where the two services read it, and restarted them. Nobody logs in to this device. There is no console session, so the per-user scheduler domain does not exist and anything scheduled there loads without error and never fires. Everything on it runs as a system daemon, as root. That detail is where this story starts.

Two changes that broke it, and a third that hid it

In August I replaced the VPN's desktop client with its command-line client and deleted the desktop app. The renewal script still called the certificate command by the desktop app's bundle path. From the first Monday after that change, the binary it wanted was not there.

The script also appended its log to a path under ~. As my user, that is my home directory. As root under the scheduler, it is root's home, which has no such directory tree. Every line the script tried to log went to a file that could not be opened.

Then the third thing. The script did not check the result of the certificate command and did not exit non-zero when it failed. It returned 0 after every failed run. The scheduler saw a clean exit each week. The uptime watchdog, which asks whether the two services answer, saw two services answering on a certificate that was still valid for most of its ninety days. Twelve Mondays passed. The only trace was twelve identical error lines, one per week at 04:00, and nothing else anywhere.

How I found it

Not through an alert. On a Saturday in October I checked the certificate the two services were actually serving:

echo | openssl s_client -connect localhost:8000 2>/dev/null \
  | openssl x509 -noout -enddate

The notAfter date was the coming Tuesday, 16:50 UTC. Three days. A third service on the same device terminates TLS inside the VPN itself and would have carried on. The two that read the certificate file would have gone dark at midday Tuesday, and the first report would have come from the person who uses them.

The fixed script

Five changes, all small.

  • Absolute paths for the binary and the log. No ~ anywhere. Root's home is not mine.
  • Fetch into .new files and refuse to install anything that openssl x509 -checkend will not vouch for.
  • Hash the certificate before and after. Restart the services only when the hash changed. A renewal that returns the same certificate is not a reason to bounce two services at four in the morning.
  • Non-zero exit on any failure. If the fetch fails or the validation fails, the old certificate stays, the new files are removed, and the job exits 1 so the scheduler records a failure.
  • A comment at the top that names the binary it must use and says, in plain words, that the other one was deleted and must never come back.

The core of it:

before="$(shasum -a 256 "$crt" 2>/dev/null | cut -d' ' -f1)"

if ! "$VPN_CLI" cert --cert-file "$crt.new" --key-file "$key.new" "$FQDN" \
    >> "$LOG" 2>&1; then
  log_error "cert fetch failed; keeping existing cert"
  rm -f "$crt.new" "$key.new"
  return 1
fi

# Refuse anything that is not a valid cert with more than 24h left.
if ! openssl x509 -in "$crt.new" -noout -checkend 86400 >/dev/null 2>&1; then
  log_error "fetched cert invalid or expires within 24h; keeping existing cert"
  rm -f "$crt.new" "$key.new"
  return 1
fi

mv -f "$crt.new" "$crt" && mv -f "$key.new" "$key"
after="$(shasum -a 256 "$crt" | cut -d' ' -f1)"
log_message "cert ok: $(openssl x509 -in "$crt" -noout -enddate)"

if [[ "$before" == "$after" ]]; then
  log_message "cert unchanged; services not restarted"
  return 0
fi

launchctl kickstart -k system/com.example.app-one
launchctl kickstart -k system/com.example.app-two
log_message "cert changed; both services restarted"

I deployed it that Saturday afternoon. The job fetched a certificate valid for another two months and restarted both services. The next day I ran the s_client check again and notAfter had moved to December. One leftover: run by hand under sudo, the script still prints a stray complaint about a log path under root's home before it writes to the real one. It is cosmetic, I have not chased it, and it is on the list.

The lesson is about the signal, not the script

The script had bugs. Scripts do. The failure that matters is that a job which only speaks when something goes wrong is mute when its voice is broken, and a scheduler that is only told about non-zero exits hears nothing from a job that reports zero after failing.

Both of my monitors asked "is anything wrong?" Neither asked "did the good thing happen?" For a renewal job the good thing is measurable from the outside, without trusting the job at all. The certificate the service is serving has a notAfter date, and that date should jump forward every few weeks. If it stops moving, the job is broken, whatever its exit code says. The check is one line and exits non-zero when fewer than fourteen days remain:

echo | openssl s_client -connect device-01:8000 2>/dev/null \
  | openssl x509 -noout -checkend $((14 * 24 * 3600))

That check belongs somewhere other than the renewal job. If it lives inside the job, it dies with the job. It goes in the daily health check, or on any other machine that can reach the service, and it alarms when it fails. Had it been there, it would have fired two weeks before the expiry, instead of me finding the problem with three days left.

What to take from it

  • Under a system scheduler, ~ is not your home. Absolute paths, always.
  • A job that can fail must exit non-zero when it does. Check every command's result, not only the last one's.
  • Validate what you fetched before you install it. openssl x509 -checkend is one line.
  • Only restart things when something changed. Hash the artifact.
  • Alert on the absence of success. Measure the result from outside, here the served certificate's expiry, and alarm when it stops improving, not only when an error is written somewhere.
  • When a service is a few days from a certificate expiring, the renewal job is the first suspect, before the issuer.