Skip to content

Commit

Permalink
[Elastic Agent] Fix issue with ensureServiceToken. (#29800)
Browse files Browse the repository at this point in the history
* Fix issue with ensureServiceToken.

* Move ensureServiceToken up to line 273.

(cherry picked from commit cb5f2eb)
  • Loading branch information
blakerouse authored and mergify-bot committed Jan 12, 2022
1 parent c74370a commit d147dd5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 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 @@ -274,6 +269,12 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
return run(streams, logToStderr)
}

if cfg.Kibana.Fleet.Setup || cfg.Fleet.Enroll {
err = ensureServiceToken(streams, &cfg)
if err != nil {
return err
}
}
if cfg.Kibana.Fleet.Setup {
client, err = kibanaClient(cfg.Kibana, cfg.Kibana.Headers)
if err != nil {
Expand Down Expand Up @@ -309,7 +310,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 @@ -349,11 +353,13 @@ func ensureServiceToken(streams *cli.IOStreams, cfg *setupConfig) error {
}

logInfo(streams, "Requesting service_token from Kibana.")

// Client is not passed in to this function because this function will use username/password and then
// all the following clients will use the created service token.
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

0 comments on commit d147dd5

Please sign in to comment.