Add test for GrantsForUser (#1287)
* Start of test * Add a test for existing GrantsForUser functionality. The test has randomization built in to allocate users across groups and both users and groups across roles. It tracks which users and groups have been assigned to which roles, then ensures that GrantsForUser returns the correct set of roles for a given user. This required modifying the CTE in GrantsForUser to return role_id as well. This doesn't test managed groups yet, that will come in a follow-on PR.
This commit is contained in:
1 parent
33716f1af5
commit
b513f21153
4 files changed
+177
-12
No files matched your search
@@ -494,20 +494,20 @@ func (v verifier) performAuthCheck() (aclResults perms.ACLResults, userId string
|
||||
}
|
||||
|
||||
var parsedGrants []perms.Grant
|
||||
var grantPairs []perms.GrantPair
|
||||
var grantTuples []perms.GrantTuple
|
||||
|
||||
// Fetch and parse grants for this user ID (which may include grants for
|
||||
// u_anon and u_auth)
|
||||
grantPairs, err = iamRepo.GrantsForUser(v.ctx, userId)
|
||||
grantTuples, err = iamRepo.GrantsForUser(v.ctx, userId)
|
||||
if err != nil {
|
||||
retErr = errors.Wrap(err, op)
|
||||
return
|
||||
}
|
||||
parsedGrants = make([]perms.Grant, 0, len(grantPairs))
|
||||
parsedGrants = make([]perms.Grant, 0, len(grantTuples))
|
||||
// Note: Below, we always skip validation so that we don't error on formats
|
||||
// that we've since restricted, e.g. "id=foo;actions=create,read". These
|
||||
// will simply not have an effect.
|
||||
for _, pair := range grantPairs {
|
||||
for _, pair := range grantTuples {
|
||||
parsed, err := perms.Parse(
|
||||
pair.ScopeId,
|
||||
pair.Grant,
|
||||
|
||||
@@ -385,7 +385,7 @@ func (r *Repository) ListRoleGrants(ctx context.Context, roleId string, opt ...O
|
||||
return roleGrants, nil
|
||||
}
|
||||
|
||||
func (r *Repository) GrantsForUser(ctx context.Context, userId string, _ ...Option) ([]perms.GrantPair, error) {
|
||||
func (r *Repository) GrantsForUser(ctx context.Context, userId string, _ ...Option) ([]perms.GrantTuple, error) {
|
||||
const op = "iam.(Repository).GrantsForUser"
|
||||
if userId == "" {
|
||||
return nil, errors.New(errors.InvalidParameter, op, "missing user id")
|
||||
@@ -433,15 +433,16 @@ roles (role_id, grant_scope_id) as (
|
||||
user_group_roles
|
||||
where public_id in (user_group_roles.role_id)
|
||||
),
|
||||
final (role_scope, role_grant) as (
|
||||
select roles.grant_scope_id,
|
||||
final (role_id, role_scope, role_grant) as (
|
||||
select roles.role_id,
|
||||
roles.grant_scope_id,
|
||||
iam_role_grant.canonical_grant
|
||||
from roles
|
||||
inner
|
||||
join iam_role_grant
|
||||
on roles.role_id = iam_role_grant.role_id
|
||||
)
|
||||
select role_scope as scope_id, role_grant as grant from final;
|
||||
select role_id as role_id, role_scope as scope_id, role_grant as grant from final;
|
||||
`
|
||||
)
|
||||
|
||||
@@ -453,14 +454,14 @@ select role_scope as scope_id, role_grant as grant from final;
|
||||
query = fmt.Sprintf(grantsQuery, authUser)
|
||||
}
|
||||
|
||||
var grants []perms.GrantPair
|
||||
var grants []perms.GrantTuple
|
||||
rows, err := r.reader.Query(ctx, query, []interface{}{userId})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var g perms.GrantPair
|
||||
var g perms.GrantTuple
|
||||
if err := r.reader.ScanRows(rows, &g); err != nil {
|
||||
return nil, errors.Wrap(err, op)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
package iam_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
mathrand "math/rand"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestGrantsForUser(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conn, _ := db.TestSetup(t, "postgres")
|
||||
wrap := db.TestWrapper(t)
|
||||
iamRepo := iam.TestRepo(t, conn, wrap)
|
||||
|
||||
userCount := 10
|
||||
groupCount := 30
|
||||
roleCount := 30
|
||||
// probFactor acts as a mod value; increasing means less probability. 2 =
|
||||
// 50%, 5 = 20%, etc.
|
||||
probFactor := 4
|
||||
|
||||
o, p := iam.TestScopes(
|
||||
t,
|
||||
iamRepo,
|
||||
iam.WithSkipAdminRoleCreation(true),
|
||||
iam.WithSkipDefaultRoleCreation(true),
|
||||
)
|
||||
|
||||
// We're going to generate a bunch of users, groups, and managed groups.
|
||||
// These will be randomly assigned and we will record assignations.
|
||||
users := func() (ret []*iam.User) {
|
||||
ret = make([]*iam.User, 0, userCount)
|
||||
scopeId := scope.Global.String()
|
||||
if mathrand.Int()%2 == 0 {
|
||||
scopeId = o.GetPublicId()
|
||||
}
|
||||
for i := 0; i < userCount; i++ {
|
||||
ret = append(ret, iam.TestUser(t, iamRepo, scopeId, iam.WithName(fmt.Sprintf("testuser%d", i))))
|
||||
}
|
||||
return
|
||||
}()
|
||||
groups := func() (ret []*iam.Group) {
|
||||
ret = make([]*iam.Group, 0, groupCount)
|
||||
scopeId := o.GetPublicId()
|
||||
if mathrand.Int()%2 == 0 {
|
||||
scopeId = p.GetPublicId()
|
||||
}
|
||||
for i := 0; i < groupCount; i++ {
|
||||
ret = append(ret, iam.TestGroup(t, conn, scopeId, iam.WithName(fmt.Sprintf("testgroup%d", i))))
|
||||
}
|
||||
return
|
||||
}()
|
||||
roles := func() (ret []*iam.Role) {
|
||||
ret = make([]*iam.Role, 0, roleCount)
|
||||
scopeId := o.GetPublicId()
|
||||
if mathrand.Int()%2 == 0 {
|
||||
scopeId = p.GetPublicId()
|
||||
}
|
||||
for i := 0; i < roleCount; i++ {
|
||||
role := iam.TestRole(t, conn, scopeId, iam.WithName(fmt.Sprintf("testrole%d", i)))
|
||||
iam.TestRoleGrant(t, conn, role.PublicId, "id=*;type=*;actions=*")
|
||||
ret = append(ret, role)
|
||||
}
|
||||
return
|
||||
}()
|
||||
|
||||
// This variable stores an easy way to lookup, given a group ID, whether a
|
||||
// user is in that group.
|
||||
userToGroupsMapping := map[string]map[string]bool{}
|
||||
for _, user := range users {
|
||||
for _, group := range groups {
|
||||
// Give each user about a chance of being in any specific
|
||||
// group
|
||||
if mathrand.Int()%probFactor == 0 {
|
||||
userId := user.PublicId
|
||||
groupId := group.PublicId
|
||||
iam.TestGroupMember(t, conn, groupId, userId)
|
||||
currentMapping := userToGroupsMapping[userId]
|
||||
if currentMapping == nil {
|
||||
currentMapping = make(map[string]bool)
|
||||
}
|
||||
currentMapping[groupId] = true
|
||||
userToGroupsMapping[userId] = currentMapping
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now, we're going to randomly assign users and groups to roles and also
|
||||
// store mappings
|
||||
userToRolesMapping := map[string]map[string]bool{}
|
||||
groupToRolesMapping := map[string]map[string]bool{}
|
||||
for _, role := range roles {
|
||||
for _, user := range users {
|
||||
// Give each user about a chance of being directly added to
|
||||
// any specific role
|
||||
if mathrand.Int()%probFactor == 0 {
|
||||
roleId := role.PublicId
|
||||
userId := user.PublicId
|
||||
iam.TestUserRole(t, conn, roleId, userId)
|
||||
currentMapping := userToRolesMapping[userId]
|
||||
if currentMapping == nil {
|
||||
currentMapping = make(map[string]bool)
|
||||
}
|
||||
currentMapping[roleId] = true
|
||||
userToRolesMapping[userId] = currentMapping
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, role := range roles {
|
||||
for _, group := range groups {
|
||||
// Give each group about a chance of being directly added to
|
||||
// any specific role
|
||||
if mathrand.Int()%probFactor == 0 {
|
||||
roleId := role.PublicId
|
||||
groupId := group.PublicId
|
||||
iam.TestGroupRole(t, conn, roleId, groupId)
|
||||
currentMapping := groupToRolesMapping[groupId]
|
||||
if currentMapping == nil {
|
||||
currentMapping = make(map[string]bool)
|
||||
}
|
||||
currentMapping[roleId] = true
|
||||
groupToRolesMapping[groupId] = currentMapping
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now, fetch the set of grants. We're going to be testing this by looking
|
||||
// at the role IDs of the matching grant tuples.
|
||||
for _, user := range users {
|
||||
tuples, err := iamRepo.GrantsForUser(ctx, user.PublicId)
|
||||
require.NoError(t, err)
|
||||
|
||||
// De-dupe role IDs
|
||||
roleIds := make(map[string]bool, len(tuples))
|
||||
for _, tuple := range tuples {
|
||||
roleIds[tuple.RoleId] = true
|
||||
}
|
||||
|
||||
// Now, using the previous maps, figure out which roles we _expect_ to
|
||||
// see returned. This is the set of roles with directly added users,
|
||||
// plus the set of roles where we added the user as a group member and
|
||||
// that group to a role.
|
||||
expectedRoleIds := make(map[string]bool, len(tuples))
|
||||
for roleId := range userToRolesMapping[user.PublicId] {
|
||||
expectedRoleIds[roleId] = true
|
||||
}
|
||||
for groupId := range userToGroupsMapping[user.PublicId] {
|
||||
for roleId := range groupToRolesMapping[groupId] {
|
||||
expectedRoleIds[roleId] = true
|
||||
}
|
||||
}
|
||||
|
||||
// Now verify that the expected set and returned set match
|
||||
assert.EqualValues(t, expectedRoleIds, roleIds)
|
||||
}
|
||||
}
|
||||
@@ -12,9 +12,10 @@ import (
|
||||
"github.com/hashicorp/boundary/internal/types/scope"
|
||||
)
|
||||
|
||||
// GrantPair is simply a struct that can be reference from other code to return
|
||||
// GrantTuple is simply a struct that can be reference from other code to return
|
||||
// a set of scopes and grants to parse
|
||||
type GrantPair struct {
|
||||
type GrantTuple struct {
|
||||
RoleId string
|
||||
ScopeId string
|
||||
Grant string
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user