From 70ac1b054be58aa3c3c9057d78d72fe8c6a623ae Mon Sep 17 00:00:00 2001 From: Mariana Dima Date: Thu, 9 Dec 2021 17:08:53 +0100 Subject: [PATCH] Agent configuration overridden by default fleet config (#29297) * replace config * changelog * add test on merge * fmt --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../pkg/agent/application/application.go | 4 +-- .../pkg/agent/application/application_test.go | 33 +++++++++++++++++++ x-pack/elastic-agent/pkg/agent/cmd/run.go | 2 +- 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 385f87aa19d..b75a405d4bb 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -91,6 +91,7 @@ - Snapshot artifact lookup will use agent.download proxy settings. {issue}27903[27903] {pull}27904[27904] - Fix lazy acker to only add new actions to the batch. {pull}27981[27981] - Allow HTTP metrics to run in bootstrap mode. Add ability to adjust timeouts for Fleet Server. {pull}28260[28260] +- Fix agent configuration overwritten by default fleet config. {pull}29297[29297] ==== New features diff --git a/x-pack/elastic-agent/pkg/agent/application/application.go b/x-pack/elastic-agent/pkg/agent/application/application.go index e2f7a55ce3e..d0de160340d 100644 --- a/x-pack/elastic-agent/pkg/agent/application/application.go +++ b/x-pack/elastic-agent/pkg/agent/application/application.go @@ -39,10 +39,11 @@ type upgraderControl interface { } // New creates a new Agent and bootstrap the required subsystem. -func New(log *logger.Logger, pathConfigFile string, reexec reexecManager, statusCtrl status.Controller, uc upgraderControl, agentInfo *info.AgentInfo) (Application, error) { +func New(log *logger.Logger, reexec reexecManager, statusCtrl status.Controller, uc upgraderControl, agentInfo *info.AgentInfo) (Application, error) { // Load configuration from disk to understand in which mode of operation // we must start the elastic-agent, the mode of operation cannot be changed without restarting the // elastic-agent. + pathConfigFile := paths.ConfigFile() rawConfig, err := config.LoadFile(pathConfigFile) if err != nil { return nil, err @@ -66,7 +67,6 @@ func createApplication( ) (Application, error) { log.Info("Detecting execution mode") ctx := context.Background() - cfg, err := configuration.NewFromConfig(rawConfig) if err != nil { return nil, err diff --git a/x-pack/elastic-agent/pkg/agent/application/application_test.go b/x-pack/elastic-agent/pkg/agent/application/application_test.go index 7c1975fef64..7ae0032d483 100644 --- a/x-pack/elastic-agent/pkg/agent/application/application_test.go +++ b/x-pack/elastic-agent/pkg/agent/application/application_test.go @@ -3,3 +3,36 @@ // you may not use this file except in compliance with the Elastic License. package application + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" +) + +func TestMergeFleetConfig(t *testing.T) { + cfg := map[string]interface{}{ + "fleet": map[string]interface{}{ + "enabled": true, + "kibana": map[string]interface{}{"host": "demo"}, + "access_api_key": "123", + }, + "agent": map[string]interface{}{ + "grpc": map[string]interface{}{ + "port": uint16(6790), + }, + }, + } + + rawConfig := config.MustNewConfigFrom(cfg) + storage, conf, err := mergeFleetConfig(rawConfig) + require.NoError(t, err) + assert.NotNil(t, storage) + assert.NotNil(t, conf) + assert.Equal(t, conf.Fleet.Enabled, cfg["fleet"].(map[string]interface{})["enabled"]) + assert.Equal(t, conf.Fleet.AccessAPIKey, cfg["fleet"].(map[string]interface{})["access_api_key"]) + assert.Equal(t, conf.Settings.GRPC.Port, cfg["agent"].(map[string]interface{})["grpc"].(map[string]interface{})["port"].(uint16)) +} diff --git a/x-pack/elastic-agent/pkg/agent/cmd/run.go b/x-pack/elastic-agent/pkg/agent/cmd/run.go index 1c8c1dd4916..ba37febd674 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/run.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/run.go @@ -142,7 +142,7 @@ func run(streams *cli.IOStreams, override cfgOverrider) error { } defer control.Stop() - app, err := application.New(logger, pathConfigFile, rex, statusCtrl, control, agentInfo) + app, err := application.New(logger, rex, statusCtrl, control, agentInfo) if err != nil { return err }