package server import ( "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/store" ) // Bans applied by the "urutau" jail (second firewall) are reported apart from // the host fail2ban jails in /api/v1/survival. func TestSurvivalByBlocker(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() attack := func(ip string, at time.Time) { st.Ingest(core.Event{Time: at, IP: ip, Cat: core.Scanner, Source: "traefik", Domain: "a.example", Method: "GET", Path: "/.env", Status: 404}) } attack("20.0.0.1", now.Add(-2*time.Hour)) st.Ingest(core.Event{Time: now.Add(-2*time.Hour + 10*time.Minute), IP: "20.0.0.1", Cat: core.Ban, Source: "fail2ban", Jail: "traefik-varredura"}) attack("20.0.0.2", now.Add(-time.Hour)) st.Ingest(core.Event{Time: now.Add(-time.Hour + time.Minute), IP: "20.0.0.2", Cat: core.Ban, Source: "fail2ban", Jail: "urutau"}) h := New(Options{Token: userTok, Version: "t", Store: st}) v := getJSON(t, h, "/api/v1/survival?range=24h", map[string]string{"Authorization": "Bearer " + userTok}) got := map[string]float64{} for _, b := range v["by_blocker"].([]any) { m := b.(map[string]any) got[m["blocker"].(string)] = m["median_s"].(float64) } if got["fail2ban"] != 600 || got["urutau"] != 60 { t.Fatalf("by_blocker = %v", v["by_blocker"]) } }