From 7a6c4eeb56b54a944613c457369808a7285fea27 Mon Sep 17 00:00:00 2001 From: Hugo <10965479+hugoghx@users.noreply.github.com> Date: Mon, 19 May 2025 15:10:05 +0100 Subject: [PATCH] feat(target): Enrich CredentialLibrary object with credential type This allows for registered subtype-specific functions to perform logic using a credential library's credential types. --- .../service_list_libraries_ext_test.go | 8 ++-- internal/credential/vault/credential_test.go | 3 +- internal/credential/vault/jobs_test.go | 15 +++---- .../repository_credential_library_test.go | 16 ++++---- .../vault/repository_credential_store_test.go | 10 ++--- .../vault/repository_credentials_test.go | 8 ++-- internal/credential/vault/testing.go | 9 ++++- .../credential/vault/testing_external_test.go | 5 ++- internal/credential/vault/testing_test.go | 3 +- .../credentiallibrary_service_test.go | 14 +++---- .../credentiallibraries/grants_test.go | 2 +- .../handlers/sessions/grants_test.go | 2 +- .../handlers/targets/tcp/grants_test.go | 2 +- .../targets/tcp/target_service_test.go | 40 +++++++++---------- internal/session/repository_session_test.go | 3 +- internal/target/credential_library.go | 6 ++- .../target/credential_library_ext_test.go | 3 +- internal/target/query.go | 6 +++ .../target/repository_credential_source.go | 32 ++++++++++++++- internal/target/repository_ext_test.go | 4 +- .../tcp/repository_credential_source_test.go | 11 ++--- .../target/tcp/repository_tcp_target_test.go | 4 +- internal/target/tcp/testing_test.go | 5 ++- internal/target/testing.go | 7 ++-- 24 files changed, 134 insertions(+), 84 deletions(-) diff --git a/internal/credential/service_list_libraries_ext_test.go b/internal/credential/service_list_libraries_ext_test.go index 614dad976..82dfb2f22 100644 --- a/internal/credential/service_list_libraries_ext_test.go +++ b/internal/credential/service_list_libraries_ext_test.go @@ -45,9 +45,9 @@ func TestLibraryService_List(t *testing.T) { fiveDaysAgo := time.Now().AddDate(0, 0, -5) credStore := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] libs := []credential.Library{ - vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0], + vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0], vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0], - vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0], + vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0], vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0], vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0], } @@ -437,7 +437,7 @@ func TestLibraryService_List(t *testing.T) { require.Empty(t, resp6.Items) // Create some new libraries - newLib1 := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0] + newLib1 := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] newLib2 := vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0] t.Cleanup(func() { _, err := repo.DeleteCredentialLibrary(ctx, credStore.ProjectId, newLib1.GetPublicId()) @@ -517,7 +517,7 @@ func TestLibraryService_List(t *testing.T) { require.Empty(t, resp3.Items) // Create some new libraries - newLibs1 := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 2) + newLibs1 := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 2) newLibs2 := vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 2) t.Cleanup(func() { _, err := repo.DeleteCredentialLibrary(ctx, credStore.ProjectId, newLibs1[0].GetPublicId()) diff --git a/internal/credential/vault/credential_test.go b/internal/credential/vault/credential_test.go index db5a9f573..f787ff48a 100644 --- a/internal/credential/vault/credential_test.go +++ b/internal/credential/vault/credential_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/credential/vault/store" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/db/sentinel" @@ -28,7 +29,7 @@ func TestCredential_New(t *testing.T) { _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStores(t, conn, wrapper, prj.PublicId, 1)[0] - lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, 1)[0] + lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, globals.UnspecifiedCredentialType, 1)[0] token := cs.Token() iamRepo := iam.TestRepo(t, conn, wrapper) diff --git a/internal/credential/vault/jobs_test.go b/internal/credential/vault/jobs_test.go index 12b029a25..bfa9cf6d1 100644 --- a/internal/credential/vault/jobs_test.go +++ b/internal/credential/vault/jobs_test.go @@ -11,6 +11,7 @@ import ( "testing" "time" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/authtoken" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/errors" @@ -866,7 +867,7 @@ func TestTokenRevocationJob_Run(t *testing.T) { h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0] static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(ctx, t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ UserId: uId, HostId: h.GetPublicId(), @@ -1066,7 +1067,7 @@ func TestCredentialRenewalJob_RunLimits(t *testing.T) { h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0] static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(ctx, t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ UserId: uId, HostId: h.GetPublicId(), @@ -1369,7 +1370,7 @@ func TestCredentialRenewalJob_NextRunIn(t *testing.T) { h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0] static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(ctx, t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ UserId: uId, HostId: h.GetPublicId(), @@ -1613,7 +1614,7 @@ func TestCredentialRevocationJob_RunLimits(t *testing.T) { h := static.TestHosts(t, conn, hc.GetPublicId(), 1)[0] static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(ctx, t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) sess := session.TestSession(t, conn, wrapper, session.ComposedOf{ UserId: uId, HostId: h.GetPublicId(), @@ -2187,7 +2188,7 @@ func TestVaultJobsCorrelationId(t *testing.T) { _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStore(t, conn, wrapper, prj.PublicId, "http://vault", "vault-token", "accessor") - lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, 1)[0] + lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, globals.UnspecifiedCredentialType, 1)[0] token := cs.Token() iamRepo := iam.TestRepo(t, conn, wrapper) @@ -2352,11 +2353,11 @@ func TestVaultJobsWorkerFilters(t *testing.T) { _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStore(t, conn, wrapper, prj.PublicId, "http://vault", "vault-token", "accessor", WithWorkerFilter("true == true")) - lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, 1)[0] + lib := TestCredentialLibraries(t, conn, wrapper, cs.PublicId, globals.UnspecifiedCredentialType, 1)[0] token := cs.Token() csNoFilter := TestCredentialStore(t, conn, wrapper, prj.PublicId, "http://vault", "vault-token-no-filter", "accessor") - libNoFilter := TestCredentialLibraries(t, conn, wrapper, csNoFilter.PublicId, 1)[0] + libNoFilter := TestCredentialLibraries(t, conn, wrapper, csNoFilter.PublicId, globals.UnspecifiedCredentialType, 1)[0] tokenNoFilter := csNoFilter.Token() iamRepo := iam.TestRepo(t, conn, wrapper) diff --git a/internal/credential/vault/repository_credential_library_test.go b/internal/credential/vault/repository_credential_library_test.go index b57d0d467..fccb90133 100644 --- a/internal/credential/vault/repository_credential_library_test.go +++ b/internal/credential/vault/repository_credential_library_test.go @@ -1559,7 +1559,7 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) { name := "test-dup-name" _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 2) + libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 2) lA, lB := libs[0], libs[1] @@ -1684,8 +1684,8 @@ func TestRepository_UpdateCredentialLibrary(t *testing.T) { csA, csB := css[0], css[1] - lA := TestCredentialLibraries(t, conn, wrapper, csA.GetPublicId(), 1)[0] - lB := TestCredentialLibraries(t, conn, wrapper, csB.GetPublicId(), 1)[0] + lA := TestCredentialLibraries(t, conn, wrapper, csA.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] + lB := TestCredentialLibraries(t, conn, wrapper, csB.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] assert.NotEqual(lA.StoreId, lB.StoreId) orig := lA.clone() @@ -1960,7 +1960,7 @@ func TestRepository_DeleteCredentialLibrary(t *testing.T) { { _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - l := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 1)[0] + l := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] badId, err := newCredentialLibraryId(ctx) require.NoError(t, err) @@ -2152,7 +2152,7 @@ func TestRepository_ListCredentialLibraries_Limits(t *testing.T) { _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) cs := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] const count = 10 - libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), count) + libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, count) tests := []struct { name string @@ -2220,7 +2220,7 @@ func TestRepository_ListCredentialLibraries_Pagination(t *testing.T) { css := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 3) for _, cs := range css[:2] { // Leave the third store empty - TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 5) + TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 5) } repo, err := NewRepository(ctx, rw, rw, kms, sche) require.NoError(err) @@ -2275,7 +2275,7 @@ func TestRepository_ListDeletedLibraryIds(t *testing.T) { sche := scheduler.TestScheduler(t, conn, wrapper) _, prj := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) store := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - libs := TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 2) + libs := TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 2) repo, err := NewRepository(ctx, rw, rw, kms, sche) require.NoError(err) @@ -2332,7 +2332,7 @@ func TestRepository_EstimatedLibraryCount(t *testing.T) { assert.Equal(0, numItems) // Create some libraries - libs := TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 2) + libs := TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 2) // Run analyze to update postgres meta tables _, err = sqlDb.ExecContext(ctx, "analyze") require.NoError(err) diff --git a/internal/credential/vault/repository_credential_store_test.go b/internal/credential/vault/repository_credential_store_test.go index ae164b23a..e17ff3d1a 100644 --- a/internal/credential/vault/repository_credential_store_test.go +++ b/internal/credential/vault/repository_credential_store_test.go @@ -1441,7 +1441,7 @@ group by store_id, status; cs := css[0] storeId = cs.GetPublicId() - libs = TestCredentialLibraries(t, conn, wrapper, storeId, 4) + libs = TestCredentialLibraries(t, conn, wrapper, storeId, globals.UnspecifiedCredentialType, 4) return }, }, @@ -1453,7 +1453,7 @@ group by store_id, status; cs := css[0] storeId = cs.GetPublicId() - libs = TestCredentialLibraries(t, conn, wrapper, storeId, 4) + libs = TestCredentialLibraries(t, conn, wrapper, storeId, globals.UnspecifiedCredentialType, 4) tokens = makeMaintainingTokens(t, conn, wrapper, tokens, projectId, storeId, 4) return }, @@ -1466,7 +1466,7 @@ group by store_id, status; cs := css[0] storeId = cs.GetPublicId() - libs = TestCredentialLibraries(t, conn, wrapper, storeId, 4) + libs = TestCredentialLibraries(t, conn, wrapper, storeId, globals.UnspecifiedCredentialType, 4) tokens = makeRevokedTokens(t, conn, wrapper, tokens, projectId, storeId, 4) return }, @@ -1479,7 +1479,7 @@ group by store_id, status; cs := css[0] storeId = cs.GetPublicId() - libs = TestCredentialLibraries(t, conn, wrapper, storeId, 4) + libs = TestCredentialLibraries(t, conn, wrapper, storeId, globals.UnspecifiedCredentialType, 4) tokens = makeExpiredTokens(t, conn, wrapper, tokens, projectId, storeId, 4) return }, @@ -1492,7 +1492,7 @@ group by store_id, status; cs := css[0] storeId = cs.GetPublicId() - libs = TestCredentialLibraries(t, conn, wrapper, storeId, 4) + libs = TestCredentialLibraries(t, conn, wrapper, storeId, globals.UnspecifiedCredentialType, 4) tokens = makeMaintainingTokens(t, conn, wrapper, tokens, projectId, storeId, 2) tokens = makeRevokedTokens(t, conn, wrapper, tokens, projectId, storeId, 3) tokens = makeExpiredTokens(t, conn, wrapper, tokens, projectId, storeId, 5) diff --git a/internal/credential/vault/repository_credentials_test.go b/internal/credential/vault/repository_credentials_test.go index 61ba3d971..19b7500ad 100644 --- a/internal/credential/vault/repository_credentials_test.go +++ b/internal/credential/vault/repository_credentials_test.go @@ -518,7 +518,7 @@ func TestRepository_Revoke(t *testing.T) { assert.NotEmpty(prj.GetPublicId()) cs := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 1)[0] + cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] hc := static.TestCatalogs(t, conn, prj.GetPublicId(), 1)[0] hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0] @@ -526,7 +526,7 @@ func TestRepository_Revoke(t *testing.T) { static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(context.Background(), t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) const ( sessionCount = 4 @@ -636,7 +636,7 @@ func Test_TerminateSession(t *testing.T) { assert.NotEmpty(prj.GetPublicId()) cs := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 1)[0] + cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] hc := static.TestCatalogs(t, conn, prj.GetPublicId(), 1)[0] hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0] @@ -644,7 +644,7 @@ func Test_TerminateSession(t *testing.T) { static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(context.Background(), t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) const ( sessionCount = 4 diff --git a/internal/credential/vault/testing.go b/internal/credential/vault/testing.go index c72c21706..cb27228a8 100644 --- a/internal/credential/vault/testing.go +++ b/internal/credential/vault/testing.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/db/common" "github.com/hashicorp/boundary/internal/errors" @@ -115,7 +116,7 @@ func TestCredentialStores(t testing.TB, conn *db.DB, wrapper wrapping.Wrapper, p // libraries in the provided DB with the provided store id. If any errors // are encountered during the creation of the credential libraries, the // test will fail. -func TestCredentialLibraries(t testing.TB, conn *db.DB, _ wrapping.Wrapper, storeId string, count int) []*CredentialLibrary { +func TestCredentialLibraries(t testing.TB, conn *db.DB, _ wrapping.Wrapper, storeId string, credType globals.CredentialType, count int) []*CredentialLibrary { t.Helper() ctx := context.Background() assert, require := assert.New(t), require.New(t) @@ -123,7 +124,11 @@ func TestCredentialLibraries(t testing.TB, conn *db.DB, _ wrapping.Wrapper, stor var libs []*CredentialLibrary for i := 0; i < count; i++ { - lib, err := NewCredentialLibrary(storeId, fmt.Sprintf("vault/path%d", i), WithMethod(MethodGet)) + opts := []Option{WithMethod(MethodGet)} + if credType != "" { + opts = append(opts, WithCredentialType(credType)) + } + lib, err := NewCredentialLibrary(storeId, fmt.Sprintf("vault/path%d", i), opts...) assert.NoError(err) require.NotNil(lib) id, err := newCredentialLibraryId(ctx) diff --git a/internal/credential/vault/testing_external_test.go b/internal/credential/vault/testing_external_test.go index 045d90e69..e1a23478f 100644 --- a/internal/credential/vault/testing_external_test.go +++ b/internal/credential/vault/testing_external_test.go @@ -7,6 +7,7 @@ import ( "context" "testing" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/authtoken" "github.com/hashicorp/boundary/internal/credential/vault" "github.com/hashicorp/boundary/internal/db" @@ -32,7 +33,7 @@ func Test_TestCredentials(t *testing.T) { assert.NotEmpty(prj.GetPublicId()) cs := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 1)[0] + cl := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] hc := static.TestCatalogs(t, conn, prj.GetPublicId(), 1)[0] hs := static.TestSets(t, conn, hc.GetPublicId(), 1)[0] @@ -40,7 +41,7 @@ func Test_TestCredentials(t *testing.T) { static.TestSetMembers(t, conn, hs.GetPublicId(), []*static.Host{h}) tar := tcp.TestTarget(context.Background(), t, conn, prj.GetPublicId(), "test", target.WithHostSources([]string{hs.GetPublicId()})) - target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId()) + target.TestCredentialLibrary(t, conn, tar.GetPublicId(), cl.GetPublicId(), string(cl.CredentialType())) at := authtoken.TestAuthToken(t, conn, kms, org.GetPublicId()) uId := at.GetIamUserId() diff --git a/internal/credential/vault/testing_test.go b/internal/credential/vault/testing_test.go index b8faec656..95b2d4b28 100644 --- a/internal/credential/vault/testing_test.go +++ b/internal/credential/vault/testing_test.go @@ -16,6 +16,7 @@ import ( "testing" "time" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/errors" "github.com/hashicorp/boundary/internal/iam" @@ -56,7 +57,7 @@ func Test_TestCredentialLibraries(t *testing.T) { cs := TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] count := 4 - libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), count) + libs := TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, count) assert.Len(libs, count) for _, lib := range libs { assert.NotEmpty(lib.GetPublicId()) diff --git a/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service_test.go b/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service_test.go index 2c7b289c7..295235acd 100644 --- a/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service_test.go +++ b/internal/daemon/controller/handlers/credentiallibraries/credentiallibrary_service_test.go @@ -108,7 +108,7 @@ func TestList(t *testing.T) { store := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] var wantLibraries []*pb.CredentialLibrary - for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 10) { + for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 10) { wantLibraries = append(wantLibraries, vaultCredentialLibraryToProto(l, prj)) } for _, l := range vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 10) { @@ -268,7 +268,7 @@ func TestList_Attributes(t *testing.T) { storeGeneric, storeSSHCertificate := ts[0], ts[1] var wantLibrariesGeneric []*pb.CredentialLibrary var wantLibrariesSSHCertificate []*pb.CredentialLibrary - for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, storeGeneric.GetPublicId(), 5) { + for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, storeGeneric.GetPublicId(), globals.UnspecifiedCredentialType, 5) { wantLibrariesGeneric = append(wantLibrariesGeneric, vaultCredentialLibraryToProto(l, prj)) } for _, l := range vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, storeSSHCertificate.GetPublicId(), 10) { @@ -382,7 +382,7 @@ func TestCreate(t *testing.T) { _, prj := iam.TestScopes(t, iamRepo) store := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - defaultCL := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0] + defaultCL := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] defaultCreated := defaultCL.GetCreateTime().GetTimestamp() cases := []struct { @@ -1169,7 +1169,7 @@ func TestGet(t *testing.T) { _, prj := iam.TestScopes(t, iamRepo) store := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - unspecifiedLib := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0] + unspecifiedLib := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] s, err := NewService(ctx, iamRepoFn, repoFn, 1000) require.NoError(t, err) repo, err := repoFn() @@ -1427,7 +1427,7 @@ func TestDelete(t *testing.T) { _, prj := iam.TestScopes(t, iamRepo) store := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] - vl := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0] + vl := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] vl2 := vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 1)[0] s, err := NewService(ctx, iamRepoFn, repoFn, 1000) require.NoError(t, err) @@ -3286,7 +3286,7 @@ func TestListPagination(t *testing.T) { credStore := vault.TestCredentialStores(t, conn, wrapper, prj.GetPublicId(), 1)[0] var allCredentialLibraries []*pb.CredentialLibrary - for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 5) { + for _, l := range vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 5) { allCredentialLibraries = append(allCredentialLibraries, vaultCredentialLibraryToProto(l, prj)) } for _, l := range vault.TestSSHCertificateCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 5) { @@ -3407,7 +3407,7 @@ func TestListPagination(t *testing.T) { ) // Create another credential library - newCredLib := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), 1)[0] + newCredLib := vault.TestCredentialLibraries(t, conn, wrapper, credStore.GetPublicId(), globals.UnspecifiedCredentialType, 1)[0] pbNewCredLib := vaultCredentialLibraryToProto(newCredLib, prj) // Prepend since this is the newest library allCredentialLibraries = append([]*pb.CredentialLibrary{pbNewCredLib}, allCredentialLibraries...) diff --git a/internal/daemon/controller/handlers/credentiallibraries/grants_test.go b/internal/daemon/controller/handlers/credentiallibraries/grants_test.go index d3d92590c..9aa4f1b22 100644 --- a/internal/daemon/controller/handlers/credentiallibraries/grants_test.go +++ b/internal/daemon/controller/handlers/credentiallibraries/grants_test.go @@ -45,7 +45,7 @@ func TestGrants_ReadActions(t *testing.T) { org, proj := iam.TestScopes(t, iamRepo) proj1CredStore := vault.TestCredentialStores(t, conn, wrap, proj.PublicId, 1) - proj1Libs := vault.TestCredentialLibraries(t, conn, wrap, proj1CredStore[0].GetPublicId(), 3) + proj1Libs := vault.TestCredentialLibraries(t, conn, wrap, proj1CredStore[0].GetPublicId(), globals.UnspecifiedCredentialType, 3) t.Run("List", func(t *testing.T) { testcases := []struct { diff --git a/internal/daemon/controller/handlers/sessions/grants_test.go b/internal/daemon/controller/handlers/sessions/grants_test.go index 6a2f95d24..85713f4a8 100644 --- a/internal/daemon/controller/handlers/sessions/grants_test.go +++ b/internal/daemon/controller/handlers/sessions/grants_test.go @@ -52,7 +52,7 @@ func testSession(t *testing.T, require.NoError(t, err) vaultStore := vault.TestCredentialStore(t, conn, wrapper, projectId, fmt.Sprintf("http://vault%s", randomString), fmt.Sprintf("vault-token-%s", randomString), fmt.Sprintf("accessor-%s", randomString)) - libIds := vault.TestCredentialLibraries(t, conn, wrapper, vaultStore.GetPublicId(), 2) + libIds := vault.TestCredentialLibraries(t, conn, wrapper, vaultStore.GetPublicId(), globals.UsernamePasswordCredentialType, 2) tcpTarget := tcp.TestTarget(context.Background(), t, conn, projectId, randomString, target.WithHostSources([]string{sets[0].GetPublicId()})) staticStore := credstatic.TestCredentialStore(t, conn, wrapper, projectId) upCreds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, randomString, randomString, staticStore.GetPublicId(), projectId, 2) diff --git a/internal/daemon/controller/handlers/targets/tcp/grants_test.go b/internal/daemon/controller/handlers/targets/tcp/grants_test.go index d1af45235..e83e4c04a 100644 --- a/internal/daemon/controller/handlers/targets/tcp/grants_test.go +++ b/internal/daemon/controller/handlers/targets/tcp/grants_test.go @@ -472,7 +472,7 @@ func TestGrants_SetTargetCredentialSources(t *testing.T) { _, proj2 := iam.TestScopes(t, iamRepo, iam.WithSkipAdminRoleCreation(true), iam.WithSkipDefaultRoleCreation(true)) proj1Vault := vault.TestCredentialStores(t, conn, wrapper, proj1.GetPublicId(), 1)[0] - proj1Cls := vault.TestCredentialLibraries(t, conn, wrapper, proj1Vault.GetPublicId(), 2) + proj1Cls := vault.TestCredentialLibraries(t, conn, wrapper, proj1Vault.GetPublicId(), globals.UsernamePasswordCredentialType, 2) proj2StoreStatic := credstatic.TestCredentialStore(t, conn, wrapper, proj2.GetPublicId()) proj2Creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "user", "pass", proj2StoreStatic.GetPublicId(), proj2.GetPublicId(), 2) diff --git a/internal/daemon/controller/handlers/targets/tcp/target_service_test.go b/internal/daemon/controller/handlers/targets/tcp/target_service_test.go index 580f3b3c2..835438671 100644 --- a/internal/daemon/controller/handlers/targets/tcp/target_service_test.go +++ b/internal/daemon/controller/handlers/targets/tcp/target_service_test.go @@ -3080,7 +3080,7 @@ func TestAddTargetCredentialSources(t *testing.T) { require.NoError(t, err, "Error when getting new target service.") storeVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - cls := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 2) + cls := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UsernamePasswordCredentialType, 2) storeStatic := credstatic.TestCredentialStore(t, conn, wrapper, proj.GetPublicId()) creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "user", "pass", storeStatic.GetPublicId(), proj.GetPublicId(), 2) @@ -3112,7 +3112,7 @@ func TestAddTargetCredentialSources(t *testing.T) { }, { name: "Add library on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for lib-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for lib-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), addSources: []string{cls[1].GetPublicId()}, resultSourceIds: []string{cls[0].GetPublicId(), cls[1].GetPublicId()}, }, @@ -3130,13 +3130,13 @@ func TestAddTargetCredentialSources(t *testing.T) { }, { name: "Add static cred on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), addSources: []string{creds[1].GetPublicId()}, resultSourceIds: []string{cls[0].GetPublicId(), creds[1].GetPublicId()}, }, { name: "Add upd static cred on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static_upd-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated for static_upd-lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), addSources: []string{updCreds[1].GetPublicId()}, resultSourceIds: []string{cls[0].GetPublicId(), updCreds[1].GetPublicId()}, }, @@ -3154,7 +3154,7 @@ func TestAddTargetCredentialSources(t *testing.T) { }, { name: "Add duplicated sources on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicated for lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicated for lib sources", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), addSources: []string{cls[1].GetPublicId(), cls[1].GetPublicId(), creds[1].GetPublicId(), creds[1].GetPublicId()}, resultSourceIds: []string{cls[0].GetPublicId(), cls[1].GetPublicId(), creds[1].GetPublicId()}, }, @@ -3291,7 +3291,7 @@ func TestSetTargetCredentialSources(t *testing.T) { require.NoError(t, err, "Error when getting new target service.") storeVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - cls := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 2) + cls := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UsernamePasswordCredentialType, 2) storeStatic := credstatic.TestCredentialStore(t, conn, wrapper, proj.GetPublicId()) creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "user", "pass", storeStatic.GetPublicId(), proj.GetPublicId(), 2) @@ -3323,19 +3323,19 @@ func TestSetTargetCredentialSources(t *testing.T) { }, { name: "Set library on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated library-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated library-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), setCredentialSources: []string{cls[1].GetPublicId()}, resultCredentialSourceIds: []string{cls[1].GetPublicId()}, }, { name: "Set static on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), setCredentialSources: []string{creds[1].GetPublicId()}, resultCredentialSourceIds: []string{creds[1].GetPublicId()}, }, { name: "Set static_upd on library populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static_upd-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "populated static_upd-library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), setCredentialSources: []string{updCreds[1].GetPublicId()}, resultCredentialSourceIds: []string{updCreds[1].GetPublicId()}, }, @@ -3371,7 +3371,7 @@ func TestSetTargetCredentialSources(t *testing.T) { }, { name: "Set duplicate library on populated target", - tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicate library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)})), + tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "duplicate library", target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))})), setCredentialSources: []string{cls[1].GetPublicId(), cls[1].GetPublicId()}, resultCredentialSourceIds: []string{cls[1].GetPublicId()}, }, @@ -3390,7 +3390,7 @@ func TestSetTargetCredentialSources(t *testing.T) { { name: "Set empty on populated target", tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "another populated", - target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose)}), + target.WithCredentialLibraries([]*target.CredentialLibrary{target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType()))}), target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose)}), target.WithStaticCredentials([]*target.StaticCredential{target.TestNewStaticCredential("", updCreds[0].GetPublicId(), credential.BrokeredPurpose)}), ), @@ -3504,7 +3504,7 @@ func TestRemoveTargetCredentialSources(t *testing.T) { require.NoError(t, err, "Error when getting new target service.") csVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - cls := vault.TestCredentialLibraries(t, conn, wrapper, csVault.GetPublicId(), 2) + cls := vault.TestCredentialLibraries(t, conn, wrapper, csVault.GetPublicId(), globals.UsernamePasswordCredentialType, 2) csStatic := credstatic.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] creds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", csStatic.GetPublicId(), proj.GetPublicId(), 2) @@ -3539,8 +3539,8 @@ func TestRemoveTargetCredentialSources(t *testing.T) { name: "Remove 1 of 2 libraries", tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove partial lib", target.WithCredentialLibraries([]*target.CredentialLibrary{ - target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose), - target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose), + target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType())), + target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose, string(cls[1].CredentialType())), })), removeCredentialSources: []string{cls[1].GetPublicId()}, resultCredentialSourceIds: []string{cls[0].GetPublicId()}, @@ -3569,8 +3569,8 @@ func TestRemoveTargetCredentialSources(t *testing.T) { name: "Remove 1 duplicate set of 2 libraries", tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove duplicate lib", target.WithCredentialLibraries([]*target.CredentialLibrary{ - target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose), - target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose), + target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType())), + target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose, string(cls[1].CredentialType())), })), removeCredentialSources: []string{ cls[1].GetPublicId(), cls[1].GetPublicId(), @@ -3605,8 +3605,8 @@ func TestRemoveTargetCredentialSources(t *testing.T) { name: "Remove mixed sources from target", tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove mixed", target.WithCredentialLibraries([]*target.CredentialLibrary{ - target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose), - target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose), + target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType())), + target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose, string(cls[1].CredentialType())), }), target.WithStaticCredentials([]*target.StaticCredential{ target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose), @@ -3625,8 +3625,8 @@ func TestRemoveTargetCredentialSources(t *testing.T) { name: "Remove all sources from target", tar: tcp.TestTarget(ctx, t, conn, proj.GetPublicId(), "remove all", target.WithCredentialLibraries([]*target.CredentialLibrary{ - target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose), - target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose), + target.TestNewCredentialLibrary("", cls[0].GetPublicId(), credential.BrokeredPurpose, string(cls[0].CredentialType())), + target.TestNewCredentialLibrary("", cls[1].GetPublicId(), credential.BrokeredPurpose, string(cls[1].CredentialType())), }), target.WithStaticCredentials([]*target.StaticCredential{ target.TestNewStaticCredential("", creds[0].GetPublicId(), credential.BrokeredPurpose), diff --git a/internal/session/repository_session_test.go b/internal/session/repository_session_test.go index a3e03d6bd..cd5ec2748 100644 --- a/internal/session/repository_session_test.go +++ b/internal/session/repository_session_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/auth/password" "github.com/hashicorp/boundary/internal/authtoken" authtokenStore "github.com/hashicorp/boundary/internal/authtoken/store" @@ -1752,7 +1753,7 @@ func testSessionCredentialParams(t *testing.T, conn *db.DB, wrapper wrapping.Wra require.NotNil(tar) vaultStore := vault.TestCredentialStores(t, conn, wrapper, params.ProjectId, 1)[0] - libIds := vault.TestCredentialLibraries(t, conn, wrapper, vaultStore.GetPublicId(), 2) + libIds := vault.TestCredentialLibraries(t, conn, wrapper, vaultStore.GetPublicId(), globals.UnspecifiedCredentialType, 2) staticStore := credstatic.TestCredentialStore(t, conn, wrapper, params.ProjectId) upCreds := credstatic.TestUsernamePasswordCredentials(t, conn, wrapper, "u", "p", staticStore.GetPublicId(), params.ProjectId, 2) diff --git a/internal/target/credential_library.go b/internal/target/credential_library.go index a3bc0326b..ec763cd6d 100644 --- a/internal/target/credential_library.go +++ b/internal/target/credential_library.go @@ -18,12 +18,13 @@ import ( // between a target and a credential library. type CredentialLibrary struct { *store.CredentialLibrary - tableName string `gorm:"-"` + CredentialType string `gorm:"-"` // The credential library's issuing credential type. + tableName string `gorm:"-"` } // NewCredentialLibrary creates a new in memory CredentialLibrary // representing the relationship between targetId and credentialLibraryId. -func NewCredentialLibrary(ctx context.Context, targetId, credentialLibraryId string, purpose credential.Purpose) (*CredentialLibrary, error) { +func NewCredentialLibrary(ctx context.Context, targetId, credentialLibraryId string, purpose credential.Purpose, credType string) (*CredentialLibrary, error) { const op = "target.NewCredentialLibrary" if targetId == "" { return nil, errors.New(ctx, errors.InvalidParameter, op, "no target id") @@ -38,6 +39,7 @@ func NewCredentialLibrary(ctx context.Context, targetId, credentialLibraryId str CredentialLibraryId: credentialLibraryId, CredentialPurpose: string(purpose), }, + CredentialType: credType, } return t, nil } diff --git a/internal/target/credential_library_ext_test.go b/internal/target/credential_library_ext_test.go index 6e56873d9..390700519 100644 --- a/internal/target/credential_library_ext_test.go +++ b/internal/target/credential_library_ext_test.go @@ -52,6 +52,7 @@ func TestCredentialLibrary_New(t *testing.T) { CredentialLibraryId: "lib_0000000", CredentialPurpose: string(credential.BrokeredPurpose), }, + CredentialType: "test", }, }, } @@ -59,7 +60,7 @@ func TestCredentialLibrary_New(t *testing.T) { tt := tt t.Run(tt.name, func(t *testing.T) { assert, require := assert.New(t), require.New(t) - got, err := target.NewCredentialLibrary(context.Background(), tt.args.targetId, tt.args.libraryId, credential.BrokeredPurpose) + got, err := target.NewCredentialLibrary(context.Background(), tt.args.targetId, tt.args.libraryId, credential.BrokeredPurpose, "test") if tt.wantErr != 0 { assert.Truef(errors.Match(errors.T(tt.wantErr), err), "want err: %q got: %q", tt.wantErr, err) assert.Nil(got) diff --git a/internal/target/query.go b/internal/target/query.go index ea1884b74..e989f683f 100644 --- a/internal/target/query.go +++ b/internal/target/query.go @@ -313,5 +313,11 @@ final as ( select * from final order by update_time desc, public_id desc; +` + + getCredentialLibraryCredentialTypeQuery = ` +select credential_type + from credential_library + where public_id = ? ` ) diff --git a/internal/target/repository_credential_source.go b/internal/target/repository_credential_source.go index ad6f59058..45e8095f0 100644 --- a/internal/target/repository_credential_source.go +++ b/internal/target/repository_credential_source.go @@ -494,7 +494,11 @@ func (r *Repository) changes(ctx context.Context, targetId string, ids []string, } switch CredentialSourceType(chg.Type) { case LibraryCredentialSourceType: - lib, err := NewCredentialLibrary(ctx, targetId, chg.SourceId, purpose) + credType, err := getCredentialLibraryCredentialType(ctx, r.reader, chg.SourceId) + if err != nil { + return nil, nil, nil, nil, errors.Wrap(ctx, err, op) + } + lib, err := NewCredentialLibrary(ctx, targetId, chg.SourceId, purpose, credType) if err != nil { return nil, nil, nil, nil, errors.Wrap(ctx, err, op) } @@ -577,7 +581,11 @@ func (r *Repository) createSources(ctx context.Context, tId string, tSubtype glo for _, id := range ids { switch credTypeById[id] { case LibraryCredentialSourceType: - lib, err := NewCredentialLibrary(ctx, tId, id, purpose) + credType, err := getCredentialLibraryCredentialType(ctx, r.reader, id) + if err != nil { + return nil, nil, errors.Wrap(ctx, err, op) + } + lib, err := NewCredentialLibrary(ctx, tId, id, purpose, credType) if err != nil { return nil, nil, errors.Wrap(ctx, err, op) } @@ -602,3 +610,23 @@ func (r *Repository) createSources(ctx context.Context, tId string, tSubtype glo return credLibs, staticCred, nil } + +func getCredentialLibraryCredentialType(ctx context.Context, reader db.Reader, clId string) (string, error) { + rows, err := reader.Query(ctx, getCredentialLibraryCredentialTypeQuery, []any{clId}) + if err != nil { + return "", err + } + defer rows.Close() + + var credType string + for rows.Next() { + if err := reader.ScanRows(ctx, rows, &credType); err != nil { + return "", err + } + } + if err := rows.Err(); err != nil { + return "", err + } + + return credType, nil +} diff --git a/internal/target/repository_ext_test.go b/internal/target/repository_ext_test.go index 9f253c62b..1362227b0 100644 --- a/internal/target/repository_ext_test.go +++ b/internal/target/repository_ext_test.go @@ -405,7 +405,7 @@ func TestRepository_SetTargetCredentialSources(t *testing.T) { _, proj := iam.TestScopes(t, iamRepo) storeVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 2) + credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UnspecifiedCredentialType, 2) lib1 := credLibs[0] lib2 := credLibs[1] @@ -415,7 +415,7 @@ func TestRepository_SetTargetCredentialSources(t *testing.T) { cred2 := credsStatic[1] setupFn := func(tar target.Target) ([]target.CredentialSource, target.CredentialSources) { - credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 5) + credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UnspecifiedCredentialType, 5) var ids target.CredentialSources for _, cl := range credLibs { ids.BrokeredCredentialIds = append(ids.BrokeredCredentialIds, cl.GetPublicId()) diff --git a/internal/target/tcp/repository_credential_source_test.go b/internal/target/tcp/repository_credential_source_test.go index 22438d46e..70aad0263 100644 --- a/internal/target/tcp/repository_credential_source_test.go +++ b/internal/target/tcp/repository_credential_source_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/credential" "github.com/hashicorp/boundary/internal/credential/static" "github.com/hashicorp/boundary/internal/credential/vault" @@ -36,7 +37,7 @@ func TestRepository_AddTargetCredentialSources(t *testing.T) { require.NoError(t, err) csVault := vault.TestCredentialStores(t, conn, wrapper, staticProj.GetPublicId(), 1)[0] - libs := vault.TestCredentialLibraries(t, conn, wrapper, csVault.GetPublicId(), 3) + libs := vault.TestCredentialLibraries(t, conn, wrapper, csVault.GetPublicId(), globals.UnspecifiedCredentialType, 3) require.Len(t, libs, 3) lib1 := libs[0] lib2 := libs[1] @@ -470,7 +471,7 @@ func TestRepository_DeleteTargetCredentialSources(t *testing.T) { tar := tcp.TestTarget(ctx, t, conn, proj.PublicId, tt.name) var ids target.CredentialSources - credLibs := vault.TestCredentialLibraries(t, conn, wrapper, csv.PublicId, tt.args.createLibCnt) + credLibs := vault.TestCredentialLibraries(t, conn, wrapper, csv.PublicId, globals.UnspecifiedCredentialType, tt.args.createLibCnt) for _, cl := range credLibs { ids.BrokeredCredentialIds = append(ids.BrokeredCredentialIds, cl.GetPublicId()) } @@ -525,7 +526,7 @@ func TestRepository_DeleteTargetCredentialSources(t *testing.T) { _, proj := iam.TestScopes(t, iamRepo) cs := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - libs := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 3) + libs := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 3) require.Len(libs, 3) lib1 := libs[0] lib2 := libs[1] @@ -581,7 +582,7 @@ func TestRepository_SetTargetCredentialSources(t *testing.T) { _, proj := iam.TestScopes(t, iamRepo) storeVault := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 2) + credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UnspecifiedCredentialType, 2) lib1 := credLibs[0] lib2 := credLibs[1] @@ -591,7 +592,7 @@ func TestRepository_SetTargetCredentialSources(t *testing.T) { cred2 := credsStatic[1] setupFn := func(tar target.Target) ([]target.CredentialSource, target.CredentialSources) { - credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), 5) + credLibs := vault.TestCredentialLibraries(t, conn, wrapper, storeVault.GetPublicId(), globals.UnspecifiedCredentialType, 5) var ids target.CredentialSources for _, cl := range credLibs { ids.BrokeredCredentialIds = append(ids.BrokeredCredentialIds, cl.GetPublicId()) diff --git a/internal/target/tcp/repository_tcp_target_test.go b/internal/target/tcp/repository_tcp_target_test.go index 9955c2660..0e17c504b 100644 --- a/internal/target/tcp/repository_tcp_target_test.go +++ b/internal/target/tcp/repository_tcp_target_test.go @@ -43,7 +43,7 @@ func TestRepository_CreateTarget(t *testing.T) { static.TestSets(t, conn, cats[0].GetPublicId(), 2) cs := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 2) + vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 2) type args struct { target target.Target @@ -825,7 +825,7 @@ func TestRepository_UpdateTcpTarget(t *testing.T) { ) } - cls := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), 5) + cls := vault.TestCredentialLibraries(t, conn, wrapper, cs.GetPublicId(), globals.UnspecifiedCredentialType, 5) var testClIds []string var testCredLibs []*target.CredentialLibrary for _, cl := range cls { diff --git a/internal/target/tcp/testing_test.go b/internal/target/tcp/testing_test.go index 24dc2a8b0..e4e27eccd 100644 --- a/internal/target/tcp/testing_test.go +++ b/internal/target/tcp/testing_test.go @@ -7,6 +7,7 @@ import ( "context" "testing" + "github.com/hashicorp/boundary/globals" "github.com/hashicorp/boundary/internal/credential/vault" "github.com/hashicorp/boundary/internal/db" "github.com/hashicorp/boundary/internal/host/static" @@ -146,12 +147,12 @@ func Test_TestCredentialLibrary(t *testing.T) { tar := tcp.TestTarget(ctx, t, conn, proj.PublicId, t.Name()) store := vault.TestCredentialStores(t, conn, wrapper, proj.GetPublicId(), 1)[0] - vlibs := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), 2) + vlibs := vault.TestCredentialLibraries(t, conn, wrapper, store.GetPublicId(), globals.UnspecifiedCredentialType, 2) var libIds []string var libs []*target.CredentialLibrary for _, v := range vlibs { libIds = append(libIds, v.GetPublicId()) - lib := target.TestCredentialLibrary(t, conn, tar.GetPublicId(), v.GetPublicId()) + lib := target.TestCredentialLibrary(t, conn, tar.GetPublicId(), v.GetPublicId(), v.GetCredentialType()) require.NotNil(lib) libs = append(libs, lib) } diff --git a/internal/target/testing.go b/internal/target/testing.go index ae299afd6..593f194c8 100644 --- a/internal/target/testing.go +++ b/internal/target/testing.go @@ -17,13 +17,14 @@ import ( // TestNewCredentialLibrary creates a new in memory CredentialLibrary // representing the relationship between targetId and credentialLibraryId with // the given purpose. -func TestNewCredentialLibrary(targetId, credentialLibraryId string, purpose credential.Purpose) *CredentialLibrary { +func TestNewCredentialLibrary(targetId, credentialLibraryId string, purpose credential.Purpose, credType string) *CredentialLibrary { return &CredentialLibrary{ CredentialLibrary: &store.CredentialLibrary{ TargetId: targetId, CredentialLibraryId: credentialLibraryId, CredentialPurpose: string(purpose), }, + CredentialType: credType, } } @@ -53,11 +54,11 @@ func TestNewStaticCredential(targetId, credentialId string, purpose credential.P // TestCredentialLibrary creates a CredentialLibrary for targetId and // libraryId with the credential purpose of brokered. -func TestCredentialLibrary(t testing.TB, conn *db.DB, targetId, libraryId string) *CredentialLibrary { +func TestCredentialLibrary(t testing.TB, conn *db.DB, targetId, libraryId, credType string) *CredentialLibrary { t.Helper() require := require.New(t) rw := db.New(conn) - lib := TestNewCredentialLibrary(targetId, libraryId, credential.BrokeredPurpose) + lib := TestNewCredentialLibrary(targetId, libraryId, credential.BrokeredPurpose, credType) err := rw.Create(context.Background(), lib) require.NoError(err) return lib