#19: fix F1-F3 defects found auditing the phases against PLAN.md

Every fix here is a case where the code and the plan disagreed, and the
code was wrong.

Certificates (section 8.2, 8.4). The parser documented that it skipped
expired entries but never checked, so an expired certificate would be
served until every handshake failed. LastReload() only advanced when
acme.json changed - and it does not change for sixty days - which made
the timestamp risk R2 monitors indistinguishable from a watcher gone
blind. Expiry is now checked on every pass, LastReload records the check
and LastChange the swap, and the <7d/<24h alerts of section 8.4 exist.

ARC (section 5). buildArcSealer resolved the DKIM key at startup, before
runDnsReconciler had provisioned one. On a first boot ARC was disabled
until someone restarted the process, which the log even admitted while
offering no way for it to start. A LazySealer binds on first use instead.

Health (section 8.4). There was no degraded state at all. /health now
reports one - still 200, because restarting cannot fix a certificate
Traefik never issued.

Bounces. DSNs were sent From postmaster@lambdamail.local, a domain that
does not resolve, so the notification was itself undeliverable. They now
carry the configured mail host, a Message-ID, and Auto-Submitted so a
vacation responder cannot loop against them.

Fan-out atomicity (section 6.1). Persist ran one transaction per
recipient, so a failure halfway through an alias fan-out left earlier
recipients with a copy while the sender was told to retry - and the retry
delivered to them twice. PersistAll commits the whole delivery or none of
it. Verified against Postgres by reverting the repository to the old
behaviour and confirming the new test fails on it.

SMTP replies. The use case encoded replies in error text ("550 5.7.1 ...")
which the adapter re-parsed by prefix, so rewording a message could turn a
permanent refusal into a retry. SmtpRejection carries the decision and the
adapter renders it.

Repository tests. These only run with TEST_DATABASE_URL, which CI does
set - so the job was red: copy and expunge seeded a 68-character digest
into a CHAR(64) column, and three others used fixed digests that collide
with the UNIQUE index on any re-run. A shared testBlobDigest() helper
fixes both; the suite now passes twice in a row against one database.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5 committed 2026-08-02 07:57:22 -03:00
1 parent 2e0e65b167
commit 33d91fa247
23 files changed
+906 -139

No files matched your search

