Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

beacon_ingest: let a late name lookup fill the cache

Claude8ab395ac9569
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go
index 97721c4d..190229ec 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/extension_windows_test.go
@@ -165,22 +165,3 @@ func TestValidWindowsName(t *testing.T) {
}
}
}
-
-// A directory service that does not answer costs one connection the lookup timeout, not the
-// client's whole budget: the event is named by SID only.
-func TestResolveNameGivesUpOnASlowLookup(t *testing.T) {
- prev := lookupAccount
- release := make(chan struct{})
- lookupAccount = func(string) (string, error) {
- <-release
- return `CONTOSO\late`, nil
- }
- t.Cleanup(func() { close(release); lookupAccount = prev })
- start := time.Now()
- if got := resolveName("S-1-5-21-1-2-3-1001"); got != "" {
- t.Fatalf("resolveName = %q, want nothing past the timeout", got)
- }
- if elapsed := time.Since(start); elapsed > nameLookupTimeout+time.Second {
- t.Fatalf("resolveName took %v", elapsed)
- }
-}
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
index f4d224aa..97017e5a 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names.go
@@ -6,9 +6,10 @@ import (
)
const (
- // nameLookupTimeout bounds one id(1) call on Unix. The name is resolved once per connection,
- // inside the server's budget for it, so a directory service that does not answer costs one
- // connection half a second and leaves the event named by its account id only.
+ // nameLookupTimeout bounds how long a connection waits for its account's name. The name is
+ // resolved inside the server's budget for the connection, so a directory service that does not
+ // answer costs one connection half a second and leaves the event named by its account id only.
+ // The lookup itself carries on, and its answer fills the cache when it comes.
nameLookupTimeout = 500 * time.Millisecond
// nameCacheTTL bounds how long a uid's name is reused. Accounts are renamed rarely; a miss is
// retried after the same interval, so a directory service that was briefly unreachable does
@@ -17,11 +18,16 @@ const (
)
// nameCache resolves the account name stamped on each event from the peer's account: a uid on
-// Unix, a SID on Windows (see resolveName for each).
+// Unix, a SID on Windows (see resolveName for each). At most one lookup per account runs at a time:
+// connections that arrive while it runs wait on that one, and one that outlasts the wait still
+// fills the cache when it returns, so a slow directory service leaves the account unnamed only
+// until it answers rather than for nameCacheTTL.
type nameCache struct {
mu sync.Mutex
entries map[string]nameEntry
+ pending map[string]chan struct{}
now func() time.Time
+ wait time.Duration
lookup1 func(uid string) string
}
@@ -31,22 +37,46 @@ type nameEntry struct {
}
func newNameCache() *nameCache {
- return &nameCache{entries: map[string]nameEntry{}, now: time.Now, lookup1: resolveName}
+ return &nameCache{
+ entries: map[string]nameEntry{},
+ pending: map[string]chan struct{}{},
+ now: time.Now,
+ wait: nameLookupTimeout,
+ lookup1: resolveName,
+ }
}
func (c *nameCache) lookup(uid string) string {
- now := c.now()
c.mu.Lock()
- entry, ok := c.entries[uid]
- c.mu.Unlock()
- if ok && now.Before(entry.expires) {
+ if entry, ok := c.entries[uid]; ok && c.now().Before(entry.expires) {
+ c.mu.Unlock()
return entry.name
}
- name := c.lookup1(uid)
- c.mu.Lock()
- c.entries[uid] = nameEntry{name: name, expires: now.Add(nameCacheTTL)}
+ done, running := c.pending[uid]
+ if !running {
+ done = make(chan struct{})
+ c.pending[uid] = done
+ go func() {
+ name := c.lookup1(uid)
+ c.mu.Lock()
+ c.entries[uid] = nameEntry{name: name, expires: c.now().Add(nameCacheTTL)}
+ delete(c.pending, uid)
+ c.mu.Unlock()
+ close(done)
+ }()
+ }
c.mu.Unlock()
- return name
+
+ timer := time.NewTimer(c.wait)
+ defer timer.Stop()
+ select {
+ case <-done:
+ case <-timer.C:
+ return ""
+ }
+ c.mu.Lock()
+ defer c.mu.Unlock()
+ return c.entries[uid].name
}
// validName accepts what an account name can be and nothing that could disturb a log line or a
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
index 3ab83e35..f0b40d31 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_test.go
@@ -50,6 +50,36 @@ func TestNameCacheRetriesAMissAfterTheTTL(t *testing.T) {
}
}
+// A lookup that outlasts the wait leaves that connection unnamed, and its late answer names the
+// next one rather than a cached miss for nameCacheTTL. Connections that arrive meanwhile share the
+// one lookup.
+func TestNameCacheKeepsALateAnswer(t *testing.T) {
+ release := make(chan struct{})
+ calls := 0
+ c := newNameCache()
+ c.wait = 20 * time.Millisecond
+ c.lookup1 = func(string) string { calls++; <-release; return `CONTOSO\late` }
+ for i := 0; i < 3; i++ {
+ if got := c.lookup("S-1-5-21-1"); got != "" {
+ t.Fatalf("lookup %d = %q before the directory answered", i, got)
+ }
+ }
+ close(release)
+ deadline := time.Now().Add(5 * time.Second)
+ for {
+ if got := c.lookup("S-1-5-21-1"); got == `CONTOSO\late` {
+ break
+ }
+ if time.Now().After(deadline) {
+ t.Fatal("the late answer never reached the cache")
+ }
+ time.Sleep(5 * time.Millisecond)
+ }
+ if calls != 1 {
+ t.Fatalf("resolved %d times, want one lookup shared by every connection", calls)
+ }
+}
+
func TestResolveNameFindsTheCurrentAccount(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("uids are a Unix concept")
diff --git a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go
index c9e644bb..69998594 100644
--- a/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go
+++ b/collector-builder/exporter/beaconjsonexporter/beaconingestextension/names_windows.go
@@ -4,32 +4,20 @@ package beaconingestextension
import (
"strings"
- "time"
"golang.org/x/sys/windows"
)
// resolveName names a SID: DOMAIN\user, the form Windows hooks write to user.name. It asks
// LookupAccountSid, which consults a domain controller on a domain-joined machine and can wait on
-// one that does not answer, so the lookup is bounded by nameLookupTimeout like id(1) on Unix: past
-// it the event is named by SID only, and the cache retries later. Windows has no id(1) to fall
-// back to.
+// one that does not answer; nameCache bounds how long a connection waits for it. Windows has no
+// id(1) to fall back to.
func resolveName(sid string) string {
- done := make(chan string, 1)
- go func() {
- name, err := lookupAccount(sid)
- if err != nil || !validWindowsName(name) {
- done <- ""
- return
- }
- done <- name
- }()
- select {
- case name := <-done:
- return name
- case <-time.After(nameLookupTimeout):
+ name, err := lookupAccount(sid)
+ if err != nil || !validWindowsName(name) {
return ""
}
+ return name
}
// lookupAccount names a SID with LookupAccountSid alone; tests replace it. Not os/user.LookupId: