Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flexible handling of IPv6 address formats for bind_address of spire-server and health-checks #5623

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading