diff --git a/cmd/spire-server/cli/run/run.go b/cmd/spire-server/cli/run/run.go index 3ba95d9c3d..7276b491be 100644 --- a/cmd/spire-server/cli/run/run.go +++ b/cmd/spire-server/cli/run/run.go @@ -14,6 +14,7 @@ import ( "path/filepath" "reflect" "sort" + "strconv" "strings" "syscall" "time" @@ -410,7 +411,7 @@ func NewServerConfig(c *Config, logOptions []log.Option, allowUnknownConfig bool sc.LogReopener = log.ReopenOnSignal(logger, reopenableFile) } - bindAddress, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", c.Server.BindAddress, c.Server.BindPort)) + bindAddress, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(strings.Trim(c.Server.BindAddress, "[]"), strconv.Itoa(c.Server.BindPort))) if err != nil { return nil, fmt.Errorf(`could not resolve bind address "%s:%d": %w`, c.Server.BindAddress, c.Server.BindPort, err) } diff --git a/cmd/spire-server/cli/run/run_test.go b/cmd/spire-server/cli/run/run_test.go index 9a9135e14d..de917cbb4b 100644 --- a/cmd/spire-server/cli/run/run_test.go +++ b/cmd/spire-server/cli/run/run_test.go @@ -515,6 +515,28 @@ func TestNewServerConfig(t *testing.T) { require.Equal(t, 1337, c.BindAddress.Port) }, }, + { + msg: "IPv6 bind_address in square brackets and bind_port should be correctly parsed", + input: func(c *Config) { + c.Server.BindAddress = "[2001:101::]" + c.Server.BindPort = 1337 + }, + test: func(t *testing.T, c *server.Config) { + require.Equal(t, "2001:101::", c.BindAddress.IP.String()) + require.Equal(t, 1337, c.BindAddress.Port) + }, + }, + { + msg: "IPv6 bind_address without square brackets and bind_port should be correctly parsed", + input: func(c *Config) { + c.Server.BindAddress = "2001:101::" + c.Server.BindPort = 1337 + }, + test: func(t *testing.T, c *server.Config) { + require.Equal(t, "2001:101::", c.BindAddress.IP.String()) + require.Equal(t, 1337, c.BindAddress.Port) + }, + }, { msg: "bind_address with hostname value should be correctly parsed", input: func(c *Config) { diff --git a/pkg/common/health/config.go b/pkg/common/health/config.go index d067e178d4..145009a183 100644 --- a/pkg/common/health/config.go +++ b/pkg/common/health/config.go @@ -1,7 +1,8 @@ package health import ( - "fmt" + "net" + "strings" "github.com/hashicorp/hcl/hcl/token" ) @@ -24,7 +25,7 @@ type Config struct { func (c *Config) getAddress() string { host := "localhost" if c.BindAddress != "" { - host = c.BindAddress + host = strings.Trim(c.BindAddress, "[]") } port := "80" @@ -32,7 +33,7 @@ func (c *Config) getAddress() string { port = c.BindPort } - return fmt.Sprintf("%s:%s", host, port) + return net.JoinHostPort(host, port) } // getReadyPath returns the configured value or a default