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

[Elastic Agent] Fix issue with ensureServiceToken. #29800

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Changes from 1 commit
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
30 changes: 18 additions & 12 deletions x-pack/elastic-agent/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,11 +199,6 @@ func containerCmd(streams *cli.IOStreams, cmd *cobra.Command) error {
}
}

err = ensureServiceToken(streams, &cfg)
if err != nil {
return err
}

// start apm-server legacy process when in cloud mode
var wg sync.WaitGroup
var apmProc *process.Info
Expand Down Expand Up @@ -287,6 +282,19 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
}
}
if cfg.Fleet.Enroll {
if cfg.FleetServer.Enable {
if client == nil {
client, err = kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
return err
}
}
err = ensureServiceToken(streams, client, &cfg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this move to the line 273? That way the call to kibanaSetup uses a service_token as well

if err != nil {
return err
}
}

var policy *kibanaPolicy
token := cfg.Fleet.EnrollmentToken
if token == "" && !cfg.FleetServer.Enable {
Expand All @@ -309,7 +317,10 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
if policy != nil {
policyID = policy.ID
}
logInfo(streams, "Policy selected for enrollment: ", policyID)
if policyID != "" {
logInfo(streams, "Policy selected for enrollment: ", policyID)
}

cmdArgs, err := buildEnrollArgs(cfg, token, policyID)
if err != nil {
return err
Expand Down Expand Up @@ -339,7 +350,7 @@ type TokenResp struct {
// ensureServiceToken will ensure that the cfg specified has the service_token attributes filled.
//
// If no token is specified it will use the elasticsearch username/password to request a new token from Kibana
func ensureServiceToken(streams *cli.IOStreams, cfg *setupConfig) error {
func ensureServiceToken(streams *cli.IOStreams, client *kibana.Client, cfg *setupConfig) error {
// There's already a service token
if cfg.Kibana.Fleet.ServiceToken != "" || cfg.FleetServer.Elasticsearch.ServiceToken != "" {
return nil
Expand All @@ -349,11 +360,6 @@ func ensureServiceToken(streams *cli.IOStreams, cfg *setupConfig) error {
}

logInfo(streams, "Requesting service_token from Kibana.")
client, err := kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
return err
}

code, r, err := client.Connection.Request("POST", "/api/fleet/service-tokens", nil, nil, nil)
if err != nil {
return fmt.Errorf("request to get security token from Kibana failed: %w", err)
Expand Down