From 8e91aa767e5c014c977807c4286e71b3e1b0f393 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 a1d5336d8b42..f5e0e8539670 100644 --- a/x-pack/elastic-agent/CHANGELOG.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.asciidoc @@ -42,3 +42,4 @@ - Allow CLI overrides of paths {pull}17781[17781] - Enable Filebeat input: S3, Azureeventhub, cloudfoundry, httpjson, netflow, o365audit. {pull}17909[17909] - Use data subfolder as default for process logs {pull}17960[17960] +- 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 edec6b4c7f8f..a4efa6b184d4 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 323937b080c8..8a8624b16754 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 c20f60972f95..9e0084bc6f13 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 6431ec039754..f1071e657de4 100644 --- a/x-pack/elastic-agent/pkg/agent/application/managed_mode.go +++ b/x-pack/elastic-agent/pkg/agent/application/managed_mode.go @@ -68,7 +68,7 @@ func newManaged( return nil, err } - path := fleetAgentConfigPath() + path := info.AgentConfigFile() // TODO(ph): Define the encryption password. store := storage.NewEncryptedDiskStore(path, []byte("")) @@ -138,9 +138,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)