Skip to content

Update user agent #45251

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff]
- removed support for a single `-` to precede multi-letter command line arguments. Use `--` instead. {issue}42117[42117] {pull}42209[42209]
- The Beats logger and file output rotate files when necessary. The beat now forces a file rotation when unexpectedly writing to a file through a symbolic link.
- Allow faccessat(2) in seccomp. {pull}43322[43322]
- Update user agent used by beats http clients {pull}45251[45251]

*Auditbeat*

Expand Down
4 changes: 2 additions & 2 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12346,11 +12346,11 @@ SOFTWARE

--------------------------------------------------------------------------------
Dependency : github.com/elastic/elastic-agent-libs
Version: v0.20.1
Version: v0.21.2
Licence type (autodetected): Apache-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.20.1/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.21.2/LICENSE:

Apache License
Version 2.0, January 2004
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ require (
github.com/elastic/bayeux v1.0.5
github.com/elastic/ebpfevents v0.7.0
github.com/elastic/elastic-agent-autodiscover v0.9.2
github.com/elastic/elastic-agent-libs v0.20.1
github.com/elastic/elastic-agent-libs v0.21.2
github.com/elastic/elastic-agent-system-metrics v0.11.11
github.com/elastic/go-elasticsearch/v8 v8.18.1
github.com/elastic/go-freelru v0.16.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,8 +364,8 @@ github.com/elastic/elastic-agent-autodiscover v0.9.2 h1:eBmru2v66HRRHOFf89rDl9OZ
github.com/elastic/elastic-agent-autodiscover v0.9.2/go.mod h1:RNaHnOTYfNptSTQUyZYnjypxmrR5AaE6BIap/175F5c=
github.com/elastic/elastic-agent-client/v7 v7.15.0 h1:nDB7v8TBoNuD6IIzC3z7Q0y+7bMgXoT2DsHfolO2CHE=
github.com/elastic/elastic-agent-client/v7 v7.15.0/go.mod h1:6h+f9QdIr3GO2ODC0Y8+aEXRwzbA5W4eV4dd/67z7nI=
github.com/elastic/elastic-agent-libs v0.20.1 h1:M7ZME7yctVhI9349OiG0VQ8a+RsuParA/ZUgCuctwBE=
github.com/elastic/elastic-agent-libs v0.20.1/go.mod h1:xSeIP3NtOIT4N2pPS4EyURmS1Q8mK0lWZ8Wd1Du6q3w=
github.com/elastic/elastic-agent-libs v0.21.2 h1:r4Tokxx8OvFUVw28foiZT/WHwGUM7JFdlb4TVJ25v9M=
github.com/elastic/elastic-agent-libs v0.21.2/go.mod h1:xSeIP3NtOIT4N2pPS4EyURmS1Q8mK0lWZ8Wd1Du6q3w=
github.com/elastic/elastic-agent-system-metrics v0.11.11 h1:Qjh3Zef23PfGlG91AF+9ciNLNQf/8cDJ4CalnLZtV3g=
github.com/elastic/elastic-agent-system-metrics v0.11.11/go.mod h1:GNqmKfvOt8PwORjbS6GllNdMfkLpOWyTa7P8oQq4E5o=
github.com/elastic/elastic-transport-go/v8 v8.7.0 h1:OgTneVuXP2uip4BA658Xi6Hfw+PeIOod2rY3GVMGoVE=
Expand Down
56 changes: 36 additions & 20 deletions libbeat/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,47 @@ type Beat struct {
Registry *reload.Registry // input, & output registry for configuration manager, should be instantiated in NewBeat
}

func (beat *Beat) userAgentMode() useragent.AgentManagementMode {
if beat.Manager == nil {
return useragent.AgentManagementModeUnknown
}
if !beat.Manager.Enabled() {
return useragent.AgentManagementModeStandalone
}

info := beat.Manager.AgentInfo()
switch info.ManagedMode {
case proto.AgentManagedMode_MANAGED:
return useragent.AgentManagementModeManaged
case proto.AgentManagedMode_STANDALONE:
return useragent.AgentManagementModeUnmanaged
}
// this is probably not reachable
return useragent.AgentManagementModeUnknown
}

func (beat *Beat) userAgentUnprivilegedMode() useragent.AgentUnprivilegedMode {
if beat.Manager == nil || !beat.Manager.Enabled() {
return useragent.AgentUnprivilegedModeUnknown
}
if beat.Manager.AgentInfo().Unprivileged {
return useragent.AgentUnprivilegedModeUnprivileged
}
return useragent.AgentUnprivilegedModePrivileged
}

// GenerateUserAgent populates the UserAgent field on the beat.Info struct
func (beat *Beat) GenerateUserAgent() {
// if we're in fleet mode, construct some additional elements for the UA comment field
comments := []string{}
if beat.Manager != nil && beat.Manager.Enabled() {
info := beat.Manager.AgentInfo()
if info.ManagedMode == proto.AgentManagedMode_MANAGED {
comments = append(comments, "Managed")
} else if info.ManagedMode == proto.AgentManagedMode_STANDALONE {
comments = append(comments, "Standalone")
}

if info.Unprivileged {
comments = append(comments, "Unprivileged")
}
userAgentProduct := "Libbeat"
if beat.Info.Beat != "" {
userAgentProduct = beat.Info.Beat
}

UserAgentProduct := beat.Info.Beat
if UserAgentProduct == "" {
UserAgentProduct = "Libbeat"
}
mode := beat.userAgentMode()
unprivileged := beat.userAgentUnprivilegedMode()

finalUserAgent := useragent.UserAgent(UserAgentProduct, version.GetDefaultVersion(),
version.Commit(), version.BuildTime().String(), comments...)
beat.Info.UserAgent = finalUserAgent
beat.Info.UserAgent = useragent.UserAgentWithBeatTelemetry(userAgentProduct, version.GetDefaultVersion(),
mode, unprivileged)
}

// BeatConfig struct contains the basic configuration of every beat
Expand Down
12 changes: 9 additions & 3 deletions libbeat/beat/beat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,30 @@ func TestUserAgentString(t *testing.T) {
name: "unmanaged-privileged",
beat: &Beat{Info: Info{Beat: "testbeat"},
Manager: testManager{isEnabled: true, isUnpriv: false, mgmtMode: proto.AgentManagedMode_STANDALONE}},
expectedComments: []string{"Standalone"},
expectedComments: []string{"Unmanaged"},
},
{
name: "unmanaged-unprivileged",
beat: &Beat{Info: Info{Beat: "testbeat"},
Manager: testManager{isEnabled: true, isUnpriv: true, mgmtMode: proto.AgentManagedMode_STANDALONE}},
expectedComments: []string{"Standalone", "Unprivileged"},
expectedComments: []string{"Unmanaged", "Unprivileged"},
},
{
name: "management-disabled",
beat: &Beat{Info: Info{Beat: "testbeat"},
Manager: testManager{isEnabled: false}},
expectedComments: []string{"Standalone"},
},
{
name: "no-management",
beat: &Beat{Info: Info{Beat: "testbeat"},
Manager: nil},
expectedComments: []string{},
},
}

// User-Agent will take the form of
// Elastic-testbeat/8.15.0 (linux; amd64; unknown; 0001-01-01 00:00:00 +0000 UTC; Standalone; Unprivileged)
// Elastic-testbeat/9.2.0 (linux; arm64; Managed; Unprivileged)
// the RFC (https://www.rfc-editor.org/rfc/rfc9110#name-user-agent) says the comment field can basically be anything,
// but we put metadata in it, delimited by '; '
uaReg := regexp.MustCompile(`Elastic-testbeat/([\d.]+) \(([\w-:+; ]+)\)`)
Expand Down
Loading