diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 1f0661b13..e6d6dbc84 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -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, diff --git a/internal/iam/repository_role_grant.go b/internal/iam/repository_role_grant.go index 280ecbf44..b589517e1 100644 --- a/internal/iam/repository_role_grant.go +++ b/internal/iam/repository_role_grant.go @@ -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) } diff --git a/internal/iam/repository_role_grant_ext_test.go b/internal/iam/repository_role_grant_ext_test.go new file mode 100644 index 000000000..ef6588f09 --- /dev/null +++ b/internal/iam/repository_role_grant_ext_test.go @@ -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) + } +} diff --git a/internal/perms/grants.go b/internal/perms/grants.go index c9dc66b4c..63450fa44 100644 --- a/internal/perms/grants.go +++ b/internal/perms/grants.go @@ -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 }