Skip to content

Commit a3fa4f4

Browse files
User Agent: Add a UserAgentLite function to display less information in the user_agent (#307)
* User Agent: Rearrange the user agent to prioritize displaying the most useful information. * User Agent: Rearrange the user agent to prioritize displaying the most useful information. * Add a new Lite function instead of changing the behavior of the existing one * Switch to known parameters + validate user agent size * Add AgentManagementMode and AgentUnprivilegedMode enums + now returning an error if length > 100 chars
1 parent 2839798 commit a3fa4f4

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

useragent/useragent.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,48 @@
1818
package useragent
1919

2020
import (
21+
"errors"
2122
"runtime"
2223
"strings"
2324
)
2425

26+
type AgentManagementMode int
27+
28+
const (
29+
// AgentManagementModeManaged indicates that the beat is managed by Fleet.
30+
AgentManagementModeManaged AgentManagementMode = iota
31+
// AgentManagementModeStandalone indicates that the beat is running in standalone mode.
32+
AgentManagementModeStandalone
33+
)
34+
35+
func (m AgentManagementMode) String() string {
36+
switch m {
37+
case AgentManagementModeManaged:
38+
return "Managed"
39+
case AgentManagementModeStandalone:
40+
return "Standalone"
41+
default:
42+
return "Unknown"
43+
}
44+
}
45+
46+
// AgentUnprivilegedMode indicates whether the beat is running in unprivileged mode.
47+
type AgentUnprivilegedMode bool
48+
49+
const (
50+
// AgentUnprivilegedModeUnprivileged indicates that the beat is running in unprivileged mode.
51+
AgentUnprivilegedModeUnprivileged AgentUnprivilegedMode = true
52+
// AgentUnprivilegedModePrivileged indicates that the beat is running in privileged mode.
53+
AgentUnprivilegedModePrivileged AgentUnprivilegedMode = false
54+
)
55+
56+
func (m AgentUnprivilegedMode) String() string {
57+
if m {
58+
return "Unprivileged"
59+
}
60+
return "Privileged"
61+
}
62+
2563
// UserAgent takes the capitalized name of the current beat and returns
2664
// an RFC compliant user agent string for that beat.
2765
func UserAgent(binaryNameCapitalized string, version, commit, buildTime string, additionalComments ...string) string {
@@ -43,3 +81,24 @@ func UserAgent(binaryNameCapitalized string, version, commit, buildTime string,
4381
builder.WriteByte(')')
4482
return builder.String()
4583
}
84+
85+
func UserAgentWithBeatTelemetry(binaryNameCapitalized string, version string, mode AgentManagementMode, unprivileged AgentUnprivilegedMode) (string, error) {
86+
var builder strings.Builder
87+
builder.WriteString("Elastic-" + binaryNameCapitalized + "/" + version + " ")
88+
uaValues := []string{
89+
runtime.GOOS,
90+
runtime.GOARCH,
91+
mode.String(),
92+
unprivileged.String(),
93+
}
94+
builder.WriteByte('(')
95+
builder.WriteString(strings.Join(uaValues, "; "))
96+
builder.WriteByte(')')
97+
98+
// Ensure the user agent string does not exceed 100 characters
99+
userAgent := builder.String()
100+
if len(userAgent) > 100 {
101+
return userAgent, errors.New("user agent string exceeds 100 characters")
102+
}
103+
return userAgent, nil
104+
}

useragent/useragent_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ import (
2525
)
2626

2727
const (
28-
v = "9.9.9"
29-
commit = "1234abcd"
30-
buildTime = "20001212"
28+
v = "10.10.11"
29+
commit = "a408c834fe3674b21546885890da17be05c91a51"
30+
buildTime = "2024-11-21 16:41:00 +0000"
31+
mode = AgentManagementModeManaged
32+
unprivileged = AgentUnprivilegedModeUnprivileged
3133
)
3234

3335
func TestUserAgent(t *testing.T) {
@@ -37,3 +39,11 @@ func TestUserAgent(t *testing.T) {
3739
ua2 := UserAgent("FakeBeat", v, commit, buildTime, "integration_name/1.2.3")
3840
assert.Regexp(t, regexp.MustCompile(`; integration_name\/1\.2\.3\)$`), ua2)
3941
}
42+
43+
func TestUserAgentWithBeatTelemetry(t *testing.T) {
44+
ua2, err := UserAgentWithBeatTelemetry("FakeBeat", v, mode, unprivileged)
45+
assert.Regexp(t, regexp.MustCompile(`^Elastic-FakeBeat`), ua2)
46+
assert.Regexp(t, regexp.MustCompile(`; Managed; Unprivileged\)$`), ua2)
47+
assert.LessOrEqual(t, len(ua2), 100, "User agent string should be less than 100 characters")
48+
assert.NoError(t, err)
49+
}

0 commit comments

Comments
 (0)