The certificate renewed, and the site expired anyway
A post-mortem on my own server: 60 hours of an expired certificate, a renewal that had worked perfectly, and the two wrong guesses I made while finding out why.
- Post-mortem
- TLS
- nginx
- Monitoring
- Self-healing
For about 60 hours this site served an expired certificate. The odd part is that the certificate had renewed correctly five weeks before it expired. The new one was sitting on disk the whole time. Nothing was wrong with renewal at all.
This is what actually happened, in the order I found it, including the parts where I was wrong.
What broke, and when
- August: a cleanup script deleted a directory the web server mounts for its logs. The running server did not care, because it already had its files open. Every health check stayed green.
- October: the certificate renewed on schedule. The deploy step then ran the web server's config test, which now failed because the log directory was gone. The deploy did what it was built to do: it put the old certificate back and exited.
- From then on the renewal tool reported, correctly, that its certificate was fine. It was. The copy the web server held was the one expiring.
- November: the served certificate expired.
Why the alerts did not save it
The alerts fired. The first one arrived eight days after the deploy failed, and it was an expiry warning, which pointed at renewal. Renewal was working. An alert that names the wrong cause sends you to the wrong place, and when you are busy it is easy to file it for later.
Every alert I had measured the symptom: how long until something expires. None of them measured the actual fault, which was two parts of the system disagreeing about which certificate was current.
The two things I got wrong while investigating
First, I decided the DNS provider API token used for renewal was dead, because a verification call rejected it. The token was fine. It was a newer, account-scoped kind that the endpoint I used does not recognise. The check that settled it was doing the real operation: reading the DNS zone and writing a test record.
Second, I decided the alert emails were landing in spam, because the mail provider showed very few of them opened. They were being delivered and read. Open tracking depends on an image most mail clients block, so the number meant almost nothing. I had fitted a believable story to a weak metric.
What changed
- A measurement of what the renewal tool holds and what the web server actually serves, from a real TLS handshake, every 15 minutes, with an alert when they differ.
- An alert when the web server's config test fails. That alone would have caught the August fault the same day, while it was a five-minute fix.
- A timer that redeploys the current certificate when the two drift apart. It refuses to act if the config test is failing, because that is not a fault a redeploy can fix, and it stops after three attempts a day so a real problem escalates to me.
- The cleanup script now refuses to delete any directory a running container mounts.
What I took from it
Alert on disagreement, not on deadlines. A deadline alert is always late by design, because it cannot fire until the deadline is close. The disagreement was measurable from the first day.
And when a check gives a surprising answer, try the operation that actually has to work before believing the check. Both of my wrong turns came from trusting an indirect signal over the thing itself.