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
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>
169 lines
6.0 KiB
Go
169 lines
6.0 KiB
Go
package fed_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/fed"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/server"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
func TestPeerPollingMergeAndOffline(t *testing.T) {
|
|
ps := store.New(store.Config{Name: "remote-self-name", Version: "t"})
|
|
ps.Ingest(core.Event{Time: time.Now(), IP: "5.5.5.5", Cat: core.Exploit, Source: "traefik", Path: "/x", Method: "GET"})
|
|
ps.Traffic(core.Event{Time: time.Now(), IP: "9.9.9.9", Cat: core.OK, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/", Status: 200})
|
|
srv := httptest.NewServer(server.New(server.Options{Token: "main-token-0123456789-abcdefghijklmnopqrs", PeerTokens: []string{"peer-secret-0123456789-abcdefghijklmnopqr"}, Version: "t", Store: ps}))
|
|
|
|
p, err := fed.ParsePeer("hostinger=" + srv.URL + "/")
|
|
if err != nil || p.Name != "hostinger" {
|
|
t.Fatal(p, err)
|
|
}
|
|
for _, bad := range []string{"nourl", "=http://x", "a=ftp://x", "a=", "a=http://example.org", "a=http://10.0.0.5:8790", "a=https://user:pw@example.org"} {
|
|
if _, err := fed.ParsePeer(bad); err == nil {
|
|
t.Errorf("%q must be rejected", bad)
|
|
}
|
|
}
|
|
|
|
good := fed.NewHub([]fed.Peer{p}, "peer-secret-0123456789-abcdefghijklmnopqr")
|
|
good.Refresh(context.Background())
|
|
sums := good.Summaries("24h", "")
|
|
if len(sums) != 1 || sums[0].Host != "hostinger" || sums[0].IPs[0].Hosts[0] != "hostinger" || sums[0].IPs[0].IP != "5.5.5.5" {
|
|
t.Fatalf("summaries %+v", sums)
|
|
}
|
|
if h := good.Hosts(); !h[0].Online || h[0].Name != "hostinger" {
|
|
t.Fatalf("hosts %+v", h)
|
|
}
|
|
if d := good.IPDetail("5.5.5.5"); len(d) != 1 || d[0].Hosts[0] != "hostinger" {
|
|
t.Fatalf("ip detail from the cached export: %v", d)
|
|
}
|
|
if tr := good.Traffic("24h", "all", ""); len(tr) != 1 || tr[0].Requests != 1 || tr[0].Host != "hostinger" {
|
|
t.Fatalf("traffic %+v", tr)
|
|
}
|
|
if tr := good.Traffic("24h", "a.example", "hostinger"); len(tr) != 1 || tr[0].Requests != 1 {
|
|
t.Fatalf("per-domain traffic %+v", tr)
|
|
}
|
|
|
|
bad := fed.NewHub([]fed.Peer{p}, "wrong")
|
|
bad.Refresh(context.Background())
|
|
if h := bad.Hosts(); h[0].Online || h[0].Error == "" || len(bad.Summaries("24h", "")) != 0 {
|
|
t.Fatalf("wrong token must mark the peer offline: %+v", h)
|
|
}
|
|
|
|
srv.Close()
|
|
good.Refresh(context.Background())
|
|
if h := good.Hosts(); h[0].Online || len(good.Summaries("24h", "")) != 0 {
|
|
t.Fatalf("closed peer must be offline: %+v", h)
|
|
}
|
|
}
|
|
|
|
func TestHubOnlyAsksForTheExportAndRefusesHostileResponses(t *testing.T) {
|
|
var paths []string
|
|
var huge atomic.Bool
|
|
var redirects atomic.Int32
|
|
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
redirects.Add(1)
|
|
w.Write([]byte("{}"))
|
|
}))
|
|
defer target.Close()
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/v1/peer/export", func(w http.ResponseWriter, r *http.Request) {
|
|
paths = append(paths, r.URL.Path)
|
|
switch {
|
|
case r.URL.Query().Get("redirect") == "1":
|
|
case huge.Load():
|
|
w.Write([]byte(`{"v":1,"pad":"` + strings.Repeat("x", 9<<20) + `"}`))
|
|
return
|
|
}
|
|
http.Redirect(w, r, target.URL+"/x", http.StatusFound)
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
p := fed.Peer{Name: "evil", URL: srv.URL}
|
|
|
|
h := fed.NewHub([]fed.Peer{p}, "t")
|
|
h.Refresh(context.Background())
|
|
if redirects.Load() != 0 {
|
|
t.Fatal("redirects must not be followed")
|
|
}
|
|
if st := h.Hosts()[0]; st.Online {
|
|
t.Fatalf("a redirect is a failure: %+v", st)
|
|
}
|
|
huge.Store(true)
|
|
h.Refresh(context.Background())
|
|
if st := h.Hosts()[0]; st.Online || !strings.Contains(st.Error, "larger") {
|
|
t.Fatalf("an oversized response must be refused: %+v", st)
|
|
}
|
|
for _, path := range paths {
|
|
if path != "/api/v1/peer/export" {
|
|
t.Fatalf("the hub asked for %s", path)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestHubBoundsAnInflatedPeer(t *testing.T) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/api/v1/peer/export", func(w http.ResponseWriter, r *http.Request) {
|
|
var e store.PeerExport
|
|
e.V = 1
|
|
e.Summary.Version = strings.Repeat("v", 5000)
|
|
for i := 0; i < 1000; i++ {
|
|
e.Summary.IPs = append(e.Summary.IPs, store.IPSummary{IP: "1.2.3." + "4", Score: 1, Samples: make([]string, 40), Hosts: make([]string, 40)})
|
|
}
|
|
e.Summary.IPs = append(e.Summary.IPs, store.IPSummary{IP: "not an ip", Score: 1e9})
|
|
e.Summary.Timeline.Series = map[string][]uint32{"exploit": make([]uint32, 100000), "evil": make([]uint32, 10)}
|
|
for i := 0; i < 200; i++ {
|
|
e.Summary.TopPaths = append(e.Summary.TopPaths, store.Count{Key: strings.Repeat("p", 9000), N: 1})
|
|
}
|
|
e.Traffic = map[string]store.TrafficData{}
|
|
for i := 0; i < 60; i++ {
|
|
e.Traffic[strings.Repeat("d", i+1)] = store.TrafficData{Timeline: struct {
|
|
Start int64 `json:"start"`
|
|
Step int64 `json:"step"`
|
|
Req []uint32 `json:"req"`
|
|
Pages []uint32 `json:"pages"`
|
|
}{Req: make([]uint32, 5000)}}
|
|
}
|
|
json.NewEncoder(w).Encode(e)
|
|
})
|
|
srv := httptest.NewServer(mux)
|
|
defer srv.Close()
|
|
h := fed.NewHub([]fed.Peer{{Name: "p", URL: srv.URL}}, "t")
|
|
h.Refresh(context.Background())
|
|
sums := h.Summaries("1h", "")
|
|
if len(sums) != 1 {
|
|
t.Fatalf("hosts %+v", h.Hosts())
|
|
}
|
|
s := sums[0]
|
|
if len(s.IPs) != 500 || len(s.Version) > 32 || len(s.TopPaths) > 50 || len(s.TopPaths[0].Key) > 300 {
|
|
t.Fatalf("caps not applied: ips=%d version=%d paths=%d", len(s.IPs), len(s.Version), len(s.TopPaths))
|
|
}
|
|
for _, x := range s.IPs {
|
|
if x.IP == "not an ip" || len(x.Samples) > 6 || len(x.Hosts) > 4 {
|
|
t.Fatalf("bad row survived: %+v", x)
|
|
}
|
|
}
|
|
if len(s.Timeline.Series["exploit"]) != 60 || s.Timeline.Series["evil"] != nil || len(s.Timeline.Series["ok"]) != 60 {
|
|
t.Fatal("the timeline grid is fixed by the range")
|
|
}
|
|
kept := 0
|
|
for i := 1; i <= 60; i++ {
|
|
for _, got := range h.Traffic("1h", strings.Repeat("d", i), "") {
|
|
kept++
|
|
if len(got.Timeline.Req) != 60 {
|
|
t.Fatalf("traffic grid must be fixed: %d", len(got.Timeline.Req))
|
|
}
|
|
}
|
|
}
|
|
if kept > 27 {
|
|
t.Fatalf("too many domains accepted from a peer: %d", kept)
|
|
}
|
|
}
|