Skip to content

Commit

Permalink
Cherry-pick #10706 to 6.7: Initialize Paths before loading the keysto…
Browse files Browse the repository at this point in the history
…re (#11497)

Cherry-pick of PR #10706  to 6.7 branch. Original message: 

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.
  • Loading branch information
ph authored Mar 29, 2019
1 parent 14e8b6e commit dca4c4b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ https://github.com/elastic/beats/compare/v6.7.0...6.x[Check the HEAD diff]

*Affecting all Beats*

- Initialize the Paths before the keystore and save the keystore into `data/{beatname}.keystore`. {pull}10706[10706]

*Auditbeat*

*Filebeat*
Expand Down
28 changes: 23 additions & 5 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,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)
Expand Down Expand Up @@ -599,11 +603,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)
}
Expand Down Expand Up @@ -1005,3 +1004,22 @@ func LoadKeystore(cfg *common.Config, name string) (keystore.Keystore, error) {
defaultPathConfig := paths.Resolve(paths.Data, fmt.Sprintf("%s.keystore", name))
return keystore.Factory(keystoreCfg, defaultPathConfig)
}

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
}

0 comments on commit dca4c4b

Please sign in to comment.