2022-08-04 11:04:34 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strings"
|
2023-12-12 10:36:43 +08:00
|
|
|
"sync"
|
2022-08-04 11:04:34 +08:00
|
|
|
|
|
|
|
"github.com/casbin/casbin/v2"
|
|
|
|
"github.com/casbin/casbin/v2/model"
|
2024-08-16 14:18:54 +08:00
|
|
|
"github.com/samber/lo"
|
2022-08-04 11:04:34 +08:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc"
|
2022-12-08 19:43:23 +08:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-06-25 17:20:43 +08:00
|
|
|
|
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/commonpb"
|
2024-01-08 15:10:49 +08:00
|
|
|
"github.com/milvus-io/milvus-proto/go-api/v2/milvuspb"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/log"
|
|
|
|
"github.com/milvus-io/milvus/pkg/util"
|
2024-04-16 16:47:19 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/contextutil"
|
2023-06-25 17:20:43 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/funcutil"
|
2024-08-16 14:18:54 +08:00
|
|
|
"github.com/milvus-io/milvus/pkg/util/paramtable"
|
2022-08-04 11:04:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type PrivilegeFunc func(ctx context.Context, req interface{}) (context.Context, error)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// sub -> role name, like admin, public
|
|
|
|
// obj -> contact object with object name, like Global-*, Collection-col1
|
|
|
|
// act -> privilege, like CreateCollection, DescribeCollection
|
|
|
|
ModelStr = `
|
|
|
|
[request_definition]
|
|
|
|
r = sub, obj, act
|
|
|
|
|
|
|
|
[policy_definition]
|
|
|
|
p = sub, obj, act
|
|
|
|
|
|
|
|
[policy_effect]
|
|
|
|
e = some(where (p.eft == allow))
|
|
|
|
|
|
|
|
[matchers]
|
2024-09-05 15:35:04 +08:00
|
|
|
m = r.sub == p.sub && globMatch(r.obj, p.obj) && globMatch(r.act, p.act) || r.sub == "admin" || (r.sub == p.sub && dbMatch(r.obj, p.obj) && privilegeGroupContains(r.act, p.act, r.obj, p.obj))
|
2022-08-04 11:04:34 +08:00
|
|
|
`
|
|
|
|
)
|
|
|
|
|
2022-12-14 10:29:22 +08:00
|
|
|
var templateModel = getPolicyModel(ModelStr)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
2023-12-12 10:36:43 +08:00
|
|
|
var (
|
2024-08-16 14:18:54 +08:00
|
|
|
enforcer *casbin.SyncedEnforcer
|
|
|
|
initOnce sync.Once
|
|
|
|
initPrivilegeGroupsOnce sync.Once
|
2023-12-12 10:36:43 +08:00
|
|
|
)
|
|
|
|
|
2024-08-16 14:18:54 +08:00
|
|
|
var roPrivileges, rwPrivileges, adminPrivileges map[string]struct{}
|
|
|
|
|
|
|
|
func initPrivilegeGroups() {
|
|
|
|
initPrivilegeGroupsOnce.Do(func() {
|
|
|
|
roGroup := paramtable.Get().CommonCfg.ReadOnlyPrivileges.GetAsStrings()
|
|
|
|
if len(roGroup) == 0 {
|
|
|
|
roGroup = util.ReadOnlyPrivilegeGroup
|
|
|
|
}
|
|
|
|
roPrivileges = lo.SliceToMap(roGroup, func(item string) (string, struct{}) { return item, struct{}{} })
|
|
|
|
|
|
|
|
rwGroup := paramtable.Get().CommonCfg.ReadWritePrivileges.GetAsStrings()
|
|
|
|
if len(rwGroup) == 0 {
|
|
|
|
rwGroup = util.ReadWritePrivilegeGroup
|
|
|
|
}
|
|
|
|
rwPrivileges = lo.SliceToMap(rwGroup, func(item string) (string, struct{}) { return item, struct{}{} })
|
|
|
|
|
|
|
|
adminGroup := paramtable.Get().CommonCfg.AdminPrivileges.GetAsStrings()
|
|
|
|
if len(adminGroup) == 0 {
|
|
|
|
adminGroup = util.AdminPrivilegeGroup
|
|
|
|
}
|
|
|
|
adminPrivileges = lo.SliceToMap(adminGroup, func(item string) (string, struct{}) { return item, struct{}{} })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:36:43 +08:00
|
|
|
func getEnforcer() *casbin.SyncedEnforcer {
|
|
|
|
initOnce.Do(func() {
|
|
|
|
e, err := casbin.NewSyncedEnforcer()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic("failed to create casbin enforcer", zap.Error(err))
|
|
|
|
}
|
|
|
|
casbinModel := getPolicyModel(ModelStr)
|
|
|
|
adapter := NewMetaCacheCasbinAdapter(func() Cache { return globalMetaCache })
|
|
|
|
e.InitWithModelAndAdapter(casbinModel, adapter)
|
|
|
|
e.AddFunction("dbMatch", DBMatchFunc)
|
2024-08-16 14:18:54 +08:00
|
|
|
e.AddFunction("privilegeGroupContains", PrivilegeGroupContains)
|
2023-12-12 10:36:43 +08:00
|
|
|
enforcer = e
|
|
|
|
})
|
|
|
|
return enforcer
|
|
|
|
}
|
|
|
|
|
2022-12-14 10:29:22 +08:00
|
|
|
func getPolicyModel(modelString string) model.Model {
|
|
|
|
m, err := model.NewModelFromString(modelString)
|
2022-08-04 11:04:34 +08:00
|
|
|
if err != nil {
|
2022-12-08 19:43:23 +08:00
|
|
|
log.Panic("NewModelFromString fail", zap.String("model", ModelStr), zap.Error(err))
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
2022-12-14 10:29:22 +08:00
|
|
|
return m
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnaryServerInterceptor returns a new unary server interceptors that performs per-request privilege access.
|
|
|
|
func UnaryServerInterceptor(privilegeFunc PrivilegeFunc) grpc.UnaryServerInterceptor {
|
2024-08-16 14:18:54 +08:00
|
|
|
initPrivilegeGroups()
|
2023-11-28 15:32:31 +08:00
|
|
|
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
|
2022-08-04 11:04:34 +08:00
|
|
|
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) {
|
2022-12-07 18:01:19 +08:00
|
|
|
if !Params.CommonCfg.AuthorizationEnabled.GetAsBool() {
|
2022-08-04 11:04:34 +08:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2023-12-12 10:36:43 +08:00
|
|
|
log := log.Ctx(ctx)
|
2024-04-09 16:19:17 +08:00
|
|
|
log.RatedDebug(60, "PrivilegeInterceptor", zap.String("type", reflect.TypeOf(req).String()))
|
2022-08-04 11:04:34 +08:00
|
|
|
privilegeExt, err := funcutil.GetPrivilegeExtObj(req)
|
|
|
|
if err != nil {
|
2024-04-09 16:19:17 +08:00
|
|
|
log.RatedInfo(60, "GetPrivilegeExtObj err", zap.Error(err))
|
2022-08-04 11:04:34 +08:00
|
|
|
return ctx, nil
|
|
|
|
}
|
2024-04-16 16:47:19 +08:00
|
|
|
username, password, err := contextutil.GetAuthInfoFromContext(ctx)
|
2022-08-04 11:04:34 +08:00
|
|
|
if err != nil {
|
2023-02-13 16:50:33 +08:00
|
|
|
log.Warn("GetCurUserFromContext fail", zap.Error(err))
|
2022-08-04 11:04:34 +08:00
|
|
|
return ctx, err
|
|
|
|
}
|
2022-08-15 16:40:48 +08:00
|
|
|
if username == util.UserRoot {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
2022-08-04 11:04:34 +08:00
|
|
|
roleNames, err := GetRole(username)
|
|
|
|
if err != nil {
|
2023-02-13 16:50:33 +08:00
|
|
|
log.Warn("GetRole fail", zap.String("username", username), zap.Error(err))
|
2022-08-04 11:04:34 +08:00
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
roleNames = append(roleNames, util.RolePublic)
|
2022-08-05 16:28:35 +08:00
|
|
|
objectType := privilegeExt.ObjectType.String()
|
|
|
|
objectNameIndex := privilegeExt.ObjectNameIndex
|
|
|
|
objectName := funcutil.GetObjectName(req, objectNameIndex)
|
|
|
|
if isCurUserObject(objectType, username, objectName) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
2024-01-08 15:10:49 +08:00
|
|
|
|
|
|
|
if isSelectMyRoleGrants(req, roleNames) {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 16:28:35 +08:00
|
|
|
objectNameIndexs := privilegeExt.ObjectNameIndexs
|
|
|
|
objectNames := funcutil.GetObjectNames(req, objectNameIndexs)
|
|
|
|
objectPrivilege := privilegeExt.ObjectPrivilege.String()
|
2023-06-25 17:20:43 +08:00
|
|
|
dbName := GetCurDBNameFromContextOrDefault(ctx)
|
2022-08-04 11:04:34 +08:00
|
|
|
|
2023-12-12 10:36:43 +08:00
|
|
|
log = log.With(zap.String("username", username), zap.Strings("role_names", roleNames),
|
2022-08-05 16:28:35 +08:00
|
|
|
zap.String("object_type", objectType), zap.String("object_privilege", objectPrivilege),
|
2023-06-25 17:20:43 +08:00
|
|
|
zap.String("db_name", dbName),
|
2022-08-05 16:28:35 +08:00
|
|
|
zap.Int32("object_index", objectNameIndex), zap.String("object_name", objectName),
|
2023-12-12 10:36:43 +08:00
|
|
|
zap.Int32("object_indexs", objectNameIndexs), zap.Strings("object_names", objectNames))
|
|
|
|
|
|
|
|
e := getEnforcer()
|
2022-08-04 11:04:34 +08:00
|
|
|
for _, roleName := range roleNames {
|
2022-08-05 16:28:35 +08:00
|
|
|
permitFunc := func(resName string) (bool, error) {
|
2023-06-25 17:20:43 +08:00
|
|
|
object := funcutil.PolicyForResource(dbName, objectType, resName)
|
2024-08-05 18:48:15 +08:00
|
|
|
isPermit, cached, version := GetPrivilegeCache(roleName, object, objectPrivilege)
|
|
|
|
if cached {
|
|
|
|
return isPermit, nil
|
|
|
|
}
|
2022-08-05 16:28:35 +08:00
|
|
|
isPermit, err := e.Enforce(roleName, object, objectPrivilege)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-08-05 18:48:15 +08:00
|
|
|
SetPrivilegeCache(roleName, object, objectPrivilege, isPermit, version)
|
2022-08-05 16:28:35 +08:00
|
|
|
return isPermit, nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 21:05:11 +08:00
|
|
|
if objectNameIndex != 0 {
|
|
|
|
// handle the api which refers one resource
|
|
|
|
permitObject, err := permitFunc(objectName)
|
2022-08-05 16:28:35 +08:00
|
|
|
if err != nil {
|
2023-06-19 13:28:41 +08:00
|
|
|
log.Warn("fail to execute permit func", zap.String("name", objectName), zap.Error(err))
|
2022-08-05 16:28:35 +08:00
|
|
|
return ctx, err
|
|
|
|
}
|
2022-09-05 21:05:11 +08:00
|
|
|
if permitObject {
|
|
|
|
return ctx, nil
|
2022-08-05 16:28:35 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-05 21:05:11 +08:00
|
|
|
|
|
|
|
if objectNameIndexs != 0 {
|
|
|
|
// handle the api which refers many resources
|
|
|
|
permitObjects := true
|
|
|
|
for _, name := range objectNames {
|
|
|
|
p, err := permitFunc(name)
|
|
|
|
if err != nil {
|
2023-06-19 13:28:41 +08:00
|
|
|
log.Warn("fail to execute permit func", zap.String("name", name), zap.Error(err))
|
2022-09-05 21:05:11 +08:00
|
|
|
return ctx, err
|
|
|
|
}
|
|
|
|
if !p {
|
|
|
|
permitObjects = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if permitObjects && len(objectNames) != 0 {
|
|
|
|
return ctx, nil
|
|
|
|
}
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-12 10:36:43 +08:00
|
|
|
log.Info("permission deny", zap.Strings("roles", roleNames))
|
2024-04-16 16:47:19 +08:00
|
|
|
|
|
|
|
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))
|
2022-08-04 11:04:34 +08:00
|
|
|
}
|
2022-08-05 16:28:35 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2023-06-25 17:20:43 +08:00
|
|
|
|
2024-01-08 15:10:49 +08:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-06-25 17:20:43 +08:00
|
|
|
func DBMatchFunc(args ...interface{}) (interface{}, error) {
|
|
|
|
name1 := args[0].(string)
|
|
|
|
name2 := args[1].(string)
|
|
|
|
|
|
|
|
db1, _ := funcutil.SplitObjectName(name1[strings.Index(name1, "-")+1:])
|
|
|
|
db2, _ := funcutil.SplitObjectName(name2[strings.Index(name2, "-")+1:])
|
|
|
|
|
|
|
|
return db1 == db2, nil
|
|
|
|
}
|
2024-08-16 14:18:54 +08:00
|
|
|
|
2024-09-05 15:35:04 +08:00
|
|
|
func collMatch(requestObj, policyObj string) bool {
|
|
|
|
_, coll1 := funcutil.SplitObjectName(requestObj[strings.Index(requestObj, "-")+1:])
|
|
|
|
_, coll2 := funcutil.SplitObjectName(policyObj[strings.Index(policyObj, "-")+1:])
|
|
|
|
|
|
|
|
return coll2 == util.AnyWord || coll1 == coll2
|
|
|
|
}
|
|
|
|
|
2024-08-16 14:18:54 +08:00
|
|
|
func PrivilegeGroupContains(args ...interface{}) (interface{}, error) {
|
|
|
|
requestPrivilege := args[0].(string)
|
|
|
|
policyPrivilege := args[1].(string)
|
2024-09-05 15:35:04 +08:00
|
|
|
requestObj := args[2].(string)
|
|
|
|
policyObj := args[3].(string)
|
2024-08-16 14:18:54 +08:00
|
|
|
|
|
|
|
switch policyPrivilege {
|
|
|
|
case commonpb.ObjectPrivilege_PrivilegeAll.String():
|
|
|
|
return true, nil
|
|
|
|
case commonpb.ObjectPrivilege_PrivilegeGroupReadOnly.String():
|
2024-09-05 15:35:04 +08:00
|
|
|
// read only belong to collection object
|
|
|
|
if !collMatch(requestObj, policyObj) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-08-16 14:18:54 +08:00
|
|
|
_, ok := roPrivileges[requestPrivilege]
|
|
|
|
return ok, nil
|
|
|
|
case commonpb.ObjectPrivilege_PrivilegeGroupReadWrite.String():
|
2024-09-05 15:35:04 +08:00
|
|
|
// read write belong to collection object
|
|
|
|
if !collMatch(requestObj, policyObj) {
|
|
|
|
return false, nil
|
|
|
|
}
|
2024-08-16 14:18:54 +08:00
|
|
|
_, ok := rwPrivileges[requestPrivilege]
|
|
|
|
return ok, nil
|
|
|
|
case commonpb.ObjectPrivilege_PrivilegeGroupAdmin.String():
|
2024-09-05 15:35:04 +08:00
|
|
|
// admin belong to global object
|
2024-08-16 14:18:54 +08:00
|
|
|
_, ok := adminPrivileges[requestPrivilege]
|
|
|
|
return ok, nil
|
|
|
|
default:
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|