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 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
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