@@ -23,6 +23,8 @@ import (
2323 "github.com/tigrisdata/tigris/lib/container"
2424 "github.com/tigrisdata/tigris/server/config"
2525 "github.com/tigrisdata/tigris/server/request"
26+ "github.com/tigrisdata/tigris/server/services/v1/auth"
27+ "github.com/tigrisdata/tigris/server/types"
2628 "google.golang.org/grpc"
2729)
2830
@@ -32,12 +34,6 @@ const (
3234)
3335
3436var (
35- // role names.
36- readOnlyRoleName = "ro"
37- editorRoleName = "e"
38- ownerRoleName = "o"
39- ClusterAdminRoleName = "cluster_admin"
40-
4137 adminNamespaces = container .NewHashSet (config .DefaultConfig .Auth .AdminNamespaces ... )
4238 readonlyMethods = container .NewHashSet (
4339 // db
@@ -434,11 +430,12 @@ func authorize(ctx context.Context) (err error) {
434430 Msg ("Empty role allowed for transition purpose" )
435431 return nil
436432 }
437- // if !isAuthorizedProject(reqMetadata, accessToken) {
438- // authorizationErr = errors.PermissionDenied("You are not allowed to perform operation: %s", reqMetadata.GetFullMethod())
439- //}
440433 var authorizationErr error
441- if ! isAuthorizedOperation (reqMetadata .GetFullMethod (), role ) {
434+ if ! isAuthorizedProject (reqMetadata , accessToken ) {
435+ authorizationErr = errors .PermissionDenied ("You are not allowed to perform operation on this project: %s" , reqMetadata .GetFullMethod ())
436+ }
437+
438+ if authorizationErr == nil && ! isAuthorizedOperation (reqMetadata .GetFullMethod (), role ) {
442439 authorizationErr = errors .PermissionDenied ("You are not allowed to perform operation: %s" , reqMetadata .GetFullMethod ())
443440 }
444441
@@ -457,6 +454,17 @@ func authorize(ctx context.Context) (err error) {
457454 return nil
458455}
459456
457+ func isAuthorizedProject (reqMetadata * request.Metadata , accessToken * types.AccessToken ) bool {
458+ if reqMetadata .GetProject () != "" && accessToken .Project != "" && reqMetadata .GetProject () != accessToken .Project {
459+ log .Error ().
460+ Str ("accessible_project" , accessToken .Project ).
461+ Str ("requested_project" , reqMetadata .GetProject ()).
462+ Msg ("Project mismatch" )
463+ return false
464+ }
465+ return true
466+ }
467+
460468func isAuthorizedOperation (method string , role string ) bool {
461469 if methods := getMethodsForRole (role ); methods != nil {
462470 return methods .Contains (method )
@@ -466,21 +474,21 @@ func isAuthorizedOperation(method string, role string) bool {
466474
467475func getMethodsForRole (role string ) * container.HashSet {
468476 switch role {
469- case ClusterAdminRoleName :
477+ case auth . ClusterAdminRoleName :
470478 return & clusterAdminMethods
471- case ownerRoleName :
479+ case auth . OwnerRoleName :
472480 return & ownerMethods
473- case editorRoleName :
481+ case auth . EditorRoleName :
474482 return & editorMethods
475- case readOnlyRoleName :
483+ case auth . ReadOnlyRoleName :
476484 return & readonlyMethods
477485 }
478486 return nil
479487}
480488
481489func getRole (reqMetadata * request.Metadata ) string {
482490 if isAdminNamespace (reqMetadata .GetNamespace ()) {
483- return ClusterAdminRoleName
491+ return auth . ClusterAdminRoleName
484492 }
485493
486494 // empty role check for transition purpose
0 commit comments