Files
threshold/internal/auth/ldap/repository_auth_method_read_test.go
T
Jim 86192f75eb feature (auth/ldap): add LDAP auth method along with associated accounts and managed groups (#2912)
* feature (auth): required schema changes for auth ldap method (#2669)

chore (auth/ldap): move schema changes to next avail migration number

* feature (auth/ldap): define AuthMethod and all its value objects (#2703)

* feature (auth/ldap): storage protos
* feature (auth/ldap): define AuthMethod and all its value objects

* feature (auth/ldap): add repo and reading an auth method (#2718)

* feature (auth/ldap): add Repository.CreateAuthMethod(...) and Repository.DeleteAuthMethod(...) (#2724)

* feature (auth/ldap): add Repository.UpdateAuthMethod(...) (#2739)

* feature (auth/ldap): add Account

* feature (auth/ldap): add AuthMethod.EnableGroups

* fix (auth/ldap): refactor AuthMethod.oplog to enforce proper constraints

* feature (auth/ldap): add Account repo functions

* refactor (auth/ldap): remove entry attributes from account

Realized the entry attributes could have absolutely anything in them
(including binary data) and since we absolutely don't have to have them
there's just no reason to take on the risk.

* feature (auth/ldap): add AuthMethod.UseTokenGroups

* feature (auth/ldap): add Authenticate(...)

* refactor (auth/ldap): ensure options take a context as the 1st parameter

* feature (auth/ldap): add Account attribute maps

* chore (auth/ldap): make fmt deltas

* feature (auth/ldap): add managed groups (#2760)

* tests (auth/ldap): add missing unit test to Repository.DeleteAccount(...)

Add bits to test the delete operation when you're not able to generate
oplog metadata

* feature (auth/ldap): add managed groups

fixup! feature (auth/ldap): add managed groups (#2760)

* feature (auth/ldap): service proto definition (#2761)

* feature (handlers/authmethods): add handlers for ldap auth method operations (#2794)

* feature (auth/ldap): add Account attribute maps

* chore (auth/ldap): update cap/ldap to latest version

* feature (auth/ldap): add ldap api generation definitions

* feature (authmethods): add ldap repo NewService(...)

* feature (authmethods/ldap): add proper mask_mapping to protobufs

* feature (authmethods): add support to get an ldap auth method

* refactor (auth/ldap): export TestGenerateCA(...)

* feature (authmethods): add support to create an ldap auth method

* feature (authmethods): add support to delete an ldap auth method

* feature (authmethods): add support to list ldap auth methods

* refactor (auth/ldap): make urls optional for NewAuthMethod(...)

* refactor (auth/ldap): export ldap.TestInvalidPem

* chore: make fmt changes

* fix (auth/ldap): properly handle group search config

Add constraints and tests to ensure when an ldap AuthMethod.EnableGroups
is true, and UseTokenGroups is false; that there's a GroupDn configured
for finding a user's associated groups

* feature (authmethods): add support to update an ldap auth methods

* chore (db/ldap): tmp mv migrations so there's no conflict with ongoing work

* feature (verifier): add ldap auth method to verifier bits

* fix (controller): prevent panic when controller stops when there's no listener

* feature (authmethods): add support to authenticate via ldap auth methods

* chore (migrations): fix whitespace in stmt

* chore: fmt fixup

* tests (auth/ldap): invalid err msg

* feature (cli/authmethods): add support for ldap auth-methods CRUD and authenticate (#2810)

* feature (authmethods): add CLI support for ldap auth methods CRUD

* tests (api/auth): ldap auth method classification tests

* feature (authmethods): add CLI support for ldap auth authenticate

* feature (auth/ldap): set request timeouts for ldap server connections

* feature (handlers/authmethods): handle u_anon listing properly.

* feature (account/handers): ldap account and managed group CRUDL APIs (#2852)

* feature (auth/ldap) add repository Listing of ManagedGroupMemberAccount

* feature (controller/handlers): add ldapRepoFn to accounts service

* feature (auth/ldap) register ldap managed group subtype

* feature (account/handlers): ldap account CRUDL APIs

* feature (controller/handlers): add ldapRepo to managed groups service

* feature (account/handlers): ldap managed group CRUDL APIs

* docs (domain): add LDAP accounts, auth-methods and managed groups (#2857)

* feature (ldap/cli) add ldap accounts and managed groups CRUDL commands (#2856)

* fix (handlers/authmethods): fix ldap authorized actions (#2892)

* feature (cli/ldap/authenticate): use primary auth method if none is provided (#2890)

* fix (auth/ldap): support setting the state attribute

* feature (cli/ldap/authenticate): use primary auth method if none is provided

* feature (wh/ldap) add tests for new ldap auth method and accounts (#2919)

* refactor (migrations/ldap): mv to correct directory

* chore: add copyright headers

* fix (api/authmethods/ldap): renumber new LdapAuthMethodAttributes field

* fix (auth/ldap): allow the auth method state to be updated (#2951)

* chore: update sdk and api versions for llb - this is tmp until merging

* tests (managed groups): add required errContains for new test
2023-03-15 11:40:40 -04:00

358 lines
13 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package ldap
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"sort"
"testing"
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/kms"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRepository_LookupAuthMethod(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()
org, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper))
orgDbWrapper, err := testKms.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
amInactive := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://ldap1.alice.com"}, WithOperationalState(testCtx, InactiveState))
amActivePriv := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://ldap2.alice.com"}, WithOperationalState(testCtx, ActivePrivateState))
amActivePub := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://ldap3.alice.com"}, WithOperationalState(testCtx, ActivePublicState))
amActivePub.IsPrimaryAuthMethod = true
iam.TestSetPrimaryAuthMethod(t, iam.TestRepo(t, testConn, testWrapper), org, amActivePub.PublicId)
amId, err := newAuthMethodId(testCtx)
require.NoError(t, err)
tests := []struct {
name string
in string
opt []Option
want *AuthMethod
wantIsPrimary bool
wantErrMatch *errors.Template
wantErrContains string
}{
{
name: "missing-public-id",
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing public id",
},
{
name: "invalid-opts",
in: amActivePub.GetPublicId(),
opt: []Option{WithBindCredential(testCtx, "", "")},
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing both dn and password",
},
{
name: "non-existing-auth-method-id",
in: amId,
},
{
name: "existing-auth-method-id",
in: amActivePriv.GetPublicId(),
want: amActivePriv,
},
{
name: "unauthenticated-user-not-found-using-active-priv",
in: amActivePriv.GetPublicId(),
opt: []Option{WithUnauthenticatedUser(testCtx, true)},
want: nil,
},
{
name: "unauthenticated-user-found-active-pub",
in: amActivePub.GetPublicId(),
opt: []Option{WithUnauthenticatedUser(testCtx, true)},
want: amActivePub,
wantIsPrimary: true,
},
{
name: "unauthenticated-user-found-inactive",
in: amInactive.GetPublicId(),
opt: []Option{WithUnauthenticatedUser(testCtx, true)},
want: nil,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
repo, err := NewRepository(testCtx, testRw, testRw, testKms)
assert.NoError(err)
require.NotNil(repo)
got, err := repo.LookupAuthMethod(testCtx, tc.in, tc.opt...)
if tc.wantErrMatch != nil {
require.Error(err)
assert.Truef(errors.Match(tc.wantErrMatch, err), "want err code: %q got: %q", tc.wantErrMatch, err)
assert.Nil(got)
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.EqualValues(tc.want, got)
})
}
}
func TestRepository_getAuthMethods(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()
tests := []struct {
name string
setupFn func() (authMethodId string, scopeIds []string, want []*AuthMethod)
opt []Option
wantErrMatch *errors.Template
wantErrContains string
}{
{
name: "valid-multi-scopes",
setupFn: func() (string, []string, []*AuthMethod) {
org, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper))
orgDBWrapper, err := testKms.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
org2, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper))
orgDBWrapper2, err := testKms.GetWrapper(context.Background(), org2.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
// make a test auth method with all options
am1a := TestAuthMethod(t, testConn, orgDBWrapper, org.PublicId, []string{"ldaps://ldap1.alice.com"}, WithOperationalState(testCtx, InactiveState))
am1b := TestAuthMethod(t, testConn, orgDBWrapper, org.PublicId, []string{"ldaps://ldap2.alice.com"}, WithOperationalState(testCtx, InactiveState))
am2 := TestAuthMethod(t, testConn, orgDBWrapper2, org2.PublicId, []string{"ldaps://ldap3.alice.com"}, WithOperationalState(testCtx, InactiveState))
return "", []string{am1a.ScopeId, am1b.ScopeId, am2.ScopeId}, []*AuthMethod{am1a, am1b, am2}
},
},
{
name: "valid-single-scopes",
setupFn: func() (string, []string, []*AuthMethod) {
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://ldap1.alice.com"}, WithOperationalState(testCtx, InactiveState))
am1b := TestAuthMethod(t, testConn, orgDBWrapper, org.PublicId, []string{"ldaps://ldap2.alice.com"}, WithOperationalState(testCtx, InactiveState))
return "", []string{am1a.ScopeId}, []*AuthMethod{am1a, am1b}
},
},
{
name: "valid-auth-method-id-all-opts",
setupFn: func() (string, []string, []*AuthMethod) {
org, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper))
orgDbWrapper, err := testKms.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
testCert, _ := TestGenerateCA(t, "localhost")
_, testPrivKey, err := ed25519.GenerateKey(rand.Reader)
require.NoError(t, err)
derPrivKey, err := x509.MarshalPKCS8PrivateKey(testPrivKey)
require.NoError(t, err)
am := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId, []string{"ldaps://alice.com"},
WithName(testCtx, "test-name"),
WithDescription(testCtx, "test-description"),
WithStartTLS(testCtx),
WithInsecureTLS(testCtx),
WithDiscoverDn(testCtx),
WithAnonGroupSearch(testCtx),
WithUpnDomain(testCtx, "alice.com"),
WithUserDn(testCtx, "user-dn"),
WithUserAttr(testCtx, "user-attr"),
WithUserFilter(testCtx, "user-filter"),
WithEnableGroups(testCtx),
WithUseTokenGroups(testCtx),
WithGroupDn(testCtx, "group-dn"),
WithGroupAttr(testCtx, "group-attr"),
WithGroupFilter(testCtx, "group-filter"),
WithBindCredential(testCtx, "bind-dn", "bind-password"),
WithCertificates(testCtx, testCert),
WithClientCertificate(testCtx, derPrivKey, testCert), // not a client cert but good enough for this test.)
WithAccountAttributeMap(testCtx, map[string]AccountToAttribute{"mail": ToEmailAttribute, "displayName": ToFullNameAttribute}),
)
return am.PublicId, nil, []*AuthMethod{am}
},
},
{
name: "with-limits",
setupFn: func() (string, []string, []*AuthMethod) {
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"})
_ = TestAuthMethod(t, testConn, orgDBWrapper, org.PublicId, []string{"ldaps://alice2.com"})
return "", []string{am1a.ScopeId}, []*AuthMethod{am1a}
},
opt: []Option{WithLimit(testCtx, 1), WithOrderByCreateTime(testCtx, true)},
},
{
name: "unauthenticated-user",
setupFn: func() (string, []string, []*AuthMethod) {
org, _ := iam.TestScopes(t, iam.TestRepo(t, testConn, testWrapper))
databaseWrapper, err := testKms.GetWrapper(context.Background(), org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
_ = TestAuthMethod(t, testConn, databaseWrapper, org.PublicId, []string{"ldaps://ldap1.alice.com"}, WithOperationalState(testCtx, InactiveState))
_ = TestAuthMethod(t, testConn, databaseWrapper, org.PublicId, []string{"ldaps://ldap2.alice.com"}, WithOperationalState(testCtx, ActivePrivateState))
amActivePub := TestAuthMethod(t, testConn, databaseWrapper, org.PublicId, []string{"ldaps://ldap3.alice.com"}, WithOperationalState(testCtx, ActivePublicState))
return "", []string{amActivePub.ScopeId}, []*AuthMethod{amActivePub}
},
opt: []Option{WithUnauthenticatedUser(testCtx, true)},
},
{
name: "not-found-auth-method-id",
setupFn: func() (string, []string, []*AuthMethod) {
return "not-a-valid-id", nil, nil
},
},
{
name: "not-found-scope-ids",
setupFn: func() (string, []string, []*AuthMethod) {
return "", []string{"not-valid-scope-1", "not-valid-scope-2"}, nil
},
},
{
name: "no-search-criteria",
setupFn: func() (string, []string, []*AuthMethod) {
return "", nil, nil
},
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing search criteria: both auth method id and scope ids are empty",
},
{
name: "search-too-many",
setupFn: func() (string, []string, []*AuthMethod) {
return "auth-method-id", []string{"scope-id"}, nil
},
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "searching for both an auth method id and scope ids is not supported",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
r, err := NewRepository(testCtx, testRw, testRw, testKms)
require.NoError(err)
authMethodId, scopeIds, want := tc.setupFn()
got, err := r.getAuthMethods(testCtx, authMethodId, scopeIds, tc.opt...)
if tc.wantErrMatch != nil {
require.Error(err)
assert.Truef(errors.Match(tc.wantErrMatch, err), "want err code: %q got: %q", tc.wantErrMatch, err)
assert.Nil(got)
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
TestSortAuthMethods(t, want)
TestSortAuthMethods(t, got)
assert.Equal(want, got)
})
}
}
// 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)
}
}
})
}
}