Skip to content

Commit

Permalink
[Elastic Agent] Fix broken enrollment command (#26749) (#26760)
Browse files Browse the repository at this point in the history
* Improve log to include URL.

* Fix issue with enroll command.

* Add changelog entry.

* Fix retry log message.

(cherry picked from commit 584ae74)

Co-authored-by: Blake Rouse <[email protected]>
  • Loading branch information
mergify[bot] and blakerouse authored Jul 7, 2021
1 parent 1b145c6 commit b42ffc4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 11 additions & 10 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}

Expand Down

0 comments on commit b42ffc4

Please sign in to comment.