All checks were successful
release / build (push) Successful in 4m8s
One block per host: version, status, rule age, feed state and finding counts, then up to five findings. Exit 1 when rules are stale, status is not ok or a high/critical finding is open. A 404 says the controller needs the update (exit 3). edr_* hints in hub findings point here.
190 lines
7 KiB
Go
190 lines
7 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package fleet_test
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.trevon.dev/trevon/hub/internal/fleet"
|
|
)
|
|
|
|
const edrHead = `{"generated_at":"2026-11-14T10:00:00Z","snapshot_age_seconds":5,"stale":false,"coverage_complete":true,"data":`
|
|
|
|
// edrServer serves body for /v1/edr and records the host query parameter of the last request.
|
|
func edrServer(t *testing.T, body string, gotHost *string) *httptest.Server {
|
|
t.Helper()
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer "+secretToken {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
if r.URL.Path != "/v1/edr" {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
if gotHost != nil {
|
|
*gotHost = r.URL.Query().Get("host")
|
|
}
|
|
_, _ = w.Write([]byte(body))
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
return srv
|
|
}
|
|
|
|
func runEdr(t *testing.T, body string, asJSON bool, host string) (int, string) {
|
|
t.Helper()
|
|
srv := edrServer(t, body, nil)
|
|
var out bytes.Buffer
|
|
code, err := fleet.Edr(context.Background(), newClient(t, srv.URL, secretToken), &out, asJSON, host)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(out.String(), secretToken) {
|
|
t.Fatal("token in output")
|
|
}
|
|
return code, out.String()
|
|
}
|
|
|
|
func edrHosts(hosts string) string {
|
|
return edrHead + `{"status":"ok","source_mtime_utc":"2026-11-14T10:00:00Z","hosts":{` + hosts + `}}}`
|
|
}
|
|
|
|
const (
|
|
edrAlphaOK = `"alpha":{"enabled":true,"status":"ok","version":"0.2.5","daemon_ok":true,"rules_age_h":23.0,"feed_ok":true,` +
|
|
`"rules_stale":false,"findings_total":1,"findings_by_severity":{"low":1},` +
|
|
`"top":[{"id":"f-1","title":"Example finding","severity":"low","score":12,"last_seen":"2026-11-14T08:00:00Z"}]}`
|
|
edrBetaStale = `"beta":{"enabled":true,"status":"ok","version":"0.2.4","rules_age_h":70.5,"feed_ok":true,` +
|
|
`"rules_stale":true,"findings_total":0,"findings_by_severity":{}}`
|
|
edrGammaHigh = `"gamma":{"enabled":true,"status":"ok","version":"0.2.5","rules_age_h":2,"feed_ok":true,` +
|
|
`"rules_stale":false,"findings_total":2,"findings_by_severity":{"low":1,"high":1}}`
|
|
edrDeltaDown = `"delta":{"enabled":true,"status":"error","daemon_ok":false}`
|
|
)
|
|
|
|
func TestEdrHealthyHostBlockAndExit(t *testing.T) {
|
|
code, out := runEdr(t, edrHosts(edrAlphaOK), false, "")
|
|
want := "alpha strawberry 0.2.5 ok rules 23h feed ok findings 1 (1 low)\n" +
|
|
" low Example finding last_seen 2026-11-14T08:00:00Z\n"
|
|
if code != 0 || out != want {
|
|
t.Errorf("code=%d out=%q want %q", code, out, want)
|
|
}
|
|
}
|
|
|
|
func TestEdrExitOneConditions(t *testing.T) {
|
|
cases := map[string]string{
|
|
"rules_stale": edrBetaStale,
|
|
"high finding": edrGammaHigh,
|
|
"status not ok": edrDeltaDown,
|
|
"critical": strings.Replace(edrGammaHigh, `"high":1`, `"critical":1`, 1),
|
|
"status missing": `"alpha":{"rules_stale":false,"findings_by_severity":{}}`,
|
|
"one of several": edrAlphaOK + "," + edrGammaHigh,
|
|
}
|
|
for name, hosts := range cases {
|
|
if code, out := runEdr(t, edrHosts(hosts), false, ""); code != 1 {
|
|
t.Errorf("%s: exit %d, want 1\n%s", name, code, out)
|
|
}
|
|
if code, _ := runEdr(t, edrHosts(hosts), true, ""); code != 1 {
|
|
t.Errorf("%s --json: exit %d, want 1", name, code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEdrFeedFailedShowsStaleButKeepsExit(t *testing.T) {
|
|
host := strings.Replace(edrAlphaOK, `"feed_ok":true`, `"feed_ok":false`, 1)
|
|
code, out := runEdr(t, edrHosts(host), false, "")
|
|
if !strings.Contains(out, "feed stale") || code != 0 {
|
|
t.Errorf("code=%d out=%q", code, out)
|
|
}
|
|
}
|
|
|
|
func TestEdrStaleLineAndSortedHosts(t *testing.T) {
|
|
_, out := runEdr(t, edrHosts(edrBetaStale+","+edrAlphaOK), false, "")
|
|
if !strings.HasPrefix(out, "alpha strawberry") || strings.Index(out, "alpha") > strings.Index(out, "beta") {
|
|
t.Errorf("hosts not sorted:\n%s", out)
|
|
}
|
|
if !strings.Contains(out, "beta strawberry 0.2.4 ok rules 70.5h feed stale findings 0 (none)\n") {
|
|
t.Errorf("stale line wrong:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestEdrSeverityCountOrderAndTopLimit(t *testing.T) {
|
|
var top []string
|
|
for i := 0; i < 8; i++ {
|
|
top = append(top, `{"id":"f","title":"T`+string(rune('0'+i))+`","severity":"low","last_seen":"2026-11-14T08:00:00Z"}`)
|
|
}
|
|
host := `"alpha":{"status":"ok","version":"0.2.5","rules_age_h":1,"findings_total":9,` +
|
|
`"findings_by_severity":{"low":5,"critical":1,"medium":3},"top":[` + strings.Join(top, ",") + `]}`
|
|
_, out := runEdr(t, edrHosts(host), false, "")
|
|
if !strings.Contains(out, "findings 9 (1 critical, 3 medium, 5 low)\n") {
|
|
t.Errorf("severity order wrong:\n%s", out)
|
|
}
|
|
if n := strings.Count(out, "\n low T"); n != 5 {
|
|
t.Errorf("printed %d finding lines, want 5:\n%s", n, out)
|
|
}
|
|
}
|
|
|
|
func TestEdrHostFilterIsSentToTheAPI(t *testing.T) {
|
|
var got string
|
|
srv := edrServer(t, edrHosts(edrAlphaOK), &got)
|
|
var out bytes.Buffer
|
|
if _, err := fleet.Edr(context.Background(), newClient(t, srv.URL, secretToken), &out, false, "alpha"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got != "alpha" {
|
|
t.Errorf("host query = %q, want alpha", got)
|
|
}
|
|
if _, err := fleet.Edr(context.Background(), newClient(t, srv.URL, secretToken), &out, false, ""); err != nil || got != "" {
|
|
t.Errorf("no filter must send no host param: %q %v", got, err)
|
|
}
|
|
}
|
|
|
|
func TestEdrNonOKAndEmpty(t *testing.T) {
|
|
if code, out := runEdr(t, edrHead+`{"status":"unavailable"}}`, false, ""); code != 0 || out != "edr unavailable\n" {
|
|
t.Errorf("unavailable: %d %q", code, out)
|
|
}
|
|
if code, out := runEdr(t, edrHead+`{"status":"error","reason":"oversize"}}`, false, ""); code != 0 || out != "edr error\n" {
|
|
t.Errorf("error: %d %q", code, out)
|
|
}
|
|
if code, out := runEdr(t, edrHosts(""), false, "nosuch"); code != 0 || out != "edr no hosts\n" {
|
|
t.Errorf("empty: %d %q", code, out)
|
|
}
|
|
// hosts of a non-ok section never decide the exit code
|
|
if code, _ := runEdr(t, edrHead+`{"status":"error","hosts":{`+edrDeltaDown+`}}}`, false, ""); code != 0 {
|
|
t.Errorf("non-ok section exit %d, want 0", code)
|
|
}
|
|
}
|
|
|
|
func TestEdrJSONPassthrough(t *testing.T) {
|
|
code, out := runEdr(t, edrHosts(edrAlphaOK), true, "")
|
|
if code != 0 || !strings.Contains(out, `"rules_age_h": 23.0`) || strings.Contains(out, "strawberry 0.2.5") {
|
|
t.Errorf("--json must print the raw envelope:\n%s", out)
|
|
}
|
|
}
|
|
|
|
func TestEdr404IsTransportWithMessage(t *testing.T) {
|
|
srv := httptest.NewServer((&api{}).handler())
|
|
defer srv.Close()
|
|
var out bytes.Buffer
|
|
code, err := fleet.Edr(context.Background(), newClient(t, srv.URL, secretToken), &out, false, "")
|
|
if err == nil {
|
|
t.Fatalf("expected error, got code %d out %q", code, out.String())
|
|
}
|
|
if fleet.ExitCodeFor(err) != fleet.ExitTransport {
|
|
t.Errorf("exit %d, want 3", fleet.ExitCodeFor(err))
|
|
}
|
|
if want := "edr: the fleet API does not have /v1/edr yet (update the controller)"; err.Error() != want {
|
|
t.Errorf("message %q", err.Error())
|
|
}
|
|
}
|
|
|
|
func TestEdrHintsPointAtHubEdr(t *testing.T) {
|
|
for _, flag := range []string{"edr_rules_stale", "edr_daemon_down", "edr_backend_degraded", "edr_finding_high", "edr_finding_critical"} {
|
|
if !strings.Contains(fleet.FlagHint(flag), "hub edr") {
|
|
t.Errorf("%s hint %q lacks hub edr", flag, fleet.FlagHint(flag))
|
|
}
|
|
}
|
|
}
|