管理集群角色(SDK)
集群角色定义了用户在集群内的权限。具体而言,集群角色控制集群用户在集群、Database 和 Collection 层级的权限。
本文将介绍如何创建角色、将内置权限组授予角色、撤销授予角色的权限组,以及删除角色。有关内置权限组的详细信息,请参考权限。
📘说明
此功能仅限 Dedicated 集群使用。
创建角色
以下示例展示了如何创建一个角色 role_a
。
角色名称需要遵循以下规则:
- 必须以字母开头且只可以包含大写或小写字母、数字和下划线
- Python
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client.create_role(role_name="role_a")
import io.milvus.v2.service.rbac.request.CreateRoleReq;
CreateRoleReq createRoleReq = CreateRoleReq.builder()
.roleName("role_a")
.build();
client.createRole(createRoleReq);
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
await milvusClient.createRole({
roleName: 'role_a',
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a"
}'
查看所有角色
在创建了多个角色后,您可以查看所有已创建的角色列表。
- Python
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client.list_roles()
List<String> roles = client.listRoles();
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
await milvusClient.listRoles(
includeUserInfo: True
);
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{}'
示例结果如下,role_a
为新创建的角色。
['role_a']
为角色分配内置权限组
以下示例展示了如何为角色 role_a
分配内置权限组 COLL_ADMIN
。
- Python
- Go
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
client.grant_privilege_v2(
role_name="role_a",
privilege="COLL_ADMIN"
collection_name='collection_01'
db_name='default',
)
client.grant_privilege_v2(
role_name="role_a",
privilege="ClusterReadOnly"
collection_name='*'
db_name='*',
)
import "github.com/milvus-io/milvus-sdk-go/v2/client"
client.GrantV2(context.Background(), "role_a", "collection_01", "COLL_ADMIN", entity.WithOperatePrivilegeDatabase("default"))
client.GrantV2(context.Background(), "role_a", "*", "ClusterReadOnly", entity.WithOperatePrivilegeDatabase("*"))
import io.milvus.v2.service.rbac.request.GrantPrivilegeReqV2
client.grantPrivilegeV2(GrantPrivilegeReqV2.builder()
.roleName("role_a")
.privilege("COLL_ADMIN")
.collectionName("collection_01")
.dbName("default")
.build());
client.grantPrivilegeV2(GrantPrivilegeReqV2.builder()
.roleName("role_a")
.privilege("ClusterReadOnly")
.collectionName("*")
.dbName("*")
.build());
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
await milvusClient.grantPrivilege({
roleName: 'role_a',
object: 'Collection',
objectName: 'collection_01',
privilegeName: 'COLL_ADMIN'
});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/grant_privilege_v2" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a",
"privilege": "COLL_ADMIN",
"collectionName": "collection_01",
"dbName":"default"
}'
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/grant_privilege_v2" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a",
"privilege": "ClusterReadOnly",
"collectionName": "*",
"dbName":"*"
}'
查看角色权限
以下示例展示如何查看角色 role_a
的权限。
- Python
- Go
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client.describe_role(role_name="role_a")
import "github.com/milvus-io/milvus-sdk-go/v2/client"
client.ListRoles(context.Background())
import io.milvus.v2.service.rbac.response.DescribeRoleResp;
import io.milvus.v2.service.rbac.request.DescribeRoleReq
DescribeRoleReq describeRoleReq = DescribeRoleReq.builder()
.roleName("role_a")
.build();
DescribeRoleResp resp = client.describeRole(describeRoleReq);
List<DescribeRoleResp.GrantInfo> infos = resp.getGrantInfos();
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
await milvusClient.describeRole({roleName: 'role_a'});
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a"
}'
示例结果如下:
{
"role": "role_a",
"privileges": [
"COLL_ADMIN"
]
}
撤销为角色分配的内置权限组
以下示例展示了如何撤销已分配给角色 role_a
的内置权限组 COLL_ADMIN
。
- Python
- Go
- Java
- cURL
from pymilvus import MilvusClient
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
client.revoke_privilege_v2(
role_name="role_a",
privilege="COLL_ADMIN"
collection_name='collection_01'
db_name='default',
)
client.revoke_privilege_v2(
role_name="role_a",
privilege="ClusterReadOnly"
collection_name='*'
db_name='*',
)
import "github.com/milvus-io/milvus-sdk-go/v2/client"
client.RevokeV2(context.Background(), "role_a", "collection_01", "COLL_ADMIN", entity.WithOperatePrivilegeDatabase("default"))
client.RevokeV2(context.Background(), "role_a", "*", "ClusterReadOnly", entity.WithOperatePrivilegeDatabase("*"))
import io.milvus.v2.service.rbac.request.RevokePrivilegeReqV2
client.revokePrivilegeV2(RevokePrivilegeReqV2.builder()
.roleName("role_a")
.privilege("COLL_ADMIN")
.collectionName("collection_01")
.dbName("default")
.build());
client.revokePrivilegeV2(RevokePrivilegeReqV2.builder()
.roleName("role_a")
.privilege("ClusterReadOnly")
.collectionName("*")
.dbName("*")
.build());
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/revoke_privilege_v2" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a",
"privilege": "COLL_ADMIN",
"collectionName": "collection_01",
"dbName":"default"
}'
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/revoke_privilege_v2" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a",
"privilege": "ClusterReadOnly",
"collectionName": "*",
"dbName":"*"
}'
删除角色
以下示例展示了如何删除角色 role_a
。
📘说明
内置的 admin
角色无法删除。
- Python
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client.drop_role(role_name="role_a")
import io.milvus.v2.service.rbac.request.DropRoleReq
DropRoleReq dropRoleReq = DropRoleReq.builder()
.roleName("role_a")
.build();
client.dropRole(dropRoleReq);
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
milvusClient.dropRole({
roleName: 'role_a',
})
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"roleName": "role_a"
}'
删除后,您可以通过查看所有角色操作检查是否删除成功。如果列表中未展示此前删除的角色则视为删除成功。
- Python
- Java
- NodeJS
- cURL
from pymilvus import MilvusClient
client.list_roles()
List<String> resp = client.listRoles();
const { MilvusClient, DataType } = require("@zilliz/milvus2-sdk-node")
milvusClient.listRoles(
includeUserInfo: True
)
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/roles/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{}'
示例结果如下,列表中无角色 role_a
,删除操作成功。
['admin']