Files
felhom.eu/hub/internal/monitor/end_dates.go
T

229 lines
9.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package monitor
import (
"encoding/json"
"fmt"
"log"
"regexp"
"sort"
"strings"
"sync"
"time"
)
// EndDateChecker (R-1379, D77, `09` §3 decision 298; `08-alarm-ladder.md`) warns the operator 14 days before two end
// dates the product itself can read. Until it, no part of the product read either, and the first sign was the
// failure on the day:
//
// - an OPERATOR ACCESS TOKEN (hub §17): 90 days by default; on its end date the release step that raises a floor
// answers 401. The hub knows the date (api_tokens.expires_at).
// - a box's WEB CERTIFICATE: only the box knows the date (it is in Traefik's store in the guest; the household's
// public names end at Cloudflare). The controller carries it in its report (`web_certs`, controllers after
// 0.352.1). Traefik renews 30 days before the end by itself — so 14 days left means the renewal has failed for
// two weeks (a Cloudflare token of the household that ended, a name check that fails).
//
// One event per token and per (household, name): told once at 14 days, and once more — as an ERROR — if the date
// passes with the same certificate or token still there. A renewed certificate has a later end date and is told
// again only when THAT one comes within 14 days, so nothing has to clear. State: the hub's time-by-key settings, no new table. Operator-only. Pinned by TestEndDates_*.
type EndDateChecker struct {
tokens func() ([]EndingToken, error)
reports func() ([]BoxCertReport, error)
told func(key string) time.Time
setTold func(key string, t time.Time) error
keys func(prefix string) []string // the marks under a prefix (store.OSAlarmKeys)
onEvent EventNotifyFunc
logger *log.Logger
now func() time.Time
every time.Duration
warnDays int
mu sync.Mutex
lastRun time.Time
}
// EndingToken is one operator access token that is still valid to be used (not revoked).
type EndingToken struct {
ID int64
Name string
ExpiresAt time.Time
}
// BoxCertReport is one household's newest controller report (raw), from which `web_certs` is read.
type BoxCertReport struct {
CustomerID string
ReportJSON string
}
const (
EventTokenEndsSoon = "operator_token_ends_soon"
EventWebCertEndsSoon = "web_cert_ends_soon"
endDateWarnDays = 14
endDateCheckEvery = time.Hour
webCertsReadMax = 8
webCertMaxMarks = 24 // per household: names × two stages
)
var endDateNameRe = regexp.MustCompile(`^[A-Za-z0-9*._-]{1,253}$`)
// a token's name is the operator's own text (hub Tokens page); anything else is shown as „token #id”.
var endDateTokenNameRe = regexp.MustCompile(`^[A-Za-z0-9 ._-]{1,64}$`)
func NewEndDateChecker(tokens func() ([]EndingToken, error), reports func() ([]BoxCertReport, error),
told func(string) time.Time, setTold func(string, time.Time) error, keys func(string) []string, onEvent EventNotifyFunc, logger *log.Logger) *EndDateChecker {
return &EndDateChecker{tokens: tokens, reports: reports, told: told, setTold: setTold, keys: keys, onEvent: onEvent, logger: logger,
now: time.Now, every: endDateCheckEvery, warnDays: endDateWarnDays}
}
// Check runs on the hub's 60 s sweep and does its work once an hour.
func (c *EndDateChecker) Check() {
c.mu.Lock()
defer c.mu.Unlock()
now := c.now()
if !c.lastRun.IsZero() && now.Sub(c.lastRun) < c.every {
return
}
c.lastRun = now
warn := time.Duration(c.warnDays) * 24 * time.Hour
// The FIRST run ever (a first deploy): a thing that has ALREADY ended is marked and not told — an old token that
// was replaced long ago and never revoked is no news. What ends within the 14 days is told as always.
baseline := c.told("end_date:baseline").IsZero()
stageOf := func(end time.Time) string {
left := end.Sub(now)
switch {
case left > warn || left < -warn:
return "" // not yet — or ended so long ago that it is no news
case left > 0:
return "soon"
}
return "ended"
}
daysLeft := func(end time.Time) int { return int(end.Sub(now) / (24 * time.Hour)) }
if toks, err := c.tokens(); err != nil {
c.logger.Printf("[WARN] end-date check: cannot list the operator tokens: %v", err)
} else {
for _, t := range toks {
stage := stageOf(t.ExpiresAt)
if stage == "" {
continue
}
k := fmt.Sprintf("end_date:token:%d:%s", t.ID, stage)
if !c.told(k).IsZero() {
continue
}
_ = c.setTold(k, now)
if baseline && stage == "ended" {
continue
}
name := t.Name
if !endDateTokenNameRe.MatchString(name) {
name = fmt.Sprintf("token #%d", t.ID)
}
sev, msg := "warning", fmt.Sprintf("The operator access token „%s” ends in %d day(s), on %s. On that day every script that uses it gets 401 (for one: the release step that raises the floor). Do: make a new token on the hub's Tokens page and put it in the store of the program that uses it.",
name, daysLeft(t.ExpiresAt), t.ExpiresAt.UTC().Format("2006-01-02"))
if stage == "ended" {
sev, msg = "error", fmt.Sprintf("The operator access token „%s” ENDED on %s and is not revoked: every script that still uses it gets 401. Do: if it was replaced, revoke it on the hub's Tokens page; if not, make a new token.",
name, t.ExpiresAt.UTC().Format("2006-01-02"))
}
c.logger.Printf("[WARN] end-date check: operator token %d (%s) %s — ends %s", t.ID, name, stage, t.ExpiresAt.UTC().Format(time.RFC3339))
details, _ := json.Marshal(map[string]any{"token_id": t.ID, "name": name, "expires_at": t.ExpiresAt.UTC().Format(time.RFC3339),
"stage": stage, "subject": fmt.Sprintf("token:%d:%s", t.ID, stage)})
c.onEvent("", EventTokenEndsSoon, sev, msg, string(details), "hub")
}
}
reps, err := c.reports()
if err != nil {
c.logger.Printf("[WARN] end-date check: cannot read the reports: %v", err)
return
}
seen := map[string]bool{}
for _, r := range reps {
if seen[r.CustomerID] {
continue
}
seen[r.CustomerID] = true
// The names and dates are the BOX's. Its marks are keyed per (household, name, stage) and hold the END DATE
// that was told, so a box cannot mint a new mark, or a new mail, by sending another date each hour: a name is
// told again only for an end date at least a day LATER than the one told (a real renewal moves it by weeks),
// and one household holds at most webCertMaxMarks marks. (Review, 2026-11-14.)
prefix := "end_date:web_cert:" + r.CustomerID + ":"
marks := len(c.keys(prefix))
for _, wc := range parseWebCerts(r.ReportJSON) {
stage := stageOf(wc.NotAfter)
if stage == "" {
continue
}
k := prefix + wc.Domain + ":" + stage
toldEnd := c.told(k)
if !toldEnd.IsZero() && !wc.NotAfter.After(toldEnd.Add(24*time.Hour)) {
continue
}
if toldEnd.IsZero() {
if marks >= webCertMaxMarks {
c.logger.Printf("[WARN] end-date check: %s already holds %d web-certificate marks — %s is not told", r.CustomerID, marks, wc.Domain)
continue
}
marks++
}
_ = c.setTold(k, wc.NotAfter)
if baseline && stage == "ended" {
continue
}
sev, msg := "warning", fmt.Sprintf("The web certificate of %s for %s ends in %d day(s), on %s. The box renews it by itself 30 days before the end, so the renewal has FAILED for about two weeks. On the end date every app address of the household shows a browser warning. Do: read the Traefik log on the box for „acme”; the usual cause is the household's Cloudflare token.",
r.CustomerID, wc.Domain, daysLeft(wc.NotAfter), wc.NotAfter.UTC().Format("2006-01-02"))
if stage == "ended" {
sev, msg = "error", fmt.Sprintf("The web certificate of %s for %s ENDED on %s and the box still reports it: every app address of the household shows a browser warning. Do: read the Traefik log on the box for „acme”.",
r.CustomerID, wc.Domain, wc.NotAfter.UTC().Format("2006-01-02"))
}
c.logger.Printf("[WARN] end-date check: web certificate of %s for %s %s — ends %s", r.CustomerID, wc.Domain, stage, wc.NotAfter.UTC().Format(time.RFC3339))
details, _ := json.Marshal(map[string]any{"domain": wc.Domain, "not_after": wc.NotAfter.UTC().Format(time.RFC3339), "stage": stage,
"issuer": wc.Issuer, "subject": wc.Domain + ":" + stage})
c.onEvent(r.CustomerID, EventWebCertEndsSoon, sev, msg, string(details), "hub")
}
}
if baseline {
_ = c.setTold("end_date:baseline", now)
}
}
type webCert struct {
Domain string
NotAfter time.Time
Issuer string // the issuer's name as the box read it from the certificate; "" when it is not a plain name
}
// parseWebCerts reads `web_certs` from a controller report: at most webCertsReadMax entries, each with a plain name
// and a date. The list is the box's: a name that is not a host name, or a date that is not one, is dropped — the
// box's words never reach a mail unchecked. An older controller sends none.
func parseWebCerts(reportJSON string) []webCert {
var w struct {
WebCerts []struct {
Domain string `json:"domain"`
NotAfter string `json:"not_after"`
Issuer string `json:"issuer"`
} `json:"web_certs"`
}
if json.Unmarshal([]byte(reportJSON), &w) != nil {
return nil
}
var out []webCert
for _, c := range w.WebCerts {
if len(out) == webCertsReadMax {
break
}
at, err := time.Parse(time.RFC3339, c.NotAfter)
if err != nil || !endDateNameRe.MatchString(c.Domain) || strings.Contains(c.Domain, "..") {
continue
}
issuer := c.Issuer
if !endDateTokenNameRe.MatchString(issuer) {
issuer = "" // the box's word: shown only when it is a plain name
}
out = append(out, webCert{Domain: c.Domain, NotAfter: at, Issuer: issuer})
}
sort.Slice(out, func(i, j int) bool { return out[i].NotAfter.Before(out[j].NotAfter) })
return out
}