All checks were successful
release / build (push) Successful in 4m3s
One view from /v1/posture, /v1/alerts, /v1/queue and /v1/backups: per host the active flags ranked by weight, how long each has been open, a short next step, the suggested approval actions with their newest queue entry, and flagged backup jobs. Flags on every fresh host are shown once as fleet-wide. alerts, queue and backups degrade on 404; posture is required. Exit 0 when clean, 1 on any flag or stale host, 2 config, 3 transport.
358 lines
15 KiB
Go
358 lines
15 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package fleet_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.trevon.dev/trevon/hub/internal/fleet"
|
|
)
|
|
|
|
const findingsHead = `{"generated_at":"2026-11-08T12:00:00Z","snapshot_age_seconds":5,"stale":false,"coverage_complete":true,"data":`
|
|
|
|
// fx maps a route to a body; a missing route answers 404.
|
|
type fx map[string]string
|
|
|
|
func findingsServer(t *testing.T, routes fx) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet || r.Header.Get("Authorization") != "Bearer "+secretToken {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
body, ok := routes[r.URL.Path]
|
|
if !ok {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(findingsHead + body + "}"))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func runFindings(t *testing.T, routes fx, asJSON bool, host string) (int, string, error) {
|
|
t.Helper()
|
|
srv := findingsServer(t, routes)
|
|
var out bytes.Buffer
|
|
code, err := fleet.FindingsView(context.Background(), newClient(t, srv.URL, secretToken), &out, asJSON, host, time.Now())
|
|
if strings.Contains(out.String(), secretToken) {
|
|
t.Fatal("token in output")
|
|
}
|
|
return code, out.String(), err
|
|
}
|
|
|
|
func mustFindings(t *testing.T, routes fx, asJSON bool, host string) (int, string) {
|
|
t.Helper()
|
|
code, out, err := runFindings(t, routes, asJSON, host)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return code, out
|
|
}
|
|
|
|
func posture(hosts string) string { return `{"status":"ok","totals":{},"hosts":{` + hosts + `}}` }
|
|
|
|
func ph(name, sev string, score int, stale bool, flags, actions string) string {
|
|
s := "false"
|
|
if stale {
|
|
s = "true"
|
|
}
|
|
n, _ := json.Marshal(score)
|
|
return `"` + name + `":{"severity":"` + sev + `","score":` + string(n) + `,"stale":` + s +
|
|
`,"last_report_at":"2026-10-01T18:51:56Z","active_flags":[` + flags + `],"action_ids":[` + actions + `]}`
|
|
}
|
|
|
|
const (
|
|
alertsEmpty = `{"alerts":[]}`
|
|
queueEmpty = `{"counts":{},"entries":[]}`
|
|
backupsNone = `{"status":"unavailable"}`
|
|
)
|
|
|
|
func routes(p, alerts, queue, backups string) fx {
|
|
return fx{"/v1/posture": p, "/v1/alerts": alerts, "/v1/queue": queue, "/v1/backups": backups}
|
|
}
|
|
|
|
func TestFindingsRankingOrder(t *testing.T) {
|
|
p := posture(ph("alpha", "high_priority", 9, false, `"zz_unknown","disk_space_low","ssh_root_login_not_disabled","aa_unknown","ci_runner_down"`, ``))
|
|
code, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if code != 1 {
|
|
t.Errorf("exit %d, want 1", code)
|
|
}
|
|
order := []string{"ssh_root_login_not_disabled", "ci_runner_down", "disk_space_low", "aa_unknown", "zz_unknown"}
|
|
last := -1
|
|
for _, f := range order {
|
|
i := strings.Index(out, " "+f)
|
|
if i < 0 || i < last {
|
|
t.Fatalf("flag %s out of order (weight desc, then name):\n%s", f, out)
|
|
}
|
|
last = i
|
|
}
|
|
if !strings.Contains(out, "w4") || !strings.Contains(out, "see the host report") {
|
|
t.Errorf("weights or unknown hint missing:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsHostOrder(t *testing.T) {
|
|
p := posture(strings.Join([]string{
|
|
ph("delta", "low", 1, false, `"disk_space_low"`, ``),
|
|
ph("gamma", "high_priority", 5, false, `"disk_space_low"`, ``),
|
|
ph("beta", "critical_priority", 4, false, `"ci_runner_down"`, ``),
|
|
ph("alpha", "critical_priority", 4, false, `"ci_runner_down","aa"`, ``),
|
|
ph("epsilon", "critical_priority", 23, false, `"edr_daemon_down"`, ``),
|
|
}, ","))
|
|
_, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
idx := func(s string) int { return strings.Index(out, "\n"+s+" ") + 1 }
|
|
if !(idx("epsilon") < idx("alpha") && idx("alpha") < idx("beta") && idx("beta") < idx("gamma") && idx("gamma") < idx("delta")) {
|
|
t.Errorf("hosts not sorted by severity, score desc, name:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsFleetWideExtraction(t *testing.T) {
|
|
two := posture(ph("alpha", "low", 3, false, `"third_party_updates_pending","disk_space_low"`, ``) + "," +
|
|
ph("beta", "low", 3, false, `"third_party_updates_pending"`, ``))
|
|
code, out := mustFindings(t, routes(two, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if !strings.HasPrefix(out, "fleet-wide: third_party_updates_pending (w3) on 2 hosts -- apply package updates") {
|
|
t.Errorf("fleet-wide line:\n%s", out)
|
|
}
|
|
if strings.Count(out, "third_party_updates_pending") != 1 {
|
|
t.Errorf("fleet-wide flag must appear once:\n%s", out)
|
|
}
|
|
if code != 1 {
|
|
t.Errorf("fleet-wide flags count as findings, exit %d", code)
|
|
}
|
|
if !strings.Contains(out, "beta low score=3\n fleet-wide flags only\n") {
|
|
t.Errorf("a host left with only fleet-wide flags must say so:\n%s", out)
|
|
}
|
|
if strings.Count(out, "fleet-wide flags only") != 1 {
|
|
t.Errorf("a host with its own flags must not say fleet-wide only:\n%s", out)
|
|
}
|
|
|
|
// one host only: not fleet-wide
|
|
one := posture(ph("alpha", "low", 3, false, `"third_party_updates_pending"`, ``))
|
|
_, out = mustFindings(t, routes(one, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if strings.Contains(out, "fleet-wide") || !strings.Contains(out, " third_party_updates_pending") {
|
|
t.Errorf("single host must keep its flag:\n%s", out)
|
|
}
|
|
|
|
// a stale host is not counted: the flag is on every non-stale host
|
|
withStale := posture(ph("alpha", "low", 3, false, `"third_party_updates_pending"`, ``) + "," +
|
|
ph("beta", "low", 3, false, `"third_party_updates_pending"`, ``) + "," +
|
|
ph("gamma", "stale_audit", 0, true, `"disk_space_low"`, ``))
|
|
_, out = mustFindings(t, routes(withStale, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if !strings.Contains(out, "on 2 hosts") {
|
|
t.Errorf("stale host must not be counted:\n%s", out)
|
|
}
|
|
// a fresh host without the flag: not fleet-wide
|
|
partial := posture(ph("alpha", "low", 3, false, `"third_party_updates_pending"`, ``) + "," + ph("beta", "low", 0, false, ``, ``))
|
|
_, out = mustFindings(t, routes(partial, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if strings.Contains(out, "fleet-wide") {
|
|
t.Errorf("flag on only some hosts:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsAlertJoinOpenedAt(t *testing.T) {
|
|
p := posture(ph("alpha", "high_priority", 4, false, `"ssh_root_login_not_disabled","disk_space_low"`, ``) + "," +
|
|
ph("beta", "low", 1, false, `"ssh_root_login_not_disabled"`, ``) + "," +
|
|
ph("gamma", "low", 0, false, ``, ``))
|
|
alerts := `{"alerts":[` +
|
|
`{"id":"a3","host":"beta","flag":"ssh_root_login_not_disabled","severity":"high","state":"open","opened_at":"2026-11-08T11:55:00Z","duration_seconds":300},` +
|
|
`{"id":"a1","host":"alpha","flag":"ssh_root_login_not_disabled","severity":"high","state":"open","opened_at":"2026-11-06T08:00:00Z","last_seen_at":"2026-11-08T12:00:00Z","duration_seconds":187200},` +
|
|
`{"id":"a2","host":"beta","flag":"disk_space_low","severity":"low","state":"open","opened_at":"2026-11-01T08:00:00Z","duration_seconds":999999}]}`
|
|
_, out := mustFindings(t, routes(p, alerts, queueEmpty, backupsNone), false, "")
|
|
if !strings.Contains(out, "open 2d4h") || !strings.Contains(out, "open 5m") {
|
|
t.Errorf("alert join (2d4h for alpha, 5m for beta):\n%s", out)
|
|
}
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if strings.Contains(line, "disk_space_low") && !strings.Contains(line, "open -") {
|
|
t.Errorf("alert of another host leaked: %q", line)
|
|
}
|
|
}
|
|
_, js := mustFindings(t, routes(p, alerts, queueEmpty, backupsNone), true, "")
|
|
if !strings.Contains(js, `"opened_at": "2026-11-06T08:00:00Z"`) || !strings.Contains(js, `"open_seconds": 187200`) {
|
|
t.Errorf("json join:\n%s", js)
|
|
}
|
|
}
|
|
|
|
func TestFindingsQueueJoinNewestEntry(t *testing.T) {
|
|
p := posture(ph("alpha", "high_priority", 4, false, `"ssh_root_login_not_disabled"`, `"remediate-ssh-root-login-host"`) + "," +
|
|
ph("beta", "low", 1, false, `"disk_space_low"`, `"remediate-ssh-root-login-host"`))
|
|
queue := `{"counts":{},"entries":[` +
|
|
`{"id":"aq-0002","action_id":"remediate-ssh-root-login-host","target":"alpha","status":"failed","created_at":"2026-11-01T10:00:00Z","updated_at":"2026-11-01T10:05:00Z"},` +
|
|
`{"id":"aq-0006","action_id":"remediate-ssh-root-login-host","target":"alpha","status":"approved","created_at":"2026-11-05T10:00:00Z","updated_at":"2026-11-05T10:05:00Z"},` +
|
|
`{"id":"aq-0004","action_id":"remediate-ssh-root-login-host","target":"alpha","status":"rejected","created_at":"2026-11-03T10:00:00Z","updated_at":"2026-11-03T10:05:00Z"},` +
|
|
`{"id":"aq-0009","action_id":"remediate-ssh-root-login-host","target":"gamma","status":"pending","created_at":"2026-11-07T10:00:00Z","updated_at":"2026-11-07T10:05:00Z"}]}`
|
|
_, out := mustFindings(t, routes(p, alertsEmpty, queue, backupsNone), false, "")
|
|
if !strings.Contains(out, " actions: remediate-ssh-root-login-host (aq-0006 approved)\n") {
|
|
t.Errorf("newest entry for alpha:\n%s", out)
|
|
}
|
|
if strings.Contains(out, "aq-0009") || strings.Contains(out, "aq-0002") {
|
|
t.Errorf("entries of other targets or older entries leaked:\n%s", out)
|
|
}
|
|
if !strings.Contains(out, " actions: remediate-ssh-root-login-host\n") {
|
|
t.Errorf("beta has no entry and must list the bare action:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsGenericActionsHidden(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 2, false, `"disk_space_low"`, `"security-audit-host","security-validate-host","security-harden-host"`))
|
|
_, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if !strings.Contains(out, "actions: security-harden-host\n") || strings.Contains(out, "security-audit-host") || strings.Contains(out, "security-validate-host") {
|
|
t.Errorf("generic actions must be hidden:\n%s", out)
|
|
}
|
|
p = posture(ph("alpha", "low", 2, false, `"disk_space_low"`, `"security-audit-host"`))
|
|
_, out = mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if strings.Contains(out, "actions:") {
|
|
t.Errorf("no actions line when only generic actions exist:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsBackupFlaggedVersusRunning(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 3, false, `"disk_space_low"`, ``))
|
|
jobs := `{"status":"ok","host":"alpha","jobs":{` +
|
|
`"files":{"state":"failed","overdue":false,"reason":"disk full"},` +
|
|
`"home":{"state":"running","overdue":false},` +
|
|
`"logs":{"state":"ok","overdue":true},` +
|
|
`"media":{"state":"ok","overdue":false}}}`
|
|
code, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, jobs), false, "")
|
|
if code != 1 {
|
|
t.Errorf("exit %d", code)
|
|
}
|
|
if !strings.Contains(out, " backup files failed reason=disk full\n") || !strings.Contains(out, " backup logs ok OVERDUE\n") {
|
|
t.Errorf("flagged jobs missing:\n%s", out)
|
|
}
|
|
if strings.Contains(out, "backup home") || strings.Contains(out, "backup media") {
|
|
t.Errorf("running or ok job shown:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsStaleHostLine(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 0, false, ``, ``) + "," + ph("gamma", "stale_audit", 3, true, `"disk_space_low"`, `"security-harden-host"`))
|
|
code, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if code != 1 {
|
|
t.Errorf("stale host must exit 1, got %d", code)
|
|
}
|
|
if !strings.Contains(out, "gamma STALE last_report=2026-10-01T18:51:56Z\n") {
|
|
t.Errorf("stale line:\n%s", out)
|
|
}
|
|
if strings.Contains(out, "disk_space_low") || strings.Contains(out, "security-harden-host") {
|
|
t.Errorf("stale host must not list flags or actions:\n%s", out)
|
|
}
|
|
_, js := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), true, "")
|
|
if strings.Contains(js, "disk_space_low") || strings.Contains(js, "security-harden-host") {
|
|
t.Errorf("stale host must not carry flags or actions in the model:\n%s", js)
|
|
}
|
|
}
|
|
|
|
func TestFindingsDegradeOn404(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 2, false, `"disk_space_low"`, ``))
|
|
for _, missing := range []string{"/v1/alerts", "/v1/queue", "/v1/backups"} {
|
|
r := routes(p, alertsEmpty, queueEmpty, backupsNone)
|
|
delete(r, missing)
|
|
code, out, err := runFindings(t, r, false, "")
|
|
if err != nil {
|
|
t.Fatalf("%s 404 must not fail the command: %v", missing, err)
|
|
}
|
|
if code != 1 || !strings.Contains(out, " disk_space_low") {
|
|
t.Errorf("%s: code=%d out:\n%s", missing, code, out)
|
|
}
|
|
if strings.Count(out, "note: ") != 1 || !strings.Contains(out, "note: "+strings.TrimPrefix(missing, "/v1/")+":") {
|
|
t.Errorf("%s: want exactly one note line:\n%s", missing, out)
|
|
}
|
|
}
|
|
_, js, _ := runFindings(t, fx{"/v1/posture": p}, true, "")
|
|
if !strings.Contains(js, `"notes": [`) || strings.Count(js, "does not have") != 3 {
|
|
t.Errorf("json notes:\n%s", js)
|
|
}
|
|
}
|
|
|
|
func TestFindingsPosture404IsTransport(t *testing.T) {
|
|
_, _, err := runFindings(t, fx{}, false, "")
|
|
if err == nil || fleet.ExitCodeFor(err) != fleet.ExitTransport {
|
|
t.Fatalf("err=%v", err)
|
|
}
|
|
if want := "posture: the fleet API does not have /v1/posture yet (update the controller)"; err.Error() != want {
|
|
t.Errorf("message %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestFindingsHostFilterAndUnknownHost(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 2, false, `"disk_space_low"`, ``) + "," + ph("beta", "low", 2, false, `"ci_runner_down"`, ``))
|
|
_, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "beta")
|
|
if !strings.Contains(out, "beta low") || strings.Contains(out, "alpha") || strings.Contains(out, "disk_space_low") {
|
|
t.Errorf("--host beta:\n%s", out)
|
|
}
|
|
_, _, err := runFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "nope")
|
|
if err == nil || fleet.ExitCodeFor(err) != fleet.ExitConfig {
|
|
t.Errorf("unknown host: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestFindingsExitZeroWhenClean(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 0, false, ``, `"security-audit-host"`) + "," + ph("beta", "low", 0, false, ``, ``))
|
|
code, out := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), false, "")
|
|
if code != 0 {
|
|
t.Errorf("exit %d\n%s", code, out)
|
|
}
|
|
}
|
|
|
|
func TestFindingsJSONKeys(t *testing.T) {
|
|
p := posture(ph("alpha", "low", 3, false, `"third_party_updates_pending","disk_space_low"`, `"security-harden-host"`) + "," +
|
|
ph("beta", "low", 3, false, `"third_party_updates_pending"`, ``))
|
|
_, js := mustFindings(t, routes(p, alertsEmpty, queueEmpty, backupsNone), true, "")
|
|
var got map[string]any
|
|
if err := json.Unmarshal([]byte(js), &got); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(js, "null") {
|
|
t.Errorf("no null values expected:\n%s", js)
|
|
}
|
|
for _, k := range []string{"generated_at", "fleet_wide", "hosts", "notes"} {
|
|
if _, ok := got[k]; !ok {
|
|
t.Errorf("missing top-level key %s", k)
|
|
}
|
|
}
|
|
host := got["hosts"].([]any)[0].(map[string]any)
|
|
for _, k := range []string{"host", "severity", "score", "stale", "last_report_at", "flags", "actions", "backups"} {
|
|
if _, ok := host[k]; !ok {
|
|
t.Errorf("missing host key %s", k)
|
|
}
|
|
}
|
|
flag := host["flags"].([]any)[0].(map[string]any)
|
|
for _, k := range []string{"flag", "weight", "hint"} {
|
|
if _, ok := flag[k]; !ok {
|
|
t.Errorf("missing flag key %s", k)
|
|
}
|
|
}
|
|
wide := got["fleet_wide"].([]any)[0].(map[string]any)
|
|
for _, k := range []string{"flag", "weight", "hosts", "hint"} {
|
|
if _, ok := wide[k]; !ok {
|
|
t.Errorf("missing fleet_wide key %s", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHumanDuration(t *testing.T) {
|
|
cases := map[int64]string{0: "0m", 300: "5m", 11520: "3h12m", 187200: "2d4h", -5: "0m"}
|
|
for in, want := range cases {
|
|
if got := fleet.HumanDuration(in); got != want {
|
|
t.Errorf("%d: %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFlagWeightAndHint(t *testing.T) {
|
|
if fleet.FlagWeight("backup_failed:nightly") != 3 || fleet.FlagWeight("nope") != 1 || fleet.FlagWeight("extra_uid0_accounts_present") != 5 {
|
|
t.Error("weights")
|
|
}
|
|
if fleet.FlagHint("compose_restart_loop") == "see the host report" || fleet.FlagHint("nope") != "see the host report" {
|
|
t.Error("hints")
|
|
}
|
|
}
|