From 4920cd69346f6320f9bdebcff602ba874666f492 Mon Sep 17 00:00:00 2001 From: Gil Raphaelli Date: Tue, 19 Mar 2019 19:58:12 -0400 Subject: [PATCH] Cherry-pick #10706 to 7.0: Initialize Paths before loading the keystore (#11325) * Initialize Paths before loading the keystore (#10706) * Initialize Paths before loading the keystore The paths were incorrectly initialized meaning that instead of creating the keystore in the data directory it was created next to the binary. The problem was the call to `paths.InitPaths()` was done after loading the keystore, this was causing a chicken and egg situation and `paths.Resolve(path.Data, "hello")` was returning "hello" instead of `data/hello`. To solve that situation we do a partial extract of the configuration, just enough to initialize the paths and we move on to the keystore and the complete unpack. (cherry picked from commit 3dbc2335a2f6be4f8f90804a98dc9709c7936a9f) * Update CHANGELOG.next.asciidoc --- CHANGELOG.next.asciidoc | 1 + libbeat/cmd/instance/beat.go | 28 +++++++++++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 70acf4f6f89..638f9f92338 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -14,6 +14,7 @@ https://github.com/elastic/beats/compare/v7.0.0-beta1...master[Check the HEAD di info from the cloud.machine.type and cloud.availability_zone. {issue}10968[10968] - Add `cleanup_timeout` option to docker autodiscover, to wait some time before removing configurations after a container is stopped. {issue]10374[10374] {pull}10905[10905] - Rename `migration.enabled` config to `migration.6_to_7.enabled`. {pull}11284[11284] +- Initialize the Paths before the keystore and save the keystore into `data/{beatname}.keystore`. {pull}10706[10706] *Auditbeat* diff --git a/libbeat/cmd/instance/beat.go b/libbeat/cmd/instance/beat.go index 1184e507ed1..40e647ea5dc 100644 --- a/libbeat/cmd/instance/beat.go +++ b/libbeat/cmd/instance/beat.go @@ -526,6 +526,10 @@ func (b *Beat) configure(settings Settings) error { return fmt.Errorf("error loading config file: %v", err) } + if err := initPaths(cfg); err != nil { + return err + } + // We have to initialize the keystore before any unpack or merging the cloud // options. store, err := LoadKeystore(cfg, b.Info.Beat) @@ -558,11 +562,6 @@ func (b *Beat) configure(settings Settings) error { b.Info.Name = name } - err = paths.InitPaths(&b.Config.Path) - if err != nil { - return fmt.Errorf("error setting default paths: %v", err) - } - if err := configure.Logging(b.Info.Beat, b.Config.Logging); err != nil { return fmt.Errorf("error initializing logging: %v", err) } @@ -917,3 +916,22 @@ func initKibanaConfig(beatConfig beatConfig) (*common.Config, error) { } return kibanaConfig, nil } + +func initPaths(cfg *common.Config) error { + // To Fix the chicken-egg problem with the Keystore and the loading of the configuration + // files we are doing a partial unpack of the configuration file and only take into consideration + // the paths field. After we will unpack the complete configuration and keystore reference + // will be correctly replaced. + partialConfig := struct { + Path paths.Path `config:"path"` + }{} + + if err := cfg.Unpack(&partialConfig); err != nil { + return fmt.Errorf("error extracting default paths: %+v", err) + } + + if err := paths.InitPaths(&partialConfig.Path); err != nil { + return fmt.Errorf("error setting default paths: %+v", err) + } + return nil +}