Skip to content

Commit

Permalink
Cherry-pick #11067 to 7.0: Fix a issue when cancelling an enroll. (#1…
Browse files Browse the repository at this point in the history
…1185)

Cherry-pick of PR #11067 to 7.0 branch. Original message: 

Fix an issue with a partial enroll, when a user refused to overrides a
local configuration actually the enroll command did already used the
token on the ES cluster, this commit move the confirm in the CM instead
of having it in the Enroll's function and is executed by sending the
token or creating any files on disk.

Fixes: #10150
  • Loading branch information
ph authored Mar 14, 2019
1 parent eaf4ec6 commit a0491cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ https://github.com/elastic/beats/compare/v7.0.0-beta1...master[Check the HEAD di
- Add missing host.* fields to fields.yml. {pull}11016[11016]
- Include ip and boolean type when generating index pattern. {pull}10995[10995]
- Using an environment variable for the password when enrolling a beat will now raise an error if the variable doesn't exist. {pull}10936[10936]
- Cancelling enrollment of a beat will not enroll the beat. {issue}10150[10150]
- Allow to configure Kafka fetching strategy for the topic metadata. {pull}10682[10682]

*Auditbeat*
Expand Down
26 changes: 20 additions & 6 deletions x-pack/libbeat/cmd/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ func genEnrollCmd(name, version string) *cobra.Command {
return err
}

confirm, err := confirmConfigOverwrite(force)
if err != nil {
return err
}

if !confirm {
fmt.Println("Enrollment was canceled by the user")
return nil
}

var enrollmentToken string
if len(args) == 2 {
// use given enrollment token
Expand All @@ -90,16 +100,12 @@ func genEnrollCmd(name, version string) *cobra.Command {
}
}

enrolled, err := management.Enroll(beat, config, enrollmentToken, force)
err = management.Enroll(beat, config, enrollmentToken)
if err != nil {
return errors.Wrap(err, "Error while enrolling")
}

if enrolled {
fmt.Println("Enrolled and ready to retrieve settings from Kibana")
} else {
fmt.Println("Enrollment was canceled by the user")
}
fmt.Println("Enrolled and ready to retrieve settings from Kibana")
return nil
}),
}
Expand All @@ -121,3 +127,11 @@ func kibanaConfig(config *common.Config) (*common.Config, error) {
}
return common.NewConfig(), nil
}

func confirmConfigOverwrite(force bool) (bool, error) {
if force {
return true, nil
}

return cli.Confirm("This will replace your current settings. Do you want to continue?", true)
}
60 changes: 24 additions & 36 deletions x-pack/libbeat/management/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/cmd/instance"
"github.com/elastic/beats/libbeat/common/cli"
"github.com/elastic/beats/libbeat/common/file"
"github.com/elastic/beats/libbeat/kibana"
"github.com/elastic/beats/x-pack/libbeat/management/api"
Expand All @@ -22,23 +21,27 @@ const accessTokenKey = "management.accesstoken"

// Enroll this beat to the given kibana
// This will use Central Management API to enroll and retrieve an access key for config retrieval
func Enroll(beat *instance.Beat, kibanaConfig *kibana.ClientConfig, enrollmentToken string, force bool) (bool, error) {
func Enroll(
beat *instance.Beat,
kibanaConfig *kibana.ClientConfig,
enrollmentToken string,
) error {
// Ignore kibana version to avoid permission errors
kibanaConfig.IgnoreVersion = true

client, err := api.NewClient(kibanaConfig)
if err != nil {
return false, err
return err
}

accessToken, err := client.Enroll(beat.Info.Beat, beat.Info.Name, beat.Info.Version, beat.Info.Hostname, beat.Info.ID, enrollmentToken)
if err != nil {
return false, err
return err
}

// Store access token in keystore
if err := storeAccessToken(beat, accessToken); err != nil {
return false, err
return err
}

// Enrolled, persist state
Expand All @@ -47,35 +50,28 @@ func Enroll(beat *instance.Beat, kibanaConfig *kibana.ClientConfig, enrollmentTo
config.AccessToken = "${" + accessTokenKey + "}"
config.Kibana = kibanaConfig

confirm, err := confirmConfigOverwrite(force)
configFile := cfgfile.GetDefaultCfgfile()

// backup current settings:
backConfigFile := configFile + ".bak"
fmt.Println("Saving a copy of current settings to " + backConfigFile)
err = file.SafeFileRotate(backConfigFile, configFile)
if err != nil {
return false, err
return errors.Wrap(err, "creating a backup copy of current settings")
}

if confirm {
configFile := cfgfile.GetDefaultCfgfile()

// backup current settings:
backConfigFile := configFile + ".bak"
fmt.Println("Saving a copy of current settings to " + backConfigFile)
err := file.SafeFileRotate(backConfigFile, configFile)
if err != nil {
return false, errors.Wrap(err, "creating a backup copy of current settings")
}

// create the new ones:
f, err := os.OpenFile(configFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return false, errors.Wrap(err, "opening settings file")
}
defer f.Close()
// create the new ones:
f, err := os.OpenFile(configFile, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
return errors.Wrap(err, "opening settings file")
}
defer f.Close()

if err := config.OverwriteConfigFile(f, beat.Beat.Info.Beat); err != nil {
return false, errors.Wrap(err, "overriding settings file")
}
if err := config.OverwriteConfigFile(f, beat.Beat.Info.Beat); err != nil {
return errors.Wrap(err, "overriding settings file")
}

return true, nil
return nil
}

func storeAccessToken(beat *instance.Beat, accessToken string) error {
Expand All @@ -92,11 +88,3 @@ func storeAccessToken(beat *instance.Beat, accessToken string) error {

return keystore.Save()
}

func confirmConfigOverwrite(force bool) (bool, error) {
if force {
return true, nil
}

return cli.Confirm("This will replace your current settings. Do you want to continue?", true)
}

0 comments on commit a0491cf

Please sign in to comment.