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:
admin committed 2026-11-14 19:08:55 +01:00
1 parent a044eb8c16
commit f618c591eb
31 files changed
+796 -88

No files matched your search

+28 -1
View File
@@ -622,6 +622,32 @@ const (
// operatorCooldownFor returns the operator-mail cooldown for an event type. Pinned by
// TestOperatorCooldown_NodeLivenessBypassesQuietHour.
// perSubjectCooldownEvents (review of 2026-11-14): events whose checker tells each THING once and keeps a durable
// mark — one token, one certificate, one job, one guest, one cause, one daily job. Their operator cooldown is keyed
// per thing (details `subject`). With the plain `customer:type` key the second thing of the same kind inside the
// hour was suppressed — and, because its mark was already set, never told again: two tokens that end the same day,
// two daily checks skipped by one outage, a box that refuses every job. A named allow-list, like perAppCooldownEvents:
// every other type's key is byte-identical. Pinned by TestSubjectCooldown_*.
var perSubjectCooldownEvents = map[string]bool{
"operator_token_ends_soon": true, "web_cert_ends_soon": true, "hub_daily_job_skipped": true,
"signed_job_not_taken": true, "signed_job_refused": true, "guest_power_gave_up": true, "guest_power_ok": true,
"proxmox_trust_problem": true,
}
// cooldownSubjectSuffix returns ":"+subject for an allow-listed type whose details carry a non-empty `subject`.
func cooldownSubjectSuffix(eventType, detailsJSON string) string {
if !perSubjectCooldownEvents[eventType] || !strings.Contains(detailsJSON, "\"subject\"") {
return ""
}
var d struct {
Subject string `json:"subject"`
}
if err := json.Unmarshal([]byte(detailsJSON), &d); err != nil || d.Subject == "" {
return ""
}
return ":" + d.Subject
}
func operatorCooldownFor(eventType string) time.Duration {
if nodeLivenessEvents[eventType] {
return nodeLivenessDedupeWindow
@@ -641,7 +667,8 @@ func (d *Dispatcher) processOperator(customerID, eventType, severity, message, d
// is byte-identical to v0.107.0's — pinned by TestR389_NoOtherEventTypeKeyChanges.
cooldownKey := customerID + ":" + eventType +
cooldownTierSuffix(detailsJSON) + cooldownRunSuffix(detailsJSON) +
cooldownStackSuffix(eventType, detailsJSON) + cooldownStorageSuffix(eventType, detailsJSON)
cooldownStackSuffix(eventType, detailsJSON) + cooldownStorageSuffix(eventType, detailsJSON) +
cooldownSubjectSuffix(eventType, detailsJSON)
window := operatorCooldownFor(eventType)
// R-723 (v0.126.0): a whole-guest tier skipped for absent storage in a box's FIRST HOUR is provisioning
// still in flight, not a fault (measured 2026-09-29: the first local backup ran 7 min after enrolment,
+8 -5
View File
@@ -24,17 +24,20 @@ const lostMailLookBack = 48 * time.Hour
// leaves an event row at once — see retryOperatorEmail). And this summary is an operator mail like any other: when the
// mail service is still down it is retried three times and can be lost too — the next start then lists it.
// Returns the number of lost mails told. Pinned by TestLostOperatorMail_*.
func (d *Dispatcher) ReportLostOperatorMails(now time.Time) int {
//
// processStart is when THIS hub process began (cmd/hub: a package-level time). Only mails that failed before it are
// lost: one that failed since may still be retried by this process's own timers.
func (d *Dispatcher) ReportLostOperatorMails(now, processStart time.Time) int {
since := now.Add(-lostMailLookBack)
if mark := d.store.OSAlarmRaised("operator_mail_lost_scan"); mark.After(since) {
since = mark
}
lost, err := d.store.LostOperatorMails(since)
lost, err := d.store.LostOperatorMails(since, processStart)
if err != nil {
d.logger.Printf("[WARN] lost operator mail scan failed: %v", err)
return 0
}
if err := d.store.SetOSAlarmRaised("operator_mail_lost_scan", now); err != nil {
if err := d.store.SetOSAlarmRaised("operator_mail_lost_scan", processStart); err != nil {
d.logger.Printf("[WARN] lost operator mail scan: could not record the scan time: %v", err)
}
if len(lost) == 0 {
@@ -53,8 +56,8 @@ func (d *Dispatcher) ReportLostOperatorMails(now time.Time) int {
break
}
msg := m.Message
if len(msg) > 400 {
msg = msg[:400] + "…"
if r := []rune(msg); len(r) > 400 { // by rune: the texts hold „ ” — and a byte cut would split one
msg = string(r[:400]) + "…"
}
cust := m.CustomerID
if cust == "" {
+25 -3
View File
@@ -65,8 +65,9 @@ func TestLostOperatorMail_ToldAtTheNextStart(t *testing.T) {
got = append(got, sentMail{to, subject, body})
return nil
}
now := time.Now().Add(time.Minute)
if n := d2.ReportLostOperatorMails(now); n != 1 {
now := time.Now().Add(2 * time.Second)
time.Sleep(1100 * time.Millisecond) // the log's times have one-second grain: the "restart" is a second later
if n := d2.ReportLostOperatorMails(now, now); n != 1 {
t.Fatalf("lost mails told = %d, want 1 (the one whose retries the restart ate; not the one that went out on a retry)", n)
}
if len(got) != 1 || got[0].to != "op@felhom.eu" {
@@ -85,7 +86,7 @@ func TestLostOperatorMail_ToldAtTheNextStart(t *testing.T) {
t.Fatalf("event rows = %+v, want one operator_mail_lost row", evs)
}
// a second restart: the same mail is not told again
if n := d2.ReportLostOperatorMails(now.Add(time.Minute)); n != 0 || len(got) != 1 {
if n := d2.ReportLostOperatorMails(now.Add(time.Minute), now.Add(time.Minute)); n != 0 || len(got) != 1 {
t.Fatalf("a second start told %d again (mails now %d)", n, len(got))
}
_ = sent
@@ -94,6 +95,27 @@ func TestLostOperatorMail_ToldAtTheNextStart(t *testing.T) {
}
}
// Review of 2026-11-14: „at a start no timer is alive” is not true for a mail that failed in THIS process (the scan
// runs some way into the start; a daily-check alarm or a box's event can already have failed and be waiting for its
// retry). Such a mail must not be told as „never sent” — it may still arrive.
func TestLostOperatorMail_AMailThatFailedInThisProcessIsNotLost(t *testing.T) {
d, _, down := lostMailDispatcher(t)
processStart := time.Now().Add(-2 * time.Second) // this process started two seconds ago
*down = true
d.afterFn = func(time.Duration, func()) {} // its retry timer is alive and has not fired yet
d.processOperator("c1", "whole_guest_backup_failed", "error", "Whole-guest backup FAILED on demo-hp", "", "box")
*down = false
if n := d.ReportLostOperatorMails(time.Now().Add(time.Second), processStart); n != 0 {
t.Fatalf("a mail that failed AFTER this process started was told as lost (%d)", n)
}
// the next process (a later start) does tell it
time.Sleep(1100 * time.Millisecond)
later := time.Now().Add(time.Second)
if n := d.ReportLostOperatorMails(later, later); n != 1 {
t.Fatalf("the next start must tell it: %d", n)
}
}
// The other half (clock list CH04-7): the hub keeps running and all three retries fail. A log line was the only
// trace; now an event row says which mail was never sent. (No mail: the mail service is what failed.)
func TestLostOperatorMail_GiveUpLeavesARow(t *testing.T) {
@@ -0,0 +1,43 @@
package notify
import (
"io"
"log"
"strings"
"testing"
)
// Review of 2026-11-14: the checkers behind these events tell each THING once and keep a durable mark. With the
// plain `customer:type` cooldown key the second thing of the same kind inside the hour was suppressed — and never
// told again, because its mark was already set. Two tokens that end the same day must give TWO mails; the same
// token again inside the hour gives none; and a type that is not on the list keeps its old key, subject or not.
// COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/review/redproof-hub-review-fixes.txt).
func TestSubjectCooldown_TwoThingsTwoMails(t *testing.T) {
st := newDispStore(t)
d := NewDispatcher(st, "test-key", "from@felhom.eu", "op@felhom.eu", true, log.New(io.Discard, "", 0))
var bodies []string
d.sendEmailFn = func(_, _, body string, _ map[string]string) error { bodies = append(bodies, body); return nil }
d.processOperator("", "operator_token_ends_soon", "warning", "token A ends", `{"subject":"token:1:soon"}`, "hub")
d.processOperator("", "operator_token_ends_soon", "warning", "token B ends", `{"subject":"token:2:soon"}`, "hub")
d.processOperator("", "operator_token_ends_soon", "warning", "token A ends", `{"subject":"token:1:soon"}`, "hub")
if len(bodies) != 2 || !strings.Contains(bodies[0], "token A") || !strings.Contains(bodies[1], "token B") {
t.Fatalf("mails = %d, want two (one per token) and none for the repeat: %q", len(bodies), bodies)
}
// every type on the list has the suffix; a type that is not on it has none, also with a subject in its details
for typ := range perSubjectCooldownEvents {
if cooldownSubjectSuffix(typ, `{"subject":"x"}`) != ":x" {
t.Errorf("%s: no per-subject key", typ)
}
if !operatorOnlyEvents[typ] {
t.Errorf("%s is keyed per subject but is not operator-only", typ)
}
}
for _, typ := range []string{"backup_failed", "app_start_failed", "tunnel_down", "box_clock_wrong"} {
if s := cooldownSubjectSuffix(typ, `{"subject":"x"}`); s != "" {
t.Errorf("%s: its cooldown key changed (%q) — only the listed types may", typ, s)
}
}
if cooldownSubjectSuffix("signed_job_refused", `{"other":1}`) != "" || cooldownSubjectSuffix("signed_job_refused", `not json "subject"`) != "" {
t.Error("no subject, or details that are not JSON, must give no suffix")
}
}