@@ -269,6 +269,12 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
269269 return run (streams , logToStderr )
270270 }
271271
272+ if cfg .Kibana .Fleet .Setup || cfg .Fleet .Enroll {
273+ err = ensureServiceToken (streams , & cfg )
274+ if err != nil {
275+ return err
276+ }
277+ }
272278 if cfg .Kibana .Fleet .Setup {
273279 client , err = kibanaClient (cfg .Kibana , cfg .Kibana .Headers )
274280 if err != nil {
@@ -282,19 +288,6 @@ func runContainerCmd(streams *cli.IOStreams, cmd *cobra.Command, cfg setupConfig
282288 }
283289 }
284290 if cfg .Fleet .Enroll {
285- if cfg .FleetServer .Enable {
286- if client == nil {
287- client , err = kibanaClient (cfg .Kibana , cfg .Kibana .Headers )
288- if err != nil {
289- return err
290- }
291- }
292- err = ensureServiceToken (streams , client , & cfg )
293- if err != nil {
294- return err
295- }
296- }
297-
298291 var policy * kibanaPolicy
299292 token := cfg .Fleet .EnrollmentToken
300293 if token == "" && ! cfg .FleetServer .Enable {
@@ -350,7 +343,7 @@ type TokenResp struct {
350343// ensureServiceToken will ensure that the cfg specified has the service_token attributes filled.
351344//
352345// If no token is specified it will use the elasticsearch username/password to request a new token from Kibana
353- func ensureServiceToken (streams * cli.IOStreams , client * kibana. Client , cfg * setupConfig ) error {
346+ func ensureServiceToken (streams * cli.IOStreams , cfg * setupConfig ) error {
354347 // There's already a service token
355348 if cfg .Kibana .Fleet .ServiceToken != "" || cfg .FleetServer .Elasticsearch .ServiceToken != "" {
356349 return nil
@@ -360,6 +353,13 @@ func ensureServiceToken(streams *cli.IOStreams, client *kibana.Client, cfg *setu
360353 }
361354
362355 logInfo (streams , "Requesting service_token from Kibana." )
356+
357+ // Client is not passed in to this function because this function will use username/password and then
358+ // all the following clients will use the created service token.
359+ client , err := kibanaClient (cfg .Kibana , cfg .Kibana .Headers )
360+ if err != nil {
361+ return err
362+ }
363363 code , r , err := client .Connection .Request ("POST" , "/api/fleet/service-tokens" , nil , nil , nil )
364364 if err != nil {
365365 return fmt .Errorf ("request to get security token from Kibana failed: %w" , err )
0 commit comments