Files
felhom.eu/hub/internal/api/proxmox_trust_test.go
T

109 lines
3.8 KiB
Go

package api
import (
"net/http"
"strings"
"testing"
"time"
"gitea.dooplex.hu/admin/felhom-hub/internal/store"
)
func trustReport(stanza string) string {
if stanza == "-" {
return validReportBody("h1")
}
b := validReportBody("h1")
return b[:strings.LastIndex(b, "}")] + `,"proxmox_trust":` + stanza + "}"
}
func trustSetup(t *testing.T) (*Handler, *store.Store) {
t.Helper()
h, st, _ := newTestHandler(t)
st.SaveCustomerConfig(&store.CustomerConfig{CustomerID: "c1", APIKey: "ckey", RetrievalPassword: "p"})
st.UpsertHost(&store.Host{HostID: "h1", CustomerID: "c1", APIKey: "HKEY"})
return h, st
}
func trustCounts(t *testing.T, st *store.Store) (problems, oks []store.Event) {
t.Helper()
since := time.Now().Add(-time.Hour)
problems, _ = st.GetEventsByType("c1", eventProxmoxTrustProblem, since)
oks, _ = st.GetEventsByType("c1", eventProxmoxTrustOK, since)
return
}
// R-1396: a cause is told ONCE (the box repeats it at every report), a second cause is told too, and one recovery
// event follows when the box reports no problem. A healthy stanza and an agent without the stanza raise nothing.
// COMPANION RED-PROOFS (observed, audits/night-build-2026-11-14/B/redproof-hub.txt).
func TestProxmoxTrust_CauseOnceThenRecovery(t *testing.T) {
h, st := trustSetup(t)
post := func(stanza string) {
t.Helper()
if rr := do(h, http.MethodPost, "/host-report", "HKEY", trustReport(stanza)); rr.Code != 200 {
t.Fatalf("report: %d %s", rr.Code, rr.Body.String())
}
}
post(`{"mode":"ca"}`)
post("-")
if p, o := trustCounts(t, st); len(p) != 0 || len(o) != 0 {
t.Fatalf("a healthy box raised %d problem(s), %d ok", len(p), len(o))
}
bad := `{"mode":"fingerprint","problem":"ca_copy_does_not_verify","detail":"<script>x</script>","ca_days_left":3560,"ca_not_after":"2036-08-17T15:44:42Z"}`
post(bad)
post(bad)
post(bad)
p, o := trustCounts(t, st)
if len(p) != 1 || len(o) != 0 {
t.Fatalf("problems=%d ok=%d, want ONE problem for three reports of the same cause", len(p), len(o))
}
if p[0].Severity != "error" || !strings.Contains(p[0].Message, "keeps its pinned certificate") || !strings.Contains(p[0].Message, "h1") {
t.Fatalf("event = %+v", p[0])
}
if strings.Contains(p[0].Message, "<script>") || strings.Contains(p[0].DetailsJSON, "<script>") {
t.Fatalf("the box's free text reached the operator's mail: %+v", p[0])
}
post(`{"mode":"ca","problem":"ca_copy_ends_soon","ca_days_left":12,"ca_not_after":"2026-11-26T00:00:00Z"}`)
p, _ = trustCounts(t, st)
if len(p) != 2 {
t.Fatalf("a second cause was not told: %d", len(p))
}
var soon store.Event
for _, e := range p {
if strings.Contains(e.Message, "ends within 30 days") {
soon = e
}
}
if soon.Severity != "warning" || !strings.Contains(soon.Message, "12 day") {
t.Fatalf("ends-soon event = %+v", soon)
}
post(`{"mode":"ca"}`)
post(`{"mode":"ca"}`)
p, o = trustCounts(t, st)
if len(p) != 2 || len(o) != 1 {
t.Fatalf("problems=%d ok=%d, want ONE recovery for both causes", len(p), len(o))
}
// after the recovery the same cause is a NEW problem
post(bad)
if p, _ = trustCounts(t, st); len(p) != 3 {
t.Fatalf("the cause came back and was not told again: %d", len(p))
}
// an agent WITHOUT the stanza neither clears nor raises
post("-")
if _, o = trustCounts(t, st); len(o) != 1 {
t.Fatalf("a report with no stanza cleared the problem: ok=%d", len(o))
}
}
// A cause the hub does not know is told as "other" — once — and its text is not passed on.
func TestProxmoxTrust_UnknownCause(t *testing.T) {
h, st := trustSetup(t)
for i := 0; i < 2; i++ {
do(h, http.MethodPost, "/host-report", "HKEY", trustReport(`{"mode":"ca","problem":"Kedves Ügyfél, kattints ide","detail":"x"}`))
}
p, _ := trustCounts(t, st)
if len(p) != 1 || !strings.Contains(p[0].Message, "a cause this hub does not know") || strings.Contains(p[0].Message, "Kedves") {
t.Fatalf("events = %+v", p)
}
}