Files
threshold/internal/auth/ldap/service_authenticate_test.go
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

335 lines
12 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package ldap
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/boundary/internal/authtoken"
"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/hashicorp/go-hclog"
"github.com/jimlambrt/gldap"
"github.com/jimlambrt/gldap/testdirectory"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthenticate(t *testing.T) {
t.Parallel()
testCtx := context.Background()
testConn, _ := db.TestSetup(t, "postgres")
testRw := db.New(testConn)
rootWrapper := db.TestWrapper(t)
testKms := kms.TestKms(t, testConn, rootWrapper)
// some standard factories for unit tests
authenticatorFn := func() (Authenticator, error) {
return NewRepository(testCtx, testRw, testRw, testKms)
}
lookupUserWithFn := func() (LookupUser, error) {
return iam.NewRepository(testCtx, testRw, testRw, testKms)
}
tokenCreatorFn := func() (AuthTokenCreator, error) {
return authtoken.NewRepository(testCtx, testRw, testRw, testKms)
}
iamRepo := iam.TestRepo(t, testConn, rootWrapper)
org, _ := iam.TestScopes(t, iamRepo)
orgDbWrapper, err := testKms.GetWrapper(testCtx, org.PublicId, kms.KeyPurposeDatabase)
require.NoError(t, err)
logger := hclog.New(&hclog.LoggerOptions{
Name: "test-logger",
Level: hclog.Error,
})
td := testdirectory.Start(t,
testdirectory.WithDefaults(t, &testdirectory.Defaults{AllowAnonymousBind: true}),
testdirectory.WithLogger(t, logger),
)
tdCerts, err := ParseCertificates(testCtx, td.Cert())
require.NoError(t, err)
groups := []*gldap.Entry{
testdirectory.NewGroup(t, "admin", []string{"alice"}),
}
users := testdirectory.NewUsers(t, []string{"alice", "bob"}, testdirectory.WithMembersOf(t, "admin"))
td.SetUsers(users...)
td.SetGroups(groups...)
testPrimaryAuthMethod := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId,
[]string{fmt.Sprintf("ldaps://%s:%d", td.Host(), td.Port())},
WithCertificates(testCtx, tdCerts...),
WithDiscoverDn(testCtx),
WithEnableGroups(testCtx),
WithUserDn(testCtx, testdirectory.DefaultUserDN),
WithGroupDn(testCtx, testdirectory.DefaultGroupDN),
)
iam.TestSetPrimaryAuthMethod(t, iamRepo, org, testPrimaryAuthMethod.PublicId)
testNotPrimaryAuthMethod := TestAuthMethod(t, testConn, orgDbWrapper, org.PublicId,
[]string{fmt.Sprintf("ldaps://%s:%d", td.Host(), td.Port())},
WithCertificates(testCtx, tdCerts...),
WithDiscoverDn(testCtx),
WithEnableGroups(testCtx),
WithUserDn(testCtx, testdirectory.DefaultUserDN),
WithGroupDn(testCtx, testdirectory.DefaultGroupDN),
)
const (
testLoginName = "alice"
testPassword = "password"
testLoginName2 = "bob"
)
testExistingAcct := TestAccount(t, testConn, testNotPrimaryAuthMethod, testLoginName2)
iam.TestUser(t, iamRepo, org.PublicId, iam.WithAccountIds(testExistingAcct.PublicId))
// order is important in these tests, so we're using a slice
tests := []struct {
order int
name string
ctx context.Context
authenticatorFn AuthenticatorFactory
lookupUserWithLoginFn LookupUserFactory
tokenCreatorFn AuthTokenCreatorFactory
authMethodId string
loginName string
password string
wantErrMatch *errors.Template
wantErrContains string
}{
{
order: 0,
name: "success-with-primary-auth-method-auto-create",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
},
{
order: 1,
name: "success-with-primary-auth-method-existing-account",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
},
{
order: 2,
name: "success-with-non-primary-auth-method",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName2,
password: testPassword,
},
{
order: 3,
name: "err-not-primary-refused-to-create-user",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.RecordNotFound),
wantErrContains: "auth method is not primary for the scope so refusing to auto-create user",
},
{
order: 4,
name: "missing-authenticator-fn",
ctx: testCtx,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing authenticator factory",
},
{
order: 5,
name: "missing-lookup-user-fn",
ctx: testCtx,
authenticatorFn: authenticatorFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing lookup user factory",
},
{
order: 6,
name: "missing-at-creator-fn",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing auth token creator factory",
},
{
order: 7,
name: "missing-auth-method-id",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing auth method id",
},
{
order: 8,
name: "missing-login-name",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
password: testPassword,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing login name",
},
{
order: 9,
name: "missing-password",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testNotPrimaryAuthMethod.PublicId,
loginName: testLoginName,
wantErrMatch: errors.T(errors.InvalidParameter),
wantErrContains: "missing password",
},
{
order: 10,
name: "authenticator-fn-err",
ctx: testCtx,
authenticatorFn: func() (Authenticator, error) {
return nil, errors.New(testCtx, errors.Internal, "test", "authenticator-fn-err")
},
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.Internal),
wantErrContains: "authenticator-fn-err",
},
{
order: 11,
name: "lookup-user-fn-err",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: func() (LookupUser, error) {
return nil, errors.New(testCtx, errors.Internal, "test", "lookup-user-fn-err")
},
tokenCreatorFn: tokenCreatorFn,
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.Internal),
wantErrContains: "lookup-user-fn-err",
},
{
order: 12,
name: "at-fn-err",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: func() (AuthTokenCreator, error) {
return nil, errors.New(testCtx, errors.Internal, "test", "at-fn-err")
},
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.Internal),
wantErrContains: "at-fn-err",
},
{
order: 13,
name: "authenticate-err",
ctx: testCtx,
authenticatorFn: func() (Authenticator, error) {
return &mockAuthenticator{authErr: errors.New(testCtx, errors.Internal, "test", "authenticate-err")}, nil
},
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: tokenCreatorFn,
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.Internal),
wantErrContains: "authenticate-err",
},
{
order: 14,
name: "token-err",
ctx: testCtx,
authenticatorFn: authenticatorFn,
lookupUserWithLoginFn: lookupUserWithFn,
tokenCreatorFn: func() (AuthTokenCreator, error) {
return &mockTokenCreator{tokenErr: errors.New(testCtx, errors.Internal, "test", "token-err")}, nil
},
authMethodId: testPrimaryAuthMethod.PublicId,
loginName: testLoginName,
password: testPassword,
wantErrMatch: errors.T(errors.Internal),
wantErrContains: "token-err",
},
}
for idx, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
require.Equal(tc.order, idx)
got, err := Authenticate(tc.ctx, tc.authenticatorFn, tc.lookupUserWithLoginFn, tc.tokenCreatorFn, tc.authMethodId, tc.loginName, tc.password)
if tc.wantErrMatch != nil {
require.Error(err)
assert.Empty(got)
assert.Truef(errors.Match(tc.wantErrMatch, err), "unexpected error")
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.NotEmpty(got)
})
}
}
type mockAuthenticator struct {
authErr error
}
func (m *mockAuthenticator) Authenticate(ctx context.Context, authMethodId, loginName, password string) (*Account, error) {
return nil, m.authErr
}
type mockTokenCreator struct {
tokenErr error
}
func (m *mockTokenCreator) CreateAuthToken(ctx context.Context, withIamUser *iam.User, withAuthAccountId string, opt ...authtoken.Option) (*authtoken.AuthToken, error) {
return nil, m.tokenErr
}