diff --git a/internal/alias/target/alias_test.go b/internal/alias/target/alias_test.go index ec292e2379..8bff6d08dc 100644 --- a/internal/alias/target/alias_test.go +++ b/internal/alias/target/alias_test.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: BUSL-1.1 -package target +package target_test import ( "context" @@ -9,18 +9,21 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/hashicorp/boundary/globals" + "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/alias/target/store" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/iam" "github.com/hashicorp/boundary/internal/target/tcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/testing/protocmp" ) func TestNewAlias(t *testing.T) { t.Run("valid", func(t *testing.T) { - a, err := NewAlias(context.Background(), "global", "valid.alias") + a, err := target.NewAlias(context.Background(), "global", "valid.alias") require.NoError(t, err) assert.NotNil(t, a) assert.Equal(t, a.ScopeId, "global") @@ -28,7 +31,7 @@ func TestNewAlias(t *testing.T) { }) t.Run("with destination", func(t *testing.T) { - a, err := NewAlias(context.Background(), "global", "with.destination", WithDestinationId("ttcp_1234567890")) + a, err := target.NewAlias(context.Background(), "global", "with.destination", target.WithDestinationId("ttcp_1234567890")) require.NoError(t, err) assert.NotNil(t, a) assert.Equal(t, a.ScopeId, "global") @@ -51,16 +54,16 @@ func TestCreate(t *testing.T) { name string scope string value string - opts []Option - validate func(*testing.T, *Alias) + opts []target.Option + validate func(*testing.T, *target.Alias) errContains string }{ { name: "valid", scope: "global", value: "valid.alias", - opts: []Option{WithDestinationId(tar.GetPublicId())}, - validate: func(t *testing.T, a *Alias) { + opts: []target.Option{target.WithDestinationId(tar.GetPublicId())}, + validate: func(t *testing.T, a *target.Alias) { t.Helper() assert.Equal(t, a.DestinationId, tar.GetPublicId()) }, @@ -69,8 +72,8 @@ func TestCreate(t *testing.T) { name: "valid with host", scope: "global", value: "host.valid.alias", - opts: []Option{WithDestinationId(tar.GetPublicId()), WithHostId("hst_1234567890")}, - validate: func(t *testing.T, a *Alias) { + opts: []target.Option{target.WithDestinationId(tar.GetPublicId()), target.WithHostId("hst_1234567890")}, + validate: func(t *testing.T, a *target.Alias) { t.Helper() assert.Equal(t, a.DestinationId, tar.GetPublicId()) assert.Equal(t, a.HostId, "hst_1234567890") @@ -80,7 +83,7 @@ func TestCreate(t *testing.T) { name: "valid no destination", scope: "global", value: "nodestination.alias", - validate: func(t *testing.T, a *Alias) { + validate: func(t *testing.T, a *target.Alias) { t.Helper() assert.Empty(t, a.DestinationId) }, @@ -89,8 +92,8 @@ func TestCreate(t *testing.T) { name: "valid with name", scope: "global", value: "valid-with-name.alias", - opts: []Option{WithName("valid-with-name")}, - validate: func(t *testing.T, a *Alias) { + opts: []target.Option{target.WithName("valid-with-name")}, + validate: func(t *testing.T, a *target.Alias) { t.Helper() assert.Equal(t, "valid-with-name", a.Name) }, @@ -99,8 +102,8 @@ func TestCreate(t *testing.T) { name: "valid with description", scope: "global", value: "valid-with-description.alias", - opts: []Option{WithName("valid-with-description"), WithDescription("a description")}, - validate: func(t *testing.T, a *Alias) { + opts: []target.Option{target.WithName("valid-with-description"), target.WithDescription("a description")}, + validate: func(t *testing.T, a *target.Alias) { t.Helper() assert.Equal(t, "valid-with-description", a.Name) assert.Equal(t, "a description", a.Description) @@ -110,7 +113,7 @@ func TestCreate(t *testing.T) { name: "host with no destination", scope: "global", value: "host.with.no.destination", - opts: []Option{WithHostId("hst_1234567890")}, + opts: []target.Option{target.WithHostId("hst_1234567890")}, errContains: `destination_id_set_when_host_id_is_set constraint failed`, }, { @@ -135,7 +138,7 @@ func TestCreate(t *testing.T) { name: "invalid dest", scope: "global", value: "invalid.dest", - opts: []Option{WithDestinationId("ttcp_unknown")}, + opts: []target.Option{target.WithDestinationId("ttcp_unknown")}, errContains: `foreign key constraint "target_fkey"`, }, { @@ -148,10 +151,10 @@ func TestCreate(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - a, err := NewAlias(ctx, c.scope, c.value, c.opts...) + a, err := target.NewAlias(ctx, c.scope, c.value, c.opts...) require.NoError(t, err) assert.NotNil(t, a) - a.PublicId, err = newAliasId(ctx) + a.PublicId, err = db.NewPublicId(ctx, globals.TargetAliasPrefix) require.NoError(t, err) start := time.Now().UTC() @@ -176,14 +179,14 @@ func TestCreate(t *testing.T) { } t.Run("case insensitive duplicate alias", func(t *testing.T) { - a := TestAlias(t, rw, "duplicate.alias") + a := target.TestAlias(t, rw, "duplicate.alias") t.Cleanup(func() { _, err := rw.Delete(ctx, a) require.NoError(t, err) }) var err error - a.PublicId, err = newAliasId(ctx) + a.PublicId, err = db.NewPublicId(ctx, globals.TargetAliasPrefix) require.NoError(t, err) a.Value = "DUPLICATE.ALIAS" err = rw.Create(ctx, a) @@ -205,20 +208,20 @@ func TestUpdate(t *testing.T) { cases := []struct { name string - startingOptions []Option - in *Alias + startingOptions []target.Option + in *target.Alias fieldMask []string nullMask []string - want *Alias + want *target.Alias errContains string }{ { name: "update alias value", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{Value: "updated.alias"}, }, fieldMask: []string{"Value"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "updated.alias", @@ -227,7 +230,7 @@ func TestUpdate(t *testing.T) { }, { name: "remove alias value", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{}, }, fieldMask: []string{"Value"}, @@ -235,12 +238,12 @@ func TestUpdate(t *testing.T) { }, { name: "update destination id", - startingOptions: []Option{WithDestinationId(tar1.GetPublicId())}, - in: &Alias{ + startingOptions: []target.Option{target.WithDestinationId(tar1.GetPublicId())}, + in: &target.Alias{ Alias: &store.Alias{DestinationId: tar2.GetPublicId()}, }, fieldMask: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -250,17 +253,17 @@ func TestUpdate(t *testing.T) { }, { name: "update destination id with host id", - startingOptions: []Option{ - WithDestinationId(tar1.GetPublicId()), - WithHostId("hst_1234567890"), + startingOptions: []target.Option{ + target.WithDestinationId(tar1.GetPublicId()), + target.WithHostId("hst_1234567890"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ DestinationId: tar2.GetPublicId(), }, }, fieldMask: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -271,12 +274,12 @@ func TestUpdate(t *testing.T) { }, { name: "remove destination id", - startingOptions: []Option{WithDestinationId(tar1.GetPublicId())}, - in: &Alias{ + startingOptions: []target.Option{target.WithDestinationId(tar1.GetPublicId())}, + in: &target.Alias{ Alias: &store.Alias{}, }, nullMask: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -285,15 +288,15 @@ func TestUpdate(t *testing.T) { }, { name: "remove destination id with host id", - startingOptions: []Option{ - WithDestinationId(tar1.GetPublicId()), - WithHostId("hst_1234567890"), + startingOptions: []target.Option{ + target.WithDestinationId(tar1.GetPublicId()), + target.WithHostId("hst_1234567890"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{}, }, nullMask: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -302,17 +305,17 @@ func TestUpdate(t *testing.T) { }, { name: "update host id", - startingOptions: []Option{ - WithDestinationId(tar1.GetPublicId()), - WithHostId("hst_1234567890"), + startingOptions: []target.Option{ + target.WithDestinationId(tar1.GetPublicId()), + target.WithHostId("hst_1234567890"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ HostId: "hst_0987654321", }, }, fieldMask: []string{"HostId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -323,15 +326,15 @@ func TestUpdate(t *testing.T) { }, { name: "remove host id", - startingOptions: []Option{ - WithDestinationId(tar1.GetPublicId()), - WithHostId("hst_1234567890"), + startingOptions: []target.Option{ + target.WithDestinationId(tar1.GetPublicId()), + target.WithHostId("hst_1234567890"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{}, }, nullMask: []string{"HostId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -341,16 +344,16 @@ func TestUpdate(t *testing.T) { }, { name: "update name", - startingOptions: []Option{ - WithName("updateName"), + startingOptions: []target.Option{ + target.WithName("updateName"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ Name: "updateName-updated", }, }, fieldMask: []string{"Name"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Name: "updateName-updated", @@ -360,14 +363,14 @@ func TestUpdate(t *testing.T) { }, { name: "remove name", - startingOptions: []Option{ - WithName("updateName"), + startingOptions: []target.Option{ + target.WithName("updateName"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{}, }, nullMask: []string{"Name"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -376,16 +379,16 @@ func TestUpdate(t *testing.T) { }, { name: "update description", - startingOptions: []Option{ - WithDescription("description"), + startingOptions: []target.Option{ + target.WithDescription("description"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ Description: "description-updated", }, }, fieldMask: []string{"Description"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Description: "description-updated", @@ -395,14 +398,14 @@ func TestUpdate(t *testing.T) { }, { name: "remove description", - startingOptions: []Option{ - WithDescription("description"), + startingOptions: []target.Option{ + target.WithDescription("description"), }, - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{}, }, nullMask: []string{"Description"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test.alias", @@ -413,13 +416,15 @@ func TestUpdate(t *testing.T) { for _, c := range cases { t.Run(c.name, func(t *testing.T) { - a := TestAlias(t, rw, "test.alias", c.startingOptions...) + a := target.TestAlias(t, rw, "test.alias", c.startingOptions...) t.Cleanup(func() { _, err := rw.Delete(ctx, a) require.NoError(t, err) }) - - in := c.in.clone() + cp := proto.Clone(c.in.Alias) + in := &target.Alias{ + Alias: cp.(*store.Alias), + } in.PublicId = a.PublicId in.Version = a.Version @@ -447,7 +452,7 @@ func TestDelete(t *testing.T) { ctx := context.Background() t.Run("delete existing", func(t *testing.T) { - a := TestAlias(t, rw, "alias.to.delete") + a := target.TestAlias(t, rw, "alias.to.delete") n, err := rw.Delete(ctx, a) assert.NoError(t, err) assert.Equal(t, 1, n) @@ -456,14 +461,16 @@ func TestDelete(t *testing.T) { t.Run("delete existing with destination", func(t *testing.T) { _, p := iam.TestScopes(t, iam.TestRepo(t, conn, db.TestWrapper(t))) tar := tcp.TestTarget(ctx, t, conn, p.GetPublicId(), "test") - a := TestAlias(t, rw, "alias.with.destination", WithDestinationId(tar.GetPublicId())) + a := target.TestAlias(t, rw, "alias.with.destination", target.WithDestinationId(tar.GetPublicId())) n, err := rw.Delete(ctx, a) assert.NoError(t, err) assert.Equal(t, 1, n) }) t.Run("delete non-existent", func(t *testing.T) { - a := allocAlias() + a := &target.Alias{ + Alias: &store.Alias{}, + } a.PublicId = "alias_does_not_exist" n, err := rw.Delete(ctx, a) assert.NoError(t, err) diff --git a/internal/alias/target/repository_alias_test.go b/internal/alias/target/repository_alias_test.go index aa149e1607..be3ca93d68 100644 --- a/internal/alias/target/repository_alias_test.go +++ b/internal/alias/target/repository_alias_test.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: BUSL-1.1 -package target +package target_test import ( "context" @@ -9,6 +9,8 @@ import ( "strings" "testing" + "github.com/hashicorp/boundary/globals" + "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/alias/target/store" "github.com/hashicorp/boundary/internal/db" dbassert "github.com/hashicorp/boundary/internal/db/assert" @@ -34,9 +36,9 @@ func TestRepository_CreateAlias(t *testing.T) { tests := []struct { name string - in *Alias - opts []Option - want *Alias + in *target.Alias + opts []target.Option + want *target.Alias errContains string }{ { @@ -45,26 +47,26 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "nil-embedded-alias", - in: &Alias{}, + in: &target.Alias{}, errContains: "nil embedded Alias", }, { name: "no-value", - in: &Alias{Alias: &store.Alias{ + in: &target.Alias{Alias: &store.Alias{ ScopeId: "global", }}, errContains: "no value", }, { name: "no-scope", - in: &Alias{Alias: &store.Alias{ + in: &target.Alias{Alias: &store.Alias{ Value: "global", }}, errContains: "no scope", }, { name: "specified-public-id", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ PublicId: "alt_1234567890", ScopeId: "global", @@ -75,13 +77,13 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "valid-with-value", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-value", }, }, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-value", @@ -90,14 +92,14 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "valid-with-name", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-name", Name: "test-name-repo", }, }, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-name", @@ -107,14 +109,14 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "valid-with-description", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-description", Description: ("test-description-repo"), }, }, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid-with-description", @@ -124,14 +126,14 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "valid-with-destination-id", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid.with.destination.id", DestinationId: tar.GetPublicId(), }, }, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "valid.with.destination.id", @@ -141,7 +143,7 @@ func TestRepository_CreateAlias(t *testing.T) { }, { name: "unknown-destination-id", - in: &Alias{ + in: &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "unknown.destination.id", @@ -156,7 +158,7 @@ func TestRepository_CreateAlias(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { assert := assert.New(t) - repo, err := NewRepository(ctx, rw, rw, kmsCache) + repo, err := target.NewRepository(ctx, rw, rw, kmsCache) assert.NoError(err) assert.NotNil(repo) got, err := repo.CreateAlias(ctx, tt.in, tt.opts...) @@ -179,10 +181,10 @@ func TestRepository_CreateAlias(t *testing.T) { t.Run("invalid-duplicate-aliases-case-insensitive", func(t *testing.T) { assert := assert.New(t) kms := kms.TestKms(t, conn, wrapper) - repo, err := NewRepository(ctx, rw, rw, kms) + repo, err := target.NewRepository(ctx, rw, rw, kms) assert.NoError(err) assert.NotNil(repo) - in := &Alias{ + in := &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test-value-repo", @@ -207,10 +209,10 @@ func TestRepository_CreateAlias(t *testing.T) { t.Run("invalid-duplicate-name", func(t *testing.T) { assert := assert.New(t) kms := kms.TestKms(t, conn, wrapper) - repo, err := NewRepository(ctx, rw, rw, kms) + repo, err := target.NewRepository(ctx, rw, rw, kms) assert.NoError(err) assert.NotNil(repo) - in := &Alias{ + in := &target.Alias{ Alias: &store.Alias{ ScopeId: "global", Value: "test-value-name-1", @@ -254,73 +256,73 @@ func TestRepository_UpdateAlias(t *testing.T) { _, _ = tar1, tar2 - repo, err := NewRepository(ctx, rw, rw, kmsCache) + repo, err := target.NewRepository(ctx, rw, rw, kmsCache) assert.NoError(t, err) assert.NotNil(t, repo) - changeValue := func(s string) func(*Alias) *Alias { - return func(c *Alias) *Alias { + changeValue := func(s string) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.Value = s return c } } - changeName := func(s string) func(*Alias) *Alias { - return func(c *Alias) *Alias { + changeName := func(s string) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.Name = s return c } } - changeDestinationId := func(s string) func(*Alias) *Alias { - return func(c *Alias) *Alias { + changeDestinationId := func(s string) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.DestinationId = s return c } } - changeHostId := func(s string) func(*Alias) *Alias { - return func(c *Alias) *Alias { + changeHostId := func(s string) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.HostId = s return c } } - changeDescription := func(s string) func(*Alias) *Alias { - return func(c *Alias) *Alias { + changeDescription := func(s string) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.Description = s return c } } - makeNil := func() func(*Alias) *Alias { - return func(c *Alias) *Alias { + makeNil := func() func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { return nil } } - makeEmbeddedNil := func() func(*Alias) *Alias { - return func(c *Alias) *Alias { - return &Alias{} + makeEmbeddedNil := func() func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { + return &target.Alias{} } } - deletePublicId := func() func(*Alias) *Alias { - return func(c *Alias) *Alias { + deletePublicId := func() func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.PublicId = "" return c } } - nonExistentPublicId := func() func(*Alias) *Alias { - return func(c *Alias) *Alias { + nonExistentPublicId := func() func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { c.PublicId = "alt_OOOOOOOOOO" return c } } - combine := func(fns ...func(c *Alias) *Alias) func(*Alias) *Alias { - return func(c *Alias) *Alias { + combine := func(fns ...func(c *target.Alias) *target.Alias) func(*target.Alias) *target.Alias { + return func(c *target.Alias) *target.Alias { for _, fn := range fns { c = fn(c) } @@ -330,16 +332,16 @@ func TestRepository_UpdateAlias(t *testing.T) { tests := []struct { name string - orig *Alias - chgFn func(*Alias) *Alias + orig *target.Alias + chgFn func(*target.Alias) *target.Alias masks []string - want *Alias + want *target.Alias wantCount int wantIsErr errors.Code }{ { name: "nil-alias", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "nil-alias", }, @@ -350,7 +352,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "nil-embedded-alias", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "nil-embedded-alias", }, @@ -361,7 +363,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "no-public-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "no-public-id", }, @@ -372,7 +374,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "updating-non-existent-alias", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "updating-non-existent-alias", }, @@ -383,7 +385,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "empty-field-mask", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "empty-field-mask", }, @@ -393,7 +395,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "read-only-fields-in-field-mask", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "read-only-fields-in-field-mask", }, @@ -404,7 +406,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "unknown-field-in-field-mask", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "unknown-field-in-field-mask", }, @@ -415,14 +417,14 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-value", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-value", }, }, chgFn: changeValue("change-value-updated"), masks: []string{"Value"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-value-updated", }, @@ -431,7 +433,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-name", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-name", Name: "change-name", @@ -439,7 +441,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeName("change-name-updated"), masks: []string{"Name"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-name", Name: "change-name-updated", @@ -449,7 +451,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "clear-name", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "clear-name", Name: "clear-name", @@ -457,7 +459,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeName(""), masks: []string{"Name"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "clear-name", }, @@ -466,7 +468,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-destination-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-destination-id", DestinationId: tar1.GetPublicId(), @@ -474,7 +476,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeDestinationId(tar2.GetPublicId()), masks: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-destination-id", DestinationId: tar2.GetPublicId(), @@ -484,7 +486,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-destination-id-to-unknown", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-destination-id-to-unknown", DestinationId: tar1.GetPublicId(), @@ -496,7 +498,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "delete-destination-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "delete-destination-id", DestinationId: tar1.GetPublicId(), @@ -504,7 +506,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeDestinationId(tar2.GetPublicId()), masks: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "delete-destination-id", }, @@ -513,7 +515,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "delete-destination-also-deletes-host-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "delete-destination-also-deletes-host-id", DestinationId: tar1.GetPublicId(), @@ -522,7 +524,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeDestinationId(""), masks: []string{"DestinationId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "delete-destination-also-deletes-host-id", }, @@ -531,7 +533,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-host-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-host-id", DestinationId: tar1.GetPublicId(), @@ -540,7 +542,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeHostId("hst_0987654321"), masks: []string{"HostId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-host-id", DestinationId: tar1.GetPublicId(), @@ -551,7 +553,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "delete-host-id", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "delete-host-id", DestinationId: tar1.GetPublicId(), @@ -560,7 +562,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeHostId(""), masks: []string{"HostId"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "delete-host-id", DestinationId: tar1.GetPublicId(), @@ -570,7 +572,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-description", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-description", Description: "test-description-repo", @@ -578,7 +580,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: changeDescription("test-update-description-repo"), masks: []string{"Description"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-description", Description: "test-update-description-repo", @@ -588,7 +590,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "change-value-and-description", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "change-value-and-description", Description: "test-description-repo", @@ -596,7 +598,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, chgFn: combine(changeDescription("test-update-description-repo"), changeValue("change-value-and-description-updated")), masks: []string{"Value", "Description"}, - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "change-value-and-description-updated", Description: "test-update-description-repo", @@ -606,7 +608,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "delete-value", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "delete-value", }, @@ -617,7 +619,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "delete-description", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "delete-description", Description: "test-description-repo", @@ -625,7 +627,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, masks: []string{"Description"}, chgFn: combine(changeDescription(""), changeValue("delete-description-updated")), - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "delete-description", }, @@ -634,7 +636,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "do-not-delete-value", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "do-not-delete-value", Description: "test-description-repo", @@ -642,7 +644,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, masks: []string{"Description"}, chgFn: combine(changeDescription("test-update-description-repo"), changeValue("")), - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "do-not-delete-value", Description: "test-update-description-repo", @@ -652,7 +654,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, { name: "do-not-delete-description", - orig: &Alias{ + orig: &target.Alias{ Alias: &store.Alias{ Value: "do-not-delete-description", Description: "test-description-repo", @@ -660,7 +662,7 @@ func TestRepository_UpdateAlias(t *testing.T) { }, masks: []string{"Value"}, chgFn: combine(changeDescription(""), changeValue("do-not-delete-description-updated")), - want: &Alias{ + want: &target.Alias{ Alias: &store.Alias{ Value: "do-not-delete-description-updated", Description: "test-description-repo", @@ -713,7 +715,7 @@ func TestRepository_UpdateAlias(t *testing.T) { t.Run("invalid-duplicate-values", func(t *testing.T) { value := "test-dup-value" - c1 := TestAlias(t, db.New(conn), "test") + c1 := target.TestAlias(t, db.New(conn), "test") c1.Value = value got1, gotCount1, err := repo.UpdateAlias(context.Background(), c1, 1, []string{"value"}) assert.NoError(t, err) @@ -721,7 +723,7 @@ func TestRepository_UpdateAlias(t *testing.T) { assert.Equal(t, value, got1.Value) assert.Equal(t, 1, gotCount1, "row count") - c2 := TestAlias(t, db.New(conn), "test2") + c2 := target.TestAlias(t, db.New(conn), "test2") c2.Value = value got2, gotCount2, err := repo.UpdateAlias(context.Background(), c2, 1, []string{"value"}) assert.Truef(t, errors.Match(errors.T(errors.NotUnique), err), "want err code: %v got err: %v", errors.NotUnique, err) @@ -731,7 +733,7 @@ func TestRepository_UpdateAlias(t *testing.T) { t.Run("invalid-duplicate-name", func(t *testing.T) { name := "test-dup-name" - c1 := TestAlias(t, db.New(conn), "duplicate.name.test") + c1 := target.TestAlias(t, db.New(conn), "duplicate.name.test") c1.Name = name got1, gotCount1, err := repo.UpdateAlias(context.Background(), c1, 1, []string{"name"}) assert.NoError(t, err) @@ -739,7 +741,7 @@ func TestRepository_UpdateAlias(t *testing.T) { assert.Equal(t, name, got1.Name) assert.Equal(t, 1, gotCount1, "row count") - c2 := TestAlias(t, db.New(conn), "duplicate.name.test2") + c2 := target.TestAlias(t, db.New(conn), "duplicate.name.test2") c2.Name = name got2, gotCount2, err := repo.UpdateAlias(context.Background(), c2, 1, []string{"name"}) assert.Truef(t, errors.Match(errors.T(errors.NotUnique), err), "want err code: %v got err: %v", errors.NotUnique, err) @@ -753,15 +755,15 @@ func TestRepository_LookupAlias(t *testing.T) { conn, _ := db.TestSetup(t, "postgres") rw := db.New(conn) wrapper := db.TestWrapper(t) - al := TestAlias(t, rw, "one") - badId, err := newAliasId(ctx) + al := target.TestAlias(t, rw, "one") + badId, err := db.NewPublicId(ctx, globals.TargetAliasPrefix) assert.NoError(t, err) assert.NotNil(t, badId) tests := []struct { name string id string - want *Alias + want *target.Alias wantErr errors.Code }{ { @@ -787,7 +789,7 @@ func TestRepository_LookupAlias(t *testing.T) { t.Run(tt.name, func(t *testing.T) { assert := assert.New(t) kms := kms.TestKms(t, conn, wrapper) - repo, err := NewRepository(ctx, rw, rw, kms) + repo, err := target.NewRepository(ctx, rw, rw, kms) assert.NoError(err) assert.NotNil(repo) @@ -818,12 +820,12 @@ func TestRepository_DeleteAlias(t *testing.T) { kmsCache := kms.TestKms(t, conn, wrapper) require.NoError(t, kmsCache.CreateKeys(context.Background(), scope.Global.String(), kms.WithRandomReader(rand.Reader))) - repo, err := NewRepository(ctx, rw, rw, kmsCache) + repo, err := target.NewRepository(ctx, rw, rw, kmsCache) assert.NoError(t, err) require.NotNil(t, repo) - al := TestAlias(t, rw, "deleted.alias") - badId, err := newAliasId(ctx) + al := target.TestAlias(t, rw, "deleted.alias") + badId, err := db.NewPublicId(ctx, globals.TargetAliasPrefix) assert.NoError(t, err) assert.NotNil(t, badId) diff --git a/internal/target/repository.go b/internal/target/repository.go index ba773e8bff..daa28dabd3 100644 --- a/internal/target/repository.go +++ b/internal/target/repository.go @@ -10,6 +10,7 @@ import ( "strings" "time" + target2 "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/boundary" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/db/timestamp" @@ -133,6 +134,7 @@ func (r *Repository) LookupTarget(ctx context.Context, publicIdOrName string, op var address string var hostSources []HostSource var credSources []CredentialSource + var aliases []*target2.Alias _, err := r.writer.DoTx( ctx, db.StdRetryCnt, @@ -156,6 +158,9 @@ func (r *Repository) LookupTarget(ctx context.Context, publicIdOrName string, op if credSources, err = fetchCredentialSources(ctx, read, target.PublicId); err != nil { return errors.Wrap(ctx, err, op) } + if aliases, err = fetchTargetAliases(ctx, read, target.PublicId); err != nil { + return errors.Wrap(ctx, err, op) + } targetAddress, err := fetchAddress(ctx, read, target.PublicId) if err != nil && !errors.IsNotFoundError(err) { return errors.Wrap(ctx, err, op) @@ -178,6 +183,7 @@ func (r *Repository) LookupTarget(ctx context.Context, publicIdOrName string, op } subtype.SetHostSources(hostSources) subtype.SetCredentialSources(credSources) + subtype.SetAliases(aliases) return subtype, nil } diff --git a/internal/target/repository_alias.go b/internal/target/repository_alias.go new file mode 100644 index 0000000000..8f8949b55d --- /dev/null +++ b/internal/target/repository_alias.go @@ -0,0 +1,21 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package target + +import ( + "context" + + "github.com/hashicorp/boundary/internal/alias/target" + "github.com/hashicorp/boundary/internal/db" + "github.com/hashicorp/boundary/internal/errors" +) + +func fetchTargetAliases(ctx context.Context, r db.Reader, targetId string) ([]*target.Alias, error) { + const op = "target.fetchTargetAliases" + var targetAliases []*target.Alias + if err := r.SearchWhere(ctx, &targetAliases, "destination_id = ?", []any{targetId}); err != nil { + return nil, errors.Wrap(ctx, err, op) + } + return targetAliases, nil +} diff --git a/internal/target/target.go b/internal/target/target.go index 2eb9d83f9a..77b017f205 100644 --- a/internal/target/target.go +++ b/internal/target/target.go @@ -9,6 +9,7 @@ import ( "fmt" "github.com/hashicorp/boundary/globals" + "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/boundary" "github.com/hashicorp/boundary/internal/db/timestamp" "github.com/hashicorp/boundary/internal/errors" @@ -36,6 +37,7 @@ type Target interface { GetEgressWorkerFilter() string GetIngressWorkerFilter() string GetAddress() string + GetAliases() []*target.Alias GetHostSources() []HostSource GetCredentialSources() []CredentialSource GetStorageBucketId() string @@ -56,6 +58,7 @@ type Target interface { SetEgressWorkerFilter(string) SetIngressWorkerFilter(string) SetAddress(string) + SetAliases([]*target.Alias) SetHostSources([]HostSource) SetCredentialSources([]CredentialSource) SetStorageBucketId(string) @@ -82,6 +85,7 @@ type targetView struct { tableName string `gorm:"-"` HostSource []HostSource `gorm:"-"` CredentialSources []CredentialSource `gorm:"-"` + Aliases []*target.Alias `gorm:"-"` } // allocTargetView will allocate a target view diff --git a/internal/target/targettest/target.go b/internal/target/targettest/target.go index 2f26d72fb2..ee1c6eff72 100644 --- a/internal/target/targettest/target.go +++ b/internal/target/targettest/target.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/hashicorp/boundary/globals" + target2 "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/db/timestamp" "github.com/hashicorp/boundary/internal/errors" @@ -34,6 +35,7 @@ type Target struct { tableName string `gorm:"-"` HostSource []target.HostSource `gorm:"-"` CredentialSources []target.CredentialSource `gorm:"-"` + Aliases []*target2.Alias `gorm:"-"` } var ( @@ -139,6 +141,10 @@ func (t *Target) GetAddress() string { return t.Address } +func (t *Target) GetAliases() []*target2.Alias { + return t.Aliases +} + func (t *Target) GetHostSources() []target.HostSource { return t.HostSource } @@ -231,6 +237,10 @@ func (t *Target) SetAddress(a string) { t.Address = a } +func (t *Target) SetAliases(aliases []*target2.Alias) { + t.Aliases = aliases +} + func (t *Target) SetHostSources(sources []target.HostSource) { t.HostSource = sources } diff --git a/internal/target/tcp/repository_alias_test.go b/internal/target/tcp/repository_alias_test.go new file mode 100644 index 0000000000..a5fd5e3c1f --- /dev/null +++ b/internal/target/tcp/repository_alias_test.go @@ -0,0 +1,78 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: BUSL-1.1 + +package tcp_test + +import ( + "context" + "testing" + + target2 "github.com/hashicorp/boundary/internal/alias/target" + "github.com/hashicorp/boundary/internal/db" + "github.com/hashicorp/boundary/internal/iam" + "github.com/hashicorp/boundary/internal/kms" + "github.com/hashicorp/boundary/internal/target" + "github.com/hashicorp/boundary/internal/target/tcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestRepository_FetchAliases(t *testing.T) { + t.Parallel() + conn, _ := db.TestSetup(t, "postgres") + rw := db.New(conn) + wrapper := db.TestWrapper(t) + testKms := kms.TestKms(t, conn, wrapper) + iamRepo := iam.TestRepo(t, conn, wrapper) + _, staticProj := iam.TestScopes(t, iamRepo) + ctx := context.Background() + repo, err := target.NewRepository(ctx, rw, rw, testKms) + require.NoError(t, err) + + t.Run("target-no-aliases", func(t *testing.T) { + tar := tcp.TestTarget(ctx, t, conn, staticProj.PublicId, "test-target-1") + tar, err := repo.LookupTarget(context.Background(), tar.GetPublicId()) + require.NoError(t, err) + + gotAliases := tar.GetAliases() + assert.Equal(t, len(gotAliases), 0) + }) + t.Run("target-one-alias", func(t *testing.T) { + aname := "test-alias" + tar := tcp.TestTarget(ctx, t, conn, staticProj.PublicId, "test-target-2") + al := target2.TestAlias(t, rw, aname, target2.WithDestinationId(tar.GetPublicId())) + + tar, err := repo.LookupTarget(context.Background(), tar.GetPublicId()) + require.NoError(t, err) + + gotAliases := tar.GetAliases() + assert.Equal(t, len(gotAliases), 1) + assert.Equal(t, al.Value, aname) + }) + t.Run("target-multiple-alias", func(t *testing.T) { + tar := tcp.TestTarget(ctx, t, conn, staticProj.PublicId, "test-target3") + aliases := make(map[string]*target2.Alias) + aname1 := "test.alias.one" + aname2 := "test.alias.two" + aname3 := "test.alias.three" + al := target2.TestAlias(t, rw, aname1, target2.WithDestinationId(tar.GetPublicId())) + aliases[aname1] = al + al2 := target2.TestAlias(t, rw, aname2, target2.WithDestinationId(tar.GetPublicId())) + aliases[aname2] = al2 + al3 := target2.TestAlias(t, rw, aname3, target2.WithDestinationId(tar.GetPublicId())) + aliases[aname3] = al3 + + tar, err := repo.LookupTarget(context.Background(), tar.GetPublicId()) + require.NoError(t, err) + + gotAliases := tar.GetAliases() + assert.Equal(t, len(gotAliases), 3) + for _, a := range gotAliases { + v, ok := aliases[a.Value] + require.Equal(t, ok, true) + require.Equal(t, a.Value, v.Value) + require.Equal(t, a.ScopeId, v.ScopeId) + require.Equal(t, a.Name, v.Name) + } + }) +} diff --git a/internal/target/tcp/target.go b/internal/target/tcp/target.go index f9dce17550..21f88a530f 100644 --- a/internal/target/tcp/target.go +++ b/internal/target/tcp/target.go @@ -12,6 +12,7 @@ import ( "strings" "github.com/hashicorp/boundary/globals" + target2 "github.com/hashicorp/boundary/internal/alias/target" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/db/timestamp" "github.com/hashicorp/boundary/internal/errors" @@ -36,6 +37,7 @@ type Target struct { tableName string `gorm:"-"` HostSource []target.HostSource `gorm:"-"` CredentialSources []target.CredentialSource `gorm:"-"` + Aliases []*target2.Alias `gorm:"-"` } // Ensure Target implements interfaces @@ -86,6 +88,7 @@ func (t *Target) Clone() target.Target { Address: t.Address, HostSource: t.HostSource, CredentialSources: t.CredentialSources, + Aliases: t.Aliases, } } @@ -141,6 +144,10 @@ func (t *Target) GetAddress() string { return t.Address } +func (t *Target) GetAliases() []*target2.Alias { + return t.Aliases +} + func (t *Target) GetHostSources() []target.HostSource { return t.HostSource } @@ -228,6 +235,10 @@ func (t *Target) SetAddress(address string) { t.Address = address } +func (t *Target) SetAliases(aliases []*target2.Alias) { + t.Aliases = aliases +} + func (t *Target) SetHostSources(sources []target.HostSource) { t.HostSource = sources }