Files
urutau/internal/server/api_egress_test.go
T
Lucas RayanandClaude Sonnet 5.5 0db50c05c4 fix(ip): show egress destination view instead of ambiguous 404
Add GET /api/v1/ip/{ip}/egress and Engine.Dst; the IP page renders who
connected, findings and linked incidents, or a definitive never-seen message.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 20:22:20 -03:00

112 lines
4.3 KiB
Go

package server
import (
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents"
"net/netip"
"path/filepath"
"strconv"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func TestEgressAPI(t *testing.T) {
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
h := New(Options{Token: userTok, Version: "t", Store: st})
auth := map[string]string{"Authorization": "Bearer " + userTok}
SetEgress(nil)
d := getJSON(t, h, "/api/v1/egress?range=24h", auth)
if d["enabled"] != false || d["findings"] == nil || d["top_dst"] == nil {
t.Fatalf("disabled: %v", d)
}
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()
ev := func(at time.Time, src, dst string, port uint16) egress.Event {
return egress.Event{Time: at, Src: netip.MustParseAddr(src), Dst: netip.MustParseAddr(dst), Proto: "tcp", DPort: port, In: "br-1"}
}
e.IngestEvent(ev(now.Add(-time.Hour), "10.0.3.5", "203.0.113.3", 6667))
e.IngestEvent(ev(now.Add(-time.Hour), "10.0.3.5", "203.0.113.4", 443))
for i := 0; i < 80; i++ {
e.IngestEvent(ev(now.Add(-30*time.Minute+time.Duration(i)*time.Second), "10.0.5.2", "198.18.0."+strconv.Itoa(i+1), 80))
}
d = getJSON(t, h, "/api/v1/egress?range=24h", auth)
if d["enabled"] != true || d["source"].(map[string]any)["state"] == nil {
t.Fatalf("enabled: %v", d)
}
var port map[string]any
for _, f := range d["findings"].([]any) {
if m := f.(map[string]any); m["kind"] == "port" {
port = m
}
}
if port == nil || port["severity"] != "medium" || port["dport"].(float64) != 6667 || port["workload"] != "container 10.0.3.5" {
t.Fatalf("findings: %v", d["findings"])
}
if len(d["scanner"].(map[string]any)["bursts"].([]any)) != 1 || len(d["workloads"].([]any)) != 2 {
t.Fatalf("scanner/workloads: %v", d)
}
if tl := d["timeline"].(map[string]any); tl["step"].(float64) != 3600 || len(tl["series"].(map[string]any)) != 2 {
t.Fatalf("timeline: %v", tl)
}
if w := do(h, "GET", "/api/v1/egress?range=bogus", cloneHdr(auth), nil); w.Code != 400 {
t.Fatalf("range: %d", w.Code)
}
if w := do(h, "GET", "/api/v1/egress?host=nope", cloneHdr(auth), nil); w.Code != 404 {
t.Fatalf("host: %d", w.Code)
}
if w := do(h, "GET", "/api/v1/egress?range=24h", nil, nil); w.Code != 401 {
t.Fatalf("auth: %d", w.Code)
}
if w := do(h, "GET", "/api/v1/egress?range=24h", map[string]string{"Authorization": "Bearer peer-secret"}, nil); w.Code == 200 {
t.Fatalf("peer token must not open the user route")
}
}
func TestIPEgressAPI(t *testing.T) {
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)
}
h := New(Options{Token: userTok, Version: "t", Store: st, Incidents: inc})
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.5.2"), Dst: netip.MustParseAddr("203.0.113.7"), Proto: "tcp", DPort: 6667, In: "br-1"})
inc.Sync([]incidents.Finding{{Host: "oracle", Kind: "beacon", Severity: "high", Workload: "ciberlab", Dst: "203.0.113.7", Proto: "tcp", DPort: 6667, First: now.Add(-time.Hour).Unix(), Last: now.Unix(), Count: 9}})
d := getJSON(t, h, "/api/v1/ip/203.0.113.7/egress?range=24h", auth)
eg := d["egress"].([]any)
if len(eg) != 1 || eg[0].(map[string]any)["host"] != "oracle" || eg[0].(map[string]any)["conns"].(float64) != 1 {
t.Fatalf("egress: %v", d)
}
ics := d["incidents"].([]any)
if len(ics) != 1 || ics[0].(map[string]any)["id"] != "oracle/INC-1" {
t.Fatalf("incidents: %v", d["incidents"])
}
if d["last_seen_inbound"].(float64) != 0 {
t.Fatalf("inbound: %v", d)
}
d = getJSON(t, h, "/api/v1/ip/203.0.113.99/egress?range=24h", auth)
if len(d["egress"].([]any)) != 0 || len(d["incidents"].([]any)) != 0 || len(d["findings"].([]any)) != 0 {
t.Fatalf("empty: %v", d)
}
if w := do(h, "GET", "/api/v1/ip/nope/egress", cloneHdr(auth), nil); w.Code != 400 {
t.Fatalf("bad ip: %d", w.Code)
}
}