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.
138 lines
3.5 KiB
Go
138 lines
3.5 KiB
Go
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package fleet
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/url"
|
|
"strconv"
|
|
)
|
|
|
|
// edrSeverityOrder is the display order of the severity counts; other keys are ignored.
|
|
var edrSeverityOrder = []string{"critical", "high", "medium", "low", "unknown"}
|
|
|
|
// edrTopLimit bounds the finding lines printed per host (the API sends at most this many).
|
|
const edrTopLimit = 5
|
|
|
|
// Edr is GET /v1/edr?host=. An older controller answers 404; that becomes a clear transport error.
|
|
func (c *Client) Edr(ctx context.Context, host string) (*Envelope, error) {
|
|
env, err := c.Get(ctx, "/v1/edr", url.Values{"host": {host}})
|
|
if isNotFound(err) {
|
|
return nil, &TransportError{"edr: the fleet API does not have /v1/edr yet (update the controller)"}
|
|
}
|
|
return env, err
|
|
}
|
|
|
|
func edrNumber(v any) string {
|
|
switch t := v.(type) {
|
|
case json.Number:
|
|
if f, err := t.Float64(); err == nil {
|
|
return strconv.FormatFloat(f, 'f', -1, 64)
|
|
}
|
|
return t.String()
|
|
case float64:
|
|
return strconv.FormatFloat(t, 'f', -1, 64)
|
|
}
|
|
return "-"
|
|
}
|
|
|
|
func edrCount(v any) int64 {
|
|
if n, ok := v.(json.Number); ok {
|
|
if i, err := n.Int64(); err == nil {
|
|
return i
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// edrFlagged reports a host that deserves exit 1: stale rules, a status other than ok, or a high/critical finding.
|
|
func edrFlagged(host map[string]any) bool {
|
|
if stale, _ := host["rules_stale"].(bool); stale {
|
|
return true
|
|
}
|
|
if status, _ := host["status"].(string); status != "ok" {
|
|
return true
|
|
}
|
|
by := obj(host["findings_by_severity"])
|
|
return edrCount(by["high"]) > 0 || edrCount(by["critical"]) > 0
|
|
}
|
|
|
|
func edrSeverityCounts(host map[string]any) string {
|
|
by := obj(host["findings_by_severity"])
|
|
out := ""
|
|
for _, sev := range edrSeverityOrder {
|
|
if n := edrCount(by[sev]); n > 0 {
|
|
if out != "" {
|
|
out += ", "
|
|
}
|
|
out += fmt.Sprintf("%d %s", n, sev)
|
|
}
|
|
}
|
|
if out == "" {
|
|
return "none"
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Edr fetches /v1/edr and prints one block per host. Exit 1 when any host has stale rules, a status other than ok,
|
|
// or a high or critical finding; 0 otherwise.
|
|
func Edr(ctx context.Context, c *Client, w io.Writer, asJSON bool, host string) (int, error) {
|
|
env, err := c.Edr(ctx, host)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
data := obj(env.Raw["data"])
|
|
hosts := map[string]any{}
|
|
if status, _ := data["status"].(string); status == "ok" {
|
|
if m, ok := data["hosts"].(map[string]any); ok {
|
|
hosts = m
|
|
}
|
|
}
|
|
code := ExitOK
|
|
for _, h := range hosts {
|
|
if m, ok := h.(map[string]any); ok && edrFlagged(m) {
|
|
code = ExitDrift
|
|
}
|
|
}
|
|
if asJSON {
|
|
return code, WriteJSON(w, env.Raw)
|
|
}
|
|
if status, _ := data["status"].(string); status != "ok" {
|
|
if status == "" {
|
|
status = "unavailable"
|
|
}
|
|
fmt.Fprintf(w, "edr %s\n", status)
|
|
return ExitOK, nil
|
|
}
|
|
if len(hosts) == 0 {
|
|
fmt.Fprintln(w, "edr no hosts")
|
|
return ExitOK, nil
|
|
}
|
|
for _, name := range sortedKeys(hosts) {
|
|
h := obj(hosts[name])
|
|
feed := "ok"
|
|
if stale, _ := h["rules_stale"].(bool); stale {
|
|
feed = "stale"
|
|
} else if ok, isBool := h["feed_ok"].(bool); isBool && !ok {
|
|
feed = "stale"
|
|
}
|
|
fmt.Fprintf(w, "%s strawberry %s %s rules %sh feed %s findings %s (%s)\n", name, dash(h["version"]),
|
|
dash(h["status"]), edrNumber(h["rules_age_h"]), feed, dash(h["findings_total"]), edrSeverityCounts(h))
|
|
top, _ := h["top"].([]any)
|
|
for i, it := range top {
|
|
if i >= edrTopLimit {
|
|
break
|
|
}
|
|
f := obj(it)
|
|
seen := "-"
|
|
if s, _ := f["last_seen"].(string); s != "" {
|
|
seen = s
|
|
}
|
|
fmt.Fprintf(w, " %s %s last_seen %s\n", dash(f["severity"]), dash(f["title"]), seen)
|
|
}
|
|
}
|
|
return code, nil
|
|
}
|