Add recursive listing to roles (#881)

This also lays the framework for adding recursive listing to the rest of the resources.
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2021-01-23 13:39:04 -05:00
1 parent 0629526319
commit 6b58a3317d
18 files changed
+430 -511

No files matched your search

@@ -7,6 +7,7 @@ import (
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/errors"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/roles"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/iam/store"
@@ -79,17 +80,76 @@ func (s Service) ListRoles(ctx context.Context, req *pbs.ListRolesRequest) (*pbs
if authResults.Error != nil {
return nil, authResults.Error
}
gl, err := s.listFromRepo(ctx, req.GetScopeId())
// Get the list of scope IDs. If it's recursive, check ACLs.
var scopeIds []string
// This will be used to memoize scope info so we can put the right scope
// info for each returned value
scopeInfoMap := map[string]*scopes.ScopeInfo{
req.GetScopeId(): authResults.Scope,
}
var err error
switch req.GetRecursive() {
case true:
repo, err := s.repoFn()
if err != nil {
return nil, err
}
// Get all scopes recursively
scps, err := repo.ListScopesRecursively(ctx, req.GetScopeId())
if err != nil {
return nil, err
}
scopeIds = make([]string, 0, len(scps))
res := perms.Resource{
Type: resource.Role,
}
// For each scope, see if we have permission to list on that scope
for _, scp := range scps {
scpId := scp.GetPublicId()
// We already checked the incoming ID so we can definitely add it
if scpId == authResults.Scope.Id {
scopeIds = append(scopeIds, scp.GetPublicId())
continue
}
res.ScopeId = scpId
aSet := authResults.FetchActionSetForType(ctx,
resource.Role,
action.ActionSet{action.List},
auth.WithResource(&res),
)
// We only passed one action in, so anything other than that one
// action back should not be included. Assuming it's correct, add
// the scope ID for lookup and memoize the scope info if needed.
if len(aSet) == 1 && aSet[0] == action.List {
scopeIds = append(scopeIds, scpId)
if scopeInfoMap[scpId] == nil {
scopeInfo := &scopes.ScopeInfo{
Id: scp.GetPublicId(),
Type: scp.GetType(),
Name: scp.GetName(),
Description: scp.GetDescription(),
}
scopeInfoMap[scpId] = scopeInfo
}
}
}
default:
scopeIds = []string{req.GetScopeId()}
}
items, err := s.listFromRepo(ctx, scopeIds)
if err != nil {
return nil, err
}
finalItems := make([]*pb.Role, 0, len(gl))
finalItems := make([]*pb.Role, 0, len(items))
res := &perms.Resource{
ScopeId: authResults.Scope.Id,
Type: resource.Role,
}
for _, item := range gl {
item.Scope = authResults.Scope
for _, item := range items {
item.Scope = scopeInfoMap[item.GetScopeId()]
item.AuthorizedActions = authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res)).Strings()
if len(item.AuthorizedActions) > 0 {
finalItems = append(finalItems, item)
@@ -374,12 +434,12 @@ func (s Service) deleteFromRepo(ctx context.Context, id string) (bool, error) {
return rows > 0, nil
}
func (s Service) listFromRepo(ctx context.Context, scopeId string) ([]*pb.Role, error) {
func (s Service) listFromRepo(ctx context.Context, scopeIds []string) ([]*pb.Role, error) {
repo, err := s.repoFn()
if err != nil {
return nil, err
}
rl, err := repo.ListRoles(ctx, scopeId)
rl, err := repo.ListRoles(ctx, scopeIds)
if err != nil {
return nil, err
}
@@ -180,7 +180,6 @@ func TestGet(t *testing.T) {
}
func TestList(t *testing.T) {
assert, require := assert.New(t), require.New(t)
conn, _ := db.TestSetup(t, "postgres")
wrap := db.TestWrapper(t)
iamRepo := iam.TestRepo(t, conn, wrap)
@@ -191,6 +190,7 @@ func TestList(t *testing.T) {
oWithRoles, pNoRoles := iam.TestScopes(t, iamRepo, iam.WithSkipDefaultRoleCreation(true))
var wantOrgRoles []*pb.Role
var wantProjRoles []*pb.Role
var totalRoles []*pb.Role
for i := 0; i < 10; i++ {
or := iam.TestRole(t, conn, oWithRoles.GetPublicId())
wantOrgRoles = append(wantOrgRoles, &pb.Role{
@@ -203,6 +203,7 @@ func TestList(t *testing.T) {
Version: or.GetVersion(),
AuthorizedActions: []string{"read", "update", "delete", "add-principals", "set-principals", "remove-principals", "add-grants", "set-grants", "remove-grants"},
})
totalRoles = append(totalRoles, wantOrgRoles[i])
pr := iam.TestRole(t, conn, pWithRoles.GetPublicId())
wantProjRoles = append(wantProjRoles, &pb.Role{
Id: pr.GetPublicId(),
@@ -214,6 +215,7 @@ func TestList(t *testing.T) {
Version: pr.GetVersion(),
AuthorizedActions: []string{"read", "update", "delete", "add-principals", "set-principals", "remove-principals", "add-grants", "set-grants", "remove-grants"},
})
totalRoles = append(totalRoles, wantProjRoles[i])
}
cases := []struct {
@@ -241,9 +243,24 @@ func TestList(t *testing.T) {
req: &pbs.ListRolesRequest{ScopeId: pNoRoles.GetPublicId()},
res: &pbs.ListRolesResponse{},
},
{
name: "List org role recursively",
req: &pbs.ListRolesRequest{ScopeId: oNoRoles.GetPublicId(), Recursive: true},
res: &pbs.ListRolesResponse{
Items: wantProjRoles,
},
},
{
name: "List global roles recursively",
req: &pbs.ListRolesRequest{ScopeId: "global", Recursive: true},
res: &pbs.ListRolesResponse{
Items: totalRoles,
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
s, err := roles.NewService(repoFn)
require.NoError(err, "Couldn't create new role service.")
@@ -704,7 +704,7 @@ func TestCreate(t *testing.T) {
if withUserId {
repo, err := repoFn()
require.NoError(err)
roles, err := repo.ListRoles(ctx, got.GetItem().GetId())
roles, err := repo.ListRoles(ctx, []string{got.GetItem().GetId()})
require.NoError(err)
switch tc.scopeId {
case defaultOrg.PublicId: