2020-06-15 15:22:41 -07:00
package groups
import (
"context"
"errors"
"fmt"
"regexp"
"strings"
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"
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-06-15 15:22:41 -07:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"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
reInvalidID = regexp . MustCompile ( "[^A-Za-z0-9]" )
)
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 {
repoFn func () ( * iam . Repository , error )
}
2020-08-10 17:15:02 -04:00
// NewService returns a group service which handles group related requests to boundary.
2020-06-15 15:22:41 -07:00
func NewService ( repo func () ( * iam . Repository , error )) ( Service , error ) {
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 ) {
2020-07-22 00:14:28 -04:00
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 00:14:28 -04:00
}
2020-06-22 10:46:45 -07:00
if err := validateListRequest ( req ); err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
gl , err := s . listFromRepo ( ctx , authResults . Scope . GetId ())
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 ) {
2020-07-22 00:14:28 -04:00
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 00:14:28 -04:00
}
2020-06-15 15:22:41 -07:00
if err := validateGetRequest ( req ); err != nil {
return nil , err
}
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 ) {
2020-07-22 00:14:28 -04:00
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 00:14:28 -04:00
}
2020-06-15 15:22:41 -07:00
if err := validateCreateRequest ( req ); err != nil {
return nil , err
}
2020-07-22 00:14:28 -04:00
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
return & pbs . CreateGroupResponse { Item : u , Uri : fmt . Sprintf ( "scopes/%s/groups/%s" , authResults . Scope . GetId (), 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 ) {
2020-07-22 00:14:28 -04:00
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 00:14:28 -04:00
}
2020-06-15 15:22:41 -07:00
if err := validateUpdateRequest ( req ); err != nil {
return nil , err
}
2020-08-07 12:55:02 -04:00
u , err := s . updateInRepo ( ctx , authResults . Scope . GetId (), req . GetId (), req . GetVersion (), 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 ) {
2020-07-22 00:14:28 -04:00
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 00:14:28 -04:00
}
2020-06-15 15:22:41 -07:00
if err := validateDeleteRequest ( req ); err != nil {
return nil , err
}
existed , err := s . deleteFromRepo ( ctx , req . GetId ())
if err != nil {
return nil , err
}
return & pbs . DeleteGroupResponse { Existed : existed }, nil
}
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 ) {
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 15:10:21 -07:00
}
if err := validateAddGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-08-08 19:27:49 -04:00
g , err := s . addMembersInRepo ( ctx , req . GetId (), req . GetItem (). GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
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 ) {
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 15:10:21 -07:00
}
if err := validateSetGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-08-08 19:27:49 -04:00
g , err := s . setMembersInRepo ( ctx , req . GetId (), req . GetItem (). GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
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 ) {
authResults := auth . Verify ( ctx )
2020-08-16 00:42:36 -04:00
if authResults . Error != nil {
return nil , authResults . Error
2020-07-22 15:10:21 -07:00
}
if err := validateRemoveGroupMembersRequest ( req ); err != nil {
return nil , err
}
2020-08-08 19:27:49 -04:00
g , err := s . removeMembersInRepo ( ctx , req . GetId (), req . GetItem (). GetMemberIds (), req . GetVersion ())
2020-07-22 15:10:21 -07:00
if err != nil {
return nil , err
}
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 )
}
return nil , err
}
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 {
return nil , status . Errorf ( codes . Internal , "Unable to build group for creation: %v." , err )
}
repo , err := s . repoFn ()
if err != nil {
return nil , err
}
out , err := repo . CreateGroup ( ctx , u )
if err != nil {
return nil , status . Errorf ( codes . Internal , "Unable to create group: %v." , err )
}
if out == nil {
return nil , status . Error ( codes . Internal , "Unable to create group but no error returned from repository." )
}
2020-07-22 15:10:21 -07:00
return toProto ( out , nil ), nil
2020-06-15 15:22:41 -07:00
}
2020-08-07 12:55:02 -04:00
func ( s Service ) updateInRepo ( ctx context . Context , scopeId , id string , version uint32 , 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-07 12:55:02 -04:00
g , err := iam . NewGroup ( scopeId , opts ... )
2020-06-15 15:22:41 -07:00
if err != nil {
return nil , status . Errorf ( codes . Internal , "Unable to build group for update: %v." , err )
}
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 {
return nil , handlers . InvalidArgumentErrorf ( "No valid fields included in the update mask." , map [ string ] string { "update_mask" : "No valid paths provided in the update mask." })
}
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 {
return nil , status . Errorf ( codes . Internal , "Unable to update group: %v." , err )
}
if rowsUpdated == 0 {
return nil , handlers . NotFoundErrorf ( "Group %q doesn't exist." , id )
}
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
}
return false , status . Errorf ( codes . Internal , "Unable to delete group: %v." , err )
}
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 {
return nil , err
}
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 {
return nil , status . Errorf ( codes . Internal , "Unable to add members to group: %v." , err )
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
return nil , status . Errorf ( codes . Internal , "Unable to look up group: %v." , err )
}
if out == nil {
return nil , status . Error ( codes . Internal , "Unable to lookup group after adding member to it." )
}
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 {
return nil , status . Errorf ( codes . Internal , "Unable to set members on group: %v." , err )
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
return nil , status . Errorf ( codes . Internal , "Unable to look up group: %v." , err )
}
if out == nil {
return nil , status . Error ( codes . Internal , "Unable to lookup group after setting members for it." )
}
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 {
return nil , status . Errorf ( codes . Internal , "Unable to remove members from group: %v." , err )
}
out , m , err := repo . LookupGroup ( ctx , groupId )
if err != nil {
return nil , status . Errorf ( codes . Internal , "Unable to look up group: %v." , err )
}
if out == nil {
return nil , status . Error ( codes . Internal , "Unable to lookup group after removing members from it." )
}
return toProto ( out , m ), nil
}
func toProto ( in * iam . Group , members [] * iam . GroupMember ) * pb . Group {
2020-06-15 15:22:41 -07:00
out := pb . Group {
Id : in . GetPublicId (),
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-07-22 00:14:28 -04:00
badFields := map [ string ] string {}
2020-06-15 15:22:41 -07:00
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
badFields [ "id" ] = "Invalid formatted group id."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Improperly formatted identifier." , badFields )
}
return nil
}
func validateCreateRequest ( req * pbs . CreateGroupRequest ) error {
2020-07-22 00:14:28 -04:00
badFields := map [ string ] string {}
2020-06-15 15:22:41 -07:00
item := req . GetItem ()
if item . GetId () != "" {
badFields [ "id" ] = "This is a read only field."
}
if item . GetCreatedTime () != nil {
badFields [ "created_time" ] = "This is a read only field."
}
if item . GetUpdatedTime () != nil {
badFields [ "updated_time" ] = "This is a read only field."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Argument errors found in the request." , badFields )
}
return nil
}
func validateUpdateRequest ( req * pbs . UpdateGroupRequest ) error {
2020-07-22 00:14:28 -04:00
badFields := map [ string ] string {}
2020-06-15 15:22:41 -07:00
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
badFields [ "group_id" ] = "Improperly formatted path identifier."
}
if req . GetUpdateMask () == nil {
badFields [ "update_mask" ] = "UpdateMask not provided but is required to update a group."
}
2020-08-07 17:56:55 -04:00
if req . GetVersion () == 0 {
badFields [ "version" ] = "Existing resource version is required for an update."
}
2020-06-15 15:22:41 -07:00
item := req . GetItem ()
if item == nil {
// It is legitimate for no item to be specified in an update request as it indicates all fields provided in
// the mask will be marked as unset.
return nil
}
if item . GetId () != "" {
badFields [ "id" ] = "This is a read only field and cannot be specified in an update request."
}
if item . GetCreatedTime () != nil {
badFields [ "created_time" ] = "This is a read only field and cannot be specified in an update request."
}
if item . GetUpdatedTime () != nil {
badFields [ "updated_time" ] = "This is a read only field and cannot be specified in an update request."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateDeleteRequest ( req * pbs . DeleteGroupRequest ) error {
2020-07-22 00:14:28 -04:00
badFields := map [ string ] string {}
2020-06-15 15:22:41 -07:00
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
badFields [ "id" ] = "Incorrectly formatted identifier."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
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-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 {}
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
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-08 19:27:49 -04:00
if len ( req . GetItem (). 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
}
func validateSetGroupMembersRequest ( req * pbs . SetGroupMembersRequest ) error {
badFields := map [ string ] string {}
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
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."
}
if len ( badFields ) > 0 {
return handlers . InvalidArgumentErrorf ( "Errors in provided fields." , badFields )
}
return nil
}
func validateRemoveGroupMembersRequest ( req * pbs . RemoveGroupMembersRequest ) error {
badFields := map [ string ] string {}
if ! validId ( req . GetId (), iam . GroupPrefix + "_" ) {
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-08 19:27:49 -04:00
if len ( req . GetItem (). 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
}
2020-06-15 15:22:41 -07:00
func validId ( id , prefix string ) bool {
if ! strings . HasPrefix ( id , prefix ) {
return false
}
id = strings . TrimPrefix ( id , prefix )
return ! reInvalidID . Match ([] byte ( id ))
}