2020-07-22 00:14:28 -04:00
package scopes
import (
"context"
"fmt"
"sort"
"strings"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/globals"
2021-04-08 20:54:32 -04:00
"github.com/hashicorp/boundary/internal/auth/oidc"
"github.com/hashicorp/boundary/internal/auth/password"
2021-04-16 12:06:03 -05:00
"github.com/hashicorp/boundary/internal/errors"
2020-08-10 17:15:02 -04:00
pb "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"
2021-01-20 10:53:01 -05:00
"github.com/hashicorp/boundary/internal/perms"
2021-05-04 19:57:35 -05:00
"github.com/hashicorp/boundary/internal/requests"
2021-07-27 09:59:52 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/auth"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller/common"
2021-01-25 19:37:40 -05:00
"github.com/hashicorp/boundary/internal/servers/controller/common/scopeids"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2021-01-20 10:53:01 -05:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authmethods"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/authtokens"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/groups"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/host_catalogs"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/roles"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/sessions"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/targets"
"github.com/hashicorp/boundary/internal/servers/controller/handlers/users"
2020-09-04 14:48:11 -07:00
"github.com/hashicorp/boundary/internal/types/action"
"github.com/hashicorp/boundary/internal/types/resource"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/types/scope"
2020-07-22 00:14:28 -04:00
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/wrapperspb"
)
2021-01-20 10:53:01 -05:00
var (
maskManager handlers . MaskManager
// IdActions contains the set of actions that can be performed on
// individual resources
IdActions = action . ActionSet {
2021-04-23 09:13:37 -05:00
action . NoOp ,
2021-01-20 10:53:01 -05:00
action . Read ,
action . Update ,
action . Delete ,
}
// CollectionActions contains the set of actions that can be performed on
// this collection
CollectionActions = action . ActionSet {
action . Create ,
action . List ,
}
2021-05-04 19:57:35 -05:00
scopeCollectionTypeMapMap = map [ string ] map [ resource . Type ] action . ActionSet {
scope . Global . String (): {
resource . AuthMethod : authmethods . CollectionActions ,
resource . AuthToken : authtokens . CollectionActions ,
resource . Group : groups . CollectionActions ,
resource . Role : roles . CollectionActions ,
resource . Scope : CollectionActions ,
resource . Session : sessions . CollectionActions ,
resource . User : users . CollectionActions ,
},
scope . Org . String (): {
resource . AuthMethod : authmethods . CollectionActions ,
resource . AuthToken : authtokens . CollectionActions ,
resource . Group : groups . CollectionActions ,
resource . Role : roles . CollectionActions ,
resource . Scope : CollectionActions ,
resource . Session : sessions . CollectionActions ,
resource . User : users . CollectionActions ,
},
scope . Project . String (): {
resource . Group : groups . CollectionActions ,
resource . HostCatalog : host_catalogs . CollectionActions ,
resource . Role : roles . CollectionActions ,
resource . Target : targets . CollectionActions ,
},
2021-01-20 10:53:01 -05:00
}
)
2020-07-22 00:14:28 -04:00
func init () {
var err error
2021-06-22 22:26:26 -04:00
if maskManager , err = handlers . NewMaskManager ( handlers . MaskDestination { & store . Scope {}}, handlers . MaskSource { & pb . Scope {}}); err != nil {
2020-07-22 00:14:28 -04:00
panic ( err )
}
}
// Service handles requests as described by the pbs.ScopeServiceServer interface.
type Service struct {
2020-11-11 14:41:05 -05:00
pbs . UnimplementedScopeServiceServer
2020-09-04 14:48:11 -07:00
repoFn common . IamRepoFactory
2020-07-22 00:14:28 -04:00
}
2020-08-10 17:15:02 -04:00
// NewService returns a project service which handles project related requests to boundary.
2020-07-22 00:14:28 -04:00
func NewService ( repo common . IamRepoFactory ) ( Service , error ) {
2021-04-16 12:06:03 -05:00
const op = "scopes.(Service).NewService"
2020-07-22 00:14:28 -04:00
if repo == nil {
2021-04-16 12:06:03 -05:00
return Service {}, errors . New ( errors . InvalidParameter , op , "missing iam repository" )
2020-07-22 00:14:28 -04:00
}
2020-09-04 14:48:11 -07:00
return Service { repoFn : repo }, nil
2020-07-22 00:14:28 -04:00
}
var _ pbs . ScopeServiceServer = Service {}
// ListScopes implements the interface pbs.ScopeServiceServer.
func ( s Service ) ListScopes ( ctx context . Context , req * pbs . ListScopesRequest ) ( * pbs . ListScopesResponse , error ) {
2020-07-22 15:10:44 -04:00
if req . GetScopeId () == "" {
2020-08-12 00:02:26 -04:00
req . ScopeId = scope . Global . String ()
2020-07-22 15:10:44 -04:00
}
2020-07-22 00:14:28 -04:00
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 {
2021-03-24 15:33:04 -04:00
// If it's forbidden, and it's a recursive request, and they're
// successfully authenticated but just not authorized, keep going as we
// may have authorization on downstream scopes.
if authResults . Error == handlers . ForbiddenError () &&
req . GetRecursive () &&
2021-04-13 16:55:40 -05:00
authResults . AuthenticationFinished {
2021-03-24 15:33:04 -04:00
} else {
return nil , authResults . Error
}
2020-09-04 14:48:11 -07:00
}
2021-01-25 19:37:40 -05:00
2021-03-24 15:33:04 -04:00
scopeIds , scopeInfoMap , err := scopeids . GetListingScopeIds (
ctx , s . repoFn , authResults , req . GetScopeId (), resource . Scope , req . GetRecursive (), false )
2021-01-25 19:37:40 -05:00
if err != nil {
return nil , err
}
2021-03-24 15:33:04 -04:00
// If no scopes match, return an empty response
if len ( scopeIds ) == 0 {
return & pbs . ListScopesResponse {}, nil
}
2021-01-25 19:37:40 -05:00
2021-05-04 19:57:35 -05:00
pl , err := s . listFromRepo ( ctx , scopeIds )
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
if len ( pl ) == 0 {
return & pbs . ListScopesResponse {}, nil
}
2021-01-25 19:37:40 -05:00
2021-02-22 10:09:25 -07:00
filter , err := handlers . NewFilter ( req . GetFilter ())
if err != nil {
return nil , err
}
2021-01-20 10:53:01 -05:00
finalItems := make ([] * pb . Scope , 0 , len ( pl ))
2021-05-04 19:57:35 -05:00
res := perms . Resource {
2021-01-25 19:37:40 -05:00
Type : resource . Scope ,
2021-01-20 10:53:01 -05:00
}
2020-07-22 00:14:28 -04:00
for _ , item := range pl {
2021-05-04 19:57:35 -05:00
res . Id = item . GetPublicId ()
res . ScopeId = item . GetParentId ()
authorizedActions := authResults . FetchActionSetForId ( ctx , item . GetPublicId (), IdActions , auth . WithResource ( & res )). Strings ()
if len ( authorizedActions ) == 0 {
2021-02-22 10:09:25 -07:00
continue
}
2021-05-04 19:57:35 -05:00
outputFields := authResults . FetchOutputFields ( res , action . List ). SelfOrDefaults ( authResults . UserId )
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( scopeInfoMap [ item . GetParentId ()]))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authorizedActions ))
}
if outputFields . Has ( globals . AuthorizedCollectionActionsField ) {
collectionActions , err := auth . CalculateAuthorizedCollectionActions ( ctx , authResults , scopeCollectionTypeMapMap [ item . Type ], item . GetPublicId (), "" )
if err != nil {
2021-01-20 10:53:01 -05:00
return nil , err
}
2021-05-04 19:57:35 -05:00
outputOpts = append ( outputOpts , handlers . WithAuthorizedCollectionActions ( collectionActions ))
2021-01-20 10:53:01 -05:00
}
2020-07-22 00:14:28 -04:00
2021-05-04 19:57:35 -05:00
item , err := ToProto ( ctx , item , outputOpts ... )
if err != nil {
return nil , err
}
if filter . Match ( item ) {
finalItems = append ( finalItems , item )
}
}
SortScopes ( finalItems )
2021-01-20 10:53:01 -05:00
return & pbs . ListScopesResponse { Items : finalItems }, nil
2020-07-22 00:14:28 -04:00
}
// GetScopes implements the interface pbs.ScopeServiceServer.
func ( s Service ) GetScope ( ctx context . Context , req * pbs . GetScopeRequest ) ( * pbs . GetScopeResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "scopes.(Service).GetScope"
2020-07-22 00:14:28 -04:00
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-07-22 00:14:28 -04:00
p , err := s . getFromRepo ( ctx , req . GetId ())
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
return nil , errors . New ( errors . Internal , op , "no request context found" )
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
2021-01-20 10:53:01 -05:00
act := IdActions
// Can't delete global so elide it
2021-05-04 19:57:35 -05:00
if p . GetPublicId () == "global" {
act = act [ 0 : 3 ]
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , p . GetPublicId (), act ). Strings ()))
2021-01-20 10:53:01 -05:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . AuthorizedCollectionActionsField ) {
collectionActions , err := auth . CalculateAuthorizedCollectionActions ( ctx , authResults , scopeCollectionTypeMapMap [ p . Type ], p . GetPublicId (), "" )
if err != nil {
return nil , err
}
outputOpts = append ( outputOpts , handlers . WithAuthorizedCollectionActions ( collectionActions ))
}
item , err := ToProto ( ctx , p , outputOpts ... )
if err != nil {
2021-01-20 10:53:01 -05:00
return nil , err
}
2021-05-04 19:57:35 -05:00
return & pbs . GetScopeResponse { Item : item }, nil
2020-07-22 00:14:28 -04:00
}
// CreateScope implements the interface pbs.ScopeServiceServer.
func ( s Service ) CreateScope ( ctx context . Context , req * pbs . CreateScopeRequest ) ( * pbs . CreateScopeResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "scopes.(Service).CreateScope"
2020-09-03 08:02:30 -07:00
if err := validateCreateRequest ( req ); err != nil {
return nil , err
2020-07-22 15:10:44 -04:00
}
2020-09-04 14:48:11 -07:00
authResults := s . authResult ( ctx , req . GetItem (). GetScopeId (), action . Create )
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-08-26 23:27:25 -04:00
p , err := s . createInRepo ( ctx , authResults , req )
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
return nil , errors . New ( errors . Internal , op , "no request context found" )
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , p . GetPublicId (), IdActions ). Strings ()))
}
if outputFields . Has ( globals . AuthorizedCollectionActionsField ) {
collectionActions , err := auth . CalculateAuthorizedCollectionActions ( ctx , authResults , scopeCollectionTypeMapMap [ p . Type ], p . GetPublicId (), "" )
if err != nil {
return nil , err
}
outputOpts = append ( outputOpts , handlers . WithAuthorizedCollectionActions ( collectionActions ))
}
item , err := ToProto ( ctx , p , outputOpts ... )
if err != nil {
2021-01-20 10:53:01 -05:00
return nil , err
}
2021-05-04 19:57:35 -05:00
return & pbs . CreateScopeResponse { Item : item , Uri : fmt . Sprintf ( "scopes/%s" , item . GetId ())}, nil
2020-07-22 00:14:28 -04:00
}
// UpdateScope implements the interface pbs.ScopeServiceServer.
func ( s Service ) UpdateScope ( ctx context . Context , req * pbs . UpdateScopeRequest ) ( * pbs . UpdateScopeResponse , error ) {
2021-05-04 19:57:35 -05:00
const op = "scopes.(Service).UpdateScope"
2020-07-22 00:14:28 -04:00
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
p , err := s . updateInRepo ( ctx , authResults . Scope , req . GetId (), req . GetUpdateMask (). GetPaths (), req . GetItem ())
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
outputFields , ok := requests . OutputFields ( ctx )
if ! ok {
return nil , errors . New ( errors . Internal , op , "no request context found" )
}
outputOpts := make ([] handlers . Option , 0 , 3 )
outputOpts = append ( outputOpts , handlers . WithOutputFields ( & outputFields ))
if outputFields . Has ( globals . ScopeField ) {
outputOpts = append ( outputOpts , handlers . WithScope ( authResults . Scope ))
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
outputOpts = append ( outputOpts , handlers . WithAuthorizedActions ( authResults . FetchActionSetForId ( ctx , p . GetPublicId (), IdActions ). Strings ()))
}
if outputFields . Has ( globals . AuthorizedCollectionActionsField ) {
collectionActions , err := auth . CalculateAuthorizedCollectionActions ( ctx , authResults , scopeCollectionTypeMapMap [ p . Type ], p . GetPublicId (), "" )
if err != nil {
return nil , err
}
outputOpts = append ( outputOpts , handlers . WithAuthorizedCollectionActions ( collectionActions ))
}
item , err := ToProto ( ctx , p , outputOpts ... )
if err != nil {
2021-01-20 10:53:01 -05:00
return nil , err
}
2021-05-04 19:57:35 -05:00
return & pbs . UpdateScopeResponse { Item : item }, nil
2020-07-22 00:14:28 -04:00
}
// DeleteScope implements the interface pbs.ScopeServiceServer.
func ( s Service ) DeleteScope ( ctx context . Context , req * pbs . DeleteScopeRequest ) ( * pbs . DeleteScopeResponse , 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-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-04-26 19:38:55 -05:00
return nil , nil
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) getFromRepo ( ctx context . Context , id string ) ( * iam . Scope , error ) {
2020-09-04 14:48:11 -07:00
repo , err := s . repoFn ()
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
out , err := repo . LookupScope ( ctx , id )
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-05-04 19:57:35 -05:00
if out == nil {
2020-07-22 00:14:28 -04:00
return nil , handlers . NotFoundErrorf ( "Scope %q doesn't exist." , id )
}
2021-05-04 19:57:35 -05:00
return out , nil
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) createInRepo ( ctx context . Context , authResults auth . VerifyResults , req * pbs . CreateScopeRequest ) ( * iam . Scope , error ) {
2021-04-16 12:06:03 -05:00
const op = "scopes.(Service).createInRepo"
2020-08-26 23:27:25 -04:00
item := req . GetItem ()
2020-07-22 00:14:28 -04: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-09-29 20:38:16 -04:00
opts = append ( opts , iam . WithSkipAdminRoleCreation ( req . GetSkipAdminRoleCreation ()))
opts = append ( opts , iam . WithSkipDefaultRoleCreation ( req . GetSkipDefaultRoleCreation ()))
2020-07-22 00:14:28 -04:00
2020-08-13 17:39:48 -04:00
parentScope := authResults . Scope
2020-07-22 00:14:28 -04:00
var iamScope * iam . Scope
var err error
switch parentScope . GetType () {
case scope . Global . String ():
iamScope , err = iam . NewOrg ( opts ... )
case scope . Org . String ():
iamScope , err = iam . NewProject ( parentScope . GetId (), opts ... )
}
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build new scope for creation: %v." , err )
2020-07-22 00:14:28 -04:00
}
2020-09-04 14:48:11 -07:00
repo , err := s . repoFn ()
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2020-08-26 23:27:25 -04:00
out , err := repo . CreateScope ( ctx , iamScope , authResults . UserId , opts ... )
2020-07-22 00:14:28 -04:00
if err != nil {
2021-04-16 12:06:03 -05:00
return nil , errors . Wrap ( err , op , errors . WithMsg ( "unable to create scope" ))
2020-07-22 00:14:28 -04:00
}
if out == nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to create scope but no error returned from repository." )
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
return out , nil
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
func ( s Service ) updateInRepo ( ctx context . Context , parentScope * pb . ScopeInfo , scopeId string , mask [] string , item * pb . Scope ) ( * iam . Scope , error ) {
2021-04-16 12:06:03 -05:00
const op = "scope.(Service).updateInRepo"
2020-07-22 00:14:28 -04:00
var opts [] iam . Option
2021-04-08 20:54:32 -04:00
var scopeDesc , scopeName , scopePrimaryAuthMethodId string
2020-07-22 00:14:28 -04:00
if desc := item . GetDescription (); desc != nil {
2021-04-08 20:54:32 -04:00
scopeDesc = desc . GetValue ()
opts = append ( opts , iam . WithDescription ( scopeDesc ))
2020-07-22 00:14:28 -04:00
}
if name := item . GetName (); name != nil {
2021-04-08 20:54:32 -04:00
scopeName = name . GetValue ()
opts = append ( opts , iam . WithName ( scopeName ))
}
if primaryAuthMethodId := item . GetPrimaryAuthMethodId (); primaryAuthMethodId != nil {
if ! handlers . ValidId ( handlers . Id ( primaryAuthMethodId . GetValue ()), password . AuthMethodPrefix , oidc . AuthMethodPrefix ) {
return nil , handlers . InvalidArgumentErrorf ( "Error in provided request." , map [ string ] string { "primary_auth_method_id" : "Improperly formatted identifier" })
}
scopePrimaryAuthMethodId = primaryAuthMethodId . GetValue ()
opts = append ( opts , iam . WithPrimaryAuthMethodId ( scopePrimaryAuthMethodId ))
2020-07-22 00:14:28 -04:00
}
2020-08-18 17:38:38 -07:00
version := item . GetVersion ()
2020-07-22 00:14:28 -04:00
var iamScope * iam . Scope
var err error
2021-04-08 20:54:32 -04:00
switch {
case scopeId == scope . Global . String ():
// boundary does not allow you to create a new global scope, so
// we'll build the required scope by hand for the update.
s := iam . AllocScope ()
s . PublicId = scopeId
iamScope = & s
iamScope . Description = scopeDesc
iamScope . Name = scopeName
iamScope . PrimaryAuthMethodId = scopePrimaryAuthMethodId
case parentScope . GetType () == scope . Global . String ():
2020-07-22 00:14:28 -04:00
iamScope , err = iam . NewOrg ( opts ... )
2021-04-08 20:54:32 -04:00
case parentScope . GetType () == scope . Org . String ():
2020-07-22 00:14:28 -04:00
iamScope , err = iam . NewProject ( parentScope . GetId (), opts ... )
}
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to build scope for update: %v." , err )
2020-07-22 00:14:28 -04:00
}
iamScope . PublicId = scopeId
dbMask := maskManager . Translate ( mask )
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-07-22 00:14:28 -04:00
}
2020-09-04 14:48:11 -07:00
repo , err := s . repoFn ()
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2020-08-07 12:55:02 -04:00
out , rowsUpdated , err := repo . UpdateScope ( ctx , iamScope , version , dbMask )
2020-07-22 00:14:28 -04:00
if err != nil {
2021-04-16 12:06:03 -05:00
return nil , errors . Wrap ( err , op , errors . WithMsg ( "unable to update project" ))
2020-07-22 00:14:28 -04:00
}
if rowsUpdated == 0 {
2020-09-25 13:41:20 -07:00
return nil , handlers . NotFoundErrorf ( "Scope %q doesn't exist or incorrect version provided." , scopeId )
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
return out , nil
2020-07-22 00:14:28 -04:00
}
func ( s Service ) deleteFromRepo ( ctx context . Context , scopeId string ) ( bool , error ) {
2021-04-16 12:06:03 -05:00
const op = "scope.(Service).deleteFromRepo"
2020-09-04 14:48:11 -07:00
repo , err := s . repoFn ()
2020-07-22 00:14:28 -04:00
if err != nil {
return false , err
}
rows , err := repo . DeleteScope ( ctx , scopeId )
if err != nil {
2021-04-16 12:06:03 -05:00
return false , errors . Wrap ( err , op , errors . WithMsg ( "unable to delete scope" ))
2020-07-22 00:14:28 -04:00
}
return rows > 0 , nil
}
func SortScopes ( scps [] * pb . Scope ) {
// We stable sort here even though the database may not return things in
// sorted order, still nice to have them as consistent as possible.
sort . SliceStable ( scps , func ( i , j int ) bool {
return scps [ i ]. GetId () < scps [ j ]. GetId ()
})
}
2021-05-04 19:57:35 -05:00
func ( s Service ) listFromRepo ( ctx context . Context , scopeIds [] string ) ([] * iam . Scope , error ) {
2020-09-04 14:48:11 -07:00
repo , err := s . repoFn ()
2020-07-22 00:14:28 -04:00
if err != nil {
return nil , err
}
2021-01-25 19:37:40 -05:00
scps , err := repo . ListScopes ( ctx , scopeIds )
2020-07-22 00:14:28 -04:00
if err != nil {
2020-09-25 13:41:20 -07:00
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "Unable to list scopes: %v" , err )
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
return scps , nil
2020-07-22 00:14:28 -04:00
}
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 . Scope ), auth . WithAction ( a )}
switch a {
case action . List , action . Create :
parentId = id
s , err := repo . LookupScope ( ctx , parentId )
if err != nil {
res . Error = err
return res
}
if s == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return res
}
default :
s , err := repo . LookupScope ( ctx , id )
if err != nil {
res . Error = err
return res
}
if s == nil {
2020-09-08 23:57:26 -04:00
res . Error = handlers . NotFoundError ()
2020-09-04 14:48:11 -07:00
return res
}
parentId = s . GetParentId ()
opts = append ( opts , auth . WithId ( id ))
}
opts = append ( opts , auth . WithScopeId ( parentId ))
return auth . Verify ( ctx , opts ... )
}
2021-05-04 19:57:35 -05:00
func ToProto ( ctx context . Context , in * iam . Scope , opt ... handlers . Option ) ( * pb . Scope , error ) {
opts := handlers . GetOpts ( opt ... )
if opts . WithOutputFields == nil {
return nil , handlers . ApiErrorWithCodeAndMessage ( codes . Internal , "output fields not found when building scope proto" )
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
outputFields := * opts . WithOutputFields
out := pb . Scope {}
if outputFields . Has ( globals . IdField ) {
out . Id = in . GetPublicId ()
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . ScopeIdField ) {
out . ScopeId = in . GetParentId ()
2020-07-22 00:14:28 -04:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . TypeField ) {
out . Type = in . GetType ()
2021-04-08 20:54:32 -04:00
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . DescriptionField ) && in . GetDescription () != "" {
out . Description = wrapperspb . String ( in . GetDescription ())
}
if outputFields . Has ( globals . NameField ) && in . GetName () != "" {
out . Name = wrapperspb . String ( in . GetName ())
}
if outputFields . Has ( globals . CreatedTimeField ) {
2021-04-14 16:19:02 -05:00
out . CreatedTime = in . GetCreateTime (). GetTimestamp ()
2021-05-04 19:57:35 -05:00
}
if outputFields . Has ( globals . UpdatedTimeField ) {
2021-04-14 16:19:02 -05:00
out . UpdatedTime = in . GetUpdateTime (). GetTimestamp ()
2021-05-04 19:57:35 -05:00
}
if outputFields . Has ( globals . VersionField ) {
2021-04-14 16:19:02 -05:00
out . Version = in . GetVersion ()
}
2021-05-04 19:57:35 -05:00
if outputFields . Has ( globals . ScopeField ) {
out . Scope = opts . WithScope
}
if outputFields . Has ( globals . AuthorizedActionsField ) {
out . AuthorizedActions = opts . WithAuthorizedActions
}
if outputFields . Has ( globals . AuthorizedCollectionActionsField ) {
out . AuthorizedCollectionActions = opts . WithAuthorizedCollectionActions
}
if outputFields . Has ( globals . PrimaryAuthMethodId ) && in . GetPrimaryAuthMethodId () != "" {
out . PrimaryAuthMethodId = & wrapperspb . StringValue { Value : in . GetPrimaryAuthMethodId ()}
}
2021-04-08 20:54:32 -04:00
2021-05-04 19:57:35 -05:00
return & out , nil
2020-07-22 00:14:28 -04:00
}
// 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 . GetScopeRequest ) error {
badFields := map [ string ] string {}
id := req . GetId ()
switch {
case id == "global" :
case strings . HasPrefix ( id , scope . Org . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Org . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
case strings . HasPrefix ( id , scope . Project . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Project . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
default :
badFields [ "id" ] = "Invalidly formatted scope id."
}
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
return nil
}
func validateCreateRequest ( req * pbs . CreateScopeRequest ) error {
badFields := map [ string ] string {}
item := req . GetItem ()
2020-09-04 14:48:11 -07:00
if item . GetScopeId () == "" {
2020-09-08 16:04:19 -07:00
badFields [ "scope_id" ] = "Missing value for scope_id."
}
switch item . GetType () {
case scope . Global . String ():
badFields [ "type" ] = "Cannot create a global scope."
case scope . Org . String ():
if ! strings . EqualFold ( scope . Global . String (), item . GetScopeId ()) {
badFields [ "type" ] = "Org scopes can only be created under the global scope."
}
case scope . Project . String ():
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( item . GetScopeId ()), scope . Org . Prefix ()) {
2020-09-08 16:04:19 -07:00
badFields [ "type" ] = "Project scopes can only be created under an org scope."
}
2020-09-03 08:02:30 -07:00
}
2020-07-22 00:14:28 -04:00
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."
}
2020-08-18 17:38:38 -07:00
if item . GetVersion () != 0 {
badFields [ "version" ] = "This cannot be specified at create time."
}
2020-07-22 00:14:28 -04:00
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
return nil
}
func validateUpdateRequest ( req * pbs . UpdateScopeRequest ) error {
badFields := map [ string ] string {}
id := req . GetId ()
switch {
case id == "global" :
case strings . HasPrefix ( id , scope . Org . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Org . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
2020-09-08 16:04:19 -07:00
if req . GetItem (). GetType () != "" && ! strings . EqualFold ( scope . Org . String (), req . GetItem (). GetType ()) {
badFields [ "type" ] = "Cannot modify the resource type."
}
2020-07-22 00:14:28 -04:00
case strings . HasPrefix ( id , scope . Project . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Project . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
2020-09-08 16:04:19 -07:00
if req . GetItem (). GetType () != "" && ! strings . EqualFold ( scope . Project . String (), req . GetItem (). GetType ()) {
badFields [ "type" ] = "Cannot modify the resource type."
}
2020-07-22 00:14:28 -04:00
default :
badFields [ "id" ] = "Invalidly formatted scope id."
}
if req . GetUpdateMask () == nil {
badFields [ "update_mask" ] = "UpdateMask not provided but is required to update a project."
}
item := req . GetItem ()
if item == nil {
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
// 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
}
2020-08-18 17:38:38 -07:00
if item . GetVersion () == 0 {
badFields [ "version" ] = "Existing resource version is required for an update."
}
2020-07-22 00:14:28 -04:00
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."
}
2021-04-08 20:54:32 -04:00
if item . GetPrimaryAuthMethodId (). GetValue () != "" && ! handlers . ValidId ( handlers . Id ( item . GetPrimaryAuthMethodId (). GetValue ()), password . AuthMethodPrefix , oidc . AuthMethodPrefix ) {
badFields [ "primary_auth_method_id" ] = "Improperly formatted identifier."
}
2020-07-22 00:14:28 -04:00
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
return nil
}
func validateDeleteRequest ( req * pbs . DeleteScopeRequest ) error {
badFields := map [ string ] string {}
id := req . GetId ()
switch {
case id == "global" :
badFields [ "id" ] = "Invalid to delete the global scope."
case strings . HasPrefix ( id , scope . Org . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Org . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
case strings . HasPrefix ( id , scope . Project . Prefix ()):
2021-04-08 20:54:32 -04:00
if ! handlers . ValidId ( handlers . Id ( id ), scope . Project . Prefix ()) {
2020-07-22 00:14:28 -04:00
badFields [ "id" ] = "Invalidly formatted scope id."
}
default :
badFields [ "id" ] = "Invalidly formatted scope id."
}
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
return nil
}
func validateListRequest ( req * pbs . ListScopesRequest ) error {
badFields := map [ string ] string {}
2021-04-08 20:54:32 -04:00
if req . GetScopeId () != scope . Global . String () && ! handlers . ValidId ( handlers . Id ( req . GetScopeId ()), scope . Org . Prefix ()) {
2020-10-07 17:43:12 -07:00
badFields [ "scope_id" ] = "Must be 'global' or a valid org scope id when listing."
}
2021-02-22 10:09:25 -07:00
if _ , err := handlers . NewFilter ( req . GetFilter ()); err != nil {
badFields [ "filter" ] = fmt . Sprintf ( "This field could not be parsed. %v" , err )
}
2020-07-22 00:14:28 -04:00
if len ( badFields ) > 0 {
2020-09-25 13:41:20 -07:00
return handlers . InvalidArgumentErrorf ( "Error in provided request." , badFields )
2020-07-22 00:14:28 -04:00
}
return nil
}