diff --git a/internal/auth/ldap/repository_auth_method_read.go b/internal/auth/ldap/repository_auth_method_read.go index a2c9dbd6f..97a5e8e25 100644 --- a/internal/auth/ldap/repository_auth_method_read.go +++ b/internal/auth/ldap/repository_auth_method_read.go @@ -32,21 +32,6 @@ func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, opt return r.lookupAuthMethod(ctx, publicId, WithUnauthenticatedUser(ctx, opts.withUnauthenticatedUser)) } -// ListAuthMethods returns a slice of AuthMethods for the scopeId. The -// WithUnauthenticatedUser, WithLimit and WithOrder options are supported and -// all other options are ignored. -func (r *Repository) ListAuthMethods(ctx context.Context, scopeIds []string, opt ...Option) ([]*AuthMethod, error) { - const op = "ldap.(Repository).ListAuthMethods" - if len(scopeIds) == 0 { - return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope IDs") - } - authMethods, err := r.getAuthMethods(ctx, "", scopeIds, opt...) - if err != nil { - return nil, errors.Wrap(ctx, err, op) - } - return authMethods, nil -} - // lookupAuthMethod will lookup a single auth method func (r *Repository) lookupAuthMethod(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethod, error) { const op = "ldap.(Repository).lookupAuthMethod" diff --git a/internal/auth/ldap/repository_auth_method_read_test.go b/internal/auth/ldap/repository_auth_method_read_test.go index a3118a877..7ea064d7f 100644 --- a/internal/auth/ldap/repository_auth_method_read_test.go +++ b/internal/auth/ldap/repository_auth_method_read_test.go @@ -8,7 +8,6 @@ import ( "crypto/ed25519" "crypto/rand" "crypto/x509" - "sort" "testing" "github.com/hashicorp/boundary/internal/db" @@ -272,88 +271,3 @@ func TestRepository_getAuthMethods(t *testing.T) { }) } } - -// TestRepository_ListAuthMethods only covers error conditions, since all the -// search criteria testing is handled in the TestRepository_getAuthMethods unit -// tests. -func TestRepository_ListAuthMethods(t *testing.T) { - testConn, _ := db.TestSetup(t, "postgres") - testRw := db.New(testConn) - testWrapper := db.TestWrapper(t) - testKms := kms.TestKms(t, testConn, testWrapper) - testCtx := context.Background() - iamRepo := iam.TestRepo(t, testConn, testWrapper) - - tests := []struct { - name string - setupFn func() (scopeIds []string, want []*AuthMethod, wantPrimaryAuthMethodId string) - opt []Option - wantErrMatch *errors.Template - }{ - { - name: "with-limits", - setupFn: func() ([]string, []*AuthMethod, string) { - org, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper)) - orgDbWrapper, err := testKms.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase) - require.NoError(t, err) - am1a := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://alice.com"}) - iam.TestSetPrimaryAuthMethod(t, iamRepo, org, am1a.PublicId) - am1a.IsPrimaryAuthMethod = true - - _ = TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://alice2.com"}) - - return []string{am1a.ScopeId}, []*AuthMethod{am1a}, am1a.PublicId - }, - opt: []Option{WithLimit(testCtx, 1), WithOrderByCreateTime(testCtx, true)}, - }, - { - name: "no-search-criteria", - setupFn: func() ([]string, []*AuthMethod, string) { - return nil, nil, "" - }, - wantErrMatch: errors.T(errors.InvalidParameter), - }, - } - - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - repo, err := NewRepository(testCtx, testRw, testRw, testKms) - assert.NoError(err) - scopeIds, want, wantPrimaryAuthMethodId := tt.setupFn() - - got, err := repo.ListAuthMethods(testCtx, scopeIds, tt.opt...) - if tt.wantErrMatch != nil { - require.Error(err) - assert.Truef(errors.Match(tt.wantErrMatch, err), "want err code: %q got: %q", tt.wantErrMatch, err) - assert.Nil(got) - return - } - require.NoError(err) - sort.Slice(want, func(a, b int) bool { - return want[a].PublicId < want[b].PublicId - }) - sort.Slice(got, func(a, b int) bool { - return got[a].PublicId < got[b].PublicId - }) - assert.Equal(want, got) - if wantPrimaryAuthMethodId != "" { - found := false - for _, am := range got { - if am.PublicId == wantPrimaryAuthMethodId { - assert.Truef(am.IsPrimaryAuthMethod, "expected IsPrimaryAuthMethod to be true for: %s", am.PublicId) - if am.IsPrimaryAuthMethod { - found = true - } - } - } - assert.Truef(found, "expected to find primary id %s in: %+v", wantPrimaryAuthMethodId, got) - } else { - for _, am := range got { - assert.Falsef(am.IsPrimaryAuthMethod, "did not expect %s to be IsPrimaryAuthMethod", am.PublicId) - } - } - }) - } -} diff --git a/internal/auth/oidc/repository_auth_method_read.go b/internal/auth/oidc/repository_auth_method_read.go index 277b8b5c7..e8ac5cd7c 100644 --- a/internal/auth/oidc/repository_auth_method_read.go +++ b/internal/auth/oidc/repository_auth_method_read.go @@ -29,21 +29,6 @@ func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, opt return r.lookupAuthMethod(ctx, publicId, WithUnauthenticatedUser(opts.withUnauthenticatedUser)) } -// ListAuthMethods returns a slice of AuthMethods for the scopeId. The -// WithUnauthenticatedUser, WithLimit and WithOrder options are supported and -// all other options are ignored. -func (r *Repository) ListAuthMethods(ctx context.Context, scopeIds []string, opt ...Option) ([]*AuthMethod, error) { - const op = "oidc.(Repository).ListAuthMethods" - if len(scopeIds) == 0 { - return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope IDs") - } - authMethods, err := r.getAuthMethods(ctx, "", scopeIds, opt...) - if err != nil { - return nil, errors.Wrap(ctx, err, op) - } - return authMethods, nil -} - // lookupAuthMethod will lookup a single auth method func (r *Repository) lookupAuthMethod(ctx context.Context, authMethodId string, opt ...Option) (*AuthMethod, error) { const op = "oidc.(Repository).lookupAuthMethod" diff --git a/internal/auth/oidc/repository_auth_method_read_test.go b/internal/auth/oidc/repository_auth_method_read_test.go index 3ee2fde90..474ce242a 100644 --- a/internal/auth/oidc/repository_auth_method_read_test.go +++ b/internal/auth/oidc/repository_auth_method_read_test.go @@ -117,120 +117,6 @@ func TestRepository_LookupAuthMethod(t *testing.T) { } } -// TestRepository_ListAuthMethods only covers error conditions, since all the -// search criteria testing is handled in the TestRepository_getAuthMethods unit -// tests. -func TestRepository_ListAuthMethods(t *testing.T) { - conn, _ := db.TestSetup(t, "postgres") - rw := db.New(conn) - wrapper := db.TestWrapper(t) - kmsCache := kms.TestKms(t, conn, wrapper) - ctx := context.Background() - iamRepo := iam.TestRepo(t, conn, wrapper) - - tests := []struct { - name string - setupFn func() (scopeIds []string, want []*AuthMethod, wantPrimaryAuthMethodId string) - opt []Option - wantErrMatch *errors.Template - }{ - { - name: "with-limits", - setupFn: func() ([]string, []*AuthMethod, string) { - org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) - databaseWrapper, err := kmsCache.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase) - require.NoError(t, err) - - am1a := TestAuthMethod(t, conn, databaseWrapper, org.PublicId, InactiveState, "alice_rp", "alices-dogs-name", - WithIssuer(TestConvertToUrls(t, "https://alice.com")[0]), WithApiUrl(TestConvertToUrls(t, "https://api.com")[0]), WithClaimsScopes("email", "profile")) - iam.TestSetPrimaryAuthMethod(t, iamRepo, org, am1a.PublicId) - am1a.IsPrimaryAuthMethod = true - - _ = TestAuthMethod(t, conn, databaseWrapper, org.PublicId, InactiveState, "alice_rp-2", "alices-cat-name", - WithIssuer(TestConvertToUrls(t, "https://alice.com")[0]), WithApiUrl(TestConvertToUrls(t, "https://api.com")[0]), WithClaimsScopes("email", "profile")) - - return []string{am1a.ScopeId}, []*AuthMethod{am1a}, am1a.PublicId - }, - opt: []Option{WithLimit(1), WithOrderByCreateTime(true)}, - }, - { - name: "with-prompts", - setupFn: func() ([]string, []*AuthMethod, string) { - org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) - databaseWrapper, err := kmsCache.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase) - require.NoError(t, err) - - am1a := TestAuthMethod( - t, - conn, - databaseWrapper, - org.PublicId, - InactiveState, - "alice_rp", - "alices-dogs-name", - WithIssuer(TestConvertToUrls(t, "https://alice.com")[0]), - WithApiUrl(TestConvertToUrls(t, "https://api.com")[0]), - WithClaimsScopes("email", "profile"), - WithPrompts(Consent, SelectAccount), - ) - iam.TestSetPrimaryAuthMethod(t, iamRepo, org, am1a.PublicId) - am1a.IsPrimaryAuthMethod = true - - return []string{am1a.ScopeId}, []*AuthMethod{am1a}, am1a.PublicId - }, - }, - { - name: "no-search-criteria", - setupFn: func() ([]string, []*AuthMethod, string) { - return nil, nil, "" - }, - wantErrMatch: errors.T(errors.InvalidParameter), - }, - } - - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - repo, err := NewRepository(ctx, rw, rw, kmsCache) - assert.NoError(err) - scopeIds, want, wantPrimaryAuthMethodId := tt.setupFn() - - got, err := repo.ListAuthMethods(ctx, scopeIds, tt.opt...) - if tt.wantErrMatch != nil { - require.Error(err) - assert.Truef(errors.Match(tt.wantErrMatch, err), "want err code: %q got: %q", tt.wantErrMatch, err) - assert.Nil(got) - return - } - require.NoError(err) - sort.Slice(want, func(a, b int) bool { - return want[a].PublicId < want[b].PublicId - }) - sort.Slice(got, func(a, b int) bool { - return got[a].PublicId < got[b].PublicId - }) - assert.Equal(want, got) - if wantPrimaryAuthMethodId != "" { - found := false - for _, am := range got { - if am.PublicId == wantPrimaryAuthMethodId { - assert.Truef(am.IsPrimaryAuthMethod, "expected IsPrimaryAuthMethod to be true for: %s", am.PublicId) - if am.IsPrimaryAuthMethod { - found = true - } - } - } - assert.Truef(found, "expected to find primary id %s in: %+v", wantPrimaryAuthMethodId, got) - } else { - for _, am := range got { - assert.Falsef(am.IsPrimaryAuthMethod, "did not expect %s to be IsPrimaryAuthMethod", am.PublicId) - } - } - }) - } -} - func TestRepository_getAuthMethods(t *testing.T) { conn, _ := db.TestSetup(t, "postgres") rw := db.New(conn) diff --git a/internal/auth/password/repository_authmethod.go b/internal/auth/password/repository_authmethod.go index 8b8487d21..5a1417b75 100644 --- a/internal/auth/password/repository_authmethod.go +++ b/internal/auth/password/repository_authmethod.go @@ -112,20 +112,6 @@ func (r *Repository) LookupAuthMethod(ctx context.Context, publicId string, _ .. return r.lookupAuthMethod(ctx, publicId) } -// ListAuthMethods returns a slice of AuthMethods for the scopeId. WithLimit and -// WithOrder options are the only option supported. -func (r *Repository) ListAuthMethods(ctx context.Context, scopeIds []string, opt ...Option) ([]*AuthMethod, error) { - const op = "password.(Repository).ListAuthMethods" - if len(scopeIds) == 0 { - return nil, errors.New(ctx, errors.InvalidParameter, op, "missing scope id") - } - authMethods, err := r.getAuthMethods(ctx, "", scopeIds, opt...) - if err != nil { - return nil, errors.Wrap(ctx, err, op) - } - return authMethods, nil -} - // DeleteAuthMethod deletes the auth method for the provided id from the repository returning a count of the // number of records deleted. All options are ignored. func (r *Repository) DeleteAuthMethod(ctx context.Context, scopeId, publicId string, opt ...Option) (int, error) { diff --git a/internal/auth/password/repository_authmethod_test.go b/internal/auth/password/repository_authmethod_test.go index 0812477e9..29c6c53ab 100644 --- a/internal/auth/password/repository_authmethod_test.go +++ b/internal/auth/password/repository_authmethod_test.go @@ -394,176 +394,6 @@ func TestRepository_DeleteAuthMethod(t *testing.T) { } } -func TestRepository_ListAuthMethods(t *testing.T) { - ctx := context.Background() - conn, _ := db.TestSetup(t, "postgres") - rw := db.New(conn) - wrapper := db.TestWrapper(t) - kms := kms.TestKms(t, conn, wrapper) - iamRepo := iam.TestRepo(t, conn, wrapper) - noAuthMethodOrg, _ := iam.TestScopes(t, iamRepo) - o, _ := iam.TestScopes(t, iamRepo) - authMethods := TestAuthMethods(t, conn, o.GetPublicId(), 3) - - tests := []struct { - name string - in []string - opts []Option - want []*AuthMethod - wantIsErr errors.Code - wantErrMsg string - }{ - { - name: "With no scope id", - wantIsErr: errors.InvalidParameter, - wantErrMsg: "password.(Repository).ListAuthMethods: missing scope id: parameter violation: error #100", - }, - { - name: "Scope with no auth methods", - in: []string{noAuthMethodOrg.GetPublicId()}, - want: nil, - }, - { - name: "With populated scope id", - in: []string{o.GetPublicId()}, - want: authMethods, - }, - } - - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - repo, err := NewRepository(ctx, rw, rw, kms) - assert.NoError(err) - require.NotNil(repo) - got, err := repo.ListAuthMethods(ctx, tt.in, tt.opts...) - if tt.wantIsErr != 0 { - assert.Truef(errors.Match(errors.T(tt.wantIsErr), err), "Unexpected error %s", err) - assert.Equal(tt.wantErrMsg, err.Error()) - return - } - require.NoError(err) - assert.Empty(cmp.Diff(tt.want, got, protocmp.Transform())) - }) - } -} - -func TestRepository_ListAuthMethods_Multiple_Scopes(t *testing.T) { - ctx := context.Background() - conn, _ := db.TestSetup(t, "postgres") - rw := db.New(conn) - wrapper := db.TestWrapper(t) - kms := kms.TestKms(t, conn, wrapper) - iamRepo := iam.TestRepo(t, conn, wrapper) - repo, err := NewRepository(ctx, rw, rw, kms) - require.NoError(t, err) - - var total int - const numPerScope = 10 - TestAuthMethods(t, conn, "global", numPerScope) - total += numPerScope - scopeIds := []string{"global"} - for i := 0; i < 10; i++ { - o := iam.TestOrg(t, iamRepo) - scopeIds = append(scopeIds, o.GetPublicId()) - ams := TestAuthMethods(t, conn, o.GetPublicId(), numPerScope) - iam.TestSetPrimaryAuthMethod(t, iam.TestRepo(t, conn, wrapper), o, ams[0].PublicId) - total += numPerScope - } - got, err := repo.ListAuthMethods(ctx, scopeIds, WithOrderByCreateTime(true)) - require.NoError(t, err) - assert.Equal(t, total, len(got)) - found := map[string]struct{}{} - for _, am := range got { - switch { - case am.Clone().ScopeId == "global": - assert.Equalf(t, false, am.IsPrimaryAuthMethod, "expected the the global auth method to not be primary") - default: - if _, ok := found[am.ScopeId]; !ok { - found[am.ScopeId] = struct{}{} - assert.Equalf(t, true, am.IsPrimaryAuthMethod, "expected the first auth method created for the scope %s to be primary", am.ScopeId) - } - } - } -} - -func TestRepository_ListAuthMethods_Limits(t *testing.T) { - ctx := context.Background() - conn, _ := db.TestSetup(t, "postgres") - rw := db.New(conn) - wrapper := db.TestWrapper(t) - kms := kms.TestKms(t, conn, wrapper) - - o, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper)) - authMethodCount := 10 - ams := TestAuthMethods(t, conn, o.GetPublicId(), authMethodCount) - primaryAuthMethod := ams[0] - iam.TestSetPrimaryAuthMethod(t, iam.TestRepo(t, conn, wrapper), o, primaryAuthMethod.PublicId) - - tests := []struct { - name string - repoOpts []Option - listOpts []Option - wantLen int - }{ - { - name: "With no limits", - wantLen: authMethodCount, - listOpts: []Option{WithOrderByCreateTime(true)}, - }, - { - name: "With repo limit", - repoOpts: []Option{WithLimit(3)}, - listOpts: []Option{WithOrderByCreateTime(true)}, - wantLen: 3, - }, - { - name: "With negative repo limit", - repoOpts: []Option{WithLimit(-1)}, - wantLen: authMethodCount, - }, - { - name: "With List limit", - listOpts: []Option{WithLimit(3), WithOrderByCreateTime(true)}, - wantLen: 3, - }, - { - name: "With negative List limit", - listOpts: []Option{WithLimit(-1), WithOrderByCreateTime(true)}, - wantLen: authMethodCount, - }, - { - name: "With repo smaller than list limit", - repoOpts: []Option{WithLimit(2)}, - listOpts: []Option{WithLimit(6), WithOrderByCreateTime(true)}, - wantLen: 6, - }, - { - name: "With repo larger than list limit", - repoOpts: []Option{WithLimit(6)}, - listOpts: []Option{WithLimit(2), WithOrderByCreateTime(true)}, - wantLen: 2, - }, - } - - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - assert, require := assert.New(t), require.New(t) - repo, err := NewRepository(ctx, rw, rw, kms, tt.repoOpts...) - assert.NoError(err) - require.NotNil(repo) - got, err := repo.ListAuthMethods(ctx, []string{ams[0].GetScopeId()}, tt.listOpts...) - require.NoError(err) - assert.Len(got, tt.wantLen) - if tt.wantLen > 0 { - assert.Equal(true, got[0].IsPrimaryAuthMethod) - } - }) - } -} - func TestRepository_UpdateAuthMethod(t *testing.T) { t.Parallel() ctx := context.Background()