feat: 1.5.0 incident management (tickets from egress findings, triage, comments, suppression with expiry)
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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lucas RayanandClaude Opus 5.5 committed 2026-11-15 09:18:06 -03:00
1 parent 3ae72e27d6
commit e5058968e2
22 files changed
+2651 -20

No files matched your search

File diff suppressed because it is too large. Load diff
+311
View File
@@ -0,0 +1,311 @@
package incidents
import (
"errors"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
)
func fnd(sev string, last int64, count int) Finding {
return Finding{Host: "oracle", Kind: "port", Severity: sev, Workload: "container 10.0.3.5", Dst: "203.0.113.3", DPort: 6667, Proto: "tcp",
Detail: "irc", First: 100, Last: last, Count: count}
}
func newStore(t *testing.T) (*Store, string, *int64) {
p := filepath.Join(t.TempDir(), "incidents.json")
s, err := Open(p, nil)
if err != nil {
t.Fatal(err)
}
clock := int64(1000)
s.now = func() time.Time { return time.Unix(clock, 0) }
return s, p, &clock
}
func str(v string) *string { return &v }
func TestPersistenceAndCorrupt(t *testing.T) {
s, p, _ := newStore(t)
s.Sync([]Finding{fnd("medium", 500, 3)})
if _, err := s.AddComment("INC-1", "visto", "ana", ""); err != nil {
t.Fatal(err)
}
st, err := os.Stat(p)
if err != nil || st.Mode().Perm() != 0o600 {
t.Fatalf("file: %v %v", st, err)
}
if _, err := os.Stat(p + ".tmp"); err == nil {
t.Fatal("tmp left behind")
}
s2, _ := Open(p, nil)
tk, err := s2.Get("INC-1")
if err != nil || tk.Count != 3 || len(tk.Comments) != 1 || tk.History[0].Field != "created" || tk.History[0].Author != "urutau" {
t.Fatalf("reload: %+v %v", tk, err)
}
if m, _ := s2.Create(NewManual{Title: "x"}); m.ID != "INC-2" {
t.Fatalf("sequence after reload: %s", m.ID)
}
os.WriteFile(p, []byte("{not json"), 0o600)
s3, err := Open(p, nil)
if err != nil || s3.List(Query{}).Total != 0 {
t.Fatalf("corrupt store not empty: %v", err)
}
if m, _ := filepath.Glob(p + ".corrupt-*"); len(m) != 1 {
t.Fatal("no timestamped .corrupt file")
}
// a read error that is not "does not exist" aborts
dir := t.TempDir()
if _, err := Open(dir, nil); err == nil {
t.Fatal("unreadable store accepted")
}
}
func TestSync(t *testing.T) {
s, _, clock := newStore(t)
if c, _ := s.Sync([]Finding{fnd("info", 500, 1)}); c != 0 {
t.Fatal("info created a ticket")
}
if c, _ := s.Sync([]Finding{fnd("medium", 500, 3)}); c != 1 {
t.Fatal("not created")
}
s.Sync([]Finding{fnd("high", 600, 5)})
tk, _ := s.Get("INC-1")
if tk.Status != "open" || tk.Count != 5 || tk.LastSeen != 600 || tk.Severity != "high" || tk.MaxSeverity != "high" || len(s.byID) != 1 {
t.Fatalf("update: %+v", tk)
}
// resolved, finding older than the resolution: stays resolved; newer: reopens
*clock = 2000
s.Update("INC-1", Patch{Status: str("resolved"), Author: "ana"})
s.Sync([]Finding{fnd("medium", 1500, 5)})
if tk, _ = s.Get("INC-1"); tk.Status != "resolved" {
t.Fatal("reopened by an old finding")
}
*clock = 3000
if _, r := s.Sync([]Finding{fnd("medium", 2500, 6)}); r != 1 {
t.Fatal("not reopened")
}
tk, _ = s.Get("INC-1")
last := tk.History[len(tk.History)-1]
if tk.Status != "open" || last.Field != "reappeared" {
t.Fatalf("reopen: %+v", tk.History)
}
// false positive is never reopened and only counts
*clock = 4000
s.Update("INC-1", Patch{Status: str("false_positive")})
s.Sync([]Finding{fnd("high", 3500, 9)})
tk, _ = s.Get("INC-1")
if tk.Status != "false_positive" || tk.Count != 9 {
t.Fatalf("fp: %+v", tk)
}
// leaving the status drops the suppression
s.Update("INC-1", Patch{Status: str("open")})
if tk, _ = s.Get("INC-1"); tk.Suppress {
t.Fatal("suppress kept")
}
if _, err := s.Update("INC-1", Patch{Suppress: &[]bool{true}[0]}); !errors.Is(err, ErrConflict) {
t.Fatal("suppress allowed in open")
}
}
func TestLimits(t *testing.T) {
s, _, clock := newStore(t)
s.Create(NewManual{Title: "a"})
for i := 0; i < MaxComments; i++ {
if _, err := s.AddComment("INC-1", "c", "", ""); err != nil {
t.Fatal(err)
}
}
if _, err := s.AddComment("INC-1", "c", "", ""); err == nil {
t.Fatal("201st comment accepted")
}
for i := 0; i < MaxHistory+20; i++ {
*clock++
st := "investigating"
if i%2 == 1 {
st = "open"
}
s.Update("INC-1", Patch{Status: &st})
}
if tk, _ := s.Get("INC-1"); len(tk.History) != MaxHistory {
t.Fatalf("history %d", len(tk.History))
}
// ticket cap: oldest closed goes first, open tickets are kept
s2, _, c2 := newStore(t)
for i := 0; i < MaxTickets; i++ {
*c2++
s2.Create(NewManual{Title: "t"})
s2.save() // keep the test honest about cost? no: cheap enough
if i > 50 {
break
}
}
s2.mu.Lock()
for i := len(s2.byID); i < MaxTickets; i++ {
s2.add(&Ticket{base: base{Key: "k" + string(rune(i)) + "x", Status: "open", UpdatedAt: 5000}})
}
s2.mu.Unlock()
s2.Update("INC-3", Patch{Status: str("resolved")})
s2.Update("INC-2", Patch{Status: str("benign"), Verdict: str("kept")}) // a verdict protects it from eviction
if tk, err := s2.Create(NewManual{Title: "new"}); err != nil || len(s2.byID) != MaxTickets {
t.Fatalf("create at cap: %v %d", err, len(s2.byID))
} else if _, err := s2.Get("INC-3"); err == nil && tk.ID != "" {
t.Fatal("INC-3 not evicted")
}
}
func TestValidation(t *testing.T) {
s, _, _ := newStore(t)
s.Create(NewManual{Title: "a"})
bad := []Patch{
{Status: str("nope")}, {Severity: str("urgent")}, {Title: str("")}, {Title: str(strings.Repeat("a", 201))},
{Verdict: str(strings.Repeat("a", 4001))}, {Verdict: str("a\x00b")}, {Assignee: str(strings.Repeat("a", 65))},
{Tags: &[]string{"UP"}}, {Tags: &[]string{"a b"}}, {Tags: &[]string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11"}},
{Author: strings.Repeat("a", 65)}, {Author: "a\nb"},
}
for i, p := range bad {
if _, err := s.Update("INC-1", p); err == nil {
t.Errorf("patch %d accepted", i)
}
}
if _, err := s.Update("INC-9", Patch{}); err != ErrNotFound {
t.Fatal("404 expected")
}
if tk, err := s.Update("INC-1", Patch{Verdict: str("a\nb\tc"), Tags: &[]string{"ok-1", "x.y_z"}}); err != nil || tk.Verdict != "a\nb\tc" {
t.Fatalf("good patch: %v", err)
}
if _, err := s.AddComment("INC-1", strings.Repeat("a", 5001), "", ""); err == nil {
t.Fatal("long comment accepted")
}
}
func TestListFilters(t *testing.T) {
s, _, _ := newStore(t)
s.Sync([]Finding{fnd("medium", 500, 3), {Host: "h2", Kind: "bad_dst", Severity: "high", Workload: "host", Dst: "198.51.100.9", DPort: 22, Proto: "tcp", Last: 700, Count: 1}})
s.Update("INC-2", Patch{Status: str("investigating")})
pg := s.List(Query{Status: []string{"investigating"}, Sort: "severity"})
if pg.Total != 1 || pg.Counts["open"] != 1 || pg.Counts["investigating"] != 1 || pg.Items[0].ID != "INC-2" {
t.Fatalf("%+v", pg)
}
if s.List(Query{Host: "oracle"}).Total != 1 || s.List(Query{Q: "BAD_DST"}).Total != 1 || s.List(Query{Severity: []string{"high"}}).Total != 1 {
t.Fatal("filters")
}
}
func TestSuppressionPolicy(t *testing.T) {
s, _, clock := newStore(t)
yes := true
s.Sync([]Finding{fnd("medium", 500, 3)})
*clock = 2000
if _, err := s.Update("INC-1", Patch{Status: str("false_positive"), Suppress: &yes, SuppressUntil: &[]int64{2000 + 91*86400}[0]}); !errors.Is(err, ErrInvalid) {
t.Fatalf("91 days accepted: %v", err)
}
tk, err := s.Update("INC-1", Patch{Status: str("false_positive"), Suppress: &yes, Author: "ana"})
if err != nil || !tk.Suppress || tk.SuppressedSev != "medium" || tk.SuppressUntil != 2000+30*86400 {
t.Fatalf("suppress: %+v %v", tk, err)
}
if tk.History[len(tk.History)-1].Author != "ana" {
t.Fatal("author")
}
// same severity: stays suppressed; worse severity: reopens
s.Sync([]Finding{fnd("medium", 2100, 4)})
if tk, _ = s.Get("INC-1"); !tk.Suppress || tk.Status != "false_positive" {
t.Fatal("reopened without cause")
}
s.Sync([]Finding{fnd("high", 2200, 4)})
tk, _ = s.Get("INC-1")
if tk.Suppress || tk.Status != "open" || tk.Severity != "high" || tk.History[len(tk.History)-2].Field != "reappeared" {
t.Fatalf("worse severity: %+v", tk)
}
// high/critical can never be suppressed
if _, err := s.Update("INC-1", Patch{Status: str("benign"), Suppress: &yes}); !errors.Is(err, ErrConflict) {
t.Fatalf("high suppressed: %v", err)
}
// TTL
s2, _, c2 := newStore(t)
s2.Sync([]Finding{fnd("low", 500, 1)})
*c2 = 5000
until := int64(5000 + 86400)
s2.Update("INC-1", Patch{Status: str("benign"), Suppress: &yes, SuppressUntil: &until})
*c2 = 5000 + 2*86400
s2.Sync([]Finding{fnd("low", *c2, 2)})
if tk, _ = s2.Get("INC-1"); tk.Suppress || tk.Status != "open" || tk.History[len(tk.History)-1].Field != "suppression expired" {
t.Fatalf("ttl: %+v", tk)
}
}
func TestSeverityFollowsFinding(t *testing.T) {
s, _, _ := newStore(t)
s.Sync([]Finding{fnd("low", 500, 1)})
s.Sync([]Finding{fnd("high", 600, 1)})
if tk, _ := s.Get("INC-1"); tk.Severity != "high" || tk.History[len(tk.History)-1].Field != "severity" {
t.Fatalf("%+v", tk)
}
s.Create(NewManual{Title: "m"})
s.Sync([]Finding{{Host: "h", Kind: "port", Severity: "low", Workload: "w", Dst: "1.1.1.1", DPort: 1, Proto: "tcp", Last: 1}})
s.Update("INC-3", Patch{Severity: str("critical")})
s.Sync([]Finding{{Host: "h", Kind: "port", Severity: "high", Workload: "w", Dst: "1.1.1.1", DPort: 1, Proto: "tcp", Last: 2}})
if tk, _ := s.Get("INC-3"); tk.Severity != "critical" || !tk.SeverityManual {
t.Fatalf("manual severity overwritten: %+v", tk)
}
}
func TestFloodQuota(t *testing.T) {
s, _, _ := newStore(t)
var fs []Finding
for i := 0; i < HostQuota+20; i++ {
fs = append(fs, Finding{Host: "h", Kind: "port", Severity: "low", Workload: "w", Dst: "10.0." + strconv.Itoa(i/250) + "." + strconv.Itoa(i%250), DPort: 80, Proto: "tcp", Last: int64(i)})
}
s.Sync(fs)
s.Sync(fs) // idempotent
fs = append(fs, Finding{Host: "h", Kind: "bad_dst", Severity: "high", Workload: "w", Dst: "9.9.9.9", DPort: 22, Proto: "tcp", Last: 5})
s.Sync(fs)
pg := s.List(Query{Host: "h"})
if pg.Total != HostQuota+2 { // quota + flood + the high one, never grouped
t.Fatalf("total %d", pg.Total)
}
var flood *Summary
for i := range pg.Items {
if pg.Items[i].Kind == "flood" {
flood = &pg.Items[i]
}
}
if flood == nil || flood.Count != 20 || !strings.Contains(flood.Detail, "port 10.0.") {
t.Fatalf("flood: %+v", flood)
}
// other hosts are unaffected
s.Sync([]Finding{{Host: "other", Kind: "port", Severity: "low", Workload: "w", Dst: "1.1.1.1", DPort: 1, Proto: "tcp", Last: 1}})
if s.List(Query{Host: "other"}).Total != 1 {
t.Fatal("other host grouped")
}
}
func TestConflictAndKeyMigration(t *testing.T) {
s, p, _ := newStore(t)
s.Sync([]Finding{fnd("medium", 500, 1)})
tk, _ := s.Get("INC-1")
stale := tk.UpdatedAt - 1
if _, err := s.Update("INC-1", Patch{Verdict: str("x"), ExpectedUpdatedAt: &stale}); !errors.Is(err, ErrConflict) {
t.Fatalf("stale write: %v", err)
}
if _, err := s.Update("INC-1", Patch{Verdict: str("x"), ExpectedUpdatedAt: &tk.UpdatedAt, Via: "token"}); err != nil {
t.Fatal(err)
}
if tk, _ = s.Get("INC-1"); tk.History[len(tk.History)-1].Author != "operator (via token)" {
t.Fatalf("author: %+v", tk.History)
}
if _, err := s.Update("INC-1", Patch{Title: str("a\nb")}); err == nil {
t.Fatal("newline in title")
}
// an old-format key is recomputed on load
b, _ := os.ReadFile(p)
old := strings.Replace(string(b), tk.Key, "oracle|port|old", 1)
os.WriteFile(p, []byte(old), 0o600)
s2, _ := Open(p, nil)
if r := s2.Lookup([]string{fnd("medium", 1, 1).Key()}); len(r) != 1 {
t.Fatal("key not migrated")
}
}