Skip to content

Commit

Permalink
fix(ipv6): do not build malformed enroll url with ipv6 host (elastic#…
Browse files Browse the repository at this point in the history
…7036)

* fix(ipv6): do not build malformed enroll url with ipv6 host

replace string concat with net func to avoid building a malformed
url if the host is an ipv6

* changelog: add fragment

---------

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
  • Loading branch information
kruskall and blakerouse authored Feb 28, 2025
1 parent 6c85131 commit 4782bd4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
32 changes: 32 additions & 0 deletions changelog/fragments/1740712347-support-ipv6-enroll-url.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Kind can be one of:
# - breaking-change: a change to previously-documented behavior
# - deprecation: functionality that is being removed in a later release
# - bug-fix: fixes a problem in a previous version
# - enhancement: extends functionality but does not break or fix existing behavior
# - feature: new functionality
# - known-issue: problems that we are aware of in a given version
# - security: impacts on the security of a product or a user’s deployment.
# - upgrade: important information for someone upgrading from a prior version
# - other: does not fit into any of the other categories
kind: bug-fix

# Change summary; a 80ish characters long description of the change.
summary: support ipv6 hosts in enroll url

# Long description; in case the summary is not enough to describe the change
# this field accommodate a description without length limits.
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
#description:

# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
component: elastic-agent

# PR URL; optional; the PR number that added the changeset.
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
# Please provide it if you are adding a fragment for a different PR.
pr: https://github.com/elastic/elastic-agent/pull/7036

# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
# If not present is automatically filled by the tooling with the issue linked to the PR number.
#issue: https://github.com/owner/repo/1234
8 changes: 5 additions & 3 deletions internal/pkg/agent/cmd/enroll_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
"fmt"
"io"
"math/rand/v2"
"net"
"os"
"os/exec"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -433,7 +435,7 @@ func (c *enrollCmd) prepareFleetTLS() error {
if c.options.FleetServer.Host == "" {
c.options.FleetServer.Host = defaultFleetServerInternalHost
}
c.options.URL = fmt.Sprintf("http://%s:%d", host, port)
c.options.URL = "http://" + net.JoinHostPort(host, strconv.Itoa(int(port)))
c.options.Insecure = true
return nil
}
Expand All @@ -453,7 +455,7 @@ func (c *enrollCmd) prepareFleetTLS() error {
}
c.options.FleetServer.Cert = string(pair.Crt)
c.options.FleetServer.CertKey = string(pair.Key)
c.options.URL = fmt.Sprintf("https://%s:%d", hostname, port)
c.options.URL = "https://" + net.JoinHostPort(hostname, strconv.Itoa(int(port)))
c.options.CAs = []string{string(ca.Crt())}
}
// running with custom Cert and CertKey; URL is required to be set
Expand All @@ -465,7 +467,7 @@ func (c *enrollCmd) prepareFleetTLS() error {
if c.options.FleetServer.InternalPort != defaultFleetServerInternalPort {
c.log.Warnf("Internal endpoint configured to: %d. Changing this value is not supported.", c.options.FleetServer.InternalPort)
}
c.options.InternalURL = fmt.Sprintf("%s:%d", defaultFleetServerInternalHost, c.options.FleetServer.InternalPort)
c.options.InternalURL = net.JoinHostPort(defaultFleetServerInternalHost, strconv.Itoa(int(c.options.FleetServer.InternalPort)))
}

return nil
Expand Down

0 comments on commit 4782bd4

Please sign in to comment.