hub + installer (unreleased): fixes after an independent review of the night's code — per-thing mail cooldown, a box's input bounded and checked, both off-site account types on a reset, a quiet first run, strict-mode installer
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
a044eb8c16
commit
f618c591eb
31 files changed
+796
-88
No files matched your search
@@ -22,14 +22,15 @@ import (
|
||||
// 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_*.
|
||||
// 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
|
||||
@@ -59,6 +60,7 @@ const (
|
||||
endDateWarnDays = 14
|
||||
endDateCheckEvery = time.Hour
|
||||
webCertsReadMax = 8
|
||||
webCertMaxMarks = 24 // per household: names × two stages
|
||||
)
|
||||
|
||||
var endDateNameRe = regexp.MustCompile(`^[A-Za-z0-9*._-]{1,253}$`)
|
||||
@@ -67,8 +69,8 @@ var endDateNameRe = regexp.MustCompile(`^[A-Za-z0-9*._-]{1,253}$`)
|
||||
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,
|
||||
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}
|
||||
}
|
||||
|
||||
@@ -82,24 +84,19 @@ func (c *EndDateChecker) Check() {
|
||||
}
|
||||
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()
|
||||
|
||||
// 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) {
|
||||
stageOf := func(end time.Time) string {
|
||||
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)
|
||||
return "" // not yet — or ended so long ago that it is no news
|
||||
case left > 0:
|
||||
stage = "soon"
|
||||
default:
|
||||
stage = "ended"
|
||||
return "soon"
|
||||
}
|
||||
k := key + ":" + stage
|
||||
if !c.told(k).IsZero() {
|
||||
return stage, false
|
||||
}
|
||||
_ = c.setTold(k, now)
|
||||
return stage, true
|
||||
return "ended"
|
||||
}
|
||||
daysLeft := func(end time.Time) int { return int(end.Sub(now) / (24 * time.Hour)) }
|
||||
|
||||
@@ -107,8 +104,16 @@ func (c *EndDateChecker) Check() {
|
||||
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 {
|
||||
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
|
||||
@@ -118,11 +123,12 @@ func (c *EndDateChecker) Check() {
|
||||
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.",
|
||||
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})
|
||||
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")
|
||||
}
|
||||
}
|
||||
@@ -138,9 +144,31 @@ func (c *EndDateChecker) Check() {
|
||||
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, ok := tell("end_date:web_cert:"+r.CustomerID+":"+wc.Domain+":"+wc.NotAfter.UTC().Format("20060102T150405Z"), wc.NotAfter)
|
||||
if !ok {
|
||||
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.",
|
||||
@@ -150,10 +178,14 @@ func (c *EndDateChecker) Check() {
|
||||
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})
|
||||
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 {
|
||||
|
||||
Reference in new issue
Block a user