Skip to content

Commit

Permalink
Flexible handling of IPv6 addresses (#5623)
Browse files Browse the repository at this point in the history
Signed-off-by: Szilard Vincze <szilard.vincze@est.tech>
  • Loading branch information
szvincze authored Dec 5, 2024
1 parent 40fb0df commit a0dd78b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
3 changes: 2 additions & 1 deletion cmd/spire-server/cli/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"reflect"
"sort"
"strconv"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 22 additions & 0 deletions cmd/spire-server/cli/run/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions pkg/common/health/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package health

import (
"fmt"
"net"
"strings"

"github.com/hashicorp/hcl/hcl/token"
)
Expand All @@ -24,15 +25,15 @@ type Config struct {
func (c *Config) getAddress() string {
host := "localhost"
if c.BindAddress != "" {
host = c.BindAddress
host = strings.Trim(c.BindAddress, "[]")
}

port := "80"
if c.BindPort != "" {
port = c.BindPort
}

return fmt.Sprintf("%s:%s", host, port)
return net.JoinHostPort(host, port)
}

// getReadyPath returns the configured value or a default
Expand Down

0 comments on commit a0dd78b

Please sign in to comment.