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

+58 -14
View File
@@ -14,6 +14,7 @@ import (
"net/http"
"regexp"
"sort"
"strconv"
"strings"
"sync"
"time"
@@ -3309,6 +3310,9 @@ var signedJobIDRe = regexp.MustCompile(`^[A-Za-z0-9._-]{1,64}$`)
// guestPowerMaxEntries bounds the give-up list read from ONE report (a box has a handful of guests).
const guestPowerMaxEntries = 16
// guestPowerMaxMarks bounds how many give-up marks ONE box can hold in the hub's settings at a time.
const guestPowerMaxMarks = 8
func (h *Handler) noteSignedJobOutcome(hostID, jobID, outcome, reason, op string) {
switch outcome {
case "rejected", "failed", "malformed":
@@ -3337,7 +3341,7 @@ func (h *Handler) noteSignedJobOutcome(hostID, jobID, outcome, reason, op string
}
h.logger.Printf("[WARN] host %s signed job %s (%s): %s — %s", hostID, jobID, op, outcome, reason)
msg := fmt.Sprintf("The signed job %s (%s) for %s was %s: %s. The job is gone from the queue and will not run again. Do: read the cause, then sign a new job.", jobID, op, hostID, what, text)
details, _ := json.Marshal(map[string]any{"host_id": hostID, "job_id": jobID, "op": op, "outcome": outcome, "reason": reason})
details, _ := json.Marshal(map[string]any{"host_id": hostID, "job_id": jobID, "op": op, "outcome": outcome, "reason": reason, "subject": hostID + ":" + jobID})
if _, err := h.store.SaveEvent(custID, eventSignedJobRefused, "error", msg, string(details), "hub"); err != nil {
h.logger.Printf("[WARN] %s event save FAILED for %s: %v", eventSignedJobRefused, hostID, err)
} else if h.dispatcher != nil {
@@ -3373,25 +3377,47 @@ func (h *Handler) checkGuestPowerGaveUp(hostID, custID string, gave []hostGuestP
go h.dispatcher.ProcessEvent(custID, typ, sev, msg, string(dj), "hub")
}
}
key := func(vmid int) string { return fmt.Sprintf("guest_power:%s:%d", hostID, vmid) }
for _, g := range gave {
if g.VMID <= 0 || !known[g.VMID] || running[g.VMID] || !h.store.OSAlarmRaised(key(g.VMID)).IsZero() {
prefix := fmt.Sprintf("guest_power:%s:", hostID)
key := func(vmid int) string { return prefix + strconv.Itoa(vmid) }
// The marks this box has now. The report's own guest list is the BOX's word too, so „the report lists the guest”
// bounds nothing by itself (review, 2026-11-14): the number of marks per box is capped, and a mark for a guest the
// box no longer lists is dropped — a destroyed guest must not hold a mark, or a slot, for ever.
raised := map[int]bool{}
for _, k := range h.store.OSAlarmKeys(prefix) {
vmid, err := strconv.Atoi(strings.TrimPrefix(k, prefix))
if err != nil {
continue
}
if !known[vmid] {
_ = h.store.SetOSAlarmRaised(k, time.Time{})
h.logger.Printf("[INFO] host %s: guest %d is no longer reported — its give-up mark is dropped", hostID, vmid)
continue
}
raised[vmid] = true
}
for _, g := range gave {
if g.VMID <= 0 || !known[g.VMID] || running[g.VMID] || raised[g.VMID] {
continue
}
if len(raised) >= guestPowerMaxMarks {
h.logger.Printf("[WARN] host %s: %d give-up marks already — guest %d is not told (a box has a handful of guests)", hostID, len(raised), g.VMID)
break
}
raised[g.VMID] = true
_ = h.store.SetOSAlarmRaised(key(g.VMID), time.Now())
h.logger.Printf("[WARN] host %s: the guest-power watchdog gave up on guest %d (%d attempts)", hostID, g.VMID, g.Attempts)
emit(eventGuestPowerGaveUp, "error",
fmt.Sprintf("Guest %d on %s is STOPPED and the agent gave up starting it after %d failed attempts. The household's apps are off. The agent does not try again by itself. Do: read `journalctl -u felhom-agent` on the box for „guest-power”, start the guest, and read why it did not start.", g.VMID, hostID, g.Attempts),
map[string]any{"host_id": hostID, "vmid": g.VMID, "attempts": g.Attempts, "since": g.Since})
map[string]any{"host_id": hostID, "vmid": g.VMID, "attempts": g.Attempts, "since": trustDate(g.Since), "subject": key(g.VMID)})
}
for vmid := range running {
if h.store.OSAlarmRaised(key(vmid)).IsZero() {
if !raised[vmid] {
continue
}
_ = h.store.SetOSAlarmRaised(key(vmid), time.Time{})
h.logger.Printf("[INFO] host %s: guest %d runs again (the guest-power watchdog had given up)", hostID, vmid)
emit(eventGuestPowerRunning, "info", fmt.Sprintf("Guest %d on %s runs again.", vmid, hostID),
map[string]any{"host_id": hostID, "vmid": vmid})
map[string]any{"host_id": hostID, "vmid": vmid, "subject": key(vmid)})
}
}
@@ -3542,12 +3568,13 @@ func (h *Handler) checkProxmoxTrust(hostID, custID string, t *hostProxmoxTrust)
}
if len(cleared) > 0 {
sort.Strings(cleared)
h.logger.Printf("[INFO] host %s proxmox trust: no problem any more (was: %s; mode %s)", hostID, strings.Join(cleared, ", "), t.Mode)
emit(eventProxmoxTrustOK, "info", fmt.Sprintf("The agent of %s trusts Proxmox again (mode: %s). Was: %s.", hostID, t.Mode, strings.Join(cleared, ", ")),
map[string]any{"host_id": hostID, "mode": t.Mode, "was": cleared})
h.logger.Printf("[INFO] host %s proxmox trust: no problem any more (was: %s; mode %s)", hostID, strings.Join(cleared, ", "), trustModeWord(t.Mode))
emit(eventProxmoxTrustOK, "info", fmt.Sprintf("The agent of %s trusts Proxmox again (mode: %s). Was: %s.", hostID, trustModeWord(t.Mode), strings.Join(cleared, ", ")),
map[string]any{"host_id": hostID, "mode": trustModeWord(t.Mode), "was": cleared})
}
return
}
mode, caEnds := trustModeWord(t.Mode), trustDate(t.CANotAfter)
cause, text := t.Problem, proxmoxTrustProblems[t.Problem]
if text == "" {
cause, text = "other", "a cause this hub does not know"
@@ -3563,11 +3590,28 @@ func (h *Handler) checkProxmoxTrust(hostID, custID string, t *hostProxmoxTrust)
}
days := ""
if t.CADaysLeft != nil {
days = fmt.Sprintf(" The root CA copy ends in %d day(s) (%s).", *t.CADaysLeft, t.CANotAfter)
days = fmt.Sprintf(" The root CA copy ends in %d day(s) (%s).", *t.CADaysLeft, caEnds)
}
h.logger.Printf("[WARN] host %s proxmox trust: %s (mode %s)", hostID, cause, t.Mode)
emit(eventProxmoxTrustProblem, sev, fmt.Sprintf("Trust in Proxmox on %s: %s (mode: %s).%s Design: `03` §12a.", hostID, text, t.Mode, days),
map[string]any{"host_id": hostID, "cause": cause, "mode": t.Mode, "reported_cause": truncateRunes(t.Problem, 60), "ca_not_after": t.CANotAfter})
h.logger.Printf("[WARN] host %s proxmox trust: %s (mode %s)", hostID, cause, mode)
emit(eventProxmoxTrustProblem, sev, fmt.Sprintf("Trust in Proxmox on %s: %s (mode: %s).%s Design: `03` §12a.", hostID, text, mode, days),
map[string]any{"host_id": hostID, "cause": cause, "mode": mode, "ca_not_after": caEnds, "subject": hostID + ":" + cause})
}
// trustModeWord / trustDate: the stanza is the BOX's. Only a word of the closed list and a real date reach an event
// (and so a mail); anything else is „other” / "" — the comment „never trusted as text” must hold for every field.
func trustModeWord(m string) string {
switch m {
case "fingerprint", "ca", "ca_file_own", "insecure", "system":
return m
}
return "other"
}
func trustDate(s string) string {
if t, err := time.Parse(time.RFC3339, s); err == nil {
return t.UTC().Format(time.RFC3339)
}
return ""
}
func truncateRunes(s string, n int) string {