Release / Publish tags (push) Successful in 18s
Release / Plan (reuse the published image?) (push) Successful in 5s
Release / ci (push) Successful in 1m47s
Release / Build image (amd64) (push) Successful in 21s
Release / Build image (arm64) (push) Successful in 28s
CI / Format and vet (push) Successful in 23s
CI / Tests (Linux, race detector) (push) Successful in 1m24s
CI / Dependency audit (push) Successful in 24s
CI / Docker image (build and smoke test) (push) Skipped
Release / Gitea release (push) Successful in 5s
Backend pagination with one pager for sessions, IPs, campaigns and events; network probe source (port-scan, T1046, second firewall reason port-scan); host kit logs inbound probes; urutau call on the report cover. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
141 lines
5.8 KiB
Go
141 lines
5.8 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/evlog"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/intel"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
func TestPageParamsLimits(t *testing.T) {
|
|
for _, c := range []struct {
|
|
q string
|
|
page, per int
|
|
}{
|
|
{"", 1, 25}, {"page=0", 1, 25}, {"page=-4&per_page=-1", 1, 25}, {"page=x&per_page=y", 1, 25},
|
|
{"page=3&per_page=500", 3, 100}, {"per=40", 1, 40}, {"per_page=10&per=40", 1, 10}, {"page=999999999", maxPageNo, 25},
|
|
} {
|
|
v, _ := url.ParseQuery(c.q)
|
|
if p, n := pageParams(v); p != c.page || n != c.per {
|
|
t.Errorf("%q: got %d/%d want %d/%d", c.q, p, n, c.page, c.per)
|
|
}
|
|
}
|
|
if m := metaOf(0, 1, 25); m.Pages != 1 {
|
|
t.Errorf("empty list has one page: %+v", m)
|
|
}
|
|
if m := metaOf(51, 1, 25); m.Pages != 3 {
|
|
t.Errorf("pages: %+v", m)
|
|
}
|
|
if f, to := window(10, 9, 25); f != 10 || to != 10 {
|
|
t.Errorf("past the end: %d %d", f, to)
|
|
}
|
|
}
|
|
|
|
// the funnel list is filtered and cut after the (federated) merge: totals and
|
|
// pages describe the merged list, and the order is the same on every call.
|
|
func TestPagedFunnelMerged(t *testing.T) {
|
|
base := time.Unix(1_800_000_000, 0).UTC()
|
|
mk := func(host string, n int) []intel.Session {
|
|
var out []intel.Session
|
|
for i := 0; i < n; i++ {
|
|
out = append(out, intel.Session{IP: fmt.Sprintf("10.%s.0.%d", host, i), Start: base, End: base.Add(time.Minute), Stage: "probe", Hosts: []string{host}, Fams: map[string]int{"wp": 1}})
|
|
}
|
|
return out
|
|
}
|
|
merged := store.MergePeriods([]store.PeriodOut{{Sessions: mk("1", 30)}, {Sessions: mk("2", 30)}})
|
|
f := store.FunnelOf(merged, nil)
|
|
q := func(s string) url.Values { v, _ := url.ParseQuery(s); return v }
|
|
p1 := pagedFunnel(f, q("page=1&per_page=25"))
|
|
if p1.Total != 60 || p1.Pages != 3 || len(p1.Sessions) != 25 || len(p1.Hosts) != 2 {
|
|
t.Fatalf("page1: total=%d pages=%d n=%d hosts=%v", p1.Total, p1.Pages, len(p1.Sessions), p1.Hosts)
|
|
}
|
|
p3 := pagedFunnel(f, q("page=3&per_page=25"))
|
|
if len(p3.Sessions) != 10 {
|
|
t.Fatalf("page3: %d", len(p3.Sessions))
|
|
}
|
|
seen := map[string]bool{}
|
|
for _, p := range []funnelPage{p1, pagedFunnel(f, q("page=2&per_page=25")), p3} {
|
|
for _, s := range p.Sessions {
|
|
if seen[s.IP] {
|
|
t.Fatalf("%s appears on two pages", s.IP)
|
|
}
|
|
seen[s.IP] = true
|
|
}
|
|
}
|
|
if len(seen) != 60 {
|
|
t.Fatalf("pages cover %d of 60", len(seen))
|
|
}
|
|
if again := pagedFunnel(f, q("page=1&per_page=25")); again.Sessions[0].IP != p1.Sessions[0].IP || again.Sessions[24].IP != p1.Sessions[24].IP {
|
|
t.Fatal("order must be stable")
|
|
}
|
|
if far := pagedFunnel(f, q("page=50")); len(far.Sessions) != 0 || far.Total != 60 {
|
|
t.Fatalf("beyond the end: %d items, total %d", len(far.Sessions), far.Total)
|
|
}
|
|
if h := pagedFunnel(f, q("host_f=2")); h.Total != 30 || h.Pages != 2 {
|
|
t.Fatalf("host filter: %d/%d", h.Total, h.Pages)
|
|
}
|
|
if h := pagedFunnel(f, q("stage=exploit")); h.Total != 0 || h.Pages != 1 {
|
|
t.Fatalf("stage filter: %d/%d", h.Total, h.Pages)
|
|
}
|
|
}
|
|
|
|
func TestPaginatedEndpoints(t *testing.T) {
|
|
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
|
l, _ := evlog.Open(evlog.Options{Dir: filepath.Join(t.TempDir(), "events")})
|
|
st.SetEventLog(l)
|
|
now := time.Now()
|
|
for i := 0; i < 60; i++ {
|
|
st.Ingest(core.Event{Time: now.Add(-time.Hour), IP: fmt.Sprintf("20.0.%d.%d", i/250, i%250+1), Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/a", Status: 404, UA: "x"})
|
|
}
|
|
var h http.Handler = New(Options{Token: userTok, Version: "t", Store: st})
|
|
auth := map[string]string{"Authorization": "Bearer " + userTok}
|
|
|
|
m := getJSON(t, h, "/api/v1/ips?range=24h", auth)
|
|
if m["total"].(float64) != 60 || m["per_page"].(float64) != 25 || m["pages"].(float64) != 3 || len(m["items"].([]any)) != 25 {
|
|
t.Fatalf("ips defaults: %v %v %v", m["total"], m["per_page"], m["pages"])
|
|
}
|
|
if m = getJSON(t, h, "/api/v1/ips?range=24h&per_page=1000&page=-2", auth); m["per_page"].(float64) != 100 || m["page"].(float64) != 1 || len(m["items"].([]any)) != 60 {
|
|
t.Fatalf("ips clamp: %v %v", m["per_page"], m["page"])
|
|
}
|
|
if m = getJSON(t, h, "/api/v1/ips?range=24h&page=9", auth); len(m["items"].([]any)) != 0 || m["total"].(float64) != 60 {
|
|
t.Fatalf("ips beyond the end: %v", m)
|
|
}
|
|
a := getJSON(t, h, "/api/v1/ips?range=24h&page=2&sort=score", auth)["items"].([]any)
|
|
b := getJSON(t, h, "/api/v1/ips?range=24h&page=2&sort=score", auth)["items"].([]any)
|
|
for i := range a {
|
|
if a[i].(map[string]any)["ip"] != b[i].(map[string]any)["ip"] {
|
|
t.Fatal("ips order unstable")
|
|
}
|
|
}
|
|
|
|
e := getJSON(t, h, "/api/v1/events?range=24h&page=2&per_page=25", auth)
|
|
if len(e["events"].([]any)) != 25 || e["page"].(float64) != 2 || e["total"].(float64) < 60 {
|
|
t.Fatalf("events page 2: n=%d page=%v total=%v", len(e["events"].([]any)), e["page"], e["total"])
|
|
}
|
|
if e = getJSON(t, h, "/api/v1/events?range=24h&page=3&per_page=25", auth); len(e["events"].([]any)) != 10 {
|
|
t.Fatalf("events last page: %d", len(e["events"].([]any)))
|
|
}
|
|
if e = getJSON(t, h, "/api/v1/events?range=24h&page=40&per_page=25", auth); len(e["events"].([]any)) != 0 || e["total"].(float64) < 60 {
|
|
t.Fatalf("events beyond the end: %v", e)
|
|
}
|
|
s := getJSON(t, h, "/api/v1/events/sessions?range=24h&per_page=10", auth)
|
|
if len(s["sessions"].([]any)) != 10 || s["total"].(float64) != 60 || s["pages"].(float64) != 6 {
|
|
t.Fatalf("event sessions: %v %v", s["total"], s["pages"])
|
|
}
|
|
f := getJSON(t, h, "/api/v1/sessions/funnel?range=24h&per_page=10&page=2", auth)
|
|
if len(f["sessions"].([]any)) != 10 || f["page"].(float64) != 2 || f["total"].(float64) < 60 {
|
|
t.Fatalf("funnel page: %v", f["total"])
|
|
}
|
|
c := getJSON(t, h, "/api/v1/campaigns?range=24h&page=0&per_page=500", auth)
|
|
if c["per_page"].(float64) != 100 || c["page"].(float64) != 1 || c["pages"] == nil {
|
|
t.Fatalf("campaigns meta: %v", c)
|
|
}
|
|
}
|