Skip to content

Commit

Permalink
test: ensure ipv6 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebko committed Sep 16, 2024
1 parent 390fca3 commit 2090ebe
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 10 deletions.
64 changes: 62 additions & 2 deletions internal/cmd/base/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
expPublicAddress: ":9202",
},
{
name: "setting public address directly with ip",
name: "setting public address directly with ipv4",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
Expand All @@ -511,7 +511,7 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
expPublicAddress: "127.0.0.1:9202",
},
{
name: "setting public address directly with ip:port",
name: "setting public address directly with ipv4:port",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
Expand All @@ -525,6 +525,66 @@ func TestSetupWorkerPublicAddress(t *testing.T) {
expErrStr: "",
expPublicAddress: "127.0.0.1:8080",
},
{
name: "setting public address directly with ipv6",
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:0:0:0:0:8888]:9202",
},
{
name: "setting public address directly with ipv6:port",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
},
Worker: &config.Worker{
PublicAddr: "[2001:4860:4860:0:0:0:0:8888]:8080",
},
},
inputFlagValue: "",
expErr: false,
expErrStr: "",
expPublicAddress: "[2001:4860:4860:0:0:0:0:8888]:8080",
},
{
name: "setting public address directly with abbreviated ipv6",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
},
Worker: &config.Worker{
PublicAddr: "[2001:4860:4860::8888]",
},
},
inputFlagValue: "",
expErr: false,
expErrStr: "",
expPublicAddress: "[2001:4860:4860::8888]:9202",
},
{
name: "setting public address directly with abbreviated ipv6:port",
inputConfig: &config.Config{
SharedConfig: &configutil.SharedConfig{
Listeners: []*listenerutil.ListenerConfig{},
},
Worker: &config.Worker{
PublicAddr: "[2001:4860:4860::8888]:8080",
},
},
inputFlagValue: "",
expErr: false,
expErrStr: "",
expPublicAddress: "[2001:4860:4860::8888]:8080",
},
{
name: "setting public address to env var",
inputConfig: &config.Config{
Expand Down
16 changes: 16 additions & 0 deletions internal/cmd/base/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"os"
"os/signal"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -57,6 +58,12 @@ const (
WorkerAuthReqFile = "auth_request_token"
)

var (
// This regular expression is used to find all instances of square brackets within a string.
// This regular expression is used to remove the square brackets from an IPv6 address.
squareBrackets = regexp.MustCompile("\\[|\\]")
)

func init() {
metric.InitializeBuildInfo(prometheus.DefaultRegisterer)
}
Expand Down Expand Up @@ -845,6 +852,15 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
return fmt.Errorf("Error splitting public adddress host/port: %w", err)
}
}

// remove the square brackets from the ipv6 address because the method
// net.JoinHostPort() will add a second pair of square brackets.
host = squareBrackets.ReplaceAllString(host, "")
ipAddr := net.ParseIP(host)
if ipAddr.To4() == nil && ipAddr.To16() == nil {
return fmt.Errorf("Error parsing address: %s", ipAddr.String())
}

conf.Worker.PublicAddr = net.JoinHostPort(host, port)
return nil
}
Expand Down
22 changes: 22 additions & 0 deletions internal/cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -132,6 +133,12 @@ kms "aead" {
defaultCsp = "default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; frame-src 'self'; font-src 'self'; connect-src 'self'; img-src 'self' data:; style-src 'self'; media-src 'self'; manifest-src 'self'; style-src-attr 'self'; frame-ancestors 'self'"
)

var (
// This regular expression is used to find all instances of square brackets within a string.
// This regular expression is used to remove the square brackets from an IPv6 address.
squareBrackets = regexp.MustCompile("\\[|\\]")
)

// Config is the configuration for the boundary controller
type Config struct {
*configutil.SharedConfig `hcl:"-"`
Expand Down Expand Up @@ -1278,6 +1285,21 @@ func (c *Config) SetupControllerPublicClusterAddress(flagValue string) error {
return fmt.Errorf("Error splitting public cluster adddress host/port: %w", err)
}
}

// remove the square brackets from the ipv6 address because the method
// net.JoinHostPort() will add a second pair of square brackets.
host = squareBrackets.ReplaceAllString(host, "")
if host == "" {
// return early if a empty host was provided
c.Controller.PublicClusterAddr = net.JoinHostPort(host, port)
return nil
}

ipAddr := net.ParseIP(host)
if ipAddr.To4() == nil && ipAddr.To16() == nil {
return fmt.Errorf("Error parsing address: %s", ipAddr.String())
}

c.Controller.PublicClusterAddr = net.JoinHostPort(host, port)
return nil
}
Expand Down
Loading

0 comments on commit 2090ebe

Please sign in to comment.