|
3 | 3 | import (
|
4 | 4 | "strings"
|
5 | 5 |
|
| 6 | + "github.com/samber/lo" |
| 7 | + |
| 8 | + "github.com/milvus-io/milvus-proto/go-api/v2/commonpb" |
| 9 | + "github.com/milvus-io/milvus-proto/go-api/v2/milvuspb" |
6 | 10 | "github.com/milvus-io/milvus/pkg/util"
|
7 | 11 | )
|
8 | 12 |
|
| 13 | +var ( |
| 14 | + builtinPrivilegeGroups = map[string][]string{ |
| 15 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupCollectionReadOnly.String()): collectionReadOnlyPrivilegeGroup, |
| 16 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupCollectionReadWrite.String()): collectionReadWritePrivilegeGroup, |
| 17 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupCollectionAdmin.String()): collectionAdminPrivilegeGroup, |
| 18 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupDatabaseReadOnly.String()): databaseReadOnlyPrivilegeGroup, |
| 19 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupDatabaseReadWrite.String()): databaseReadWritePrivilegeGroup, |
| 20 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupDatabaseAdmin.String()): databaseAdminPrivilegeGroup, |
| 21 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupClusterReadOnly.String()): clusterReadOnlyPrivilegeGroup, |
| 22 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupClusterReadWrite.String()): clusterReadWritePrivilegeGroup, |
| 23 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGroupClusterAdmin.String()): clusterAdminPrivilegeGroup, |
| 24 | + } |
| 25 | + |
| 26 | + collectionReadOnlyPrivilegeGroup = []string{ |
| 27 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeQuery.String()), |
| 28 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeSearch.String()), |
| 29 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeIndexDetail.String()), |
| 30 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGetFlushState.String()), |
| 31 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGetLoadState.String()), |
| 32 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGetLoadingProgress.String()), |
| 33 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeHasPartition.String()), |
| 34 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeShowPartitions.String()), |
| 35 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDescribeCollection.String()), |
| 36 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDescribeAlias.String()), |
| 37 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeGetStatistics.String()), |
| 38 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeListAliases.String()), |
| 39 | + } |
| 40 | + |
| 41 | + collectionReadWritePrivilegeGroup = append(collectionReadOnlyPrivilegeGroup, |
| 42 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeLoad.String()), |
| 43 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeRelease.String()), |
| 44 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeInsert.String()), |
| 45 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDelete.String()), |
| 46 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeUpsert.String()), |
| 47 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeImport.String()), |
| 48 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeFlush.String()), |
| 49 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCompaction.String()), |
| 50 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeLoadBalance.String()), |
| 51 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateIndex.String()), |
| 52 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropIndex.String()), |
| 53 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreatePartition.String()), |
| 54 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropPartition.String()), |
| 55 | + ) |
| 56 | + |
| 57 | + collectionAdminPrivilegeGroup = append(collectionReadWritePrivilegeGroup, |
| 58 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateAlias.String()), |
| 59 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropAlias.String()), |
| 60 | + ) |
| 61 | + |
| 62 | + databaseReadOnlyPrivilegeGroup = []string{ |
| 63 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeShowCollections.String()), |
| 64 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDescribeDatabase.String()), |
| 65 | + } |
| 66 | + |
| 67 | + databaseReadWritePrivilegeGroup = append(databaseReadOnlyPrivilegeGroup, |
| 68 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeAlterDatabase.String()), |
| 69 | + ) |
| 70 | + |
| 71 | + databaseAdminPrivilegeGroup = append(databaseReadWritePrivilegeGroup, |
| 72 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateCollection.String()), |
| 73 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropCollection.String()), |
| 74 | + ) |
| 75 | + |
| 76 | + clusterReadOnlyPrivilegeGroup = []string{ |
| 77 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeListDatabases.String()), |
| 78 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeSelectOwnership.String()), |
| 79 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeSelectUser.String()), |
| 80 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDescribeResourceGroup.String()), |
| 81 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeListResourceGroups.String()), |
| 82 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeListPrivilegeGroups.String()), |
| 83 | + } |
| 84 | + |
| 85 | + clusterReadWritePrivilegeGroup = append(clusterReadOnlyPrivilegeGroup, |
| 86 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeFlushAll.String()), |
| 87 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeTransferNode.String()), |
| 88 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeTransferReplica.String()), |
| 89 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeUpdateResourceGroups.String()), |
| 90 | + ) |
| 91 | + |
| 92 | + clusterAdminPrivilegeGroup = append(clusterReadWritePrivilegeGroup, |
| 93 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeBackupRBAC.String()), |
| 94 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeRestoreRBAC.String()), |
| 95 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateDatabase.String()), |
| 96 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropDatabase.String()), |
| 97 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateOwnership.String()), |
| 98 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropOwnership.String()), |
| 99 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeManageOwnership.String()), |
| 100 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreateResourceGroup.String()), |
| 101 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropResourceGroup.String()), |
| 102 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeUpdateUser.String()), |
| 103 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeRenameCollection.String()), |
| 104 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeCreatePrivilegeGroup.String()), |
| 105 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeDropPrivilegeGroup.String()), |
| 106 | + util.MetaStore2API(commonpb.ObjectPrivilege_PrivilegeOperatePrivilegeGroup.String()), |
| 107 | + ) |
| 108 | +) |
| 109 | + |
9 | 110 | type rbacConfig struct {
|
10 | 111 | Enabled ParamItem `refreshable:"false"`
|
11 | 112 | ClusterReadOnlyPrivileges ParamItem `refreshable:"false"`
|
|
23 | 124 |
|
24 | 125 | func (p *rbacConfig) init(base *BaseTable) {
|
25 | 126 | p.Enabled = ParamItem{
|
26 |
| - Key: "common.security.rbac.overrideBuiltInPrivilgeGroups.enabled", |
| 127 | + Key: "common.security.rbac.overrideBuiltInPrivilegeGroups.enabled", |
27 | 128 | DefaultValue: "false",
|
28 | 129 | Version: "2.4.16",
|
29 | 130 | Doc: "Whether to override build-in privilege groups",
|
|
33 | 134 |
|
34 | 135 | p.ClusterReadOnlyPrivileges = ParamItem{
|
35 | 136 | Key: "common.security.rbac.cluster.readonly.privileges",
|
36 |
| - DefaultValue: strings.Join(util.ClusterReadOnlyPrivilegeGroup, ","), |
| 137 | + DefaultValue: strings.Join(clusterReadOnlyPrivilegeGroup, ","), |
37 | 138 | Version: "2.4.16",
|
38 | 139 | Doc: "Cluster level readonly privileges",
|
39 | 140 | Export: true,
|
|
42 | 143 |
|
43 | 144 | p.ClusterReadWritePrivileges = ParamItem{
|
44 | 145 | Key: "common.security.rbac.cluster.readwrite.privileges",
|
45 |
| - DefaultValue: strings.Join(util.ClusterReadWritePrivilegeGroup, ","), |
| 146 | + DefaultValue: strings.Join(clusterReadWritePrivilegeGroup, ","), |
46 | 147 | Version: "2.4.16",
|
47 | 148 | Doc: "Cluster level readwrite privileges",
|
48 | 149 | Export: true,
|
|
51 | 152 |
|
52 | 153 | p.ClusterAdminPrivileges = ParamItem{
|
53 | 154 | Key: "common.security.rbac.cluster.admin.privileges",
|
54 |
| - DefaultValue: strings.Join(util.ClusterAdminPrivilegeGroup, ","), |
| 155 | + DefaultValue: strings.Join(clusterAdminPrivilegeGroup, ","), |
55 | 156 | Version: "2.4.16",
|
56 | 157 | Doc: "Cluster level admin privileges",
|
57 | 158 | Export: true,
|
|
60 | 161 |
|
61 | 162 | p.DBReadOnlyPrivileges = ParamItem{
|
62 | 163 | Key: "common.security.rbac.database.readonly.privileges",
|
63 |
| - DefaultValue: strings.Join(util.DatabaseReadOnlyPrivilegeGroup, ","), |
| 164 | + DefaultValue: strings.Join(databaseReadOnlyPrivilegeGroup, ","), |
64 | 165 | Version: "2.4.16",
|
65 | 166 | Doc: "Database level readonly privileges",
|
66 | 167 | Export: true,
|
|
69 | 170 |
|
70 | 171 | p.DBReadWritePrivileges = ParamItem{
|
71 | 172 | Key: "common.security.rbac.database.readwrite.privileges",
|
72 |
| - DefaultValue: strings.Join(util.DatabaseReadWritePrivilegeGroup, ","), |
| 173 | + DefaultValue: strings.Join(databaseReadWritePrivilegeGroup, ","), |
73 | 174 | Version: "2.4.16",
|
74 | 175 | Doc: "Database level readwrite privileges",
|
75 | 176 | Export: true,
|
|
78 | 179 |
|
79 | 180 | p.DBAdminPrivileges = ParamItem{
|
80 | 181 | Key: "common.security.rbac.database.admin.privileges",
|
81 |
| - DefaultValue: strings.Join(util.DatabaseAdminPrivilegeGroup, ","), |
| 182 | + DefaultValue: strings.Join(databaseAdminPrivilegeGroup, ","), |
82 | 183 | Version: "2.4.16",
|
83 | 184 | Doc: "Database level admin privileges",
|
84 | 185 | Export: true,
|
|
87 | 188 |
|
88 | 189 | p.CollectionReadOnlyPrivileges = ParamItem{
|
89 | 190 | Key: "common.security.rbac.collection.readonly.privileges",
|
90 |
| - DefaultValue: strings.Join(util.CollectionReadOnlyPrivilegeGroup, ","), |
| 191 | + DefaultValue: strings.Join(collectionReadOnlyPrivilegeGroup, ","), |
91 | 192 | Version: "2.4.16",
|
92 | 193 | Doc: "Collection level readonly privileges",
|
93 | 194 | Export: true,
|
|
96 | 197 |
|
97 | 198 | p.CollectionReadWritePrivileges = ParamItem{
|
98 | 199 | Key: "common.security.rbac.collection.readwrite.privileges",
|
99 |
| - DefaultValue: strings.Join(util.CollectionReadWritePrivilegeGroup, ","), |
| 200 | + DefaultValue: strings.Join(collectionReadWritePrivilegeGroup, ","), |
100 | 201 | Version: "2.4.16",
|
101 | 202 | Doc: "Collection level readwrite privileges",
|
102 | 203 | Export: true,
|
|
105 | 206 |
|
106 | 207 | p.CollectionAdminPrivileges = ParamItem{
|
107 | 208 | Key: "common.security.rbac.collection.admin.privileges",
|
108 |
| - DefaultValue: strings.Join(util.CollectionAdminPrivilegeGroup, ","), |
| 209 | + DefaultValue: strings.Join(collectionAdminPrivilegeGroup, ","), |
109 | 210 | Version: "2.4.16",
|
110 | 211 | Doc: "Collection level admin privileges",
|
111 | 212 | Export: true,
|
112 | 213 | }
|
113 | 214 | p.CollectionAdminPrivileges.Init(base.mgr)
|
114 | 215 | }
|
| 216 | + |
| 217 | +func (p *rbacConfig) GetDefaultPrivilegeGroups() []*milvuspb.PrivilegeGroupInfo { |
| 218 | + privilegeGroupConfigs := []struct { |
| 219 | + GroupName string |
| 220 | + Privileges func() []string |
| 221 | + }{ |
| 222 | + {"ClusterReadOnly", p.ClusterReadOnlyPrivileges.GetAsStrings}, |
| 223 | + {"ClusterReadWrite", p.ClusterReadWritePrivileges.GetAsStrings}, |
| 224 | + {"ClusterAdmin", p.ClusterAdminPrivileges.GetAsStrings}, |
| 225 | + {"DatabaseReadOnly", p.DBReadOnlyPrivileges.GetAsStrings}, |
| 226 | + {"DatabaseReadWrite", p.DBReadWritePrivileges.GetAsStrings}, |
| 227 | + {"DatabaseAdmin", p.DBAdminPrivileges.GetAsStrings}, |
| 228 | + {"CollectionReadOnly", p.CollectionReadOnlyPrivileges.GetAsStrings}, |
| 229 | + {"CollectionReadWrite", p.CollectionReadWritePrivileges.GetAsStrings}, |
| 230 | + {"CollectionAdmin", p.CollectionAdminPrivileges.GetAsStrings}, |
| 231 | + } |
| 232 | + |
| 233 | + builtinGroups := make([]*milvuspb.PrivilegeGroupInfo, 0, len(privilegeGroupConfigs)) |
| 234 | + for _, config := range privilegeGroupConfigs { |
| 235 | + privileges := lo.Map(config.Privileges(), func(name string, _ int) *milvuspb.PrivilegeEntity { |
| 236 | + return &milvuspb.PrivilegeEntity{Name: name} |
| 237 | + }) |
| 238 | + builtinGroups = append(builtinGroups, &milvuspb.PrivilegeGroupInfo{ |
| 239 | + GroupName: config.GroupName, |
| 240 | + Privileges: privileges, |
| 241 | + }) |
| 242 | + } |
| 243 | + return builtinGroups |
| 244 | +} |
| 245 | + |
| 246 | +func (p *rbacConfig) GetDefaultPrivilegeGroup(privName string) *milvuspb.PrivilegeGroupInfo { |
| 247 | + for _, group := range p.GetDefaultPrivilegeGroups() { |
| 248 | + if group.GroupName == privName { |
| 249 | + return group |
| 250 | + } |
| 251 | + } |
| 252 | + return nil |
| 253 | +} |
| 254 | + |
| 255 | +func (p *rbacConfig) GetDefaultPrivilegeGroupNames() []string { |
| 256 | + return lo.Keys(builtinPrivilegeGroups) |
| 257 | +} |
| 258 | + |
| 259 | +func (p *rbacConfig) IsCollectionPrivilegeGroup(privName string) bool { |
| 260 | + collectionPrivilegeGroups := lo.PickBy(builtinPrivilegeGroups, func(groupName string, _ []string) bool { |
| 261 | + return strings.Contains(groupName, "Collection") |
| 262 | + }) |
| 263 | + _, exists := collectionPrivilegeGroups[privName] |
| 264 | + return exists |
| 265 | +} |
0 commit comments