Skip to content

Commit a0dd78b

Browse files
authored
Flexible handling of IPv6 addresses (#5623)
Signed-off-by: Szilard Vincze <[email protected]>
1 parent 40fb0df commit a0dd78b

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

cmd/spire-server/cli/run/run.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"path/filepath"
1515
"reflect"
1616
"sort"
17+
"strconv"
1718
"strings"
1819
"syscall"
1920
"time"
@@ -410,7 +411,7 @@ func NewServerConfig(c *Config, logOptions []log.Option, allowUnknownConfig bool
410411
sc.LogReopener = log.ReopenOnSignal(logger, reopenableFile)
411412
}
412413

413-
bindAddress, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", c.Server.BindAddress, c.Server.BindPort))
414+
bindAddress, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(strings.Trim(c.Server.BindAddress, "[]"), strconv.Itoa(c.Server.BindPort)))
414415
if err != nil {
415416
return nil, fmt.Errorf(`could not resolve bind address "%s:%d": %w`, c.Server.BindAddress, c.Server.BindPort, err)
416417
}

cmd/spire-server/cli/run/run_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,28 @@ func TestNewServerConfig(t *testing.T) {
515515
require.Equal(t, 1337, c.BindAddress.Port)
516516
},
517517
},
518+
{
519+
msg: "IPv6 bind_address in square brackets and bind_port should be correctly parsed",
520+
input: func(c *Config) {
521+
c.Server.BindAddress = "[2001:101::]"
522+
c.Server.BindPort = 1337
523+
},
524+
test: func(t *testing.T, c *server.Config) {
525+
require.Equal(t, "2001:101::", c.BindAddress.IP.String())
526+
require.Equal(t, 1337, c.BindAddress.Port)
527+
},
528+
},
529+
{
530+
msg: "IPv6 bind_address without square brackets and bind_port should be correctly parsed",
531+
input: func(c *Config) {
532+
c.Server.BindAddress = "2001:101::"
533+
c.Server.BindPort = 1337
534+
},
535+
test: func(t *testing.T, c *server.Config) {
536+
require.Equal(t, "2001:101::", c.BindAddress.IP.String())
537+
require.Equal(t, 1337, c.BindAddress.Port)
538+
},
539+
},
518540
{
519541
msg: "bind_address with hostname value should be correctly parsed",
520542
input: func(c *Config) {

pkg/common/health/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package health
22

33
import (
4-
"fmt"
4+
"net"
5+
"strings"
56

67
"github.com/hashicorp/hcl/hcl/token"
78
)
@@ -24,15 +25,15 @@ type Config struct {
2425
func (c *Config) getAddress() string {
2526
host := "localhost"
2627
if c.BindAddress != "" {
27-
host = c.BindAddress
28+
host = strings.Trim(c.BindAddress, "[]")
2829
}
2930

3031
port := "80"
3132
if c.BindPort != "" {
3233
port = c.BindPort
3334
}
3435

35-
return fmt.Sprintf("%s:%s", host, port)
36+
return net.JoinHostPort(host, port)
3637
}
3738

3839
// getReadyPath returns the configured value or a default

0 commit comments

Comments
 (0)