Apply custom --user-agent to BHE ingest requests#193
Apply custom --user-agent to BHE ingest requests#193ChrisJr404 wants to merge 1 commit intoSpecterOps:mainfrom
Conversation
The --user-agent flag added in f7bf368 only flowed through rest.NewRequest, so requests built directly in BHEClient.Ingest still hardcoded the default azurehound/<version> string. Anyone running azurehound against a hardened BHE proxy or wanting consistent UA across all outbound traffic still saw the default value on /api/v2/ingest. Pull the resolution logic into a reusable rest.UserAgent() helper and have both call sites use it. Tests cover default fallback, custom value, and end-to-end on the BHE ingest path. Closes SpecterOps#135.
|
All contributors have signed the CLA ✍️ ✅ |
|
I have read the CLA Document and I hereby sign the CLA |
WalkthroughThe PR centralizes User-Agent header selection into an exported ChangesUser-Agent Header Centralization
Estimated Code Review Effort🎯 2 (Simple) | ⏱️ ~12 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
client/rest/http_test.go (1)
30-31: ⚡ Quick winRestore prior global config value instead of always resetting to empty.
Lines 30-31, 40-42, 50-52, and 64-65 currently force
config.UserAgentto""in cleanup. That can leak state assumptions across tests; restoring the prior value keeps tests hermetic.Proposed change
func TestUserAgent_HonorsConfigValue(t *testing.T) { const custom = "my-custom-agent/1.2.3" + prev, _ := config.UserAgent.Value().(string) config.UserAgent.Set(custom) - t.Cleanup(func() { config.UserAgent.Set("") }) + t.Cleanup(func() { config.UserAgent.Set(prev) })func TestNewRequest_DefaultUserAgentWhenConfigEmpty(t *testing.T) { + prev, _ := config.UserAgent.Value().(string) config.UserAgent.Set("") - t.Cleanup(func() { config.UserAgent.Set("") }) + t.Cleanup(func() { config.UserAgent.Set(prev) })Also applies to: 40-42, 50-52, 64-65
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@client/rest/http_test.go` around lines 30 - 31, Tests currently set config.UserAgent to "" and unconditionally reset it to "" in the t.Cleanup handlers, which can leak state; capture the prior value before calling config.UserAgent.Set("") and in the t.Cleanup restore that saved value (i.e. call config.UserAgent.Set(prevUserAgent)) instead of always setting "". Update all places that set config.UserAgent.Set("") and register t.Cleanup (the occurrences around config.UserAgent.Set("") / t.Cleanup in this test file) to use this save-and-restore pattern so global state is preserved.client/bloodhound/client_test.go (1)
149-150: ⚡ Quick winUse prior-value restoration for
config.UserAgentin cleanup.Line 150 and Line 173 always reset to
"". Restoring the previous value is safer for test isolation and avoids hidden order dependencies.Proposed change
t.Run("custom user agent applied", func(t *testing.T) { const custom = "test-agent/9.9.9" + prev, _ := config.UserAgent.Value().(string) config.UserAgent.Set(custom) - t.Cleanup(func() { config.UserAgent.Set("") }) + t.Cleanup(func() { config.UserAgent.Set(prev) })t.Run("default user agent when config empty", func(t *testing.T) { + prev, _ := config.UserAgent.Value().(string) config.UserAgent.Set("") - t.Cleanup(func() { config.UserAgent.Set("") }) + t.Cleanup(func() { config.UserAgent.Set(prev) })Also applies to: 172-173
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@client/bloodhound/client_test.go` around lines 149 - 150, The test currently sets config.UserAgent.Set(custom) and unconditionally restores it to "" in t.Cleanup, which can create test-ordering bugs; change the test to capture the previous value (e.g., prev := config.UserAgent.Get()) before calling config.UserAgent.Set(custom) and then in t.Cleanup restore that previous value via config.UserAgent.Set(prev) (apply the same change for the second occurrence around lines where config.UserAgent.Set(...) and t.Cleanup are used).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@client/bloodhound/client_test.go`:
- Around line 149-150: The test currently sets config.UserAgent.Set(custom) and
unconditionally restores it to "" in t.Cleanup, which can create test-ordering
bugs; change the test to capture the previous value (e.g., prev :=
config.UserAgent.Get()) before calling config.UserAgent.Set(custom) and then in
t.Cleanup restore that previous value via config.UserAgent.Set(prev) (apply the
same change for the second occurrence around lines where
config.UserAgent.Set(...) and t.Cleanup are used).
In `@client/rest/http_test.go`:
- Around line 30-31: Tests currently set config.UserAgent to "" and
unconditionally reset it to "" in the t.Cleanup handlers, which can leak state;
capture the prior value before calling config.UserAgent.Set("") and in the
t.Cleanup restore that saved value (i.e. call
config.UserAgent.Set(prevUserAgent)) instead of always setting "". Update all
places that set config.UserAgent.Set("") and register t.Cleanup (the occurrences
around config.UserAgent.Set("") / t.Cleanup in this test file) to use this
save-and-restore pattern so global state is preserved.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 59fc78de-02b0-4e00-90d5-ccd5a8cfd194
📒 Files selected for processing (4)
client/bloodhound/client.goclient/bloodhound/client_test.goclient/rest/http.goclient/rest/http_test.go
The
--user-agentglobal flag added in f7bf368 only flows throughrest.NewRequest. The BHE ingest path inBHEClient.Ingestbuilds its request inline and hardcodes the defaultazurehound/<version>value, so/api/v2/ingeststill gets the default UA even when--user-agentis set.This finishes plumbing the flag everywhere by:
rest.NewRequestinto a smallrest.UserAgent()helper,rest.NewRequestandBHEClient.Ingest.Closes #135.
Tests
client/rest/http_test.go(new):TestUserAgent_DefaultsWhenUnset/TestUserAgent_HonorsConfigValueTestNewRequest_AppliesCustomUserAgent/TestNewRequest_DefaultUserAgentWhenConfigEmptyclient/bloodhound/client_test.go(added subtests onTestBHEClient_Ingest):custom user agent applied— spins up an httptest server, setsconfig.UserAgent, verifies the recorded UA on/api/v2/ingestdefault user agent when config empty— same harness with the flag unset, verifies fallback toconstants.UserAgent()Local run:
All green.
Notes
--user-agent(-U) was already inGlobalConfig.Summary by CodeRabbit
Refactor
Tests