Files
urutau/internal/store/store_test.go
T
Lucas RayanandClaude Opus 5.5 b4a2c2c9ec
Release / Plan (reuse the published image?) (push) Successful in 4s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 1m29s
CI / Tests (Linux, race detector) (push) Failing after 1m48s
CI / Dependency audit (push) Successful in 21s
Release / ci (push) Failing after 1m50s
Release / Build image (amd64) (push) Skipped
Release / Build image (arm64) (push) Skipped
Release / Publish tags (push) Skipped
Release / Gitea release (push) Skipped
feat: Urutau 1.0.0, lightweight threat intelligence and web analytics
Always-on Go service (stdlib only) that follows the Traefik JSON access
log, the sshd log and the fail2ban log of a host, classifies attacks,
scores sources, keeps bounded aggregates with snapshots, and serves an
embedded dashboard with live updates, offline geolocation (DB-IP Lite),
privacy-friendly traffic analytics, an exportable blocklist and
token-authenticated federation between instances.

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

171 lines
5.1 KiB
Go

package store
import (
"fmt"
"path/filepath"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
)
var t0 = time.Date(2026, 11, 14, 12, 0, 0, 0, time.UTC)
func newStore(name string, max int) *Store {
s := New(Config{Name: name, Version: "t", MaxIPs: max})
s.now = func() time.Time { return t0 }
return s
}
func ev(ip string, cat core.Category, ago time.Duration) core.Event {
return core.Event{Time: t0.Add(-ago), IP: ip, Cat: cat, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/.env", UA: "curl/8"}
}
func TestScoringTimelineAndTops(t *testing.T) {
s := newStore("oracle", 100)
for i := 0; i < 6; i++ {
s.Ingest(ev("1.1.1.1", core.Exploit, time.Duration(i)*time.Minute)) // 48
}
s.Ingest(ev("1.1.1.1", core.Scanner, 30*time.Minute)) // +3 = 51
s.Ingest(ev("2.2.2.2", core.Scanner, 2*time.Hour))
s.Ingest(ev("3.3.3.3", core.OK, time.Minute))
s.Ingest(ev("4.4.4.4", core.NotFound, time.Minute))
sum := s.Summary("1h")
if sum.Totals.Requests != 9 || sum.Totals.Malicious != 7 {
t.Fatalf("1h totals %+v", sum.Totals)
}
if sum.Totals.HostileIPs != 1 || sum.IPs[0].IP != "1.1.1.1" || sum.IPs[0].Score != 51 || sum.IPs[0].Level != core.LevelHostile {
t.Fatalf("ips %+v", sum.IPs[0])
}
if len(sum.Timeline.Series["exploit"]) != 60 {
t.Fatal("grid")
}
var ex uint32
for _, v := range sum.Timeline.Series["exploit"] {
ex += v
}
if ex != 6 {
t.Fatalf("exploit series %d", ex)
}
s24 := s.Summary("24h")
if s24.Totals.Counts["scanner"] != 2 {
t.Fatalf("24h scanner %d", s24.Totals.Counts["scanner"])
}
if len(s24.TopPaths) != 1 || s24.TopPaths[0].Key != "/.env" || s24.TopDomains[0].Key != "a.example" || s24.TopUAs[0].Key != "curl/8" {
t.Fatalf("tops %+v", s24.TopPaths)
}
if _, ok := s.IP("3.3.3.3"); ok {
t.Fatal("ok-only visitors must not enter the table")
}
old := ev("9.9.9.9", core.Exploit, 8*24*time.Hour)
s.Ingest(old)
if _, ok := s.IP("4.4.4.4"); ok {
t.Fatal("a 404 alone must not create an IP record")
}
if s.IPCount() != 2 {
t.Fatal("events beyond retention must be dropped")
}
}
func TestBansAndFeed(t *testing.T) {
s := newStore("oracle", 100)
b := core.Event{Time: t0.Add(-time.Minute), IP: "5.5.5.5", Cat: core.Ban, Source: "fail2ban", Jail: "sshd"}
s.Ingest(b)
b.Jail = "recidive"
s.Ingest(b)
if sum := s.Summary("24h"); sum.Totals.ActiveBans != 1 || !sum.IPs[0].Banned || len(sum.IPs[0].Jails) != 2 {
t.Fatalf("%+v", sum.Totals)
}
s.cache = map[string]cachedSummary{}
b.Unban, b.Jail = true, "sshd"
s.Ingest(b)
r, _ := s.IP("5.5.5.5")
if len(r.Jails) != 1 || r.Jails[0] != "recidive" || r.Score != 20 {
t.Fatalf("%+v", r)
}
if rec := s.Recent(10); len(rec) != 3 || !rec[0].Unban {
t.Fatalf("feed %+v", rec)
}
ch, stop := s.Subscribe()
defer stop()
s.Ingest(ev("6.6.6.6", core.Exploit, 0))
select {
case e := <-ch:
if e.IP != "6.6.6.6" {
t.Fatal(e)
}
case <-time.After(time.Second):
t.Fatal("no sse event")
}
}
func TestCapsAreHard(t *testing.T) {
s := newStore("x", 1000)
for i := 0; i < 5000; i++ {
e := ev(fmt.Sprintf("10.%d.%d.1", i/250, i%250), core.Scanner, time.Minute)
s.Ingest(e)
}
s.Ingest(core.Event{Time: t0, IP: "7.7.7.7", Cat: core.Ban, Source: "fail2ban", Jail: "sshd"})
for i := 0; i < 3000; i++ {
e := ev(fmt.Sprintf("11.%d.%d.1", i/250, i%250), core.Scanner, time.Minute)
e.Path = fmt.Sprintf("/p%d", i)
s.Ingest(e)
}
if n := s.IPCount(); n > 1000 {
t.Fatalf("ip table %d exceeds cap", n)
}
if _, ok := s.IP("7.7.7.7"); !ok {
t.Fatal("banned IP evicted")
}
h := s.hours[int((t0.Unix()-60)/3600%hourSlots)]
if len(h.Paths) > topCap {
t.Fatalf("top paths %d", len(h.Paths))
}
}
func TestMerge(t *testing.T) {
a, b := newStore("oracle", 100), newStore("hostinger", 100)
a.Ingest(ev("1.1.1.1", core.Exploit, time.Minute))
b.Ingest(ev("1.1.1.1", core.Scanner, 5*time.Minute))
b.Ingest(ev("2.2.2.2", core.Exploit, 5*time.Minute))
b.now = func() time.Time { return t0.Add(20 * time.Second) } // clock skew
m := Merge("all", []Summary{a.Summary("1h"), b.Summary("1h")})
if m.Totals.Malicious != 3 || len(m.Hosts) != 2 || m.Totals.UniqueIPs != 2 {
t.Fatalf("%+v", m.Totals)
}
top := m.IPs[0]
if top.IP != "1.1.1.1" || top.Score != 11 || len(top.Hosts) != 2 || top.Counts["exploit"] != 1 || top.Counts["scanner"] != 1 {
t.Fatalf("%+v", top)
}
var ex uint32
for _, v := range m.Timeline.Series["exploit"] {
ex += v
}
if ex != 2 {
t.Fatalf("merged timeline %d", ex)
}
if m.TopPaths[0].N != 3 {
t.Fatalf("%+v", m.TopPaths)
}
}
func TestSnapshotRoundTrip(t *testing.T) {
s := newStore("oracle", 100)
s.Ingest(ev("1.1.1.1", core.Exploit, time.Minute))
p := filepath.Join(t.TempDir(), "state.gob")
if err := s.Save(p, map[string]Offset{"traefik": {Inode: 7, Offset: 99}}); err != nil {
t.Fatal(err)
}
s2 := newStore("oracle", 100)
off, err := s2.Load(p)
if err != nil || off["traefik"].Offset != 99 {
t.Fatalf("%v %v", off, err)
}
if r, ok := s2.IP("1.1.1.1"); !ok || r.Score != 8 || s2.Summary("1h").Totals.Malicious != 1 {
t.Fatal("state lost")
}
if off, err := newStore("x", 1).Load(filepath.Join(t.TempDir(), "none")); err != nil || len(off) != 0 {
t.Fatal("missing snapshot must be a fresh start")
}
}