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

Backport elastic-agent#1867 #38785

Closed
Show file tree
Hide file tree
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
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 @@ -97,6 +97,7 @@
- Allow the - char to appear as part of variable names in eql expressions. {pull}32350[32350]
- Allow the / char to appear as part of variable names in eql expressions. {pull}32528{32528}
- Fix add_fields processor on Docker provider {pull}33269{33269}
- Change local fleet-server connection to localhost:8221. {pull}38785[38785]

==== New features

Expand Down
15 changes: 12 additions & 3 deletions x-pack/elastic-agent/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func (c *enrollCmd) writeDelayEnroll(streams *cli.IOStreams) error {
func (c *enrollCmd) fleetServerBootstrap(ctx context.Context, persistentConfig map[string]interface{}) (string, error) {
c.log.Debug("verifying communication with running Elastic Agent daemon")
agentRunning := true
if c.options.FleetServer.InternalPort == 0 {
c.options.FleetServer.InternalPort = defaultFleetServerInternalPort
}
_, err := getDaemonStatus(ctx)
if err != nil {
if !c.options.FleetServer.SpawnAgent {
Expand Down Expand Up @@ -321,6 +324,7 @@ func (c *enrollCmd) fleetServerBootstrap(ctx context.Context, persistentConfig m
if err != nil {
return "", err
}
c.options.FleetServer.InternalPort = fleetConfig.Server.InternalPort

configToStore := map[string]interface{}{
"agent": agentConfig,
Expand Down Expand Up @@ -360,7 +364,7 @@ func (c *enrollCmd) fleetServerBootstrap(ctx context.Context, persistentConfig m
func (c *enrollCmd) prepareFleetTLS() error {
host := c.options.FleetServer.Host
if host == "" {
host = "localhost"
host = defaultFleetServerInternalHost
}
port := c.options.FleetServer.Port
if port == 0 {
Expand All @@ -376,7 +380,7 @@ func (c *enrollCmd) prepareFleetTLS() error {
if c.options.FleetServer.Insecure {
// running insecure, force the binding to localhost (unless specified)
if c.options.FleetServer.Host == "" {
c.options.FleetServer.Host = "localhost"
c.options.FleetServer.Host = defaultFleetServerInternalHost
}
c.options.URL = fmt.Sprintf("http://%s:%d", host, port)
c.options.Insecure = true
Expand Down Expand Up @@ -531,6 +535,9 @@ func (c *enrollCmd) enroll(ctx context.Context, persistentConfig map[string]inte
// use internal URL for future requests
if c.options.InternalURL != "" {
fleetConfig.Client.Host = c.options.InternalURL
// fleet-server will bind the internal listenter to localhost:8221
// InternalURL is localhost:8221, however cert uses $HOSTNAME, so we need to disable hostname verification.
fleetConfig.Client.Transport.TLS.VerificationMode = tlscommon.VerifyCertificate
}
}

Expand Down Expand Up @@ -843,7 +850,9 @@ func storeAgentInfo(s saver, reader io.Reader) error {
if err := fileLock.TryLock(); err != nil {
return err
}
defer fileLock.Unlock() //nolint:errcheck // defered call
defer func() {
_ = fileLock.Unlock()
}()

if err := s.Save(reader); err != nil {
return errors.New(err, "could not save enrollment information", errors.TypeFilesystem)
Expand Down
Loading