package app import ( "fmt" "io" "log/slog" "testing" "time" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/wire" ) func ingressNode(t *testing.T) (*Runner, *store.Store) { t.Helper() st := store.New(store.Config{Name: "t", Version: "t", Retention: 30 * 24 * time.Hour}) log := slog.New(slog.NewTextHandler(io.Discard, nil)) r, err := NewRemoteRunner(Config{DataDir: t.TempDir()}, st, log, t.TempDir()) if err != nil { t.Fatal(err) } r.StartRemote(nil, nil) r.IngressFeed = newIngress(st, t.TempDir(), log).Ingest return r, st } func lines(src string, ls ...string) []wire.Line { var out []wire.Line for i, l := range ls { out = append(out, wire.Line{Src: src, Text: l, End: wire.Pos{Inode: 7, Offset: int64(i+1) * 100}}) } return out } // Attempts from a banned address are classified from the node's ban state and // never become events, scores or evidence: the store learns nothing from them. func TestIngressNeverFeedsTheStore(t *testing.T) { r, st := ingressNode(t) // the node knows 203.0.113.9 is banned by a jail r.ApplyBatch(lines(wire.SrcFail2ban, "2026-11-20 10:00:00,100 fail2ban.actions [1]: NOTICE [sshd] Ban 203.0.113.9")) if !st.Banned("203.0.113.9") || st.Banned("198.51.100.1") { t.Fatal("ban state not read") } before := st.IPCount() now := time.Now().UTC().Format("2006-01-02T15:04:05+00:00") var in []string for i := 0; i < 500; i++ { in = append(in, fmt.Sprintf("%s h kernel: URUTAU-IN: IN=eth0 OUT= SRC=203.0.113.9 DST=10.0.0.5 PROTO=TCP SPT=1 DPT=%d SYN", now, 1000+i%40)) in = append(in, fmt.Sprintf("%s h kernel: URUTAU-IN: IN=eth0 OUT= SRC=198.51.100.%d DST=10.0.0.5 PROTO=TCP SPT=1 DPT=23 SYN", now, 1+i%200)) } acc, _ := r.ApplyBatch(lines(wire.SrcIngress, in...)) if acc != 1000 { t.Fatalf("accepted %d", acc) } r.ApplyBatch(lines(wire.SrcIngressCnt, "ts=1 total=oops")) if st.IPCount() != before { t.Fatalf("ingress created IP records: %d -> %d", before, st.IPCount()) } if st.KnownHostile("198.51.100.1") { t.Fatal("an ingress attempt became evidence") } // the sources are not events: no runner source exists for them if r.find("ingress") != nil || r.find("ingresscnt") != nil { t.Fatal("ingress sources must not be event sources") } } func TestIngressDuplicateLinesAreDropped(t *testing.T) { r, _ := ingressNode(t) l := lines(wire.SrcIngress, "x URUTAU-IN: SRC=198.51.100.1 DST=1.1.1.1 PROTO=TCP DPT=23 SYN") if a, _ := r.ApplyBatch(l); a != 1 { t.Fatal("first not accepted") } if a, d := r.ApplyBatch(l); a != 0 || d != 1 { t.Fatalf("replay accepted %d dup %d", a, d) } }