Each server runs an ultralight probe (scratch image, ~7 MiB RSS) that tails the logs, ships raw lines to the hub over HTTPS with an on-disk spool, and applies enforce decisions locally. The hub keeps one node per probe (primary node = the 1.x data dir, unchanged), does all analysis and serves the UI. Peer federation is removed. 'urutau import' brings 1.x data dirs in with hash and count verification. UI: probes page, host qualified incidents, IPs never wrap, egress heatmap labels, live and replay globes no longer auto-rotate, replay repeat, live traffic metrics. BREAKING CHANGE: URUTAU_PEERS, URUTAU_PEER_* and /api/v1/peer/* are gone; log path variables move from the hub to the probe. See docs/migration-2.0.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/enforce"
|
|
"git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store"
|
|
)
|
|
|
|
func unbanRig(t *testing.T) (http.Handler, *enforce.Engine, string) {
|
|
t.Helper()
|
|
dir := t.TempDir()
|
|
st := store.New(store.Config{Name: "oracle", Version: "t", Retention: 720 * time.Hour})
|
|
e := enforce.New(enforce.Config{Mode: enforce.ModeOn, Path: filepath.Join(dir, "decisions.log"), UnbanPath: filepath.Join(dir, "unban.log"),
|
|
Host: "oracle", Threshold: 3, Share: true})
|
|
SetEnforce(e)
|
|
t.Cleanup(func() { SetEnforce(nil) })
|
|
return New(Options{Token: userTok, Version: "t", Store: st}), e, filepath.Join(dir, "unban.log")
|
|
}
|
|
|
|
func req(h http.Handler, method, path, body string, hdr map[string]string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, path, strings.NewReader(body))
|
|
r.RemoteAddr = "198.51.100.1:1234"
|
|
for k, v := range hdr {
|
|
r.Header.Set(k, v)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
h.ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
func TestUnbanRoute(t *testing.T) {
|
|
h, e, file := unbanRig(t)
|
|
auth := map[string]string{"Authorization": "Bearer " + userTok}
|
|
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7","allow_s":60}`, nil); w.Code != 401 {
|
|
t.Fatalf("no auth: %d", w.Code)
|
|
}
|
|
cross := map[string]string{"Authorization": "Bearer " + userTok, "Sec-Fetch-Site": "cross-site"}
|
|
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7"}`, cross); w.Code != 403 {
|
|
t.Fatalf("cross-site: %d", w.Code)
|
|
}
|
|
for _, b := range []string{`nope`, `{"ip":"203.0.113.7","x":1}`, `{"ip":"203.0.113.7\nroot","allow_s":1}`, `{"ip":"1.2.3.4 allow=9","allow_s":0}`,
|
|
`{"ip":"fe80::1%eth0"}`, `{"ip":"203.0.113.7","allow_s":-1}`, `{"ip":"203.0.113.7","allow_s":2592001}`, `{"ip":"999.1.1.1"}`,
|
|
`{"ip":"203.0.113.7"}{"ip":"1.1.1.1"}`, `{"ip":"` + strings.Repeat("a", 2000) + `"}`} {
|
|
if w := req(h, "POST", "/api/v1/enforce/unban", b, auth); w.Code != 400 {
|
|
t.Errorf("%.40q: %d", b, w.Code)
|
|
}
|
|
}
|
|
if b, _ := os.ReadFile(file); len(b) != 0 {
|
|
t.Fatalf("invalid requests wrote: %q", b)
|
|
}
|
|
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"203.0.113.7","allow_s":86400}`, auth); w.Code != 200 {
|
|
t.Fatalf("ok: %d %s", w.Code, w.Body)
|
|
}
|
|
if w := req(h, "POST", "/api/v1/enforce/unban", `{"ip":"::ffff:198.51.100.9"}`, auth); w.Code != 200 {
|
|
t.Fatalf("mapped: %d", w.Code)
|
|
}
|
|
b, _ := os.ReadFile(file)
|
|
lines := strings.Split(strings.TrimSpace(string(b)), "\n")
|
|
re := regexp.MustCompile(`^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\dZ urutau unban ip=203\.0\.113\.7 allow=86400$`)
|
|
if len(lines) != 2 || !re.MatchString(lines[0]) || !strings.HasSuffix(lines[1], " urutau unban ip=198.51.100.9 allow=0") {
|
|
t.Fatalf("file: %q", b)
|
|
}
|
|
d := getJSON(t, h, "/api/v1/enforce?range=24h", auth)
|
|
al := d["allows"].([]any)
|
|
if len(al) != 1 || al[0].(map[string]any)["ip"] != "203.0.113.7" || al[0].(map[string]any)["by"] != "operator" || d["unban_log_writable"] != true {
|
|
t.Fatalf("allows: %v", d)
|
|
}
|
|
// DELETE
|
|
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=bad", "", auth); w.Code != 400 {
|
|
t.Fatalf("bad ip: %d", w.Code)
|
|
}
|
|
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", cross); w.Code != 403 {
|
|
t.Fatalf("delete cross-site: %d", w.Code)
|
|
}
|
|
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", auth); w.Code != 200 {
|
|
t.Fatalf("delete: %d", w.Code)
|
|
}
|
|
if w := req(h, "DELETE", "/api/v1/enforce/allow?ip=203.0.113.7", "", auth); w.Code != 404 {
|
|
t.Fatalf("delete again: %d", w.Code)
|
|
}
|
|
if len(e.Allows()) != 0 {
|
|
t.Fatal("allow remains")
|
|
}
|
|
if after, _ := os.ReadFile(file); string(after) != string(b) {
|
|
t.Fatal("DELETE wrote to the host file")
|
|
}
|
|
}
|