Files
Lucas RayanandClaude Sonnet 5.5 aefdcea8d2 feat(ingress): firewall telemetry of inbound attempts (host kit --ingress)
Observation-only nftables table (prerouting -150, counters and a
rate-limited log, never drops or modifies), a per-minute counters file,
probe sources ingress and ingresscnt, a per-host hourly aggregate in the
hub (exact totals, sampled ports/ASNs/countries, banned share by the
hub's own ban state, no ban feedback loop), GET /api/v1/ingress, a
Defense section, doctor check, docs EN/pt-BR and tests.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-23 09:09:16 -03:00

78 lines
2.9 KiB
Go

package server
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/egress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/incidents"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/ingress"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire"
)
type fakeNodes struct{ m map[string]*ingress.Agg }
func (f fakeNodes) Hosts() []string {
return []string{"oracle", "hostinger"}
}
func (f fakeNodes) Enforce(string) *enforce.Engine { return nil }
func (f fakeNodes) Egress(string) *egress.Engine { return nil }
func (f fakeNodes) Incidents(string) *incidents.Store { return nil }
func (f fakeNodes) Ingress(h string) *ingress.Agg { return f.m[h] }
func TestIngressAPI(t *testing.T) {
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
now := time.Now()
mk := func(n int) *ingress.Agg {
a := ingress.New()
ts := now.Add(-10 * time.Minute).Unix()
a.Ingest(wire.SrcIngressCnt, []byte(fmt.Sprintf("ts=%d total=0 tcp=0 udp=0 icmp=0 open=0", ts)))
a.Ingest(wire.SrcIngressCnt, []byte(fmt.Sprintf("ts=%d total=%d tcp=%d udp=0 icmp=0 open=%d", ts+60, n, n, n/4)))
return a
}
nodes := fakeNodes{m: map[string]*ingress.Agg{"oracle": mk(400), "hostinger": mk(100)}}
h := New(Options{Token: userTok, Version: "t", Store: st, Nodes: nodes})
auth := map[string]string{"Authorization": "Bearer " + userTok}
d := getJSON(t, h, "/api/v1/ingress?range=24h", auth)
tot := d["totals"].(map[string]any)
if tot["attempts"] != float64(500) || tot["open"] != float64(125) || tot["closed"] != float64(375) || tot["closed_pct"] != float64(75) || d["enabled"] != true {
t.Fatalf("all: %v", d)
}
for _, k := range []string{"hosts", "source", "sample", "hourly", "ports", "asns", "countries"} {
if d[k] == nil {
t.Errorf("missing %q", k)
}
}
d = getJSON(t, h, "/api/v1/ingress?range=24h&host=hostinger", auth)
if d["totals"].(map[string]any)["attempts"] != float64(100) {
t.Fatalf("one host: %v", d)
}
for _, path := range []string{"/api/v1/ingress?host=nope", "/api/v1/ingress?range=bogus"} {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", path, nil)
req.Header.Set("Authorization", "Bearer "+userTok)
h.ServeHTTP(rec, req)
if rec.Code != http.StatusNotFound && rec.Code != http.StatusBadRequest {
t.Errorf("%s: %d", path, rec.Code)
}
}
// no auth
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest("GET", "/api/v1/ingress", nil))
if rec.Code != http.StatusUnauthorized {
t.Errorf("unauthenticated: %d", rec.Code)
}
// standalone (no nodes): empty, not an error
h2 := New(Options{Token: userTok, Version: "t", Store: st})
d = getJSON(t, h2, "/api/v1/ingress", auth)
if d["enabled"] != false || len(d["hosts"].([]any)) != 0 {
t.Fatalf("standalone: %v", d)
}
}