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

Fix IPv6 Handling Between Controllers, Workers, and Targets #5221

Merged
merged 1 commit into from
Nov 1, 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 enos/ci/bootstrap/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
source = "hashicorp/aws"
version = "5.72.1"
}
}

Expand Down
3 changes: 2 additions & 1 deletion enos/enos.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ terraform "default" {
}

aws = {
source = "hashicorp/aws"
source = "hashicorp/aws"
version = "5.72.1"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/alias/target/alias_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestCreate(t *testing.T) {
a.PublicId, err = db.NewPublicId(ctx, globals.TargetAliasPrefix)
require.NoError(t, err)

start := time.Now().UTC()
start := time.Now().UTC().Round(time.Second)

err = rw.Create(ctx, a)
if c.errContains != "" {
Expand All @@ -169,8 +169,8 @@ func TestCreate(t *testing.T) {
assert.Equal(t, a.Version, uint32(1))
assert.Equal(t, a.ScopeId, c.scope)
assert.Equal(t, a.Value, c.value)
assert.GreaterOrEqual(t, a.CreateTime.AsTime(), start)
assert.GreaterOrEqual(t, a.UpdateTime.AsTime(), start)
assert.GreaterOrEqual(t, a.CreateTime.AsTime().Round(time.Second), start)
assert.GreaterOrEqual(t, a.UpdateTime.AsTime().Round(time.Second), start)
if c.validate != nil {
c.validate(t, a)
}
Expand Down
32 changes: 18 additions & 14 deletions internal/cmd/base/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/boundary/testing/dbtest"
capoidc "github.com/hashicorp/cap/oidc"
"github.com/jimlambrt/gldap"
Expand Down Expand Up @@ -242,13 +243,9 @@ func (b *Server) CreateDevLdapAuthMethod(ctx context.Context) error {
if purpose != "api" {
continue
}
host, _, err = net.SplitHostPort(ln.Config.Address)
host, _, err = util.SplitHostPort(ln.Config.Address)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
host = ln.Config.Address
} else {
return fmt.Errorf("error splitting host/port: %w", err)
}
return fmt.Errorf("error splitting host/port: %w", err)
}
}
if host == "" {
Expand All @@ -259,6 +256,16 @@ func (b *Server) CreateDevLdapAuthMethod(ctx context.Context) error {
tb := &oidcLogger{}

port = testdirectory.FreePort(tb)

// The util.SplitHostPort() method removes the square brackets that enclose the
// host address when the address type is ipv6. The square brackets must be
// 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 {
host = fmt.Sprintf("[%s]", host)
}
}
b.DevLdapSetup.testDirectory = testdirectory.Start(tb,
testdirectory.WithNoTLS(tb),
testdirectory.WithHost(tb, host),
Expand Down Expand Up @@ -455,15 +462,12 @@ func (b *Server) CreateDevOidcAuthMethod(ctx context.Context) error {
if purpose != "api" {
continue
}
b.DevOidcSetup.hostAddr, b.DevOidcSetup.callbackPort, err = net.SplitHostPort(ln.Config.Address)
b.DevOidcSetup.hostAddr, b.DevOidcSetup.callbackPort, err = util.SplitHostPort(ln.Config.Address)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
b.DevOidcSetup.hostAddr = ln.Config.Address
// Use the default API port in the callback
b.DevOidcSetup.callbackPort = "9200"
} else {
return fmt.Errorf("error splitting host/port: %w", err)
}
return fmt.Errorf("error splitting host/port: %w", err)
}
if b.DevOidcSetup.callbackPort == "" {
b.DevOidcSetup.callbackPort = "9200"
}
}
if b.DevOidcSetup.hostAddr == "" {
Expand Down
33 changes: 16 additions & 17 deletions internal/cmd/base/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
_ "crypto/sha512"
"crypto/tls"

"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/go-secure-stdlib/listenerutil"
"github.com/hashicorp/go-secure-stdlib/reloadutil"
"github.com/mitchellh/cli"
Expand Down Expand Up @@ -137,24 +138,22 @@ func tcpListenerFactory(purpose string, l *listenerutil.ListenerConfig, ui cli.U
}
}

host, port, err := net.SplitHostPort(l.Address)
host, port, err := util.SplitHostPort(l.Address)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
switch purpose {
case "api":
port = "9200"
case "cluster":
port = "9201"
case "proxy":
port = "9202"
case "ops":
port = "9203"
default:
return "", nil, errors.New("no purpose provided for listener and no port discoverable")
}
host = l.Address
} else {
return "", nil, fmt.Errorf("error splitting host/port: %w", err)
return "", nil, fmt.Errorf("error splitting host/port: %w", err)
}
if port == "" {
switch purpose {
case "api":
port = "9200"
case "cluster":
port = "9201"
case "proxy":
port = "9202"
case "ops":
port = "9203"
default:
return "", nil, errors.New("no purpose provided for listener and no port discoverable")
}
}

Expand Down
24 changes: 6 additions & 18 deletions internal/cmd/base/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ import (
"errors"
"fmt"
"io"
"net"
"os"
"os/signal"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -58,10 +56,6 @@ const (
WorkerAuthReqFile = "auth_request_token"
)

// 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.
var squareBrackets = regexp.MustCompile("\\[|\\]")

func init() {
metric.InitializeBuildInfo(prometheus.DefaultRegisterer)
}
Expand Down Expand Up @@ -841,20 +835,14 @@ func (b *Server) SetupWorkerPublicAddress(conf *config.Config, flagValue string)
}
}

