Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick #11067 to 6.7: Fix a issue when cancelling an enroll. #11186

Merged
merged 2 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ https://github.com/elastic/beats/compare/v6.6.0...6.x[Check the HEAD diff]
- Fix encoding of timestamps when using disk spool. {issue}10099[10099]
- 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]

*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.UUID, 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)
}