#21: fix the DKIM, onboarding and console defects found reviewing the new work

DKIM rotation broke the domain it was run on. It retired the live key and
inserted a placeholder as ACTIVE - private_key_enc was the literal string
"dummy-encrypted-key" - so FindActiveKeys then failed for the whole
domain and outbound mail stopped being signed, with the real key already
gone. Reproduced against a database before changing anything.

The root cause runs deeper: the auth service derives its encryption key
with a plain SHA-256 of the master key while the protocols service uses
HKDF with its own info string, so nothing the auth service seals can ever
be opened by the signer. Rotation therefore moves to the protocols
service, which owns that vault and the real key generator. It retires the
previous key with a seven-day overlap in the same transaction rather than
deleting it, because mail already sent under it is still being verified.

Domain onboarding had three defects in one function. It built the alias
destination by interpolating the domain name into the SQL text, which was
injectable; it reused one placeholder for a CITEXT and a VARCHAR column,
which made the statement fail and happened to hide the injection; and it
ignored its scope argument entirely, so any DOMAIN_ADMIN could create
domains. It also provisioned postmaster, abuse and security, missing the
dmarc and tlsrpt aliases that the published records point at - PLAN.md
section 7.4b is explicit that reports bounce without them.

The TLS panel returned fixed values: acme_status "VALID" and 72 days to
expiry, whatever the process actually held. It now reads the certificate
watcher, and a compile-time assertion keeps the wiring from silently
regressing into "disabled". The audit log and the readiness scorecard were
fixtures too, and their field names did not match the API they now read.

Two tests failed and a third asserted a fabricated constant: one depended
on test order through a shared fixture that had a second factor enrolled,
one used a fixed domain name and so passed once per database, and one
expected "records: 13" from a function that never touches DNS. All three
are fixed, and 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 13:33:33 -03:00
1 parent 972189a589
commit 30014f0f41
33 files changed
+2848 -150

No files matched your search

@@ -0,0 +1,107 @@
package httppresentation
import (
"net/http"
"time"
)
// TlsStatusSource reports what the certificate watcher actually sees. The
// admin console used to receive fixed values - "VALID", 72 days - which told
// an operator the certificate was healthy no matter what the process held.
type TlsStatusSource interface {
EarliestExpiry() (host string, expiry time.Time, ok bool)
LastReload() time.Time
LastChange() time.Time
UnknownSNICount() int64
HasCertificateFor(host string) bool
}
type adminTlsAPI struct {
source TlsStatusSource
mailHost string
tlsMode string
sessions *WebSessionVerifier
pollPeriod time.Duration
}
func (a *adminTlsAPI) handleStatus(w http.ResponseWriter, r *http.Request) {
token := bearerOrCookie(r, "lm_admin_session")
if token == "" {
writeError(w, http.StatusUnauthorized, "UNAUTHORIZED", "Admin session required")
return
}
if _, err := a.sessions.RequireSurface(token, "admin"); err != nil {
writeError(w, http.StatusUnauthorized, "UNAUTHORIZED", "Admin session required")
return
}
status := map[string]any{
"tls_mode": a.tlsMode,
"mail_host": a.mailHost,
"has_certificate": a.source.HasCertificateFor(a.mailHost),
"unknown_sni_total": a.source.UnknownSNICount(),
"last_reload": nil,
"last_change": nil,
"watcher_healthy": false,
"certificate_expires": nil,
"expires_in_days": nil,
"state": "UNKNOWN",
}
if last := a.source.LastReload(); !last.IsZero() {
status["last_reload"] = last.UTC().Format(time.RFC3339)
// Two missed polls is the smallest gap that is not a scheduling
// hiccup, and it is the symptom of a watcher gone blind (risk R2).
status["watcher_healthy"] = time.Since(last) < 3*a.pollPeriod
}
if change := a.source.LastChange(); !change.IsZero() {
status["last_change"] = change.UTC().Format(time.RFC3339)
}
if host, expiry, ok := a.source.EarliestExpiry(); ok {
remaining := time.Until(expiry)
status["certificate_expires"] = expiry.UTC().Format(time.RFC3339)
status["expires_in_days"] = int(remaining.Hours() / 24)
status["soonest_expiring_host"] = host
switch {
case remaining <= 0:
status["state"] = "EXPIRED"
case remaining < 24*time.Hour:
status["state"] = "CRITICAL"
case remaining < 7*24*time.Hour:
status["state"] = "WARNING"
default:
status["state"] = "OK"
}
}
if !status["has_certificate"].(bool) {
status["state"] = "NO_CERTIFICATE"
}
writeJSON(w, http.StatusOK, status)
}
// SetAdminTlsAPI wires the TLS panel to the live certificate source.
func (r *Router) SetAdminTlsAPI(source TlsStatusSource, mailHost, tlsMode, sessionSecret string, pollPeriod time.Duration) {
if source == nil || sessionSecret == "" {
return
}
if pollPeriod <= 0 {
pollPeriod = time.Minute
}
r.tls = &adminTlsAPI{
source: source, mailHost: mailHost, tlsMode: tlsMode,
sessions: NewWebSessionVerifier(sessionSecret), pollPeriod: pollPeriod,
}
}
func (r *Router) handleAdminTls(w http.ResponseWriter, req *http.Request) {
if r.tls == nil {
writeError(w, http.StatusServiceUnavailable, "TLS_API_DISABLED",
"The TLS panel needs a certificate watcher and JWT_SECRET")
return
}
r.tls.handleStatus(w, req)
}