191 lines
7.7 KiB
Go
191 lines
7.7 KiB
Go
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, end date): 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 new end date, 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
|
|
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
|
|
)
|
|
|
|
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, onEvent EventNotifyFunc, logger *log.Logger) *EndDateChecker {
|
|
return &EndDateChecker{tokens: tokens, reports: reports, told: told, setTold: setTold, 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
|
|
|
|
// stage: "soon" inside the 14 days, "ended" from the end date on. Each stage is told once per key.
|
|
tell := func(key string, end time.Time) (stage string, ok bool) {
|
|
left := end.Sub(now)
|
|
switch {
|
|
case left > warn || left < -warn:
|
|
return "", false // not yet — or ended so long ago that it is no news (an old token nobody replaced)
|
|
case left > 0:
|
|
stage = "soon"
|
|
default:
|
|
stage = "ended"
|
|
}
|
|
k := key + ":" + stage
|
|
if !c.told(k).IsZero() {
|
|
return stage, false
|
|
}
|
|
_ = c.setTold(k, now)
|
|
return stage, true
|
|
}
|
|
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, ok := tell(fmt.Sprintf("end_date:token:%d", t.ID), t.ExpiresAt)
|
|
if !ok {
|
|
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 replaced: every script that uses it gets 401. Do: make a new token on the hub's Tokens page.",
|
|
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})
|
|
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
|
|
for _, wc := range parseWebCerts(r.ReportJSON) {
|
|
stage, ok := tell("end_date:web_cert:"+r.CustomerID+":"+wc.Domain+":"+wc.NotAfter.UTC().Format("20060102T150405Z"), wc.NotAfter)
|
|
if !ok {
|
|
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})
|
|
c.onEvent(r.CustomerID, EventWebCertEndsSoon, sev, msg, string(details), "hub")
|
|
}
|
|
}
|
|
}
|
|
|
|
type webCert struct {
|
|
Domain string
|
|
NotAfter time.Time
|
|
}
|
|
|
|
// 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"`
|
|
} `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
|
|
}
|
|
out = append(out, webCert{Domain: c.Domain, NotAfter: at})
|
|
}
|
|
sort.Slice(out, func(i, j int) bool { return out[i].NotAfter.Before(out[j].NotAfter) })
|
|
return out
|
|
}
|