2020-06-15 15:22:41 -07:00
package groups
import (
"context"
"errors"
"fmt"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/auth"
"github.com/hashicorp/boundary/internal/db"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/groups"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/boundary/internal/iam/store"
2020-09-04 14:48:11 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/common"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-09-04 14:48:11 -07:00
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
2020-09-02 19:43:41 -07:00
"github.com/hashicorp/boundary/internal/types/scope"
2020-06-15 15:22:41 -07:00
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var (
2020-07-20 11:17:09 -07:00
maskManager handlers . MaskManager
2020-06-15 15:22:41 -07:00
)
2020-07-20 11:17:09 -07:00
func init () {
var err error
2020-08-14 14:08:12 -07:00
if maskManager , err = handlers . NewMaskManager ( & store . Group {}, & pb . Group {}); err != nil {
2020-07-20 11:17:09 -07:00
panic ( err )
}
}
2020-06-15 15:22:41 -07:00
// Service handles request as described by the pbs.GroupServiceServer interface.
type Service struct {
2020-09-04 14:48:11 -07:00
repoFn common . IamRepoFactory
2020-06-15 15:22:41 -07:00
}
2020-08-10 17:15:02 -04:00
// NewService returns a group service which handles group related requests to boundary.
2020-09-04 14:48:11 -07:00
func NewService ( repo common . IamRepoFactory ) ( Service , error ) {
2020-06-15 15:22:41 -07:00
if repo == nil {
2020-06-29 11:12:40 -07:00
return Service {}, fmt . Errorf ( "nil iam repository provided" )
2020-06-15 15:22:41 -07:00
}
return Service { repoFn : repo }, nil
}
var _ pbs . GroupServiceServer = Service {}
2020-06-22 10:46:45 -07:00
// ListGroups implements the interface pbs.GroupServiceServer.
func ( s Service ) ListGroups ( ctx context . Context , req * pbs . ListGroupsRequest ) ( * pbs . ListGroupsResponse , error ) {
if err := validateListRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetScopeId (), action . List )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-09-02 19:43:41 -07:00
gl , err := s . listFromRepo ( ctx , req . GetScopeId ())
2020-06-22 10:46:45 -07:00
if err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
for _ , item := range gl {
item . Scope = authResults . Scope
}
2020-06-22 10:46:45 -07:00
return & pbs . ListGroupsResponse { Items : gl }, nil
2020-06-15 15:22:41 -07:00
}
// GetGroups implements the interface pbs.GroupServiceServer.
func ( s Service ) GetGroup ( ctx context . Context , req * pbs . GetGroupRequest ) ( * pbs . GetGroupResponse , error ) {
if err := validateGetRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . Read )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-06-15 15:22:41 -07:00
u , err := s . getFromRepo ( ctx , req . GetId ())
if err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
u . Scope = authResults . Scope
2020-06-15 15:22:41 -07:00
return & pbs . GetGroupResponse { Item : u }, nil
}
// CreateGroup implements the interface pbs.GroupServiceServer.
func ( s Service ) CreateGroup ( ctx context . Context , req * pbs . CreateGroupRequest ) ( * pbs . CreateGroupResponse , error ) {
if err := validateCreateRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetItem (). GetScopeId (), action . Create )
if authResults . Error != nil {
return nil , authResults . Error
}
u , err := s . createInRepo ( ctx , authResults . Scope . GetId (), req . GetItem ())
2020-06-15 15:22:41 -07:00
if err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
u . Scope = authResults . Scope
2020-09-02 19:43:41 -07:00
return & pbs . CreateGroupResponse { Item : u , Uri : fmt . Sprintf ( "groups/%s" , u . GetId ())}, nil
2020-06-15 15:22:41 -07:00
}
// UpdateGroup implements the interface pbs.GroupServiceServer.
func ( s Service ) UpdateGroup ( ctx context . Context , req * pbs . UpdateGroupRequest ) ( * pbs . UpdateGroupResponse , error ) {
if err := validateUpdateRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . Update )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-18 17:38:38 -07:00
u , err := s . updateInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetUpdateMask (). GetPaths (), req . GetItem ())
2020-06-15 15:22:41 -07:00
if err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
u . Scope = authResults . Scope
2020-06-15 15:22:41 -07:00
return & pbs . UpdateGroupResponse { Item : u }, nil
}
// DeleteGroup implements the interface pbs.GroupServiceServer.
func ( s Service ) DeleteGroup ( ctx context . Context , req * pbs . DeleteGroupRequest ) ( * pbs . DeleteGroupResponse , error ) {
if err := validateDeleteRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . Delete )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-09-08 23:57:26 -04:00
_ , err := s . deleteFromRepo ( ctx , req . GetId ())
2020-06-15 15:22:41 -07:00
if err != nil {
return nil , err
}
2020-09-21 12:09:30 -07:00
return & pbs . DeleteGroupResponse {}, nil
2020-06-15 15:22:41 -07:00
}
2020-07-22 15:10:21 -07:00
// AddGroupMembers implements the interface pbs.GroupServiceServer.
func ( s Service ) AddGroupMembers ( ctx context . Context , req * pbs . AddGroupMembersRequest ) ( * pbs . AddGroupMembersResponse , error ) {
if err := validateAddGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . AddMembers )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-18 17:38:38 -07:00
g , err := s . addMembersInRepo ( ctx , req . GetId (), req . GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
2020-09-09 11:37:23 -04:00
g . Scope = authResults . Scope
2020-07-22 15:10:21 -07:00
return & pbs . AddGroupMembersResponse { Item : g }, nil
}
// SetGroupMembers implements the interface pbs.GroupServiceServer.
func ( s Service ) SetGroupMembers ( ctx context . Context , req * pbs . SetGroupMembersRequest ) ( * pbs . SetGroupMembersResponse , error ) {
if err := validateSetGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . SetMembers )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-18 17:38:38 -07:00
g , err := s . setMembersInRepo ( ctx , req . GetId (), req . GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
2020-09-09 11:37:23 -04:00
g . Scope = authResults . Scope
2020-07-22 15:10:21 -07:00
return & pbs . SetGroupMembersResponse { Item : g }, nil
}
// RemoveGroupMembers implements the interface pbs.GroupServiceServer.
func ( s Service ) RemoveGroupMembers ( ctx context . Context , req * pbs . RemoveGroupMembersRequest ) ( * pbs . RemoveGroupMembersResponse , error ) {
if err := validateRemoveGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetId (), action . RemoveMembers )
if authResults . Error != nil {
return nil , authResults . Error
}
2020-08-18 17:38:38 -07:00
g , err := s . removeMembersInRepo ( ctx , req . GetId (), req . GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
2020-09-09 11:37:23 -04:00
g . Scope = authResults . Scope
2020-07-22 15:10:21 -07:00
return & pbs . RemoveGroupMembersResponse { Item : g }, nil
}
2020-06-15 15:22:41 -07:00
func ( s Service ) getFromRepo ( ctx context . Context , id string ) ( * pb . Group , error ) {
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
2020-07-22 15:10:21 -07:00
g , m , err := repo . LookupGroup ( ctx , id )
2020-06-15 15:22:41 -07:00
if err != nil {
if errors . Is ( err , db . ErrRecordNotFound ) {
return nil , handlers . NotFoundErrorf ( "Group %q doesn't exist." , id )
}
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to get group: %w" , err )
2020-06-15 15:22:41 -07:00
}
2020-07-22 15:10:21 -07:00
if g == nil {
2020-06-15 15:22:41 -07:00
return nil , handlers . NotFoundErrorf ( "Group %q doesn't exist." , id )
}
2020-07-22 15:10:21 -07:00
return toProto ( g , m ), nil
2020-06-15 15:22:41 -07:00
}
2020-06-29 11:12:40 -07:00
func ( s Service ) createInRepo ( ctx context . Context , scopeId string , item * pb . Group ) ( * pb . Group , error ) {
2020-06-15 15:22:41 -07:00
var opts [] iam . Option
if item . GetName () != nil {
opts = append ( opts , iam . WithName ( item . GetName (). GetValue ()))
}
if item . GetDescription () != nil {
opts = append ( opts , iam . WithDescription ( item . GetDescription (). GetValue ()))
}
2020-06-29 11:12:40 -07:00
u , err := iam . NewGroup ( scopeId , opts ... )
2020-06-15 15:22:41 -07:00
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build group for creation: %v." , err )
2020-06-15 15:22:41 -07:00
}
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
out , err := repo . CreateGroup ( ctx , u )
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to create group: %w" , err )
2020-06-15 15:22:41 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to create group but no error returned from repository." )
2020-06-15 15:22:41 -07:00
}
2020-07-22 15:10:21 -07:00
return toProto ( out , nil ), nil
2020-06-15 15:22:41 -07:00
}
2020-08-18 17:38:38 -07:00
func ( s Service ) updateInRepo ( ctx context . Context , scopeId , id string , mask [] string , item * pb . Group ) ( * pb . Group , error ) {
2020-06-15 15:22:41 -07:00
var opts [] iam . Option
if desc := item . GetDescription (); desc != nil {
opts = append ( opts , iam . WithDescription ( desc . GetValue ()))
}
if name := item . GetName (); name != nil {
opts = append ( opts , iam . WithName ( name . GetValue ()))
}
2020-08-18 17:38:38 -07:00
version := item . GetVersion ()
2020-08-07 12:55:02 -04:00
g , err := iam . NewGroup ( scopeId , opts ... )
2020-06-15 15:22:41 -07:00
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build group for update: %v." , err )
2020-06-15 15:22:41 -07:00
}
2020-08-07 12:55:02 -04:00
g . PublicId = id
2020-07-20 11:17:09 -07:00
dbMask := maskManager . Translate ( mask )
2020-06-15 15:22:41 -07:00
if len ( dbMask ) == 0 {
2020-08-28 00:17:09 -04:00
return nil , handlers . InvalidArgumentErrorf ( "No valid fields included in the update mask." , map [ string ] string { "update_mask" : "No valid fields provided in the update mask." })
2020-06-15 15:22:41 -07:00
}
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
2020-08-07 12:55:02 -04:00
out , m , rowsUpdated , err := repo . UpdateGroup ( ctx , g , version , dbMask )
2020-06-15 15:22:41 -07:00
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to update group: %w" , err )
2020-06-15 15:22:41 -07:00
}
if rowsUpdated == 0 {
2020-09-25 13:41:20 -07:00
return nil , handlers . NotFoundErrorf ( "Group %q doesn't exist or incorrect version provided." , id )
2020-06-15 15:22:41 -07:00
}
2020-07-22 15:10:21 -07:00
return toProto ( out , m ), nil
2020-06-15 15:22:41 -07:00
}
func ( s Service ) deleteFromRepo ( ctx context . Context , id string ) ( bool , error ) {
repo , err := s . repoFn ()
if err != nil {
return false , err
}
rows , err := repo . DeleteGroup ( ctx , id )
if err != nil {
if errors . Is ( err , db . ErrRecordNotFound ) {
return false , nil
}
2020-09-25 13:41:20 -07:00
return false , fmt . Errorf ( "unable to delete group: %w" , err )
2020-06-15 15:22:41 -07:00
}
return rows > 0 , nil
}
2020-07-22 00:14:28 -04:00
func ( s Service ) listFromRepo ( ctx context . Context , scopeId string ) ([] * pb . Group , error ) {
2020-06-22 10:46:45 -07:00
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
gl , err := repo . ListGroups ( ctx , scopeId )
2020-06-22 10:46:45 -07:00
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to list groups: %w" , err )
2020-06-22 10:46:45 -07:00
}
var outGl [] * pb . Group
for _ , g := range gl {
2020-07-22 15:10:21 -07:00
outGl = append ( outGl , toProto ( g , nil ))
2020-06-22 10:46:45 -07:00
}
return outGl , nil
}
2020-07-22 15:10:21 -07:00
func ( s Service ) addMembersInRepo ( ctx context . Context , groupId string , userIds [] string , version uint32 ) ( * pb . Group , error ) {
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
_ , err = repo . AddGroupMembers ( ctx , groupId , version , userIds )
if err != nil {
2020-09-25 13:41:20 -07:00
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to add members to group: %v." , err )
2020-07-22 15:10:21 -07:00
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up group after adding members: %w" , err )
2020-07-22 15:10:21 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup group after adding member to it." )
2020-07-22 15:10:21 -07:00
}
return toProto ( out , m ), nil
}
func ( s Service ) setMembersInRepo ( ctx context . Context , groupId string , userIds [] string , version uint32 ) ( * pb . Group , error ) {
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
_ , _ , err = repo . SetGroupMembers ( ctx , groupId , version , userIds )
if err != nil {
2020-09-25 13:41:20 -07:00
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to set members on group: %v." , err )
2020-07-22 15:10:21 -07:00
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up group after setting members: %w" , err )
2020-07-22 15:10:21 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup group after setting members for it." )
2020-07-22 15:10:21 -07:00
}
return toProto ( out , m ), nil
}
func ( s Service ) removeMembersInRepo ( ctx context . Context , groupId string , userIds [] string , version uint32 ) ( * pb . Group , error ) {
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
_ , err = repo . DeleteGroupMembers ( ctx , groupId , version , userIds )
if err != nil {
2020-09-25 13:41:20 -07:00
// TODO: Figure out a way to surface more helpful error info beyond the Internal error.
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to remove members from group: %v." , err )
2020-07-22 15:10:21 -07:00
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , fmt . Errorf ( "unable to look up group after removing members: %w" , err )
2020-07-22 15:10:21 -07:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to lookup group after removing members from it." )
2020-07-22 15:10:21 -07:00
}
return toProto ( out , m ), nil
}
2020-09-04 14:48:11 -07:00
func ( s Service ) authResult ( ctx context . Context , id string , a action . Type ) auth . VerifyResults {
res := auth . VerifyResults {}
repo , err := s . repoFn ()
if err != nil {
res . Error = err
return res
}
var parentId string
opts := [] auth . Option { auth . WithType ( resource . Group ), auth . WithAction ( a )}
switch a {
case action . List , action . Create :
parentId = id
scp , err := repo . LookupScope ( ctx , parentId )
if err != nil {
res . Error = err
return res
}
if scp == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return res
}
default :
grp , _ , err := repo . LookupGroup ( ctx , id )
if err != nil {
res . Error = err
return res
}
if grp == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return res
}
parentId = grp . GetScopeId ()
opts = append ( opts , auth . WithId ( id ))
}
opts = append ( opts , auth . WithScopeId ( parentId ))
return auth . Verify ( ctx , opts ... )
}
2020-07-22 15:10:21 -07:00
func toProto ( in * iam . Group , members [] * iam . GroupMember ) * pb . Group {
2020-06-15 15:22:41 -07:00
out := pb . Group {
Id : in . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : in . GetScopeId (),
2020-06-15 15:22:41 -07:00
CreatedTime : in . GetCreateTime (). GetTimestamp (),
UpdatedTime : in . GetUpdateTime (). GetTimestamp (),
2020-07-22 15:10:21 -07:00
Version : in . Version ,
2020-06-15 15:22:41 -07:00
}
if in . GetDescription () != "" {
out . Description = & wrapperspb . StringValue { Value : in . GetDescription ()}
}
if in . GetName () != "" {
out . Name = & wrapperspb . StringValue { Value : in . GetName ()}
}
2020-07-22 15:10:21 -07:00
for _ , m := range members {
out . MemberIds = append ( out . MemberIds , m . GetMemberId ())
out . Members = append ( out . Members , & pb . Member {
Id : m . GetMemberId (),
ScopeId : m . GetMemberScopeId (),
})
}
2020-06-15 15:22:41 -07:00
return & out
}
// A validateX method should exist for each method above. These methods do not make calls to any backing service but enforce
// requirements on the structure of the request. They verify that:
// * The path passed in is correctly formatted
// * All required parameters are set
// * There are no conflicting parameters provided
func validateGetRequest ( req * pbs . GetGroupRequest ) error {
2020-08-21 08:15:32 -07:00
return handlers . ValidateGetRequest ( iam . GroupPrefix , req , handlers . NoopValidatorFn )
2020-06-15 15:22:41 -07:00
}
func validateCreateRequest ( req * pbs . CreateGroupRequest ) error {
2020-09-02 19:43:41 -07:00
return handlers . ValidateCreateRequest ( req . GetItem (), func () map [ string ] string {
badFields := map [ string ] string {}
2020-09-03 08:02:30 -07:00
if ! handlers . ValidId ( scope . Org . Prefix (), req . GetItem (). GetScopeId ()) &&
! handlers . ValidId ( scope . Project . Prefix (), req . GetItem (). GetScopeId ()) &&
scope . Global . String () != req . GetItem (). GetScopeId () {
badFields [ "scope_id" ] = "This field is missing or improperly formatted."
2020-09-02 19:43:41 -07:00
}
return badFields
})
2020-06-15 15:22:41 -07:00
}
func validateUpdateRequest ( req * pbs . UpdateGroupRequest ) error {
2020-08-21 08:15:32 -07:00
return handlers . ValidateUpdateRequest ( iam . GroupPrefix , req , req . GetItem (), handlers . NoopValidatorFn )
2020-06-15 15:22:41 -07:00
}
func validateDeleteRequest ( req * pbs . DeleteGroupRequest ) error {
2020-08-21 08:15:32 -07:00
return handlers . ValidateDeleteRequest ( iam . GroupPrefix , req , handlers . NoopValidatorFn )
2020-06-15 15:22:41 -07:00
}
2020-06-22 10:46:45 -07:00
func validateListRequest ( req * pbs . ListGroupsRequest ) error {
2020-07-22 00:14:28 -04:00
badFields := map [ string ] string {}
2020-09-08 23:57:26 -04:00
if ! handlers . ValidId ( scope . Org . Prefix (), req . GetScopeId ()) &&
! handlers . ValidId ( scope . Project . Prefix (), req . GetScopeId ()) &&
req . GetScopeId () != scope . Global . String () {
2020-09-02 19:43:41 -07:00
badFields [ "scope_id" ] = "Incorrectly formatted identifier."
}
2020-06-22 10:46:45 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Improperly formatted identifier." , badFields )
}
return nil
}
2020-07-22 15:10:21 -07:00
func validateAddGroupMembersRequest ( req * pbs . AddGroupMembersRequest ) error {
badFields := map [ string ] string {}
2020-08-21 08:15:32 -07:00
if ! handlers . ValidId ( iam . GroupPrefix , req . GetId ()) {
2020-07-22 15:10:21 -07:00
badFields [ "id" ] = "Incorrectly formatted identifier."
}
2020-08-07 12:55:02 -04:00
if req . GetVersion () == 0 {
2020-07-22 15:10:21 -07:00
badFields [ "version" ] = "Required field."
}
2020-08-18 17:38:38 -07:00
if len ( req . GetMemberIds ()) == 0 {
2020-07-22 15:10:21 -07:00
badFields [ "member_ids" ] = "Must be non-empty."
}
2020-08-20 13:34:03 -04:00
for _ , id := range req . GetMemberIds () {
if id == "u_recovery" {
badFields [ "member_ids" ] = "u_recovery cannot be assigned to a group"
}
}
2020-07-22 15:10:21 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateSetGroupMembersRequest ( req * pbs . SetGroupMembersRequest ) error {
badFields := map [ string ] string {}
2020-08-21 08:15:32 -07:00
if ! handlers . ValidId ( iam . GroupPrefix , req . GetId ()) {
2020-07-22 15:10:21 -07:00
badFields [ "id" ] = "Incorrectly formatted identifier."
}
2020-08-07 12:55:02 -04:00
if req . GetVersion () == 0 {
2020-07-22 15:10:21 -07:00
badFields [ "version" ] = "Required field."
}
2020-08-20 13:34:03 -04:00
for _ , id := range req . GetMemberIds () {
if id == "u_recovery" {
badFields [ "member_ids" ] = "u_recovery cannot be assigned to a group"
}
}
2020-07-22 15:10:21 -07:00
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateRemoveGroupMembersRequest ( req * pbs . RemoveGroupMembersRequest ) error {
badFields := map [ string ] string {}
2020-08-21 08:15:32 -07:00
if ! handlers . ValidId ( iam . GroupPrefix , req . GetId ()) {
2020-07-22 15:10:21 -07:00
badFields [ "id" ] = "Incorrectly formatted identifier."
}
2020-08-07 12:55:02 -04:00
if req . GetVersion () == 0 {
2020-07-22 15:10:21 -07:00
badFields [ "version" ] = "Required field."
}
2020-08-18 17:38:38 -07:00
if len ( req . GetMemberIds ()) == 0 {
2020-07-22 15:10:21 -07:00
badFields [ "member_ids" ] = "Must be non-empty."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}