Skip to content

Commit

Permalink
Merge pull request #1268 from tigrisdata/main
Browse files Browse the repository at this point in the history
Beta release
  • Loading branch information
JigarJoshi committed Jun 8, 2023
2 parents 9dbae00 + b1a26b1 commit 61e03f4
Show file tree
Hide file tree
Showing 6 changed files with 488 additions and 366 deletions.
38 changes: 23 additions & 15 deletions server/middleware/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"github.com/tigrisdata/tigris/lib/container"
"github.com/tigrisdata/tigris/server/config"
"github.com/tigrisdata/tigris/server/request"
"github.com/tigrisdata/tigris/server/services/v1/auth"
"github.com/tigrisdata/tigris/server/types"
"google.golang.org/grpc"
)

Expand All @@ -32,12 +34,6 @@ const (
)

var (
// role names.
readOnlyRoleName = "ro"
editorRoleName = "e"
ownerRoleName = "o"
ClusterAdminRoleName = "cluster_admin"

adminNamespaces = container.NewHashSet(config.DefaultConfig.Auth.AdminNamespaces...)
readonlyMethods = container.NewHashSet(
// db
Expand Down Expand Up @@ -434,11 +430,12 @@ func authorize(ctx context.Context) (err error) {
Msg("Empty role allowed for transition purpose")
return nil
}
// if !isAuthorizedProject(reqMetadata, accessToken) {
// authorizationErr = errors.PermissionDenied("You are not allowed to perform operation: %s", reqMetadata.GetFullMethod())
//}
var authorizationErr error
if !isAuthorizedOperation(reqMetadata.GetFullMethod(), role) {
if !isAuthorizedProject(reqMetadata, accessToken) {
authorizationErr = errors.PermissionDenied("You are not allowed to perform operation on this project: %s", reqMetadata.GetFullMethod())
}

if authorizationErr == nil && !isAuthorizedOperation(reqMetadata.GetFullMethod(), role) {
authorizationErr = errors.PermissionDenied("You are not allowed to perform operation: %s", reqMetadata.GetFullMethod())
}

Expand All @@ -457,6 +454,17 @@ func authorize(ctx context.Context) (err error) {
return nil
}

func isAuthorizedProject(reqMetadata *request.Metadata, accessToken *types.AccessToken) bool {
if reqMetadata.GetProject() != "" && accessToken.Project != "" && reqMetadata.GetProject() != accessToken.Project {
log.Error().
Str("accessible_project", accessToken.Project).
Str("requested_project", reqMetadata.GetProject()).
Msg("Project mismatch")
return false
}
return true
}

func isAuthorizedOperation(method string, role string) bool {
if methods := getMethodsForRole(role); methods != nil {
return methods.Contains(method)
Expand All @@ -466,21 +474,21 @@ func isAuthorizedOperation(method string, role string) bool {

func getMethodsForRole(role string) *container.HashSet {
switch role {
case ClusterAdminRoleName:
case auth.ClusterAdminRoleName:
return &clusterAdminMethods
case ownerRoleName:
case auth.OwnerRoleName:
return &ownerMethods
case editorRoleName:
case auth.EditorRoleName:
return &editorMethods
case readOnlyRoleName:
case auth.ReadOnlyRoleName:
return &readonlyMethods
}
return nil
}

func getRole(reqMetadata *request.Metadata) string {
if isAdminNamespace(reqMetadata.GetNamespace()) {
return ClusterAdminRoleName
return auth.ClusterAdminRoleName
}

// empty role check for transition purpose
Expand Down
Loading

0 comments on commit 61e03f4

Please sign in to comment.