Skip to content

Commit adc57ca

Browse files
authored
Remove use of github.com/elastic/elastic-agent-client from OSS Beats (#48353)
* Remove elastic-agent-client import for useragent. * Remove elastic-agent-client from manager interface. * Remove use of elastic-agent-client in features. * Move cloned types to separate file. * Add changelog. * Add depguard rule to forbid future use. * Fix OtelManager * Fix osquerybeat tests * Run mage fmt * Fix lint warning: remove unnecessary cast
1 parent 990735b commit adc57ca

14 files changed

Lines changed: 271 additions & 143 deletions

File tree

.golangci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ linters:
3737
deny:
3838
- pkg: github.com/elastic/beats/v7/x-pack
3939
desc: Apache 2.0 licensed code cannot depend on Elastic licensed code (x-pack/).
40+
- pkg: github.com/elastic/elastic-agent-client
41+
desc: Apache 2.0 licensed code cannot depend on Elastic licensed code (elastic-agent-client).
4042
main:
4143
list-mode: lax
4244
deny:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# REQUIRED
2+
# Kind can be one of:
3+
# - breaking-change: a change to previously-documented behavior
4+
# - deprecation: functionality that is being removed in a later release
5+
# - bug-fix: fixes a problem in a previous version
6+
# - enhancement: extends functionality but does not break or fix existing behavior
7+
# - feature: new functionality
8+
# - known-issue: problems that we are aware of in a given version
9+
# - security: impacts on the security of a product or a user’s deployment.
10+
# - upgrade: important information for someone upgrading from a prior version
11+
# - other: does not fit into any of the other categories
12+
kind: bug-fix
13+
14+
# REQUIRED for all kinds
15+
# Change summary; a 80ish characters long description of the change.
16+
summary: Remove use of github.com/elastic/elastic-agent-client from OSS Beats.
17+
18+
# REQUIRED for breaking-change, deprecation, known-issue
19+
# Long description; in case the summary is not enough to describe the change
20+
# this field accommodate a description without length limits.
21+
# description:
22+
23+
# REQUIRED for breaking-change, deprecation, known-issue
24+
# impact:
25+
26+
# REQUIRED for breaking-change, deprecation, known-issue
27+
# action:
28+
29+
# REQUIRED for all kinds
30+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
31+
component: "all"
32+
33+
# AUTOMATED
34+
# OPTIONAL to manually add other PR URLs
35+
# PR URL: A link the PR that added the changeset.
36+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
37+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
38+
# Please provide it if you are adding a fragment for a different PR.
39+
pr: https://github.com/elastic/beats/pull/48353
40+
41+
# AUTOMATED
42+
# OPTIONAL to manually add other issue URLs
43+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
44+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
45+
# issue: https://github.com/owner/repo/1234

libbeat/beat/beat.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/elastic/beats/v7/libbeat/instrumentation"
2424
"github.com/elastic/beats/v7/libbeat/management"
2525
"github.com/elastic/beats/v7/libbeat/version"
26-
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
2726
"github.com/elastic/elastic-agent-libs/config"
2827
"github.com/elastic/elastic-agent-libs/keystore"
2928
"github.com/elastic/elastic-agent-libs/paths"
@@ -102,9 +101,9 @@ func (beat *Beat) userAgentMode() useragent.AgentManagementMode {
102101

103102
info := beat.Manager.AgentInfo()
104103
switch info.ManagedMode {
105-
case proto.AgentManagedMode_MANAGED:
104+
case management.AgentManagedMode_MANAGED:
106105
return useragent.AgentManagementModeManaged
107-
case proto.AgentManagedMode_STANDALONE:
106+
case management.AgentManagedMode_STANDALONE:
108107
return useragent.AgentManagementModeUnmanaged
109108
}
110109
// this is probably not reachable

libbeat/beat/beat_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,30 @@ import (
2424

2525
"github.com/stretchr/testify/require"
2626

27+
"github.com/elastic/beats/v7/libbeat/management"
2728
"github.com/elastic/beats/v7/libbeat/management/status"
28-
"github.com/elastic/elastic-agent-client/v7/pkg/client"
29-
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
3029
"github.com/elastic/elastic-agent-libs/config"
3130
)
3231

3332
type testManager struct {
3433
isUnpriv bool
35-
mgmtMode proto.AgentManagedMode
34+
mgmtMode management.AgentManagedMode
3635
isEnabled bool
3736
}
3837

3938
func (tm testManager) UpdateStatus(_ status.Status, _ string) {}
4039
func (tm testManager) Enabled() bool { return tm.isEnabled }
4140
func (tm testManager) Start() error { return nil }
4241
func (tm testManager) Stop() {}
43-
func (tm testManager) AgentInfo() client.AgentInfo {
44-
return client.AgentInfo{Unprivileged: tm.isUnpriv, ManagedMode: tm.mgmtMode}
42+
func (tm testManager) AgentInfo() management.AgentInfo {
43+
return management.AgentInfo{Unprivileged: tm.isUnpriv, ManagedMode: tm.mgmtMode}
4544
}
46-
func (tm testManager) SetStopCallback(_ func()) {}
47-
func (tm testManager) CheckRawConfig(_ *config.C) error { return nil }
48-
func (tm testManager) RegisterAction(_ client.Action) {}
49-
func (tm testManager) UnregisterAction(_ client.Action) {}
50-
func (tm testManager) SetPayload(_ map[string]interface{}) {}
51-
func (tm testManager) RegisterDiagnosticHook(_ string, _ string, _ string, _ string, _ client.DiagnosticHook) {
45+
func (tm testManager) SetStopCallback(_ func()) {}
46+
func (tm testManager) CheckRawConfig(_ *config.C) error { return nil }
47+
func (tm testManager) RegisterAction(_ management.Action) {}
48+
func (tm testManager) UnregisterAction(_ management.Action) {}
49+
func (tm testManager) SetPayload(_ map[string]interface{}) {}
50+
func (tm testManager) RegisterDiagnosticHook(_ string, _ string, _ string, _ string, _ management.DiagnosticHook) {
5251
}
5352

5453
func TestUserAgentString(t *testing.T) {
@@ -60,25 +59,25 @@ func TestUserAgentString(t *testing.T) {
6059
{
6160
name: "managed-unprivileged",
6261
beat: &Beat{Info: Info{Beat: "testbeat"},
63-
Manager: testManager{isEnabled: true, isUnpriv: true, mgmtMode: proto.AgentManagedMode_MANAGED}},
62+
Manager: testManager{isEnabled: true, isUnpriv: true, mgmtMode: management.AgentManagedMode_MANAGED}},
6463
expectedComments: []string{"Managed", "Unprivileged"},
6564
},
6665
{
6766
name: "managed-privileged",
6867
beat: &Beat{Info: Info{Beat: "testbeat"},
69-
Manager: testManager{isEnabled: true, isUnpriv: false, mgmtMode: proto.AgentManagedMode_MANAGED}},
68+
Manager: testManager{isEnabled: true, isUnpriv: false, mgmtMode: management.AgentManagedMode_MANAGED}},
7069
expectedComments: []string{"Managed"},
7170
},
7271
{
7372
name: "unmanaged-privileged",
7473
beat: &Beat{Info: Info{Beat: "testbeat"},
75-
Manager: testManager{isEnabled: true, isUnpriv: false, mgmtMode: proto.AgentManagedMode_STANDALONE}},
74+
Manager: testManager{isEnabled: true, isUnpriv: false, mgmtMode: management.AgentManagedMode_STANDALONE}},
7675
expectedComments: []string{"Unmanaged"},
7776
},
7877
{
7978
name: "unmanaged-unprivileged",
8079
beat: &Beat{Info: Info{Beat: "testbeat"},
81-
Manager: testManager{isEnabled: true, isUnpriv: true, mgmtMode: proto.AgentManagedMode_STANDALONE}},
80+
Manager: testManager{isEnabled: true, isUnpriv: true, mgmtMode: management.AgentManagedMode_STANDALONE}},
8281
expectedComments: []string{"Unmanaged", "Unprivileged"},
8382
},
8483
{

libbeat/cmd/instance/beat_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/elastic/beats/v7/libbeat/management/status"
3535
"github.com/elastic/beats/v7/libbeat/outputs"
3636
"github.com/elastic/beats/v7/libbeat/publisher/queue/memqueue"
37-
"github.com/elastic/elastic-agent-client/v7/pkg/client"
3837
"github.com/elastic/elastic-agent-libs/config"
3938
"github.com/elastic/elastic-agent-libs/logp"
4039
"github.com/elastic/elastic-agent-libs/logp/logptest"
@@ -499,18 +498,18 @@ type mockManager struct {
499498
enabled bool
500499
}
501500

502-
func (m mockManager) AgentInfo() client.AgentInfo { return client.AgentInfo{} }
503-
func (m mockManager) CheckRawConfig(cfg *config.C) error { return nil }
504-
func (m mockManager) Enabled() bool { return m.enabled }
505-
func (m mockManager) RegisterAction(action client.Action) {}
506-
func (m mockManager) RegisterDiagnosticHook(name, description, filename, contentType string, hook client.DiagnosticHook) {
501+
func (m mockManager) AgentInfo() management.AgentInfo { return management.AgentInfo{} }
502+
func (m mockManager) CheckRawConfig(cfg *config.C) error { return nil }
503+
func (m mockManager) Enabled() bool { return m.enabled }
504+
func (m mockManager) RegisterAction(action management.Action) {}
505+
func (m mockManager) RegisterDiagnosticHook(name, description, filename, contentType string, hook management.DiagnosticHook) {
507506
}
508507
func (m mockManager) SetPayload(payload map[string]any) {}
509508
func (m mockManager) SetStopCallback(f func()) {}
510509
func (m mockManager) Start() error { return nil }
511510
func (m mockManager) Status() status.Status { return status.Status(-42) }
512511
func (m mockManager) Stop() {}
513-
func (m mockManager) UnregisterAction(action client.Action) {}
512+
func (m mockManager) UnregisterAction(action management.Action) {}
514513
func (m mockManager) UpdateStatus(status status.Status, msg string) {}
515514

516515
func TestManager(t *testing.T) {

libbeat/features/features.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"sync"
2323
"sync/atomic"
2424

25-
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
2625
conf "github.com/elastic/elastic-agent-libs/config"
2726
)
2827

@@ -43,32 +42,6 @@ type fflags struct {
4342
logRunAsFilestream atomic.Bool
4443
}
4544

46-
// NewConfigFromProto converts the given *proto.Features object to
47-
// a *config.C object.
48-
func NewConfigFromProto(f *proto.Features) (*conf.C, error) {
49-
if f == nil {
50-
return nil, nil
51-
}
52-
53-
var beatCfg struct {
54-
Features *proto.Features `config:"features"`
55-
}
56-
57-
beatCfg.Features = f
58-
59-
c, err := conf.NewConfigFrom(&beatCfg)
60-
if err != nil {
61-
return nil, fmt.Errorf("unable to parse feature flags message into beat configuration: %w", err)
62-
}
63-
64-
_, err = c.Remove("features.source", -1)
65-
if err != nil {
66-
return nil, fmt.Errorf("unable to convert feature flags message to beat configuration: %w", err)
67-
}
68-
69-
return c, nil
70-
}
71-
7245
// UpdateFromConfig updates the feature flags configuration. If c is nil UpdateFromConfig is no-op.
7346
func UpdateFromConfig(c *conf.C) error {
7447
if c == nil {

libbeat/features/features_test.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,11 @@ package features
2020
import (
2121
"testing"
2222

23-
"google.golang.org/protobuf/types/known/structpb"
24-
25-
"github.com/elastic/elastic-agent-client/v7/pkg/proto"
2623
"github.com/elastic/elastic-agent-libs/config"
2724

2825
"github.com/stretchr/testify/require"
2926
)
3027

31-
func TestNewConfigFromProto(t *testing.T) {
32-
source, err := structpb.NewStruct(map[string]interface{}{
33-
"fqdn": map[string]interface{}{
34-
"enabled": false,
35-
},
36-
})
37-
require.NoError(t, err)
38-
39-
tests := map[string]struct {
40-
protoFeatures *proto.Features
41-
expected *config.C
42-
}{
43-
"nil": {
44-
protoFeatures: nil,
45-
expected: nil,
46-
},
47-
"fqdn_enabled": {
48-
protoFeatures: &proto.Features{Fqdn: &proto.FQDNFeature{Enabled: true}},
49-
expected: config.MustNewConfigFrom(`
50-
features:
51-
fqdn:
52-
enabled: true
53-
`),
54-
},
55-
"fqdn_disabled": {
56-
protoFeatures: &proto.Features{Fqdn: &proto.FQDNFeature{Enabled: false}},
57-
expected: config.MustNewConfigFrom(`
58-
features:
59-
fqdn:
60-
enabled: false
61-
`),
62-
},
63-
"with_source": {
64-
protoFeatures: &proto.Features{Fqdn: &proto.FQDNFeature{Enabled: true}, Source: source},
65-
expected: config.MustNewConfigFrom(`
66-
features:
67-
fqdn:
68-
enabled: true
69-
`),
70-
},
71-
}
72-
73-
for name, test := range tests {
74-
t.Run(name, func(t *testing.T) {
75-
c, err := NewConfigFromProto(test.protoFeatures)
76-
require.NoError(t, err)
77-
78-
require.Equal(t, test.expected, c)
79-
})
80-
}
81-
82-
}
83-
8428
func TestFQDN(t *testing.T) {
8529
tcs := []struct {
8630
name string

libbeat/management/management.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222

2323
"github.com/elastic/beats/v7/libbeat/common/reload"
2424
"github.com/elastic/beats/v7/libbeat/management/status"
25-
"github.com/elastic/elastic-agent-client/v7/pkg/client"
2625
"github.com/elastic/elastic-agent-libs/config"
2726
"github.com/elastic/elastic-agent-libs/logp"
2827
)
@@ -56,7 +55,7 @@ type Manager interface {
5655
Stop()
5756

5857
// AgentInfo returns the information of the agent to which the manager is connected.
59-
AgentInfo() client.AgentInfo
58+
AgentInfo() AgentInfo
6059

6160
// SetStopCallback accepts a function that need to be called when the manager want to shutdown the
6261
// beats. This is needed when you want your beats to be gracefully shutdown remotely by the Elastic Agent
@@ -67,16 +66,16 @@ type Manager interface {
6766
CheckRawConfig(cfg *config.C) error
6867

6968
// RegisterAction registers action handler with the client
70-
RegisterAction(action client.Action)
69+
RegisterAction(action Action)
7170

7271
// UnregisterAction unregisters action handler with the client
73-
UnregisterAction(action client.Action)
72+
UnregisterAction(action Action)
7473

7574
// SetPayload Allows to add additional metadata to future requests made by the manager.
7675
SetPayload(map[string]interface{})
7776

7877
// RegisterDiagnosticHook registers a callback for elastic-agent diagnostics
79-
RegisterDiagnosticHook(name string, description string, filename string, contentType string, hook client.DiagnosticHook)
78+
RegisterDiagnosticHook(name string, description string, filename string, contentType string, hook DiagnosticHook)
8079
}
8180

8281
// ManagerFactory is the factory type for creating a config manager
@@ -162,12 +161,12 @@ func (n *FallbackManager) Stop() {
162161
// the nilManager is still used for shutdown on some cases,
163162
// but that does not mean the Beat is being managed externally,
164163
// hence it will always return false.
165-
func (n *FallbackManager) Enabled() bool { return false }
166-
func (n *FallbackManager) AgentInfo() client.AgentInfo { return client.AgentInfo{} }
167-
func (n *FallbackManager) Start() error { return nil }
168-
func (n *FallbackManager) CheckRawConfig(cfg *config.C) error { return nil }
169-
func (n *FallbackManager) RegisterAction(action client.Action) {}
170-
func (n *FallbackManager) UnregisterAction(action client.Action) {}
171-
func (n *FallbackManager) SetPayload(map[string]interface{}) {}
172-
func (n *FallbackManager) RegisterDiagnosticHook(_ string, _ string, _ string, _ string, _ client.DiagnosticHook) {
164+
func (n *FallbackManager) Enabled() bool { return false }
165+
func (n *FallbackManager) AgentInfo() AgentInfo { return AgentInfo{} }
166+
func (n *FallbackManager) Start() error { return nil }
167+
func (n *FallbackManager) CheckRawConfig(cfg *config.C) error { return nil }
168+
func (n *FallbackManager) RegisterAction(action Action) {}
169+
func (n *FallbackManager) UnregisterAction(action Action) {}
170+
func (n *FallbackManager) SetPayload(map[string]interface{}) {}
171+
func (n *FallbackManager) RegisterDiagnosticHook(_ string, _ string, _ string, _ string, _ DiagnosticHook) {
173172
}

0 commit comments

Comments
 (0)