Skip to content

Commit 800020f

Browse files
committed
test(groups): add grants tests for groups API (#5403)
* first test with all the required setup * v1 of test * add primitive func and more test * refactor read tests into a single top level * move token generation to a function * add test for creates * add delete tests * add update test * only check for version and update_time * move setup resource into testcase to support grants with specific ID * add member tests * add group-member test example with multiple actions * remove duplicate group membership tests * ran make gen * fix missing parentID bug * fix typo * fix test names and add test cases * switch from google/uuid to hashicorp/go-uuid * add comment to groupmember tests * small comment change * pull shared test utility code from PR #5418 * refactor role grants out of authtoken package * unexport utility function * Remove dead code * lint and make gen * fix role cration logic * fix password TestAccountFunc implementation * implement TestAccountFunc for LDAP * implement TestAccountFunc for OIDC * implement TestUserFunc for managed groups * use managed groups in grants test * undo removal of authtoken.TestAuthTokenWithRoles for future refactor * switch from list to map based test case for create tests * undo merge mistakes * fix merge mistakes * lint * add setup examples * add output fields tests for getgroup * reimplement with reflect * add test for CreateGroup * add all single resource action tests * add list test * rename function argument * move AssertOutputFields to handlers package * fix lint * make gen * use proto.Message instead of custom interface * switch to hashicorp/go-uuid * fix typo * fix error message * id= to ids= * make generating test accounts more randomized * Trigger CI checks * refactor auth/iam grants test setup * move a test to _test package * lint * minor comment fix * use Id instead of ID * make user/account setup in iam returns account instead of just account ID * missed one change * reorganize tests * make gen # Conflicts: # internal/auth/ldap/testing.go
1 parent 71df234 commit 800020f

File tree

25 files changed

+2415
-151
lines changed

25 files changed

+2415
-151
lines changed

internal/auth/db_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) HashiCorp, Inc.
22
// SPDX-License-Identifier: BUSL-1.1
33

4-
package auth
4+
package auth_test
55

