Skip to content

Commit 3bcb53f

Browse files
Enroll options contains parsable URI after FromFleetConfig call (#9594)
* Enroll options contains parsable URI after FromFleetConfig call * fix TestComputeEnrollOptions
1 parent 000007e commit 3bcb53f

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

internal/pkg/agent/application/coordinator/coordinator_unit_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ func TestComputeEnrollOptions(t *testing.T) {
19141914
assert.NotNil(t, options)
19151915

19161916
assert.Equal(t, "123", options.EnrollAPIKey, "EnrollAPIKey mismatch")
1917-
assert.Equal(t, "localhost:1234", options.URL, "URL mismatch")
1917+
assert.Equal(t, "http://localhost:1234", options.URL, "URL mismatch")
19181918

19191919
assert.Equal(t, []string{"sha1", "sha2"}, options.CASha256, "CASha256 mismatch")
19201920
assert.Equal(t, true, options.Insecure, "Insecure mismatch")

internal/pkg/agent/application/enroll/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package enroll
77
import (
88
"encoding/json"
99
"fmt"
10+
"strings"
1011
"time"
1112

1213
"github.com/elastic/elastic-agent-libs/transport/httpcommon"
@@ -177,6 +178,12 @@ func FromFleetConfig(cfg *configuration.FleetAgentConfig) EnrollOptions {
177178
options.URL = cfg.Client.Hosts[0]
178179
}
179180

181+
// URL at this point may not be parsable by url.Parse (missing protocol)
182+
if host := options.URL; len(host) > 0 && !strings.HasPrefix(host, string(remote.ProtocolHTTPS)+"://") &&
183+
!strings.HasPrefix(host, string(remote.ProtocolHTTP)+"://") {
184+
options.URL = string(cfg.Client.Protocol) + "://" + host
185+
}
186+
180187
if cfg.Client.Transport.TLS != nil {
181188
options.CAs = cfg.Client.Transport.TLS.CAs
182189
options.CASha256 = cfg.Client.Transport.TLS.CASha256

internal/pkg/agent/application/enroll/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func TestFromFleetConfig(t *testing.T) {
380380
"default config",
381381
defaultFleetAgentCfg,
382382
EnrollOptions{
383-
URL: defaultFleetAgentCfg.Client.Host,
383+
URL: string(defaultFleetAgentCfg.Client.Protocol) + "://" + defaultFleetAgentCfg.Client.Host,
384384
EnrollAPIKey: defaultFleetAgentCfg.AccessAPIKey,
385385
ProxyHeaders: make(map[string]string),
386386
},

0 commit comments

Comments
 (0)