From 1c6b278fe51ce10bd2b36401e46b232537b800cf Mon Sep 17 00:00:00 2001 From: Michal Pristas Date: Mon, 4 May 2020 17:32:14 +0200 Subject: [PATCH] [Elastic-Agent] Follow home path for all config files (#18161) * fleet.yml at home * action_store * changelog --- x-pack/elastic-agent/CHANGELOG.asciidoc | 1 + .../pkg/agent/application/config.go | 11 ------- .../pkg/agent/application/enroll_cmd.go | 2 +- .../pkg/agent/application/info/agent_id.go | 33 ++++++++++++++----- .../pkg/agent/application/managed_mode.go | 6 ++-- 5 files changed, 29 insertions(+), 24 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.asciidoc b/x-pack/elastic-agent/CHANGELOG.asciidoc index 13f15a9f5b0..7c155d0eee5 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -47,3 +47,4 @@ - Use data subfolder as default for process logs {pull}17960[17960] - Do not require unnecessary configuration {pull}18003[18003] - Enable debug log level for Metricbeat and Filebeat when run under the Elastic Agent. {pull}17935[17935] +- Follow home path for all config files {pull}18161[18161] diff --git a/x-pack/elastic-agent/pkg/agent/application/config.go b/x-pack/elastic-agent/pkg/agent/application/config.go index 57945ba465b..bca9e615e17 100644 --- a/x-pack/elastic-agent/pkg/agent/application/config.go +++ b/x-pack/elastic-agent/pkg/agent/application/config.go @@ -8,7 +8,6 @@ import ( "fmt" "time" - "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/info" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/kibana" @@ -16,16 +15,6 @@ import ( logreporter "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/reporter/log" ) -// TODO(ph) correctly setup global path. -func fleetAgentConfigPath() string { - return info.AgentConfigFile -} - -// TODO(ph) correctly setup with global path. -func fleetActionStoreFile() string { - return info.AgentActionStoreFile -} - // Config define the configuration of the Agent. type Config struct { Management *config.Config `config:"management"` diff --git a/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go b/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go index 323937b080c..8a8624b1675 100644 --- a/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/application/enroll_cmd.go @@ -91,7 +91,7 @@ func NewEnrollCmd( store := storage.NewReplaceOnSuccessStore( configPath, DefaultAgentFleetConfig, - storage.NewEncryptedDiskStore(fleetAgentConfigPath(), []byte("")), + storage.NewEncryptedDiskStore(info.AgentConfigFile(), []byte("")), ) return NewEnrollCmdWithStore( diff --git a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go index c20f60972f9..9e0084bc6f1 100644 --- a/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go +++ b/x-pack/elastic-agent/pkg/agent/application/info/agent_id.go @@ -8,21 +8,23 @@ import ( "bytes" "fmt" "io" + "path/filepath" "github.com/gofrs/uuid" "gopkg.in/yaml.v2" + "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/application/paths" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/errors" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/agent/storage" "github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/config" ) -// AgentConfigFile is a name of file used to store agent information -const AgentConfigFile = "fleet.yml" +// defaultAgentConfigFile is a name of file used to store agent information +const defaultAgentConfigFile = "fleet.yml" const agentInfoKey = "agent_info" -// AgentActionStoreFile is the file that will contains the action that can be replayed after restart. -const AgentActionStoreFile = "action_store.yml" +// defaultAgentActionStoreFile is the file that will contains the action that can be replayed after restart. +const defaultAgentActionStoreFile = "action_store.yml" type persistentAgentInfo struct { ID string `json:"ID" yaml:"ID" config:"ID"` @@ -33,6 +35,16 @@ type ioStore interface { Load() (io.ReadCloser, error) } +// AgentConfigFile is a name of file used to store agent information +func AgentConfigFile() string { + return filepath.Join(paths.Home(), defaultAgentConfigFile) +} + +// AgentActionStoreFile is the file that will contains the action that can be replayed after restart. +func AgentActionStoreFile() string { + return filepath.Join(paths.Home(), defaultAgentActionStoreFile) +} + func generateAgentID() (string, error) { uid, err := uuid.NewV4() if err != nil { @@ -43,7 +55,8 @@ func generateAgentID() (string, error) { } func loadAgentInfo(forceUpdate bool) (*persistentAgentInfo, error) { - s := storage.NewEncryptedDiskStore(AgentConfigFile, []byte("")) + agentConfigFile := AgentConfigFile() + s := storage.NewEncryptedDiskStore(agentConfigFile, []byte("")) agentinfo, err := getInfoFromStore(s) if err != nil { @@ -67,6 +80,7 @@ func loadAgentInfo(forceUpdate bool) (*persistentAgentInfo, error) { } func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) { + agentConfigFile := AgentConfigFile() reader, err := s.Load() if err != nil { return nil, err @@ -75,9 +89,9 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) { cfg, err := config.NewConfigFrom(reader) if err != nil { return nil, errors.New(err, - fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile), + fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile), errors.TypeFilesystem, - errors.M(errors.MetaKeyPath, AgentConfigFile)) + errors.M(errors.MetaKeyPath, agentConfigFile)) } if err := reader.Close(); err != nil { @@ -110,6 +124,7 @@ func getInfoFromStore(s ioStore) (*persistentAgentInfo, error) { } func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error { + agentConfigFile := AgentConfigFile() reader, err := s.Load() if err != nil { return err @@ -117,9 +132,9 @@ func updateAgentInfo(s ioStore, agentInfo *persistentAgentInfo) error { cfg, err := config.NewConfigFrom(reader) if err != nil { - return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", AgentConfigFile), + return errors.New(err, fmt.Sprintf("fail to read configuration %s for the agent", agentConfigFile), errors.TypeFilesystem, - errors.M(errors.MetaKeyPath, AgentConfigFile)) + errors.M(errors.MetaKeyPath, agentConfigFile)) } if err := reader.Close(); err != nil { diff --git a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go index c440c6e1243..98d45fe2c88 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -57,7 +57,7 @@ func newManaged( return nil, err } - path := fleetAgentConfigPath() + path := info.AgentConfigFile() // TODO(ph): Define the encryption password. store := storage.NewEncryptedDiskStore(path, []byte("")) @@ -145,9 +145,9 @@ func newManaged( batchedAcker := newLazyAcker(acker) // Create the action store that will persist the last good policy change on disk. - actionStore, err := newActionStore(log, storage.NewDiskStore(fleetActionStoreFile())) + actionStore, err := newActionStore(log, storage.NewDiskStore(info.AgentActionStoreFile())) if err != nil { - return nil, errors.New(err, fmt.Sprintf("fail to read action store '%s'", fleetActionStoreFile())) + return nil, errors.New(err, fmt.Sprintf("fail to read action store '%s'", info.AgentActionStoreFile())) } actionAcker := newActionStoreAcker(batchedAcker, actionStore)