[fix] allow IPv6 controllers, workers, and targets (#5221)
Co-authored-by: Damian Debkowski <damian.debkowski@hashicorp.com>
This commit is contained in:
1 parent
680bb02af9
commit
62c41191ff
42 files changed
+1996
-546
No files matched your search
@@ -32,8 +32,9 @@ type Host struct {
|
||||
// Name and description are the only valid options. All other options are
|
||||
// ignored.
|
||||
func NewHost(ctx context.Context, catalogId string, opt ...Option) (*Host, error) {
|
||||
const op = "static.NewHost"
|
||||
if catalogId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, "static.NewHost", "no catalog id")
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no catalog id")
|
||||
}
|
||||
|
||||
opts := getOpts(opt...)
|
||||
|
||||
@@ -131,30 +131,30 @@ func TestHost_New(t *testing.T) {
|
||||
for _, tt := range tests {
|
||||
tt := tt
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
require, assert := require.New(t), assert.New(t)
|
||||
got, err := NewHost(ctx, tt.args.catalogId, tt.args.opts...)
|
||||
if tt.wantCreateErr {
|
||||
assert.Error(err)
|
||||
require.Error(err)
|
||||
assert.Nil(got)
|
||||
} else {
|
||||
assert.NoError(err)
|
||||
if assert.NotNil(got) {
|
||||
assert.Emptyf(got.PublicId, "PublicId set")
|
||||
assert.Equal(tt.want, got)
|
||||
|
||||
id, err := newHostId(ctx)
|
||||
assert.NoError(err)
|
||||
|
||||
tt.want.PublicId = id
|
||||
got.PublicId = id
|
||||
|
||||
w := db.New(conn)
|
||||
err2 := w.Create(ctx, got)
|
||||
if tt.wantWriteErr {
|
||||
assert.Error(err2)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
require.NoError(err)
|
||||
require.NotNil(got)
|
||||
assert.Emptyf(got.PublicId, "PublicId set")
|
||||
assert.Equal(tt.want, got)
|
||||
|
||||
id, err := newHostId(ctx)
|
||||
require.NoError(err)
|
||||
tt.want.PublicId = id
|
||||
got.PublicId = id
|
||||
|
||||
w := db.New(conn)
|
||||
dbWriteErr := w.Create(ctx, got)
|
||||
if tt.wantWriteErr {
|
||||
require.Error(dbWriteErr)
|
||||
return
|
||||
}
|
||||
require.NoError(dbWriteErr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/internal/oplog"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/go-dbw"
|
||||
)
|
||||
|
||||
@@ -44,9 +45,10 @@ func (r *Repository) CreateHost(ctx context.Context, projectId string, h *Host,
|
||||
if projectId == "" {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "no project id")
|
||||
}
|
||||
h.Address = strings.TrimSpace(h.Address)
|
||||
if len(h.Address) < MinHostAddressLength || len(h.Address) > MaxHostAddressLength {
|
||||
return nil, errors.New(ctx, errors.InvalidAddress, op, "invalid address")
|
||||
var err error
|
||||
h.Address, err = util.ParseAddress(ctx, h.Address)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidAddress), errors.WithMsg("invalid address"))
|
||||
}
|
||||
h = h.clone()
|
||||
|
||||
@@ -138,9 +140,10 @@ func (r *Repository) UpdateHost(ctx context.Context, projectId string, h *Host,
|
||||
case strings.EqualFold("Name", f):
|
||||
case strings.EqualFold("Description", f):
|
||||
case strings.EqualFold("Address", f):
|
||||
h.Address = strings.TrimSpace(h.Address)
|
||||
if len(h.Address) < MinHostAddressLength || len(h.Address) > MaxHostAddressLength {
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidAddress, op, "invalid address")
|
||||
var err error
|
||||
h.Address, err = util.ParseAddress(ctx, h.Address)
|
||||
if err != nil {
|
||||
return nil, db.NoRowsAffected, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidAddress), errors.WithMsg("invalid address"))
|
||||
}
|
||||
default:
|
||||
return nil, db.NoRowsAffected, errors.New(ctx, errors.InvalidFieldMask, op, fmt.Sprintf("invalid field mask: %s", f))
|
||||
|
||||
@@ -69,6 +69,21 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
},
|
||||
wantIsErr: errors.InvalidParameter,
|
||||
},
|
||||
{
|
||||
name: "valid-dns-name",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "www.google.com",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "www.google.com",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-ipv4-address",
|
||||
in: &Host{
|
||||
@@ -84,6 +99,16 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid-ipv4-address-with-port",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "127.0.0.1:80",
|
||||
},
|
||||
},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "valid-abbreviated-ipv6-address",
|
||||
in: &Host{
|
||||
@@ -99,6 +124,16 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid-abbreviated-ipv6-address-with-port",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860::8888]:80",
|
||||
},
|
||||
},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "valid-ipv6-address",
|
||||
in: &Host{
|
||||
@@ -114,6 +149,46 @@ func TestRepository_CreateHost(t *testing.T) {
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "invalid-ipv6-address-with-port",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860:0:0:0:0:8888]:80",
|
||||
},
|
||||
},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "valid-abbreviated-[ipv6]-address",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860::8888]",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860::8888]",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-[ipv6]-address",
|
||||
in: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860:0:0:0:0:8888]",
|
||||
},
|
||||
},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
CatalogId: catalog.PublicId,
|
||||
Address: "[2001:4860:4860:0:0:0:0:8888]",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "valid-with-name",
|
||||
in: &Host{
|
||||
@@ -574,6 +649,22 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-dns-name",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "www.google.com",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("www.hashicorp.com"),
|
||||
masks: []string{"Address"},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "www.hashicorp.com",
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-ipv4-address",
|
||||
orig: &Host{
|
||||
@@ -590,6 +681,39 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-invalid-ipv4-address",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("10.0.0.1:80"),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "change-invalid-abbreviated-ipv6-address",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("[2001:4860:4860::8888]:80"),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "change-invalid-ipv6-address",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("[2001:4860:4860:0:0:0:0:8888]:80"),
|
||||
masks: []string{"Address"},
|
||||
wantIsErr: errors.InvalidAddress,
|
||||
},
|
||||
{
|
||||
name: "change-abbreviated-ipv6-address",
|
||||
orig: &Host{
|
||||
@@ -622,6 +746,38 @@ func TestRepository_UpdateHost(t *testing.T) {
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-abbreviated-[ipv6]-address",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("[2001:4860:4860::8888]"),
|
||||
masks: []string{"Address"},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "[2001:4860:4860::8888]",
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-[ipv6]-address",
|
||||
orig: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "127.0.0.1",
|
||||
},
|
||||
},
|
||||
chgFn: changeAddress("[2001:4860:4860:0:0:0:0:8888]"),
|
||||
masks: []string{"Address"},
|
||||
want: &Host{
|
||||
Host: &store.Host{
|
||||
Address: "[2001:4860:4860:0:0:0:0:8888]",
|
||||
},
|
||||
},
|
||||
wantCount: 1,
|
||||
},
|
||||
{
|
||||
name: "change-short-address",
|
||||
orig: &Host{
|
||||
|
||||
@@ -37,6 +37,30 @@ func TestCatalogs(t testing.TB, conn *db.DB, projectId string, count int) []*Hos
|
||||
return cats
|
||||
}
|
||||
|
||||
// TestHost creates a static host to the provided DB with the provided catalog id.
|
||||
// The catalog must have been created previously. If any errors are encountered
|
||||
// during the creation of the host, the test will fail.
|
||||
func TestHost(t testing.TB, conn *db.DB, catalogId string, opt ...Option) *Host {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
assert := assert.New(t)
|
||||
|
||||
host, err := NewHost(ctx, catalogId, opt...)
|
||||
assert.NoError(err)
|
||||
assert.NotNil(host)
|
||||
|
||||
id, err := newHostId(ctx)
|
||||
assert.NoError(err)
|
||||
assert.NotEmpty(id)
|
||||
host.PublicId = id
|
||||
|
||||
w := db.New(conn)
|
||||
err2 := w.Create(ctx, host)
|
||||
assert.NoError(err2)
|
||||
|
||||
return host
|
||||
}
|
||||
|
||||
// TestHosts creates count number of static hosts to the provided DB
|
||||
// with the provided catalog id. The catalog must have been created previously.
|
||||
// If any errors are encountered during the creation of the host, the test will fail.
|
||||
|
||||
Reference in new issue
Block a user