mirror of
https://gitee.com/milvus-io/milvus.git
synced 2025-12-07 01:28:27 +08:00
Related to #44761 This commit refactors the privilege management system in the proxy component by: 1. **Separation of Concerns**: Extracts privilege-related functionality from MetaCache into a dedicated `internal/proxy/privilege` package, improving code organization and maintainability. 2. **New Package Structure**: Creates `internal/proxy/privilege/` with: - `cache.go`: Core privilege cache implementation (PrivilegeCache) - `result_cache.go`: Privilege enforcement result caching - `model.go`: Casbin model and policy enforcement functions - `meta_cache_adapter.go`: Casbin adapter for MetaCache integration - Corresponding test files and mock implementations 3. **MetaCache Simplification**: Removes privilege and credential management methods from MetaCache interface and implementation: - Removed: GetCredentialInfo, RemoveCredential, UpdateCredential - Removed: GetPrivilegeInfo, GetUserRole, RefreshPolicyInfo, InitPolicyInfo - Deleted: meta_cache_adapter.go, privilege_cache.go and their tests 4. **Updated References**: Updates all callsites to use the new privilegeCache global: - Authentication interceptor now uses privilegeCache for password verification - Credential cache operations (InvalidateCredentialCache, UpdateCredentialCache, UpdateCredential) now use privilegeCache - Policy refresh operations (RefreshPolicyInfoCache) now use privilegeCache - Privilege interceptor uses new privilege.GetEnforcer() and privilege result cache 5. **Improved API**: Renames cache functions for clarity: - GetPrivilegeCache → GetResultCache - SetPrivilegeCache → SetResultCache - CleanPrivilegeCache → CleanResultCache This refactoring makes the codebase more modular, separates privilege management concerns from general metadata caching, and provides a clearer API for privilege enforcement operations. --------- Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
170 lines
5.4 KiB
Go
170 lines
5.4 KiB
Go
package proxy
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"sync"
|
|
|
|
"github.com/casbin/casbin/v2"
|
|
"go.uber.org/zap"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
|
"github.com/milvus-io/milvus/internal/proxy/privilege"
|
|
"github.com/milvus-io/milvus/pkg/v2/log"
|
|
"github.com/milvus-io/milvus/pkg/v2/util"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/contextutil"
|
|
"github.com/milvus-io/milvus/pkg/v2/util/funcutil"
|
|
)
|
|
|
|
type PrivilegeFunc func(ctx context.Context, req interface{}) (context.Context, error)
|
|
|
|
var (
|
|
enforcer *casbin.SyncedEnforcer
|
|
initOnce sync.Once
|
|
initPrivilegeGroupsOnce sync.Once
|
|
)
|
|
|
|
var roPrivileges, rwPrivileges, adminPrivileges map[string]struct{}
|
|
|
|
// UnaryServerInterceptor returns a new unary server interceptors that performs per-request privilege access.
|
|
func UnaryServerInterceptor(privilegeFunc PrivilegeFunc) grpc.UnaryServerInterceptor {
|
|
privilege.InitPrivilegeGroups()
|
|
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
|
newCtx, err := privilegeFunc(ctx, req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return handler(newCtx, req)
|
|
}
|
|
}
|
|
|
|
func PrivilegeInterceptor(ctx context.Context, req interface{}) (context.Context, error) {
|
|
if !Params.CommonCfg.AuthorizationEnabled.GetAsBool() {
|
|
return ctx, nil
|
|
}
|
|
log := log.Ctx(ctx)
|
|
log.RatedDebug(60, "PrivilegeInterceptor", zap.String("type", reflect.TypeOf(req).String()))
|
|
privilegeExt, err := funcutil.GetPrivilegeExtObj(req)
|
|
if err != nil {
|
|
log.RatedInfo(60, "GetPrivilegeExtObj err", zap.Error(err))
|
|
return ctx, nil
|
|
}
|
|
username, password, err := contextutil.GetAuthInfoFromContext(ctx)
|
|
if err != nil {
|
|
log.Warn("GetCurUserFromContext fail", zap.Error(err))
|
|
return ctx, err
|
|
}
|
|
if !Params.CommonCfg.RootShouldBindRole.GetAsBool() && username == util.UserRoot {
|
|
return ctx, nil
|
|
}
|
|
roleNames, err := GetRole(username)
|
|
if err != nil {
|
|
log.Warn("GetRole fail", zap.String("username", username), zap.Error(err))
|
|
return ctx, err
|
|
}
|
|
roleNames = append(roleNames, util.RolePublic)
|
|
objectType := privilegeExt.ObjectType.String()
|
|
objectNameIndex := privilegeExt.ObjectNameIndex
|
|
objectName := funcutil.GetObjectName(req, objectNameIndex)
|
|
if isCurUserObject(objectType, username, objectName) {
|
|
return ctx, nil
|
|
}
|
|
|
|
if isSelectMyRoleGrants(req, roleNames) {
|
|
return ctx, nil
|
|
}
|
|
|
|
objectNameIndexs := privilegeExt.ObjectNameIndexs
|
|
objectNames := funcutil.GetObjectNames(req, objectNameIndexs)
|
|
objectPrivilege := privilegeExt.ObjectPrivilege.String()
|
|
dbName := GetCurDBNameFromContextOrDefault(ctx)
|
|
|
|
log = log.With(zap.String("username", username), zap.Strings("role_names", roleNames),
|
|
zap.String("object_type", objectType), zap.String("object_privilege", objectPrivilege),
|
|
zap.String("db_name", dbName),
|
|
zap.Int32("object_index", objectNameIndex), zap.String("object_name", objectName),
|
|
zap.Int32("object_indexs", objectNameIndexs), zap.Strings("object_names", objectNames))
|
|
|
|
e := privilege.GetEnforcer()
|
|
for _, roleName := range roleNames {
|
|
permitFunc := func(objectName string) (bool, error) {
|
|
object := funcutil.PolicyForResource(dbName, objectType, objectName)
|
|
isPermit, cached, version := privilege.GetResultCache(roleName, object, objectPrivilege)
|
|
if cached {
|
|
return isPermit, nil
|
|
}
|
|
isPermit, err := e.Enforce(roleName, object, objectPrivilege)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
privilege.SetResultCache(roleName, object, objectPrivilege, isPermit, version)
|
|
return isPermit, nil
|
|
}
|
|
|
|
if objectNameIndex != 0 {
|
|
// handle the api which refers one resource
|
|
permitObject, err := permitFunc(objectName)
|
|
if err != nil {
|
|
log.Warn("fail to execute permit func", zap.String("name", objectName), zap.Error(err))
|
|
return ctx, err
|
|
}
|
|
if permitObject {
|
|
return ctx, nil
|
|
}
|
|
}
|
|
|
|
if objectNameIndexs != 0 {
|
|
// handle the api which refers many resources
|
|
permitObjects := true
|
|
for _, name := range objectNames {
|
|
p, err := permitFunc(name)
|
|
if err != nil {
|
|
log.Warn("fail to execute permit func", zap.String("name", name), zap.Error(err))
|
|
return ctx, err
|
|
}
|
|
if !p {
|
|
permitObjects = false
|
|
break
|
|
}
|
|
}
|
|
if permitObjects && len(objectNames) != 0 {
|
|
return ctx, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
log.Info("permission deny", zap.Strings("roles", roleNames))
|
|
|
|
if password == util.PasswordHolder {
|
|
username = "apikey user"
|
|
}
|
|
|
|
return ctx, status.Error(codes.PermissionDenied,
|
|
fmt.Sprintf("%s: permission deny to %s in the `%s` database", objectPrivilege, username, dbName))
|
|
}
|
|
|
|
// isCurUserObject Determine whether it is an Object of type User that operates on its own user information,
|
|
// like updating password or viewing your own role information.
|
|
// make users operate their own user information when the related privileges are not granted.
|
|
func isCurUserObject(objectType string, curUser string, object string) bool {
|
|
if objectType != commonpb.ObjectType_User.String() {
|
|
return false
|
|
}
|
|
return curUser == object
|
|
}
|
|
|
|
func isSelectMyRoleGrants(req interface{}, roleNames []string) bool {
|
|
selectGrantReq, ok := req.(*milvuspb.SelectGrantRequest)
|
|
if !ok {
|
|
return false
|
|
}
|
|
filterGrantEntity := selectGrantReq.GetEntity()
|
|
roleName := filterGrantEntity.GetRole().GetName()
|
|
return funcutil.SliceContain(roleNames, roleName)
|
|
}
|