Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta release #1268

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@
"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 @@
)

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 @@
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())
}

Check warning on line 436 in server/middleware/authz.go

View check run for this annotation

Codecov / codecov/patch

server/middleware/authz.go#L435-L436

Added lines #L435 - L436 were not covered by tests

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 @@
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
}

Check warning on line 464 in server/middleware/authz.go

View check run for this annotation

Codecov / codecov/patch

server/middleware/authz.go#L459-L464

Added lines #L459 - L464 were not covered by tests
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 getMethodsForRole(role string) *container.HashSet {
switch role {
case ClusterAdminRoleName:
case auth.ClusterAdminRoleName:

Check warning on line 477 in server/middleware/authz.go

View check run for this annotation

Codecov / codecov/patch

server/middleware/authz.go#L477

Added line #L477 was not covered by tests
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

Check warning on line 491 in server/middleware/authz.go

View check run for this annotation

Codecov / codecov/patch

server/middleware/authz.go#L491

Added line #L491 was not covered by tests
}

// empty role check for transition purpose
Expand Down