Files
threshold/internal/auth/ldap/repository_managed_group_members_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

234 lines
7.1 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package ldap_test
import (
"context"
"encoding/json"
"math/rand"
"testing"
"github.com/hashicorp/boundary/internal/auth/ldap"
"github.com/hashicorp/boundary/internal/auth/oidc/store"
"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 Test_ManagedGroupMemberships(t *testing.T) {
t.Parallel()
testConn, _ := db.TestSetup(t, "postgres")
testRootWrapper := db.TestWrapper(t)
testRw := db.New(testConn)
testKms := kms.TestKms(t, testConn, testRootWrapper)
iamRepo := iam.TestRepo(t, testConn, testRootWrapper)
testCtx := context.Background()
testGlobalDbWrapper, err := testKms.GetWrapper(testCtx, "global", kms.KeyPurposeDatabase)
require.NoError(t, err)
testAuthMethod := ldap.TestAuthMethod(t, testConn, testGlobalDbWrapper, "global", []string{"ldaps://ldap1"})
testAuthMethodStatic := ldap.TestAuthMethod(t, testConn, testGlobalDbWrapper, "global", []string{"ldaps://ldap1"})
repo, err := ldap.NewRepository(testCtx, testRw, testRw, testKms)
require.NoError(t, err)
require.NotNil(t, repo)
// make some groups with GroupNames which will initially match no accounts
mgs := make([]*ldap.ManagedGroup, 0, 100)
for i := 0; i < 100; i++ {
got := ldap.TestManagedGroup(t, testConn, testAuthMethod, []string{"foo", "bar"})
mgs = append(mgs, got)
}
testUser := iam.TestUser(t, iamRepo, "global")
staticAccount := ldap.TestAccount(t, testConn, testAuthMethodStatic, "test-static-login-name", ldap.WithMemberOfGroups(testCtx, "static"))
staticGroup := ldap.TestManagedGroup(t, testConn, testAuthMethod, []string{"static"})
const staticMembershipCount = 1
testGroupNames := []string{"admin", "users"}
testGroupNamesEncodes, err := json.Marshal(testGroupNames)
require.NoError(t, err)
// Fetch existing default admin user u_1234567890 which will have 20 static
// groups associated with it. Then associate a new ldap account to that
// user
account := ldap.TestAccount(t, testConn, testAuthMethod, "test-login-name", ldap.WithMemberOfGroups(testCtx, testGroupNames...))
adminUser, _, err := iamRepo.LookupUser(testCtx, testUser.PublicId)
require.NoError(t, err)
accts, err := iamRepo.AddUserAccounts(testCtx, testUser.PublicId, adminUser.Version, []string{account.PublicId, staticAccount.PublicId})
require.NoError(t, err)
require.Len(t, accts, 2)
tests := []struct {
name string
account *ldap.Account
wantMgsCount int
specificMgs []*ldap.ManagedGroup
wantErr errors.Code
wantErrContains string
}{
{
name: "valid fixed",
account: account,
specificMgs: mgs[0:20],
},
{
name: "valid fixed, same values",
account: account,
specificMgs: mgs[0:20],
},
{
name: "valid fixed, new values",
account: account,
specificMgs: mgs[20:40],
},
{
name: "valid none",
account: account,
wantMgsCount: 0,
},
{
name: "valid none, second test, testing gracefully aborting",
account: account,
wantMgsCount: 0,
},
{
name: "valid fixed, prep for random",
account: account,
specificMgs: mgs[20:50],
},
{
name: "valid random",
account: account,
wantMgsCount: 30,
},
{
name: "valid random, second test",
account: account,
wantMgsCount: 20,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
// We are intentionally carrying things over between tests to be
// more realistic but that means we need correct versions, so update
// them first.
currMgs, err := repo.ListManagedGroups(testCtx, testAuthMethod.PublicId)
require.NoError(err)
require.Len(currMgs, 101)
currVersionMap := make(map[string]uint32, len(currMgs))
for _, currMg := range currMgs {
currVersionMap[currMg.PublicId] = currMg.Version
}
for _, mg := range mgs {
mg.Version = currVersionMap[mg.PublicId]
}
var mgsToTest []*ldap.ManagedGroup
var finalMgs map[string]*ldap.ManagedGroup
mgsToTest = tc.specificMgs
if mgsToTest == nil {
// Select at random
mgsToTest = make([]*ldap.ManagedGroup, tc.wantMgsCount)
for i := 0; i < tc.wantMgsCount; i++ {
mg := mgs[rand.Int()%len(mgs)]
mgsToTest[i] = mg
}
}
finalMgs = make(map[string]*ldap.ManagedGroup)
for _, v := range mgsToTest {
v.GroupNames = string(testGroupNamesEncodes)
v, _, err = repo.UpdateManagedGroup(testCtx, "global", v, v.Version, []string{"GroupNames"})
require.NoError(err)
finalMgs[v.PublicId] = v
}
// Ensure the same set was found; all memberships found should have
// been in the finalMgs map, and when they are all removed there
// should be nothing left.
for _, mship := range mgsToTest {
// Randomly check a few to ensure the MembershipsByGroup function works
members, err := repo.ListManagedGroupMembershipsByGroup(testCtx, mship.PublicId)
require.NoError(err)
require.NotEmpty(members)
var found bool
for _, v := range members {
if v.MemberId == tc.account.GetPublicId() {
found = true
break
}
}
assert.True(found)
delete(finalMgs, mship.PublicId)
}
assert.Len(finalMgs, 0)
// Now check that the static account still has the same memberships
memberships, err := repo.ListManagedGroupMembershipsByMember(testCtx, staticAccount.PublicId)
require.NoError(err)
assert.Len(memberships, staticMembershipCount)
assert.Equal(memberships[0].ManagedGroupId, staticGroup.PublicId)
})
}
t.Run("ListManagedGroupMembershipsByGroup-invalid-parameters", func(t *testing.T) {
got, err := repo.ListManagedGroupMembershipsByGroup(testCtx, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing managed group id")
assert.Nil(t, got)
})
t.Run("ListManagedGroupMembershipsByMember-invalid-parameter", func(t *testing.T) {
got, err := repo.ListManagedGroupMembershipsByMember(testCtx, "")
assert.Error(t, err)
assert.Contains(t, err.Error(), "missing account id")
assert.Nil(t, got)
})
}
func TestManagedGroupMemberAccount_SetTableName(t *testing.T) {
t.Parallel()
allocFn := func() *ldap.ManagedGroupMemberAccount {
return &ldap.ManagedGroupMemberAccount{
ManagedGroupMemberAccount: &store.ManagedGroupMemberAccount{},
}
}
defaultTableName := "auth_ldap_managed_group_member_account"
tests := []struct {
name string
setNameTo string
want string
}{
{
name: "new-name",
setNameTo: "new-name",
want: "new-name",
},
{
name: "reset to default",
setNameTo: "",
want: defaultTableName,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
def := allocFn()
require.Equal(defaultTableName, def.TableName())
m := allocFn()
m.SetTableName(tc.setNameTo)
assert.Equal(tc.want, m.TableName())
})
}
}