66
import (
77
"context"

internal/auth/ldap/testing.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ import (
1212
"crypto/x509/pkix"
1313
"encoding/json"
1414
"encoding/pem"
15+
"fmt"
1516
"math/big"
1617
"net"
1718
"net/url"
1819
"sort"
1920
"testing"
2021
"time"
2122

23+
"github.com/hashicorp/boundary/internal/auth"
2224
"github.com/hashicorp/boundary/internal/db"
25+
"github.com/hashicorp/boundary/internal/kms"
2326
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
27+
"github.com/hashicorp/go-uuid"
2428
"github.com/hashicorp/go-secure-stdlib/parseutil"
2529
"github.com/stretchr/testify/require"
2630
)
@@ -176,6 +180,21 @@ func TestAccount(t testing.TB, conn *db.DB, am *AuthMethod, loginName string, op
176180
return a
177181
}
178182

183+
// TestAuthMethodWithAccountInManagedGroup creates an authMethod, and an account within that authmethod, an
184+
// LDAP managed group, and add the newly created account as a member of the LDAP managed group.
185+
func TestAuthMethodWithAccountInManagedGroup(t *testing.T, conn *db.DB, kmsCache *kms.Kms, scopeId string) (auth.AuthMethod, auth.Account, auth.ManagedGroup) {
186+
t.Helper()
187+
uuid, err := uuid.GenerateUUID()
188+
require.NoError(t, err)
189+
ctx := context.Background()
190+
databaseWrapper, err := kmsCache.GetWrapper(context.Background(), scopeId, kms.KeyPurposeDatabase)
191+
require.NoError(t, err)
192+
am := TestAuthMethod(t, conn, databaseWrapper, scopeId, []string{fmt.Sprintf("ldap://%s", uuid)})
193+
managedGroup := TestManagedGroup(t, conn, am, []string{uuid})
194+
acct := TestAccount(t, conn, am, "testacct", WithMemberOfGroups(ctx, uuid))
195+
return am, acct, managedGroup
196+
}
197+
179198
// TestManagedGroup creates a test ldap managed group.
180199
func TestManagedGroup(t testing.TB, conn *db.DB, am *AuthMethod, grpNames []string, opt ...Option) *ManagedGroup {
181200
t.Helper()

internal/auth/oidc/testing.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/hashicorp/boundary/internal/auth"
2728
"github.com/hashicorp/boundary/internal/auth/oidc/request"
2829
"github.com/hashicorp/boundary/internal/authtoken"
2930
"github.com/hashicorp/boundary/internal/db"
@@ -32,6 +33,7 @@ import (
3233
"github.com/hashicorp/boundary/internal/kms"
3334
"github.com/hashicorp/cap/oidc"
3435
wrapping "github.com/hashicorp/go-kms-wrapping/v2"
36+
"github.com/hashicorp/go-uuid"
3537
"github.com/stretchr/testify/require"
3638
"google.golang.org/protobuf/types/known/timestamppb"
3739
)
@@ -192,6 +194,25 @@ func TestAccount(t testing.TB, conn *db.DB, am *AuthMethod, subject string, opt
192194
return a
193195
}
194196

197+
// TestAuthMethodWithAccountInManagedGroup creates an authMethod, and an account within that authmethod, an
198+
// OIDC managed group, and add the newly created account as a member of the OIDC managed group.
199+
func TestAuthMethodWithAccountInManagedGroup(t *testing.T, conn *db.DB, kmsCache *kms.Kms, scopeId string) (auth.AuthMethod, auth.Account, auth.ManagedGroup) {
200+
t.Helper()
201+
uuid, err := uuid.GenerateUUID()
202+
require.NoError(t, err)
203+
databaseWrapper, err := kmsCache.GetWrapper(context.Background(), scopeId, kms.KeyPurposeDatabase)
204+
require.NoError(t, err)
205+
testAuthMethod := TestAuthMethod(t, conn, databaseWrapper, scopeId, ActivePublicState,
206+
"alice-rp", "fido",
207+
WithIssuer(TestConvertToUrls(t, fmt.Sprintf("https://%s.com", uuid))[0]),
208+
WithSigningAlgs(Alg(oidc.RS256)),
209+
WithApiUrl(TestConvertToUrls(t, fmt.Sprintf("https://%s.com/callback", uuid))[0]))
210+
account := TestAccount(t, conn, testAuthMethod, "testacct")
211+
managedGroup := TestManagedGroup(t, conn, testAuthMethod, `"/token/sub" matches ".*"`)
212+
TestManagedGroupMember(t, conn, managedGroup.PublicId, account.PublicId)
213+
return testAuthMethod, account, managedGroup
214+
}
215+
195216
// TestManagedGroup creates a test oidc managed group.
196217
func TestManagedGroup(t testing.TB, conn *db.DB, am *AuthMethod, filter string, opt ...Option) *ManagedGroup {
197218
t.Helper()

internal/auth/password/testing.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ import (
88
"fmt"
99
"testing"
1010

11+
"github.com/hashicorp/boundary/globals"
12+
"github.com/hashicorp/boundary/internal/auth"
1113
"github.com/hashicorp/boundary/internal/db"
14+
"github.com/hashicorp/go-uuid"
1215
"github.com/stretchr/testify/assert"
1316
"github.com/stretchr/testify/require"
1417
)
@@ -71,6 +74,16 @@ func TestMultipleAccounts(t testing.TB, conn *db.DB, authMethodId string, count
7174
return auts
7275
}
7376

77+
// TestAuthMethodWithAccount creates an authMethod and an account within that authmethod
78+
// returing both the AM and the account
79+
func TestAuthMethodWithAccount(t *testing.T, conn *db.DB) (auth.AuthMethod, auth.Account) {
80+
authMethod := TestAuthMethod(t, conn, globals.GlobalPrefix)
81+
loginName, err := uuid.GenerateUUID()
82+
require.NoError(t, err)
83+
acct := TestAccount(t, conn, authMethod.GetPublicId(), loginName)
84+
return authMethod, acct
85+
}
86+
7487
// TestAccount creates a password account to the provided DB with the provided
7588
// auth method id and loginName. The auth method must have been created
7689
// previously. See password.NewAccount(...) for a list of supported options.

internal/auth/testing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,15 @@ import (
1010

1111
"github.com/hashicorp/boundary/internal/db"
1212
"github.com/hashicorp/boundary/internal/db/timestamp"
13+
"github.com/hashicorp/boundary/internal/kms"
1314
"github.com/stretchr/testify/require"
1415
)
1516

17+
type (
18+
TestAuthMethodWithAccountFunc func(t *testing.T, conn *db.DB) (AuthMethod, Account)
19+
TestAuthMethodWithAccountInManagedGroup func(t *testing.T, conn *db.DB, kmsCache *kms.Kms, scopeId string) (AuthMethod, Account, ManagedGroup)
20+
)
21+
1622
// ManagedGroupMemberAccount represents an entry from
1723
// auth_managed_group_member_account. These are used to determine the account
1824
// ids where are a member of managed groups. See: oidc and ldap managed groups

internal/authtoken/testing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestAuthToken(t testing.TB, conn *db.DB, kms *kms.Kms, scopeId string, opt
5151
// TestRoleGrantsForToken contains information used by TestAuthTokenWithRoles to create
5252
// roles and their associated grants (with grant scopes)
5353
type TestRoleGrantsForToken struct {
54-
RoleScopeID string
54+
RoleScopeId string
5555
GrantStrings []string
5656
GrantScopes []string
5757
}
@@ -75,7 +75,7 @@ func TestAuthTokenWithRoles(t testing.TB, conn *db.DB, kms *kms.Kms, scopeId str
7575
acct := password.TestAccount(t, conn, authMethod.GetPublicId(), loginName)
7676
user := iam.TestUser(t, iamRepo, scopeId, iam.WithAccountIds(acct.GetPublicId()))
7777
for _, r := range roles {
78-
role := iam.TestRoleWithGrants(t, conn, r.RoleScopeID, r.GrantScopes, r.GrantStrings)
78+
role := iam.TestRoleWithGrants(t, conn, r.RoleScopeId, r.GrantScopes, r.GrantStrings)
7979
_ = iam.TestUserRole(t, conn, role.PublicId, user.PublicId)
8080
}
8181
fullGrantToken, err := atRepo.CreateAuthToken(ctx, user, acct.GetPublicId())

internal/daemon/controller/handlers/accounts/grants_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestListPassword_Grants(t *testing.T) {
6161
},
6262
roleRequest: []authtoken.TestRoleGrantsForToken{
6363
{
64-
RoleScopeID: globals.GlobalPrefix,
64+
RoleScopeId: globals.GlobalPrefix,
6565
GrantStrings: []string{"ids=*;type=*;actions=list,read"},
6666
GrantScopes: []string{globals.GrantScopeChildren},
6767
},
@@ -77,7 +77,7 @@ func TestListPassword_Grants(t *testing.T) {
7777
},
7878
roleRequest: []authtoken.TestRoleGrantsForToken{
7979
{
80-
RoleScopeID: org.GetPublicId(),
80+
RoleScopeId: org.GetPublicId(),
8181
GrantStrings: []string{"ids=*;type=*;actions=list,read"},
8282
GrantScopes: []string{globals.GrantScopeChildren},
8383
},

internal/daemon/controller/handlers/aliases/grants_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestGrants_ReadActions(t *testing.T) {
6161
},
6262
rolesToCreate: []authtoken.TestRoleGrantsForToken{
6363
{
64-
RoleScopeID: globals.GlobalPrefix,
64+
RoleScopeId: globals.GlobalPrefix,
6565
GrantStrings: []string{"ids=*;type=alias;actions=list,read"},
6666
GrantScopes: []string{globals.GrantScopeThis},
6767
},
@@ -77,7 +77,7 @@ func TestGrants_ReadActions(t *testing.T) {
7777
},
7878
rolesToCreate: []authtoken.TestRoleGrantsForToken{
7979
{
80-
RoleScopeID: globals.GlobalPrefix,
80+
RoleScopeId: globals.GlobalPrefix,
8181
GrantStrings: []string{"ids=*;type=group;actions=list,read"},
8282
GrantScopes: []string{globals.GrantScopeThis},
8383
},

internal/daemon/controller/handlers/authmethods/grants_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestGrants_ReadActions(t *testing.T) {
100100
},
101101
rolesToCreate: []authtoken.TestRoleGrantsForToken{
102102
{
103-
RoleScopeID: globals.GlobalPrefix,
103+
RoleScopeId: globals.GlobalPrefix,
104104
GrantStrings: []string{"ids=*;type=auth-method;actions=list,read"},
105105
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
106106
},
@@ -149,7 +149,7 @@ func TestGrants_ReadActions(t *testing.T) {
149149
},
150150
rolesToCreate: []authtoken.TestRoleGrantsForToken{
151151
{
152-
RoleScopeID: org1.PublicId,
152+
RoleScopeId: org1.PublicId,
153153
GrantStrings: []string{"ids=*;type=auth-method;actions=list,read"},
154154
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
155155
},
@@ -205,7 +205,7 @@ func TestGrants_ReadActions(t *testing.T) {
205205
},
206206
rolesToCreate: []authtoken.TestRoleGrantsForToken{
207207
{
208-
RoleScopeID: globals.GlobalPrefix,
208+
RoleScopeId: globals.GlobalPrefix,
209209
GrantStrings: []string{"ids=*;type=auth-method;actions=list,read"},
210210
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
211211
},
@@ -219,7 +219,7 @@ func TestGrants_ReadActions(t *testing.T) {
219219
},
220220
rolesToCreate: []authtoken.TestRoleGrantsForToken{
221221
{
222-
RoleScopeID: globals.GlobalPrefix,
222+
RoleScopeId: globals.GlobalPrefix,
223223
GrantStrings: []string{"ids=*;type=auth-method;actions=list,read"},
224224
GrantScopes: []string{globals.GrantScopeChildren},
225225
},

internal/daemon/controller/handlers/authtokens/grants_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestGrants_ReadActions(t *testing.T) {
7979
},
8080
rolesToCreate: []authtoken.TestRoleGrantsForToken{
8181
{
82-
RoleScopeID: globals.GlobalPrefix,
82+
RoleScopeId: globals.GlobalPrefix,
8383
GrantStrings: []string{"ids=*;type=auth-token;actions=list,read"},
8484
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
8585
},
@@ -95,7 +95,7 @@ func TestGrants_ReadActions(t *testing.T) {
9595
},
9696
rolesToCreate: []authtoken.TestRoleGrantsForToken{
9797
{
98-
RoleScopeID: org1.PublicId,
98+
RoleScopeId: org1.PublicId,
9999
GrantStrings: []string{"ids=*;type=auth-token;actions=list,read"},
100100
GrantScopes: []string{globals.GrantScopeThis, globals.GrantScopeChildren},
101101
},

0 commit comments

Comments
 (0)