host, port, err := net.SplitHostPort(conf.Worker.PublicAddr)
host, port, err := util.SplitHostPort(conf.Worker.PublicAddr)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
port = "9202"
host = conf.Worker.PublicAddr
} else {
return fmt.Errorf("Error splitting public adddress host/port: %w", err)
}
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, "")
conf.Worker.PublicAddr = net.JoinHostPort(host, port)
if port == "" {
port = "9202"
}
conf.Worker.PublicAddr = util.JoinHostPort(host, port)

return nil
}
Expand Down
24 changes: 8 additions & 16 deletions internal/cmd/commands/connect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"fmt"
"io"
"math"
"net"
"net/netip"
"os"
"strconv"
Expand All @@ -22,6 +21,7 @@ import (
apiproxy "github.com/hashicorp/boundary/api/proxy"
"github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/util"
"github.com/mitchellh/cli"
"github.com/posener/complete"
"go.uber.org/atomic"
Expand Down Expand Up @@ -476,14 +476,10 @@ func (c *Command) Run(args []string) (retCode int) {

proxyAddr := clientProxy.ListenerAddress(context.Background())
var clientProxyHost, clientProxyPort string
clientProxyHost, clientProxyPort, err = net.SplitHostPort(proxyAddr)
clientProxyHost, clientProxyPort, err = util.SplitHostPort(proxyAddr)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
clientProxyHost = proxyAddr
} else {
c.PrintCliError(fmt.Errorf("error splitting listener addr: %w", err))
return base.CommandCliError
}
c.PrintCliError(fmt.Errorf("error splitting listener addr: %w", err))
return base.CommandCliError
}
c.sessInfo.Address = clientProxyHost

Expand Down Expand Up @@ -605,15 +601,11 @@ func (c *Command) handleExec(clientProxy *apiproxy.ClientProxy, passthroughArgs
addr := clientProxy.ListenerAddress(context.Background())
var host, port string
var err error
host, port, err = net.SplitHostPort(addr)
host, port, err = util.SplitHostPort(addr)
if err != nil {
if strings.Contains(err.Error(), "missing port") {
host = addr
} else {
c.PrintCliError(fmt.Errorf("Error splitting listener addr: %w", err))
c.execCmdReturnValue.Store(int32(3))
return
}
c.PrintCliError(fmt.Errorf("Error splitting listener addr: %w", err))
c.execCmdReturnValue.Store(int32(3))
return
}

var args []string
Expand Down
11 changes: 4 additions & 7 deletions internal/cmd/commands/dev/dev.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"math/rand"
"net"
"os"
"runtime"
"strings"
Expand All @@ -27,6 +26,7 @@ import (
"github.com/hashicorp/boundary/internal/server"
"github.com/hashicorp/boundary/internal/server/store"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/go-secure-stdlib/parseutil"
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/nodeenrollment"
Expand Down Expand Up @@ -592,13 +592,10 @@ func (c *Command) Run(args []string) int {
return base.CommandUserError
}

host, port, err := net.SplitHostPort(c.flagHostAddress)
host, port, err := util.SplitHostPort(c.flagHostAddress)
if err != nil {
if !strings.Contains(err.Error(), "missing port") {
c.UI.Error(fmt.Errorf("Invalid host address specified: %w", err).Error())
return base.CommandUserError
}
host = c.flagHostAddress
c.UI.Error(fmt.Errorf("Invalid host address specified: %w", err).Error())
return base.CommandUserError
}
if port != "" {
c.UI.Error(`Port must not be specified as part of the dev host address`)
Expand Down
21 changes: 7 additions & 14 deletions internal/cmd/commands/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/event"
"github.com/hashicorp/boundary/internal/kms"
"github.com/hashicorp/boundary/internal/util"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-secure-stdlib/mlock"
"github.com/hashicorp/go-secure-stdlib/parseutil"
Expand Down Expand Up @@ -355,14 +356,10 @@ func (c *Command) Run(args []string) int {
}
}
for _, upstream := range c.Config.Worker.InitialUpstreams {
host, _, err := net.SplitHostPort(upstream)
host, _, err := util.SplitHostPort(upstream)
if err != nil {
if strings.Contains(err.Error(), globals.MissingPortErrStr) {
host = upstream
} else {
c.UI.Error(fmt.Errorf("Invalid worker upstream address %q: %w", upstream, err).Error())
return base.CommandUserError
}
c.UI.Error(fmt.Errorf("Invalid worker upstream address %q: %w", upstream, err).Error())
return base.CommandUserError
}
ip := net.ParseIP(host)
if ip != nil {
Expand Down Expand Up @@ -413,14 +410,10 @@ func (c *Command) Run(args []string) int {
if purpose != "cluster" {
continue
}
host, _, err := net.SplitHostPort(ln.Address)
host, _, err := util.SplitHostPort(ln.Address)
if err != nil {
if strings.Contains(err.Error(), globals.MissingPortErrStr) {
host = ln.Address
} else {
c.UI.Error(fmt.Errorf("Invalid cluster listener address %q: %w", ln.Address, err).Error())
return base.CommandUserError
}
c.UI.Error(fmt.Errorf("Invalid cluster listener address %q: %w", ln.Address, err).Error())
return base.CommandUserError
}
ip := net.ParseIP(host)
if ip != nil {
Expand Down
Loading
Loading