Files
urutau/internal/server/api_enforce_test.go
T
Lucas RayanandClaude Opus 5.5 8546d14cef
CI / Format and vet (pull_request) Failing after 53s
CI / Dependency audit (pull_request) Successful in 33s
CI / Tests (Linux, race detector) (pull_request) Successful in 2m1s
CI / Docker images (build and smoke test) (pull_request) Failing after 47s
feat!: Urutau 2.0.0, ultralight probes and a single analysis hub
Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that
tails the logs, ships raw lines to the hub over HTTPS with an on-disk
spool, and applies enforce decisions locally. The hub keeps one node per
probe (primary node = the 1.x data dir, unchanged), does all analysis and
serves the UI. Peer federation is removed. 'urutau import' brings 1.x
data dirs in with hash and count verification. UI: probes page, host
qualified incidents, IPs never wrap, egress heatmap labels, live and
replay globes no longer auto-rotate, replay repeat, live traffic metrics.

BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone;
log path variables move from the hub to the probe. See docs/migration-2.0.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 22:14:50 -03:00

116 lines
4.0 KiB
Go

package server
import (
"context"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
)
func enforceRig(t *testing.T, share bool) (http.Handler, *enforce.Engine, string) {
t.Helper()
log := filepath.Join(t.TempDir(), "decisions.log")
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: log, Host: "oracle", Threshold: 3, Share: share})
st.SetIngestHook(e.Observe)
SetEnforce(e)
t.Cleanup(func() { SetEnforce(nil) })
return New(Options{Token: userTok, Version: "t", Store: st}), e, log
}
func TestEnforceAPI(t *testing.T) {
h, e, log := enforceRig(t, true)
auth := map[string]string{"Authorization": "Bearer " + userTok}
d := getJSON(t, h, "/api/v1/enforce?range=24h", auth)
if d["mode"] != "on" || d["share"] != "on" || d["threshold"].(float64) != 3 || d["log_writable"] != true {
t.Fatalf("%v", d)
}
for _, k := range []string{"coverage", "counts", "recent"} {
if d[k] == nil {
t.Fatalf("missing %s: %v", k, d)
}
}
if sk := d["counts"].(map[string]any)["skipped"].(map[string]any); len(sk) != 8 {
t.Fatalf("skipped %v", sk)
}
// feed events through the store hook and the engine worker
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go e.Run(ctx)
e.Observe(core.Event{Time: time.Now(), IP: "203.0.114.5", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/x", Status: 404, UA: "x"}, "")
for i := 0; i < 200 && e.Report(0).Coverage.AttackIPs == 0; i++ {
time.Sleep(10 * time.Millisecond)
}
e.AcceptPeer("hostinger", []enforce.Wire{{T: time.Now().Unix(), IP: "203.0.114.5", Reason: "rce", Score: 90, TTL: 3600}})
d = getJSON(t, h, "/api/v1/enforce?range=24h", auth)
rec := d["recent"].([]any)
if len(rec) != 1 {
t.Fatalf("recent %v", d)
}
r := rec[0].(map[string]any)
if r["action"] != "ban" || r["src"] != "peer:hostinger" || r["ip"] != "203.0.114.5" {
t.Fatalf("%v", r)
}
if b, _ := os.ReadFile(log); !strings.Contains(string(b), "src=peer:hostinger") {
t.Fatalf("log %q", b)
}
if w := do(h, "GET", "/api/v1/enforce?range=24h", nil, nil); w.Code != 401 {
t.Fatalf("anonymous %d", w.Code)
}
if w := do(h, "GET", "/api/v1/enforce?range=bad", cloneHdr(auth), nil); w.Code != 400 {
t.Fatalf("bad range %d", w.Code)
}
}
func TestEnforceAPIOff(t *testing.T) {
st := store.New(store.Config{Name: "oracle", Version: "t"})
SetEnforce(nil)
h := New(Options{Token: userTok, Store: st})
d := getJSON(t, h, "/api/v1/enforce", map[string]string{"Authorization": "Bearer " + userTok})
if d["mode"] != "off" {
t.Fatalf("%v", d)
}
}
func post(h http.Handler, path, tok, body string) *httptest.ResponseRecorder {
r := httptest.NewRequest("POST", path, strings.NewReader(body))
r.RemoteAddr = "198.51.100.1:1234"
r.Header.Set("Content-Type", "application/json")
if tok != "" {
r.Header.Set("Authorization", "Bearer "+tok)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
return w
}
func TestEnforceFromStoreHook(t *testing.T) {
log := filepath.Join(t.TempDir(), "d.log")
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: log, Host: "oracle", Threshold: 3})
st.SetIngestHook(e.Observe)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go e.Run(ctx)
for i := 0; i < 3; i++ {
st.Ingest(core.Event{Time: time.Now(), IP: "203.0.114.9", Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/x" + strconv.Itoa(i), Status: 404, UA: "nuclei"})
}
deadline := time.Now().Add(2 * time.Second)
for time.Now().Before(deadline) {
if b, _ := os.ReadFile(log); strings.Contains(string(b), "ip=203.0.114.9 ") {
return
}
time.Sleep(10 * time.Millisecond)
}
t.Fatal("no decision written")
}