fix(ip): show egress destination view instead of ambiguous 404
Add GET /api/v1/ip/{ip}/egress and Engine.Dst; the IP page renders who
connected, findings and linked incidents, or a definitive never-seen message.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
19afd221e2
commit
0db50c05c4
5 files changed
+351
-6
No files matched your search
@@ -681,3 +681,22 @@ func TestBeaconMetadataAndKnownCDN(t *testing.T) {
|
||||
t.Fatalf("surbl: %+v", f)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEngineDst(t *testing.T) {
|
||||
e := newEng(t, Config{Workloads: "ciberlab=10.0.5.0/24:scanner"})
|
||||
for i := 0; i < 70; i++ { // more destinations than the top-50
|
||||
e.IngestEvent(mk(t0.Add(time.Duration(i)*time.Second), "10.0.3.5", fmt.Sprintf("198.18.0.%d", i+1), "tcp", 80))
|
||||
}
|
||||
e.IngestEvent(mk(t0.Add(time.Hour), "10.0.3.5", "198.18.0.70", "tcp", 443))
|
||||
from, to := t0.Unix()-60, t0.Unix()+7200
|
||||
d, _, ok := e.Dst(from, to, ip("198.18.0.70"))
|
||||
if !ok || d.Conns != 2 || d.IP != "198.18.0.70" || len(d.Workloads) != 1 || len(d.Ports) != 2 || d.Ports[0] != 80 {
|
||||
t.Fatalf("dst: %+v ok=%v", d, ok)
|
||||
}
|
||||
if _, _, ok := e.Dst(from, to, ip("203.0.113.9")); ok {
|
||||
t.Fatal("unknown dst found")
|
||||
}
|
||||
if _, _, ok := e.Dst(from-10*86400, from-9*86400, ip("198.18.0.70")); ok {
|
||||
t.Fatal("dst found outside the window")
|
||||
}
|
||||
}
|
||||
@@ -454,3 +454,46 @@ func EmptyReport() Report {
|
||||
return Report{Workloads: []WorkloadOut{}, TopDst: []DstOut{}, Findings: []Finding{}, ByCountry: []CountryOut{}, Scanner: ScannerOut{Bursts: []BurstOut{}},
|
||||
Timeline: Timeline{Step: 3600, Series: map[string][]uint32{}}}
|
||||
}
|
||||
|
||||
// Dst looks one destination up in the dst table (not limited to the top 50):
|
||||
// its connections in [from, to] and the findings that target it. ok is false
|
||||
// when the window holds neither.
|
||||
func (e *Engine) Dst(from, to int64, ip netip.Addr) (DstOut, []Finding, bool) {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
if to <= from {
|
||||
to = from + 1
|
||||
}
|
||||
fs := []Finding{}
|
||||
for _, f := range e.findings {
|
||||
if f.Last >= from && f.First <= to && f.Dst == ip.String() {
|
||||
c := *f
|
||||
c.Samples = append([]int64(nil), f.Samples...)
|
||||
fs = append(fs, c)
|
||||
}
|
||||
}
|
||||
sort.Slice(fs, func(i, j int) bool {
|
||||
if fs[i].Last != fs[j].Last {
|
||||
return fs[i].Last > fs[j].Last
|
||||
}
|
||||
return fs[i].ID < fs[j].ID
|
||||
})
|
||||
out := DstOut{IP: ip.String(), Workloads: []string{}, Ports: []uint16{}}
|
||||
if el, ok := e.dsts.m[ip]; ok { // read-only: no LRU promotion
|
||||
ds := el.Value.(*lent[netip.Addr, *dstStat]).v
|
||||
d0, d1 := int32(from/86400), int32(to/86400)
|
||||
for _, d := range ds.days {
|
||||
if d.day >= d0 && d.day <= d1 {
|
||||
out.Conns += uint64(d.n)
|
||||
}
|
||||
}
|
||||
out.CC, out.ASN, out.Org = ds.cc, ds.asn, ds.og
|
||||
out.Workloads = append(out.Workloads, ds.wls...)
|
||||
out.Ports = append(out.Ports, ds.ports...)
|
||||
sort.Slice(out.Ports, func(a, b int) bool { return out.Ports[a] < out.Ports[b] })
|
||||
}
|
||||
if out.Conns == 0 && len(fs) == 0 {
|
||||
return DstOut{}, nil, false
|
||||
}
|
||||
return out, fs, true
|
||||
}
|
||||
Reference in new issue
Block a user