hub (unreleased): four silent failures tell the operator (R-1383, D78)
gates / gates (push) Successful in 2m9s
gates / gates (push) Successful in 2m9s
A hub daily check skipped while the hub was down; an operator mail that was never sent; a signed job a box did not take; a signed job a box refused, and the guest-power watchdog's give-up (both from the agent). Operator-only. No new table. Not deployed. Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
88e636445a
commit
1886030782
22 files changed
+1269
-2
No files matched your search
@@ -12,6 +12,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -778,6 +779,9 @@ type hostReportPayload struct {
|
||||
// ProxmoxTrust (R-1380, R-1396; `03` §12a): how the agent trusts Proxmox's certificate and, when something about it
|
||||
// is wrong, the CAUSE. nil = an agent without the stanza (older than release 2b).
|
||||
ProxmoxTrust *hostProxmoxTrust `json:"proxmox_trust"`
|
||||
// GuestPowerGaveUp (R-1383, D78): the guests the agent's guest-power watchdog stopped trying to start (three
|
||||
// failed starts). nil/empty = none, or an agent that does not report it.
|
||||
GuestPowerGaveUp []hostGuestPowerGaveUp `json:"guest_power_gave_up"`
|
||||
// SentAt (R-1384, D79): the box's clock at the moment the agent SENDS the report (stamped in the agent's
|
||||
// Client.Report, after the collect). "" = an agent that does not stamp it → the clock is not judged.
|
||||
// reported_at is NOT used for this: it is stamped when the collect starts, and a collect can take long.
|
||||
@@ -1018,6 +1022,13 @@ func (h *Handler) handleHostReport(w http.ResponseWriter, r *http.Request) {
|
||||
h.checkWGTunnel(hostID, custID, rep.Wireguard)
|
||||
h.checkProxmoxTrust(hostID, custID, rep.ProxmoxTrust)
|
||||
h.checkBoxClock(hostID, custID, rep.SentAt, received)
|
||||
runningGuests := map[int]bool{}
|
||||
for _, g := range rep.Guests {
|
||||
if g.Status == "running" {
|
||||
runningGuests[g.VMID] = true
|
||||
}
|
||||
}
|
||||
h.checkGuestPowerGaveUp(hostID, custID, rep.GuestPowerGaveUp, runningGuests)
|
||||
h.checkCrash(hostID, custID, body)
|
||||
h.checkDeliberateOff(hostID, custID, &rep)
|
||||
|
||||
@@ -2072,6 +2083,7 @@ func (h *Handler) handleDeleteJob(w http.ResponseWriter, r *http.Request, pathHo
|
||||
http.Error(w, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
h.noteSignedJobOutcome(pathHostID, jobID, r.URL.Query().Get("outcome"), r.URL.Query().Get("reason"), r.URL.Query().Get("op"))
|
||||
h.logger.Printf("[INFO] host %s cleared signed-op job %s (executed or rejected)", pathHostID, jobID)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
@@ -3246,6 +3258,120 @@ func (h *Handler) checkTunnel(hostID, custID, status, detail string) {
|
||||
}
|
||||
}
|
||||
|
||||
// The four silent failures, the two a BOX tells (R-1383, D78, `09` §3 decision 299; `08-alarm-ladder.md`).
|
||||
//
|
||||
// (a) signed_job_refused — the agent clears a signed job from its queue when it ran it AND when it refused it; until
|
||||
// now the hub could not tell the two apart („executed or rejected”), so an agent update that was refused left no
|
||||
// trace outside the box's journal (clock list CA09-4, CA09-7). An agent of release 3 says how the job ended in the
|
||||
// DELETE's query: outcome = done | rejected | failed | malformed, a reason from a CLOSED list, and the op's name.
|
||||
// Anything but `done` → one operator-only event. No outcome (an older agent) → nothing. The box's words never reach
|
||||
// a mail: an unknown reason is told as „other”, an op name is cut to [a-z0-9_].
|
||||
// (b) guest_power_gave_up — see checkGuestPowerGaveUp.
|
||||
// Pinned by TestSignedJobOutcome_* and TestGuestPowerGaveUp_*.
|
||||
const (
|
||||
eventSignedJobRefused = "signed_job_refused"
|
||||
eventGuestPowerGaveUp = "guest_power_gave_up"
|
||||
eventGuestPowerRunning = "guest_power_ok"
|
||||
)
|
||||
|
||||
// signedJobReasons is the closed list of the agent's reasons (signedjobs.reasonCode), each with the operator's
|
||||
// sentence. The two clock reasons name the clock, because a box with a wrong clock refuses EVERY job.
|
||||
var signedJobReasons = map[string]string{
|
||||
"expired": "its validity had run out when the box read it. If the job was fresh, the box's clock is AHEAD",
|
||||
"not_yet_valid": "it was not valid yet by the box's clock. The box's clock is BEHIND",
|
||||
"window_too_long": "its validity window is longer than the box allows",
|
||||
"replay": "the box saw this job's number before (a job runs once)",
|
||||
"bad_signature": "the signature did not verify",
|
||||
"unknown_signer": "the signing key is not one the box trusts",
|
||||
"target": "it names another box",
|
||||
"namespace": "it was signed for another use",
|
||||
"malformed": "the box could not read it",
|
||||
"role_denied": "the signing key may not sign this kind of job",
|
||||
"binding_mismatch": "it is signed for another action or device",
|
||||
"execution_failed": "it was accepted and its run FAILED on the box",
|
||||
}
|
||||
|
||||
var signedJobOpRe = regexp.MustCompile(`^[a-z0-9_]{1,40}$`)
|
||||
|
||||
func (h *Handler) noteSignedJobOutcome(hostID, jobID, outcome, reason, op string) {
|
||||
switch outcome {
|
||||
case "rejected", "failed", "malformed":
|
||||
default:
|
||||
return // "done", or an agent that does not say
|
||||
}
|
||||
key := "signed_job_refused:" + hostID + ":" + jobID
|
||||
if !h.store.OSAlarmRaised(key).IsZero() {
|
||||
return // the agent repeats a DELETE whose answer it did not get
|
||||
}
|
||||
_ = h.store.SetOSAlarmRaised(key, time.Now())
|
||||
text := signedJobReasons[reason]
|
||||
if text == "" {
|
||||
reason, text = "other", "the box gave a reason this hub does not know"
|
||||
}
|
||||
if !signedJobOpRe.MatchString(op) {
|
||||
op = "unknown"
|
||||
}
|
||||
custID := ""
|
||||
if host, err := h.store.GetHost(hostID); err == nil && host != nil {
|
||||
custID = host.CustomerID
|
||||
}
|
||||
what := "REFUSED"
|
||||
if outcome == "failed" {
|
||||
what = "ran and FAILED"
|
||||
}
|
||||
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})
|
||||
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 {
|
||||
go h.dispatcher.ProcessEvent(custID, eventSignedJobRefused, "error", msg, string(details), "hub")
|
||||
}
|
||||
}
|
||||
|
||||
type hostGuestPowerGaveUp struct {
|
||||
VMID int `json:"vmid"`
|
||||
Since string `json:"since"`
|
||||
Attempts int `json:"attempts"`
|
||||
}
|
||||
|
||||
// checkGuestPowerGaveUp: the agent's guest-power watchdog starts a guest that must run and is stopped; after three
|
||||
// failed starts it stops trying, and until now it wrote one line in the host's journal and told nobody (clock list
|
||||
// CA07-7) — the household's apps are off and the only outside sign was „the box does not report”, which is not even
|
||||
// true here (the HOST reports). One operator-only `guest_power_gave_up` (error) per guest, told once for one down
|
||||
// period; one `guest_power_ok` when a later report shows that guest RUNNING. The agent's record is memory: after an
|
||||
// agent restart the entry is gone while the guest is still down — so „gone from the list” alone never clears.
|
||||
func (h *Handler) checkGuestPowerGaveUp(hostID, custID string, gave []hostGuestPowerGaveUp, running map[int]bool) {
|
||||
emit := func(typ, sev, msg string, details map[string]any) {
|
||||
dj, _ := json.Marshal(details)
|
||||
if _, err := h.store.SaveEvent(custID, typ, sev, msg, string(dj), "hub"); err != nil {
|
||||
h.logger.Printf("[WARN] %s event save FAILED for %s: %v", typ, hostID, err)
|
||||
} else if h.dispatcher != nil {
|
||||
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 || running[g.VMID] || !h.store.OSAlarmRaised(key(g.VMID)).IsZero() {
|
||||
continue
|
||||
}
|
||||
_ = 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})
|
||||
}
|
||||
for vmid := range running {
|
||||
if h.store.OSAlarmRaised(key(vmid)).IsZero() {
|
||||
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})
|
||||
}
|
||||
}
|
||||
|
||||
// Box clock alarm (R-1384, D79, `09` §3 decision 300; `08-alarm-ladder.md`). The hub compares the box's clock at
|
||||
// the moment of SENDING (`sent_at`) with its own clock at the moment the request arrives. More than one minute apart,
|
||||
// in either direction → one operator-only `box_clock_wrong`; back within 30 seconds → one `box_clock_ok` (the gap
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
|
||||
)
|
||||
|
||||
func r1383Events(t *testing.T, st *store.Store, typ string) []store.Event {
|
||||
t.Helper()
|
||||
evs, err := st.GetEventsByType("c1", typ, time.Now().Add(-time.Hour))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return evs
|
||||
}
|
||||
|
||||
// R-1383 (D78): an agent that clears a signed job says HOW it ended. A refused job (here: expired — the box's clock
|
||||
// is ahead, so an agent update never arrives) leaves one operator event that names the job, the op and the cause.
|
||||
// A job that ran, and an agent that says nothing (older), leave none. The stored effect is the event row; the queue
|
||||
// is emptied in every case. COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub-box-told.txt).
|
||||
func TestSignedJobOutcome_RefusedIsToldDoneIsNot(t *testing.T) {
|
||||
h, st := trustSetup(t)
|
||||
del := func(job, query string) {
|
||||
t.Helper()
|
||||
if err := st.EnqueueSignedJob("h1", job, []byte("opaque")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rr := do(h, http.MethodDelete, "/hosts/h1/jobs/"+job+query, "HKEY", ""); rr.Code != 200 {
|
||||
t.Fatalf("DELETE %s: %d %s", job, rr.Code, rr.Body.String())
|
||||
}
|
||||
if n, _ := st.CountSignedJobs("h1"); n != 0 {
|
||||
t.Fatalf("job %s is still queued", job)
|
||||
}
|
||||
}
|
||||
del("ran", "?outcome=done&op=agent_update")
|
||||
del("old-agent", "")
|
||||
if evs := r1383Events(t, st, eventSignedJobRefused); len(evs) != 0 {
|
||||
t.Fatalf("a job that ran, or an agent that says nothing, raised %d event(s): %+v", len(evs), evs)
|
||||
}
|
||||
del("j-exp", "?outcome=rejected&reason=expired&op=agent_update")
|
||||
// the agent repeats a DELETE whose answer it lost: told once
|
||||
if rr := do(h, http.MethodDelete, "/hosts/h1/jobs/j-exp?outcome=rejected&reason=expired&op=agent_update", "HKEY", ""); rr.Code != 200 {
|
||||
t.Fatalf("repeat DELETE: %d", rr.Code)
|
||||
}
|
||||
evs := r1383Events(t, st, eventSignedJobRefused)
|
||||
if len(evs) != 1 {
|
||||
t.Fatalf("events=%d, want ONE for the refused job", len(evs))
|
||||
}
|
||||
for _, want := range []string{"j-exp", "agent_update", "h1", "REFUSED", "clock is AHEAD"} {
|
||||
if !strings.Contains(evs[0].Message, want) {
|
||||
t.Errorf("the event lacks %q: %s", want, evs[0].Message)
|
||||
}
|
||||
}
|
||||
if evs[0].Severity != "error" || !strings.Contains(evs[0].DetailsJSON, `"reason":"expired"`) {
|
||||
t.Errorf("event = %+v", evs[0])
|
||||
}
|
||||
// a failed run, and words the hub does not know: told, and the box's own text never reaches the mail
|
||||
del("j-fail", "?outcome=failed&reason=execution_failed&op=agent_config_update")
|
||||
del("j-odd", "?outcome=rejected&reason=%3Cscript%3E&op=%3Cb%3EX")
|
||||
evs = r1383Events(t, st, eventSignedJobRefused)
|
||||
if len(evs) != 3 {
|
||||
t.Fatalf("events=%d, want 3", len(evs))
|
||||
}
|
||||
all := ""
|
||||
for _, e := range evs {
|
||||
all += e.Message + e.DetailsJSON + "\n"
|
||||
}
|
||||
if !strings.Contains(all, "ran and FAILED") || !strings.Contains(all, `"reason":"other"`) || !strings.Contains(all, `"op":"unknown"`) {
|
||||
t.Errorf("the failed run or the unknown words are not told as designed:\n%s", all)
|
||||
}
|
||||
if strings.Contains(all, "script") || strings.Contains(all, "<b>") {
|
||||
t.Errorf("the box's own text reached an event:\n%s", all)
|
||||
}
|
||||
}
|
||||
|
||||
func gaveUpReport(guestStatus, stanza string) string {
|
||||
b := validReportBody("h1")
|
||||
b = strings.Replace(b, `{"vmid":100,"name":"acme","status":"running"`, `{"vmid":100,"name":"acme","status":"`+guestStatus+`"`, 1)
|
||||
if stanza == "" {
|
||||
return b
|
||||
}
|
||||
return b[:strings.LastIndex(b, "}")] + `,"guest_power_gave_up":` + stanza + "}"
|
||||
}
|
||||
|
||||
// R-1383 (D78): the guest-power watchdog gave up — the household's apps are off and the agent wrote one journal line.
|
||||
// One operator event per down period; the agent's list is memory, so the entry vanishing (an agent restart) while
|
||||
// the guest is still stopped must neither clear nor alarm a second time; the recovery is the guest RUNNING.
|
||||
func TestGuestPowerGaveUp_ToldOnceClearedOnlyByARunningGuest(t *testing.T) {
|
||||
h, st := trustSetup(t)
|
||||
post := func(status, stanza string) {
|
||||
t.Helper()
|
||||
if rr := do(h, http.MethodPost, "/host-report", "HKEY", gaveUpReport(status, stanza)); rr.Code != 200 {
|
||||
t.Fatalf("report: %d %s", rr.Code, rr.Body.String())
|
||||
}
|
||||
}
|
||||
counts := func() string {
|
||||
return fmt.Sprintf("gave_up=%d ok=%d", len(r1383Events(t, st, eventGuestPowerGaveUp)), len(r1383Events(t, st, eventGuestPowerRunning)))
|
||||
}
|
||||
post("running", "")
|
||||
post("stopped", "") // stopped, the watchdog still tries: nothing
|
||||
if c := counts(); c != "gave_up=0 ok=0" {
|
||||
t.Fatalf("before a give-up: %s", c)
|
||||
}
|
||||
stanza := `[{"vmid":100,"since":"2026-11-14T02:00:00Z","attempts":3}]`
|
||||
post("stopped", stanza)
|
||||
post("stopped", stanza)
|
||||
if c := counts(); c != "gave_up=1 ok=0" {
|
||||
t.Fatalf("two reports of the same give-up: %s, want ONE alarm", c)
|
||||
}
|
||||
e := r1383Events(t, st, eventGuestPowerGaveUp)[0]
|
||||
if e.Severity != "error" || !strings.Contains(e.Message, "Guest 100 on h1 is STOPPED") || !strings.Contains(e.Message, "3 failed attempts") {
|
||||
t.Errorf("alarm = %+v", e)
|
||||
}
|
||||
post("stopped", "") // the agent restarted: its list is empty, the guest is still down
|
||||
if c := counts(); c != "gave_up=1 ok=0" {
|
||||
t.Fatalf("the entry vanished while the guest is still stopped: %s — that is not a recovery", c)
|
||||
}
|
||||
post("stopped", `[{"vmid":100,"since":"2026-11-14T02:20:00Z","attempts":3}]`) // it gave up again: same down period
|
||||
if c := counts(); c != "gave_up=1 ok=0" {
|
||||
t.Fatalf("the second give-up of the same down period: %s", c)
|
||||
}
|
||||
post("running", "")
|
||||
post("running", "")
|
||||
if c := counts(); c != "gave_up=1 ok=1" {
|
||||
t.Fatalf("after the guest runs again: %s, want one recovery", c)
|
||||
}
|
||||
post("stopped", stanza) // a new down period is told again
|
||||
if c := counts(); c != "gave_up=2 ok=1" {
|
||||
t.Fatalf("a new down period: %s", c)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user