Files
urutau/internal/server/api_campaigns_test.go
Lucas RayanandClaude Opus 5.5 2ebda1e4d7
Release / Plan (reuse the published image?) (push) Successful in 3s
CI / Docker image (build and smoke test) (push) Skipped
CI / Format and vet (push) Successful in 28s
CI / Dependency audit (push) Successful in 20s
CI / Tests (Linux, race detector) (push) Successful in 1m10s
Release / ci (push) Successful in 1m10s
Release / Build image (arm64) (push) Successful in 55s
Release / Build image (amd64) (push) Successful in 1m16s
Release / Publish tags (push) Successful in 14s
Release / Gitea release (push) Skipped
feat: 1.3.0 thirteen new pages, charts, tooltips and error analysis
New pages grouped in the rail (Monitor, Investigate, Analyze, Operate,
Reports): Services (SLO, Apdex, latency app vs proxy), Sessions (stage
funnel), Campaigns, Event search, MITRE ATT&CK matrix, Evolution,
Concentration (Pareto, Gini), Survival (Kaplan-Meier per attack
episode, recidivists), Where errors come from (application vs failed
attack vs proxy vs client), Protocols and TLS, Defense (fail2ban, SSH,
blocklist), Integrity, and a printable executive report.

Chart library (donut, radar, stacked area, bars, heatmap, gauge),
glossary with accessible "?" tooltips, self-explanatory category names
(nonexistent page 404), page loader through pages.json.

Unique visitors fixed: real browsers opening a page, one per IP per day,
deduplicated across domains, with non-human clients counted apart.
Anonymous legitimate visits on the Live page (?visits=1, no IP).
Traefik origin, service, protocol and TLS fields parsed. Paths and
Referer paths drop e-mail and token segments.

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

82 lines
3.1 KiB
Go

package server
import (
"net/http"
"path/filepath"
"strconv"
"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/store"
)
func campaignServer(t *testing.T) http.Handler {
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 _, ip := range []string{"20.0.0.1", "20.0.0.2", "20.0.0.3"} {
for _, p := range []string{"/wp-login.php", "/xmlrpc.php"} {
st.Ingest(core.Event{Time: now.Add(-time.Hour), IP: ip, Cat: core.Scanner, Source: "traefik", Method: "GET", Path: p, Status: 404, UA: "zgrab/0.x"})
}
}
return New(Options{Token: userTok, Version: "t", Store: st})
}
func TestCampaignsAndSessionsAPI(t *testing.T) {
h := campaignServer(t)
auth := map[string]string{"Authorization": "Bearer " + userTok}
list := getJSON(t, h, "/api/v1/campaigns?range=24h", auth)
cs := list["campaigns"].([]any)
if len(cs) != 1 || list["ips_n"].(float64) != 3 {
t.Fatalf("campaigns: %v", list)
}
id := cs[0].(map[string]any)["id"].(string)
d := getJSON(t, h, "/api/v1/campaigns/"+id+"?range=24h", auth)
if len(d["members"].([]any)) != 3 {
t.Fatalf("members: %v", d)
}
f := getJSON(t, h, "/api/v1/sessions/funnel?range=24h", auth)
if f["sessions_n"].(float64) < 3 || f["stages"] == nil {
t.Fatalf("funnel: %v", f)
}
for path, want := range map[string]int{
"/api/v1/campaigns/c-zzzz?range=24h": 404,
"/api/v1/campaigns/bad%20id?range=24h": 400,
"/api/v1/campaigns?range=24h&gap=1": 400,
"/api/v1/campaigns?range=24h&host=nowhere": 404,
} {
if w := do(h, "GET", path, cloneHdr(auth), nil); w.Code != want {
t.Errorf("%s: %d want %d", path, w.Code, want)
}
}
if w := do(h, "GET", "/api/v1/campaigns", nil, nil); w.Code != 401 && w.Code != 302 && w.Code != 403 {
t.Errorf("anonymous: %d", w.Code)
}
}
func TestFunnelCountsEverySession(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 < 260; i++ {
ip := "30.0." + strconv.Itoa(i/250) + "." + strconv.Itoa(i%250+1)
st.Ingest(core.Event{Time: now.Add(-time.Hour), IP: ip, Cat: core.Scanner, Source: "traefik", Method: "GET", Path: "/.env", Status: 404, UA: "curl/8"})
}
h := New(Options{Token: userTok, Version: "t", Store: st})
f := getJSON(t, h, "/api/v1/sessions/funnel?range=24h", map[string]string{"Authorization": "Bearer " + userTok})
if f["sessions_n"].(float64) != 260 || f["sampled"] != true || len(f["sessions"].([]any)) > 200 {
t.Fatalf("funnel: sessions_n=%v sampled=%v list=%d", f["sessions_n"], f["sampled"], len(f["sessions"].([]any)))
}
sum := 0.0
for _, v := range f["stages"].(map[string]any) {
sum += v.(float64)
}
if sum != 260 || f["reached"].(map[string]any)["recon"].(float64) != 260 {
t.Fatalf("stages %v reached %v", f["stages"], f["reached"])
}
}