Files
urutau/internal/server/api_egress_test.go
T
Lucas RayanandClaude Opus 5.5 c33cca22e3
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 16s
CI / Tests (Linux, race detector) (push) Successful in 1m3s
Release / Plan (reuse the published image?) (push) Successful in 4s
Release / ci (push) Skipped
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Successful in 13s
Release / Gitea release (push) Successful in 3s
feat: 1.4.0 second firewall, egress analysis and HTML report
Optional second firewall (URUTAU_ENFORCE off/observe/on) through the host
fail2ban, outbound traffic analysis (beaconing, mining, spam, DNS bypass,
unauthorised scans) with authorised scanner workloads, host kit in
deploy/host and a printable HTML report.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 23:35:34 -03:00

100 lines
3.5 KiB
Go

package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/netip"
"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")
}
// peer endpoint (registered by the daemon next to the other peer routes)
s := &Server{opt: Options{PeerTokens: []string{"peer-secret"}, Token: userTok, Store: st, Now: time.Now}, ses: newSessions(), lim: newLimiter()}
m := http.NewServeMux()
m.HandleFunc("GET /api/v1/peer/egress", s.peerEgress)
call := func(tok string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "/api/v1/peer/egress?range=24h", nil)
r.RemoteAddr = "198.51.100.1:1234"
if tok != "" {
r.Header.Set("Authorization", "Bearer "+tok)
}
w := httptest.NewRecorder()
m.ServeHTTP(w, r)
return w
}
if w := call(""); w.Code != 401 {
t.Fatalf("peer no token: %d", w.Code)
}
if w := call(userTok); w.Code != 401 {
t.Fatalf("user token on peer route: %d", w.Code)
}
w := call("peer-secret")
var rep egress.Report
if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &rep) != nil || len(rep.Findings) == 0 {
t.Fatalf("peer: %d %s", w.Code, w.Body)
}
}