feat: Normalize fields across various Boundary components (#5599)
Normalizes various IP/Address/Host fields across Boundary resources/components to comply with IPv6 specifications.
This commit is contained in:
37 files changed
+1028
-348
No files matched your search
@@ -244,7 +244,7 @@ func (b *Server) CreateDevLdapAuthMethod(ctx context.Context) error {
|
||||
continue
|
||||
}
|
||||
host, _, err = util.SplitHostPort(ln.Config.Address)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, util.ErrMissingPort) {
|
||||
return fmt.Errorf("error splitting host/port: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -262,7 +262,7 @@ func (b *Server) CreateDevLdapAuthMethod(ctx context.Context) error {
|
||||
// added back, otherwise the gldap server will fail to start due to a parsing
|
||||
// error.
|
||||
if ip := net.ParseIP(host); ip != nil {
|
||||
if ip.To16() != nil {
|
||||
if ip.To4() == nil && ip.To16() != nil {
|
||||
host = fmt.Sprintf("[%s]", host)
|
||||
}
|
||||
}
|
||||
@@ -463,7 +463,7 @@ func (b *Server) CreateDevOidcAuthMethod(ctx context.Context) error {
|
||||
continue
|
||||
}
|
||||
b.DevOidcSetup.hostAddr, b.DevOidcSetup.callbackPort, err = util.SplitHostPort(ln.Config.Address)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, util.ErrMissingPort) {
|
||||
return fmt.Errorf("error splitting host/port: %w", err)
|
||||
}
|
||||
if b.DevOidcSetup.callbackPort == "" {
|
||||
|
||||
@@ -18,6 +18,7 @@ import (
|
||||
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/go-secure-stdlib/listenerutil"
|
||||
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
||||
"github.com/hashicorp/go-secure-stdlib/reloadutil"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/pires/go-proxyproto"
|
||||
@@ -139,7 +140,7 @@ func tcpListenerFactory(purpose string, l *listenerutil.ListenerConfig, ui cli.U
|
||||
}
|
||||
|
||||
host, port, err := util.SplitHostPort(l.Address)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, util.ErrMissingPort) {
|
||||
return "", nil, fmt.Errorf("error splitting host/port: %w", err)
|
||||
}
|
||||
if port == "" {
|
||||
@@ -173,10 +174,15 @@ func tcpListenerFactory(purpose string, l *listenerutil.ListenerConfig, ui cli.U
|
||||
}
|
||||
|
||||
if l.RandomPort {
|
||||
port = ""
|
||||
port = "0" // net.Listen will choose an available port automatically. Used for tests.
|
||||
}
|
||||
|
||||
finalListenAddr := net.JoinHostPort(host, port)
|
||||
normalizedListenAddr, err := parseutil.NormalizeAddr(finalListenAddr)
|
||||
if err != nil {
|
||||
return "", nil, fmt.Errorf("failed to normalize final listen addr %q: %w", finalListenAddr, err)
|
||||
}
|
||||
finalListenAddr = normalizedListenAddr
|
||||
|
||||
ln, err := net.Listen(bindProto, finalListenAddr)
|
||||
if err != nil {
|
||||
|
||||
@@ -525,6 +525,35 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
expErrStr: "",
|
||||
expPublicAddress: "127.0.0.1:8080",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with invalid ipv6",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860:0:0:0:8888]",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: true,
|
||||
expErrStr: "Error normalizing worker address",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ipv6 but no brackets",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "2001:4860:4860:0:0:0:0:8888",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860::8888]:9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ipv6",
|
||||
inputConfig: &config.Config{
|
||||
@@ -532,13 +561,13 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860:0:0:0:0:8888]",
|
||||
PublicAddr: "2001:4860:4860:0:0:0:0:8888",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860:0:0:0:0:8888]:9202",
|
||||
expPublicAddress: "[2001:4860:4860::8888]:9202",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with ipv6:port",
|
||||
@@ -553,7 +582,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
inputFlagValue: "",
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "[2001:4860:4860:0:0:0:0:8888]:8080",
|
||||
expPublicAddress: "[2001:4860:4860::8888]:8080",
|
||||
},
|
||||
{
|
||||
name: "setting public address directly with abbreviated ipv6",
|
||||
@@ -562,7 +591,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
Listeners: []*listenerutil.ListenerConfig{},
|
||||
},
|
||||
Worker: &config.Worker{
|
||||
PublicAddr: "[2001:4860:4860::8888]",
|
||||
PublicAddr: "2001:4860:4860::8888",
|
||||
},
|
||||
},
|
||||
inputFlagValue: "",
|
||||
@@ -781,6 +810,20 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
expErrStr: "",
|
||||
expPublicAddress: ":9202",
|
||||
},
|
||||
{
|
||||
name: "read unix address from listeners ip only",
|
||||
inputConfig: &config.Config{
|
||||
SharedConfig: &configutil.SharedConfig{
|
||||
Listeners: []*listenerutil.ListenerConfig{
|
||||
{Purpose: []string{"proxy"}, Address: "someaddr", Type: "unix"},
|
||||
},
|
||||
},
|
||||
Worker: &config.Worker{},
|
||||
},
|
||||
expErr: false,
|
||||
expErrStr: "",
|
||||
expPublicAddress: "someaddr:9202",
|
||||
},
|
||||
{
|
||||
name: "using flag value to point to nonexistent file",
|
||||
inputConfig: &config.Config{
|
||||
@@ -802,9 +845,9 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
|
||||
},
|
||||
Worker: &config.Worker{},
|
||||
},
|
||||
inputFlagValue: "abc::123",
|
||||
inputFlagValue: "abc::123:::",
|
||||
expErr: true,
|
||||
expErrStr: "Error splitting public adddress host/port: address abc::123: too many colons in address",
|
||||
expErrStr: "Error splitting public adddress host/port: too many colons in address",
|
||||
expPublicAddress: "",
|
||||
},
|
||||
{
|
||||
|
||||
@@ -812,12 +812,16 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
|
||||
if flagValue != "" {
|
||||
conf.Worker.PublicAddr = flagValue
|
||||
}
|
||||
isUnixListener := false
|
||||
if conf.Worker.PublicAddr == "" {
|
||||
FindAddr:
|
||||
for _, listener := range conf.Listeners {
|
||||
for _, purpose := range listener.Purpose {
|
||||
if purpose == "proxy" {
|
||||
conf.Worker.PublicAddr = listener.Address
|
||||
if strings.EqualFold(listener.Type, "unix") {
|
||||
isUnixListener = true
|
||||
}
|
||||
break FindAddr
|
||||
}
|
||||
}
|
||||
@@ -836,14 +840,29 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
|
||||
}
|
||||
|
||||
host, port, err := util.SplitHostPort(conf.Worker.PublicAddr)
|
||||
if err != nil {
|
||||
if err != nil && !errors.Is(err, util.ErrMissingPort) {
|
||||
return fmt.Errorf("Error splitting public adddress host/port: %w", err)
|
||||
}
|
||||
if host != "" {
|
||||
if host, err = parseutil.NormalizeAddr(host); err != nil {
|
||||
return fmt.Errorf("Error normalizing worker address")
|
||||
}
|
||||
}
|
||||
if port == "" {
|
||||
port = "9202"
|
||||
}
|
||||
conf.Worker.PublicAddr = util.JoinHostPort(host, port)
|
||||
|
||||
if host != "" && !isUnixListener {
|
||||
// NormalizeAddr requires that a host be present, but that is not
|
||||
// guaranteed in this code path. Additionally, if no host is present,
|
||||
// there's no need to normalize.
|
||||
conf.Worker.PublicAddr, err = parseutil.NormalizeAddr(conf.Worker.PublicAddr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Failed to normalize worker public adddress: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user