@@ -3,6 +3,7 @@ package tlsprovider
import (
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
@@ -37,8 +38,15 @@ type AcmeCertWatcher struct {
// using a consistent map.
certificates atomic.Pointer[map[string]*tls.Certificate]
lastHash atomic.Pointer[[32]byte]
lastHash atomic.Pointer[[32]byte]
// lastReloadAt is the last time the store was successfully read and found
// usable, whether or not its content had changed. It is what PLAN.md risk
// R2 monitors: acme.json normally sits untouched for sixty days, so a
// timestamp that only advanced on change could not distinguish a healthy
// idle watcher from one that has gone blind.
lastReloadAt atomic.Pointer[time.Time]
// lastChangeAt is the last time the certificates themselves were swapped.
lastChangeAt atomic.Pointer[time.Time]
// unknownSNI counts handshakes for names we hold no certificate for; it
// backs the tls_unknown_sni_total metric of PLAN.md section 8.4.
@@ -110,31 +118,80 @@ func (w *AcmeCertWatcher) reload() error {
return fmt.Errorf("read %s: %w", w.path, err)
}
hash := sha256.Sum256(raw)
if previous := w.lastHash.Load(); previous != nil && *previous == hash {
return nil
}
certificates, err := parseAcmeStore(raw)
if err != nil {
return err
}
if len(certificates) == 0 {
return fmt.Errorf("acme watcher: %s holds no usable certificate", w.path)
}
w.certificates.Store(&certificates)
w.lastHash.Store(&hash)
now := time.Now()
w.lastReloadAt.Store(&now)
hash := sha256.Sum256(raw)
if previous := w.lastHash.Load(); previous == nil || *previous != hash {
certificates, err := parseAcmeStore(raw, now)
if err != nil {
return err
}
if len(certificates) == 0 {
return fmt.Errorf("acme watcher: %s holds no usable certificate", w.path)
}
w.certificates.Store(&certificates)
w.lastHash.Store(&hash)
w.lastChangeAt.Store(&now)
}
// Expiry is checked on every pass, not only on change: a certificate ages
// out while the file it lives in stays byte-identical.
w.reportExpiry(now)
w.lastReloadAt.Store(&now)
return nil
}
// reportExpiry raises the alerts of PLAN.md section 8.4 for certificates that
// are close to expiring. Renewal is Traefik's job, so all this side can do is
// make the failure loud before it becomes an outage.
func (w *AcmeCertWatcher) reportExpiry(now time.Time) {
host, expiry, ok := w.EarliestExpiry()
if !ok {
return
}
switch remaining := expiry.Sub(now); {
case remaining <= 0:
log.Printf("CRITICAL: the certificate for %s expired at %s and Traefik has not replaced it", host, expiry.Format(time.RFC3339))
case remaining < 24*time.Hour:
log.Printf("CRITICAL: the certificate for %s expires in %s", host, remaining.Round(time.Minute))
case remaining < 7*24*time.Hour:
log.Printf("WARNING: the certificate for %s expires in %s", host, remaining.Round(time.Hour))
}
}
// EarliestExpiry reports the soonest expiry across the loaded certificates,
// which is the one that decides when the deployment breaks.
func (w *AcmeCertWatcher) EarliestExpiry() (string, time.Time, bool) {
loaded := w.certificates.Load()
if loaded == nil {
return "", time.Time{}, false
}
var (
soonestHost string
soonest time.Time
)
for host, cert := range *loaded {
if cert.Leaf == nil {
continue
}
if soonest.IsZero() || cert.Leaf.NotAfter.Before(soonest) {
soonest, soonestHost = cert.Leaf.NotAfter, host
}
}
if soonest.IsZero() {
return "", time.Time{}, false
}
return soonestHost, soonest, true
}
// parseAcmeStore walks every resolver in the file. A single unusable entry -
// an expired certificate, or one whose key does not match - is skipped rather
// than failing the whole load, so one stale domain cannot take the server down.
func parseAcmeStore(raw []byte) (map[string]*tls.Certificate, error) {
func parseAcmeStore(raw []byte, now time.Time) (map[string]*tls.Certificate, error) {
var store acmeFile
if err := json.Unmarshal(raw, &store); err != nil {
return nil, fmt.Errorf("parse acme store: %w", err)
@@ -161,6 +218,23 @@ func parseAcmeStore(raw []byte) (map[string]*tls.Certificate, error) {
continue
}
// X509KeyPair leaves Leaf nil. Parsing it here is what makes the
// expiry checks below and in reportExpiry possible at all.
leaf, err := x509.ParseCertificate(pair.Certificate[0])
if err != nil {
log.Printf("acme watcher: skipping %s in resolver %q: leaf certificate is unparseable: %v", entry.Domain.Main, resolverName, err)
continue
}
// An expired entry is worse than a missing one: it would be served
// in a handshake every peer then rejects. Skipping it lets the SNI
// fallback or the self-signed provider take over instead
// (PLAN.md section 8.2, trap 3).
if now.After(leaf.NotAfter) {
log.Printf("acme watcher: skipping %s in resolver %q: the certificate expired at %s", entry.Domain.Main, resolverName, leaf.NotAfter.Format(time.RFC3339))
continue
}
pair.Leaf = leaf
for _, name := range append([]string{entry.Domain.Main}, entry.Domain.SANs...) {
if name != "" {
certificates[strings.ToLower(name)] = &pair
@@ -210,9 +284,9 @@ func wildcardOf(name string) string {
return ""
}
// LastReload reports when the store was last successfully refreshed. A
// timestamp that stops advancing is the signal that the watcher went blind
// (PLAN.md risk R2).
// LastReload reports when the store was last read successfully, changed or
// not. A timestamp that stops advancing is the signal that the watcher went
// blind (PLAN.md risk R2).
func (w *AcmeCertWatcher) LastReload() time.Time {
if t := w.lastReloadAt.Load(); t != nil {
return *t
@@ -220,6 +294,15 @@ func (w *AcmeCertWatcher) LastReload() time.Time {
return time.Time{}
}
// LastChange reports when the served certificates were last swapped, which is
// how a renewal that never arrived is detected.
func (w *AcmeCertWatcher) LastChange() time.Time {
if t := w.lastChangeAt.Load(); t != nil {
return *t
}
return time.Time{}
}
// UnknownSNICount backs the tls_unknown_sni_total metric.
func (w *AcmeCertWatcher) UnknownSNICount() int64 {
return w.unknownSNI.Load()