From b42ffc4d79fb3063bebbc97bd8a1103f4a90c1c3 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 7 Jul 2021 12:38:58 -0400 Subject: [PATCH] [Elastic Agent] Fix broken enrollment command (#26749) (#26760) * Improve log to include URL. * Fix issue with enroll command. * Add changelog entry. * Fix retry log message. (cherry picked from commit 584ae74a6c6df7353b67a5bcb39307f73d6c92a3) Co-authored-by: Blake Rouse --- x-pack/elastic-agent/CHANGELOG.next.asciidoc | 1 + .../elastic-agent/pkg/agent/cmd/enroll_cmd.go | 21 ++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/x-pack/elastic-agent/CHANGELOG.next.asciidoc b/x-pack/elastic-agent/CHANGELOG.next.asciidoc index 4b63cf2eb53..0795c2e06df 100644 --- a/x-pack/elastic-agent/CHANGELOG.next.asciidoc +++ b/x-pack/elastic-agent/CHANGELOG.next.asciidoc @@ -74,6 +74,7 @@ - Change timestamp in elatic-agent-json.log to use UTC {issue}25391[25391] - Fix add support for Logstash output. {pull}24305[24305] - Do not log Elasticsearch configuration for monitoring output when running with debug. {pull}26583[26583] +- Fix issue where proxy enrollment options broke enrollment command. {pull}26749[26749] ==== New features diff --git a/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go b/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go index e4c4c69b89a..4adfada0085 100644 --- a/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go +++ b/x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go @@ -200,6 +200,15 @@ func (c *enrollCmd) Execute(ctx context.Context) error { // will communicate to the internal fleet server on localhost only. // Connection setup should disable proxies in that case. localFleetServer := c.options.FleetServer.ConnStr != "" + if localFleetServer { + token, err := c.fleetServerBootstrap(ctx) + if err != nil { + return err + } + if c.options.EnrollAPIKey == "" && token != "" { + c.options.EnrollAPIKey = token + } + } c.remoteConfig, err = c.options.remoteConfig() if err != nil { @@ -208,19 +217,10 @@ func (c *enrollCmd) Execute(ctx context.Context) error { errors.TypeConfig, errors.M(errors.MetaKeyURI, c.options.URL)) } - if localFleetServer { // Ensure that the agent does not use a proxy configuration // when connecting to the local fleet server. c.remoteConfig.Transport.Proxy.Disable = true - - token, err := c.fleetServerBootstrap(ctx) - if err != nil { - return err - } - if c.options.EnrollAPIKey == "" && token != "" { - c.options.EnrollAPIKey = token - } } c.client, err = fleetclient.NewWithConfig(c.log, c.remoteConfig) @@ -402,6 +402,7 @@ func (c *enrollCmd) daemonReload(ctx context.Context) error { func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[string]interface{}) error { delay(ctx, enrollDelay) + c.log.Infof("Starting enrollment to URL: %s", c.client.URI()) err := c.enroll(ctx, persistentConfig) signal := make(chan struct{}) backExp := backoff.NewExpBackoff(signal, 60*time.Second, 10*time.Minute) @@ -419,7 +420,7 @@ func (c *enrollCmd) enrollWithBackoff(ctx context.Context, persistentConfig map[ break } backExp.Wait() - c.log.Info("Retrying to enroll...") + c.log.Infof("Retrying enrollment to URL: %s", c.client.URI()) err = c.enroll(ctx, persistentConfig) }