Add authorized actions output on resources (#870)
This commit is contained in:
1 parent
ec6151d174
commit
dcb15cffbd
93 files changed
+2475
-1038
No files matched your search
@@ -11,6 +11,7 @@ import (
|
||||
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/iam/store"
|
||||
"github.com/hashicorp/boundary/internal/perms"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/common"
|
||||
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
|
||||
"github.com/hashicorp/boundary/internal/types/action"
|
||||
@@ -21,7 +22,27 @@ import (
|
||||
"google.golang.org/protobuf/types/known/wrapperspb"
|
||||
)
|
||||
|
||||
var maskManager handlers.MaskManager
|
||||
var (
|
||||
maskManager handlers.MaskManager
|
||||
|
||||
// IdActions contains the set of actions that can be performed on
|
||||
// individual resources
|
||||
IdActions = action.ActionSet{
|
||||
action.Read,
|
||||
action.Update,
|
||||
action.Delete,
|
||||
action.AddAccounts,
|
||||
action.SetAccounts,
|
||||
action.RemoveAccounts,
|
||||
}
|
||||
|
||||
// CollectionActions contains the set of actions that can be performed on
|
||||
// this collection
|
||||
CollectionActions = action.ActionSet{
|
||||
action.Create,
|
||||
action.List,
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
@@ -60,10 +81,19 @@ func (s Service) ListUsers(ctx context.Context, req *pbs.ListUsersRequest) (*pbs
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
finalItems := make([]*pb.User, 0, len(ul))
|
||||
res := &perms.Resource{
|
||||
ScopeId: authResults.Scope.Id,
|
||||
Type: resource.User,
|
||||
}
|
||||
for _, item := range ul {
|
||||
item.Scope = authResults.Scope
|
||||
item.AuthorizedActions = authResults.FetchActionSetForId(ctx, item.Id, IdActions, auth.WithResource(res)).Strings()
|
||||
if len(item.AuthorizedActions) > 0 {
|
||||
finalItems = append(finalItems, item)
|
||||
}
|
||||
}
|
||||
return &pbs.ListUsersResponse{Items: ul}, nil
|
||||
return &pbs.ListUsersResponse{Items: finalItems}, nil
|
||||
}
|
||||
|
||||
// GetUsers implements the interface pbs.UserServiceServer.
|
||||
@@ -80,6 +110,7 @@ func (s Service) GetUser(ctx context.Context, req *pbs.GetUserRequest) (*pbs.Get
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.GetUserResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
@@ -97,6 +128,7 @@ func (s Service) CreateUser(ctx context.Context, req *pbs.CreateUserRequest) (*p
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.CreateUserResponse{Item: u, Uri: fmt.Sprintf("users/%s", u.GetId())}, nil
|
||||
}
|
||||
|
||||
@@ -114,6 +146,7 @@ func (s Service) UpdateUser(ctx context.Context, req *pbs.UpdateUserRequest) (*p
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.UpdateUserResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
@@ -147,6 +180,7 @@ func (s Service) AddUserAccounts(ctx context.Context, req *pbs.AddUserAccountsRe
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.AddUserAccountsResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
@@ -164,6 +198,7 @@ func (s Service) SetUserAccounts(ctx context.Context, req *pbs.SetUserAccountsRe
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.SetUserAccountsResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
@@ -181,6 +216,7 @@ func (s Service) RemoveUserAccounts(ctx context.Context, req *pbs.RemoveUserAcco
|
||||
return nil, err
|
||||
}
|
||||
u.Scope = authResults.Scope
|
||||
u.AuthorizedActions = authResults.FetchActionSetForId(ctx, u.Id, IdActions).Strings()
|
||||
return &pbs.RemoveUserAccountsResponse{Item: u}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -49,14 +49,15 @@ func TestGet(t *testing.T) {
|
||||
}
|
||||
|
||||
wantU := &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.ScopeId, Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: u.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: u.GetDescription()},
|
||||
CreatedTime: u.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: u.UpdateTime.GetTimestamp(),
|
||||
Version: 1,
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.ScopeId, Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: u.GetName()},
|
||||
Description: &wrapperspb.StringValue{Value: u.GetDescription()},
|
||||
CreatedTime: u.CreateTime.GetTimestamp(),
|
||||
UpdatedTime: u.UpdateTime.GetTimestamp(),
|
||||
Version: 1,
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
}
|
||||
|
||||
cases := []struct {
|
||||
@@ -128,12 +129,13 @@ func TestList(t *testing.T) {
|
||||
u, err := repo.CreateUser(context.Background(), newU)
|
||||
require.NoError(t, err)
|
||||
wantUsers = append(wantUsers, &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: u.GetUpdateTime().GetTimestamp(),
|
||||
Version: 1,
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
UpdatedTime: u.GetUpdateTime().GetTimestamp(),
|
||||
Version: 1,
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -254,11 +256,12 @@ func TestCreate(t *testing.T) {
|
||||
res: &pbs.CreateUserResponse{
|
||||
Uri: fmt.Sprintf("users/%s_", iam.UserPrefix),
|
||||
Item: &pb.User{
|
||||
ScopeId: defaultUser.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: defaultUser.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
Version: 1,
|
||||
ScopeId: defaultUser.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: defaultUser.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
Version: 1,
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -272,11 +275,12 @@ func TestCreate(t *testing.T) {
|
||||
res: &pbs.CreateUserResponse{
|
||||
Uri: fmt.Sprintf("users/%s_", iam.UserPrefix),
|
||||
Item: &pb.User{
|
||||
ScopeId: scope.Global.String(),
|
||||
Scope: &scopes.ScopeInfo{Id: scope.Global.String(), Type: scope.Global.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
Version: 1,
|
||||
ScopeId: scope.Global.String(),
|
||||
Scope: &scopes.ScopeInfo{Id: scope.Global.String(), Type: scope.Global.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "name"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
Version: 1,
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -381,12 +385,13 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateUserResponse{
|
||||
Item: &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -403,12 +408,13 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateUserResponse{
|
||||
Item: &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "new"},
|
||||
Description: &wrapperspb.StringValue{Value: "desc"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -456,11 +462,12 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateUserResponse{
|
||||
Item: &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -477,12 +484,13 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateUserResponse{
|
||||
Item: &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "updated"},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "updated"},
|
||||
Description: &wrapperspb.StringValue{Value: "default"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -499,12 +507,13 @@ func TestUpdate(t *testing.T) {
|
||||
},
|
||||
res: &pbs.UpdateUserResponse{
|
||||
Item: &pb.User{
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "default"},
|
||||
Description: &wrapperspb.StringValue{Value: "notignored"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
Id: u.GetPublicId(),
|
||||
ScopeId: u.GetScopeId(),
|
||||
Scope: &scopes.ScopeInfo{Id: u.GetScopeId(), Type: scope.Org.String()},
|
||||
Name: &wrapperspb.StringValue{Value: "default"},
|
||||
Description: &wrapperspb.StringValue{Value: "notignored"},
|
||||
CreatedTime: u.GetCreateTime().GetTimestamp(),
|
||||
AuthorizedActions: []string{"read", "update", "delete", "add-accounts", "set-accounts", "remove-accounts"},
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user