Files
urutau/internal/server/api_incidents_test.go
T
Lucas RayanandClaude Opus 5.5 e5058968e2
Release / Publish tags (push) Successful in 14s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m14s
Release / Build image (amd64) (push) Successful in 24s
Release / Build image (arm64) (push) Successful in 1m13s
CI / Format and vet (push) Successful in 41s
CI / Tests (Linux, race detector) (push) Successful in 1m1s
CI / Dependency audit (push) Successful in 33s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 4s
feat: 1.5.0 incident management (tickets from egress findings, triage, comments, suppression with expiry)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 09:18:06 -03:00

170 lines
7.1 KiB
Go

package server
import (
"context"
"encoding/json"
"net/http"
"net/netip"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func incRig(t *testing.T) http.Handler {
t.Helper()
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
inc, err := incidents.Open(filepath.Join(t.TempDir(), "incidents.json"), nil)
if err != nil {
t.Fatal(err)
}
return New(Options{Token: userTok, PeerTokens: []string{peerTok}, Version: "t", Store: st, Incidents: inc})
}
func TestIncidentsEgressSuppression(t *testing.T) {
h := incRig(t)
auth := map[string]string{"Authorization": "Bearer " + userTok}
e, err := egress.New(egress.Config{Workloads: "ciberlab=10.0.5.0/24:scanner"})
if err != nil {
t.Fatal(err)
}
SetEgress(e)
defer SetEgress(nil)
now := time.Now()
e.IngestEvent(egress.Event{Time: now.Add(-time.Hour), Src: netip.MustParseAddr("10.0.3.5"), Dst: netip.MustParseAddr("203.0.113.3"), Proto: "tcp", DPort: 6667, In: "br-1"})
d := getJSON(t, h, "/api/v1/egress?range=24h", auth)
if w := req(h, "GET", "/api/v1/incidents", ``, auth); !strings.Contains(w.Body.String(), `"total":0`) {
t.Fatalf("a request fed the incidents: %s", w.Body)
}
incidentServer.Load().syncNow(context.Background())
d = getJSON(t, h, "/api/v1/egress?range=24h", auth)
var f map[string]any
for _, x := range d["findings"].([]any) {
if m := x.(map[string]any); m["kind"] == "port" {
f = m
}
}
if f == nil || f["ticket_id"] != "INC-1" || f["ticket_status"] != "open" || f["suppressed"] != false || d["suppressed_count"].(float64) != 0 {
t.Fatalf("annotated: %v", d)
}
// suppression needs false_positive/benign
if w := do(h, "GET", "/api/v1/incidents", cloneHdr(auth), nil); w.Code != 200 || !strings.Contains(w.Body.String(), `"INC-1"`) || strings.Contains(w.Body.String(), `"history"`) || !strings.Contains(w.Body.String(), `"comment_count"`) {
t.Fatalf("list: %d %s", w.Code, w.Body)
}
if w := req(h, "PATCH", "/api/v1/incidents/INC-1", `{"suppress":true}`, auth); w.Code != 409 {
t.Fatalf("suppress in open: %d", w.Code)
}
if w := req(h, "PATCH", "/api/v1/incidents/INC-1", `{"status":"false_positive","suppress":true,"author":"ana"}`, auth); w.Code != 200 {
t.Fatalf("patch: %d %s", w.Code, w.Body)
}
incidentServer.Load().syncNow(context.Background())
d = getJSON(t, h, "/api/v1/egress?range=24h", auth)
for _, x := range d["findings"].([]any) {
if x.(map[string]any)["kind"] == "port" {
t.Fatalf("suppressed finding still listed: %v", x)
}
}
if d["suppressed_count"].(float64) != 1 {
t.Fatalf("suppressed_count: %v", d["suppressed_count"])
}
d = getJSON(t, h, "/api/v1/egress?range=24h&suppressed=1", auth)
shown := false
for _, x := range d["findings"].([]any) {
if m := x.(map[string]any); m["kind"] == "port" && m["suppressed"] == true && m["ticket_status"] == "false_positive" {
shown = true
}
}
if !shown {
t.Fatalf("?suppressed=1: %v", d["findings"])
}
// another destination is not covered by the exact key
e.IngestEvent(egress.Event{Time: now.Add(-time.Hour), Src: netip.MustParseAddr("10.0.3.5"), Dst: netip.MustParseAddr("203.0.113.4"), Proto: "tcp", DPort: 6667, In: "br-1"})
d = getJSON(t, h, "/api/v1/egress?range=24h", auth)
if len(d["findings"].([]any)) == 0 {
t.Fatal("a different key was suppressed")
}
}
func TestIncidentsAPI(t *testing.T) {
h := incRig(t)
auth := map[string]string{"Authorization": "Bearer " + userTok}
paths := [][2]string{{"GET", "/api/v1/incidents"}, {"GET", "/api/v1/incidents/INC-1"}, {"POST", "/api/v1/incidents"}, {"PATCH", "/api/v1/incidents/INC-1"}, {"POST", "/api/v1/incidents/INC-1/comments"}, {"GET", "/api/v1/incidents/export.csv"}}
for _, p := range paths {
if w := req(h, p[0], p[1], `{}`, nil); w.Code != 401 {
t.Errorf("%v no auth: %d", p, w.Code)
}
if w := req(h, p[0], p[1], `{}`, map[string]string{"Authorization": "Bearer " + peerTok}); w.Code != 403 {
t.Errorf("%v peer token: %d", p, w.Code)
}
}
cross := map[string]string{"Authorization": "Bearer " + userTok, "Sec-Fetch-Site": "cross-site"}
for _, p := range paths[2:5] {
if w := req(h, p[0], p[1], `{"title":"x"}`, cross); w.Code != 403 {
t.Errorf("%v cross-site: %d", p, w.Code)
}
}
w := req(h, "POST", "/api/v1/incidents", `{"title":"Servidor estranho","detail":"d","severity":"high","host":"oracle","tags":["a-b"],"author":"ana"}`, auth)
if w.Code != 201 {
t.Fatalf("create: %d %s", w.Code, w.Body)
}
var tk map[string]any
json.Unmarshal(w.Body.Bytes(), &tk)
if tk["id"] != "INC-1" || tk["source"] != "manual" || tk["status"] != "open" {
t.Fatalf("ticket: %v", tk)
}
bad := []struct{ m, p, b string }{
{"POST", "/api/v1/incidents", `nope`},
{"POST", "/api/v1/incidents", `{"title":"x","extra":1}`},
{"POST", "/api/v1/incidents", `{"title":""}`},
{"POST", "/api/v1/incidents", `{"title":"x","severity":"urgent"}`},
{"POST", "/api/v1/incidents", `{"title":"x"}{"title":"y"}`},
{"POST", "/api/v1/incidents", `{"title":"` + strings.Repeat("a", 70<<10) + `"}`},
{"PATCH", "/api/v1/incidents/INC-1", `{"status":"bogus"}`},
{"PATCH", "/api/v1/incidents/INC-1", `{"tags":["BAD"]}`},
{"PATCH", "/api/v1/incidents/INC-1", `{"title":"a\u0000b"}`},
{"PATCH", "/api/v1/incidents/INC-1", `{"id":"INC-2"}`},
{"POST", "/api/v1/incidents/INC-1/comments", `{"text":""}`},
{"POST", "/api/v1/incidents/INC-1/comments", `{"text":"` + strings.Repeat("a", 5001) + `"}`},
{"GET", "/api/v1/incidents?status=nope", ``},
{"GET", "/api/v1/incidents?sort=x", ``},
{"GET", "/api/v1/incidents?page=0", ``},
}
for _, c := range bad {
if w := req(h, c.m, c.p, c.b, auth); w.Code != 400 {
t.Errorf("%s %s %.40q: %d", c.m, c.p, c.b, w.Code)
}
}
for _, p := range [][2]string{{"GET", "/api/v1/incidents/INC-99"}, {"PATCH", "/api/v1/incidents/INC-99"}, {"POST", "/api/v1/incidents/INC-99/comments"}} {
body := `{"title":"x"}`
if strings.HasSuffix(p[1], "/comments") {
body = `{"text":"x"}`
}
if w := req(h, p[0], p[1], body, auth); w.Code != 404 {
t.Errorf("%v: %d", p, w.Code)
}
}
if w := req(h, "POST", "/api/v1/incidents/INC-1/comments", `{"text":"visto"}`, auth); w.Code != 201 || !strings.Contains(w.Body.String(), `operator (via token)`) {
t.Fatalf("comment: %d %s", w.Code, w.Body)
}
if w := req(h, "PATCH", "/api/v1/incidents/INC-1", `{"status":"resolved","verdict":"ok","assignee":"bia"}`, auth); w.Code != 200 {
t.Fatalf("patch: %d %s", w.Code, w.Body)
}
d := getJSON(t, h, "/api/v1/incidents/INC-1", auth)
if d["status"] != "resolved" || len(d["history"].([]any)) != 4 || len(d["comments"].([]any)) != 1 {
t.Fatalf("full: %v", d)
}
l := getJSON(t, h, "/api/v1/incidents?status=resolved,open&per_page=1&page="+strconv.Itoa(1), auth)
if l["total"].(float64) != 1 || l["counts"].(map[string]any)["resolved"].(float64) != 1 {
t.Fatalf("list: %v", l)
}
if w := req(h, "GET", "/api/v1/incidents/export.csv", ``, auth); w.Code != 200 || !strings.HasPrefix(w.Body.String(), "id,status") || !strings.Contains(w.Body.String(), "INC-1") {
t.Fatalf("csv: %d %s", w.Code, w.Body)
}
}