Skip to content

Commit

Permalink
Add logic for beats User-Agent (#39403)
Browse files Browse the repository at this point in the history
* add logic for user-agent header

* change UA name

* fix linter

* fix tests

* linter...

* Revert "linter..."

This reverts commit 03505d8.

* oops

* major update for new features

* fix tests

* linter

* somehow changed go versions

* fix versioning, docs

* remove log line

* change test

* fix notice

* linter
  • Loading branch information
fearful-symmetry authored Jun 25, 2024
1 parent cef7b39 commit 72b6de3
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 16 deletions.
4 changes: 2 additions & 2 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18496,11 +18496,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

--------------------------------------------------------------------------------
Dependency : github.com/google/uuid
Version: v1.3.1
Version: v1.6.0
Licence type (autodetected): BSD-3-Clause
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/google/uuid@v1.3.1/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/google/uuid@v1.6.0/LICENSE:

Copyright (c) 2009,2014 Google Inc. All rights reserved.

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/elastic/beats/v7

go 1.21.0
go 1.21.9

toolchain go1.21.11

Expand Down Expand Up @@ -101,7 +101,7 @@ require (
github.com/google/flatbuffers v23.5.26+incompatible
github.com/google/go-cmp v0.6.0
github.com/google/gopacket v1.1.19
github.com/google/uuid v1.3.1
github.com/google/uuid v1.6.0
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75
github.com/h2non/filetype v1.1.1
github.com/hashicorp/go-retryablehttp v0.6.6
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,9 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/enterprise-certificate-proxy v0.2.4 h1:uGy6JWR/uMIILU8wbf+OkstIrNiMjGpEIyhx8f6W7s4=
github.com/googleapis/enterprise-certificate-proxy v0.2.4/go.mod h1:AwSRAtLfXpU5Nm3pW+v7rGDHp09LsPtGY9MduiEsR9k=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand Down
23 changes: 12 additions & 11 deletions libbeat/beat/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,18 @@ import (

// Info stores a beats instance meta data.
type Info struct {
Beat string // The actual beat's name
IndexPrefix string // The beat's index prefix in Elasticsearch.
Version string // The beat version. Defaults to the libbeat version when an implementation does not set a version
ElasticLicensed bool // Whether the beat is licensed under and Elastic License
Name string // configured beat name
Hostname string // hostname
FQDN string // FQDN
ID uuid.UUID // ID assigned to beat machine
EphemeralID uuid.UUID // ID assigned to beat process invocation (PID)
FirstStart time.Time // The time of the first start of the Beat.
StartTime time.Time // The time of last start of the Beat. Updated when the Beat is started or restarted.
Beat string // The actual beat's name
IndexPrefix string // The beat's index prefix in Elasticsearch.
Version string // The beat version. Defaults to the libbeat version when an implementation does not set a version
ElasticLicensed bool // Whether the beat is licensed under and Elastic License
Name string // configured beat name
Hostname string // hostname
FQDN string // FQDN
ID uuid.UUID // ID assigned to beat machine
EphemeralID uuid.UUID // ID assigned to beat process invocation (PID)
FirstStart time.Time // The time of the first start of the Beat.
StartTime time.Time // The time of last start of the Beat. Updated when the Beat is started or restarted.
UserAgentPostfix string // A string postfix that's postfixed to the existing user-agent string used by the elasticsearch output

// Monitoring-related fields
Monitoring struct {
Expand Down
13 changes: 13 additions & 0 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import (
"github.com/elastic/beats/v7/libbeat/publisher/processing"
"github.com/elastic/beats/v7/libbeat/publisher/queue/diskqueue"
"github.com/elastic/beats/v7/libbeat/version"
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/elastic/elastic-agent-libs/config"
"github.com/elastic/elastic-agent-libs/file"
"github.com/elastic/elastic-agent-libs/filewatcher"
Expand Down Expand Up @@ -864,6 +865,18 @@ func (b *Beat) configure(settings Settings) error {
version.SetPackageVersion(b.Info.Version)
}

// if we're in fleet mode, construct a custom user-agent
if fleetmode.Enabled() {
userAgent := ""
info := b.Manager.AgentInfo()
if info.ManagedMode == proto.AgentManagedMode_MANAGED {
userAgent = "Agent-Managed"
} else if info.ManagedMode == proto.AgentManagedMode_STANDALONE {
userAgent = "Agent-Standalone"
}
b.Info.UserAgentPostfix = userAgent
}

if err := b.Manager.CheckRawConfig(b.RawConfig); err != nil {
return err
}
Expand Down
9 changes: 9 additions & 0 deletions libbeat/esleg/eslegclient/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ type ConnectionSettings struct {
IdleConnTimeout time.Duration

Transport httpcommon.HTTPTransportSettings

// UserAgentPostfix can be used to report the agent running mode
// to ES via the User Agent string. If running under Agent (fleetmode.Enabled() == true)
// then this string will be appended to the user agent.
UserAgentPostfix string
}

type ESPingData struct {
Expand Down Expand Up @@ -135,6 +140,10 @@ func NewConnection(s ConnectionSettings) (*Connection, error) {
if s.Beatname == "" {
s.Beatname = "Libbeat"
}
if s.UserAgentPostfix != "" {
s.Beatname = fmt.Sprintf("%s-%s", s.Beatname, s.UserAgentPostfix)
}

userAgent := useragent.UserAgent(s.Beatname, version.GetDefaultVersion(), version.Commit(), version.BuildTime().String())

// Default the product origin header to beats if it wasn't already set.
Expand Down
21 changes: 21 additions & 0 deletions libbeat/esleg/eslegclient/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"context"
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -104,9 +106,28 @@ func TestHeaders(t *testing.T) {
require.NoError(t, err)

require.Equal(t, req.Header, http.Header(td.expected))

}
}

func TestUserAgentHeader(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
prefix := "Elastic-testbeat-Agent"
if !strings.HasPrefix(r.UserAgent(), prefix) {
t.Errorf("User-Agent must start with '%s', got '%s'", prefix, r.UserAgent())
}
_, _ = w.Write([]byte("{}"))
}))
defer server.Close()
conn, err := NewConnection(ConnectionSettings{
URL: server.URL,
Beatname: "testbeat",
UserAgentPostfix: "Agent",
})
require.NoError(t, err)
require.NoError(t, conn.Connect(), "conn.Connect must not return an error")
}

func BenchmarkExecHTTPRequest(b *testing.B) {
for _, td := range []struct {
input map[string]string
Expand Down
1 change: 1 addition & 0 deletions libbeat/outputs/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func makeES(
EscapeHTML: esConfig.EscapeHTML,
Transport: esConfig.Transport,
IdleConnTimeout: esConfig.Transport.IdleConnTimeout,
UserAgentPostfix: beatInfo.UserAgentPostfix,
},
indexSelector: indexSelector,
pipelineSelector: pipelineSelector,
Expand Down

0 comments on commit 72b6de3

Please sign in to comment.