RBACExample.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package io.milvus;
  20. import io.milvus.client.MilvusServiceClient;
  21. import io.milvus.grpc.ListCredUsersResponse;
  22. import io.milvus.grpc.SelectRoleResponse;
  23. import io.milvus.param.ConnectParam;
  24. import io.milvus.param.R;
  25. import io.milvus.param.RpcStatus;
  26. import io.milvus.param.credential.CreateCredentialParam;
  27. import io.milvus.param.credential.ListCredUsersParam;
  28. import io.milvus.param.role.*;
  29. import org.apache.commons.lang3.Validate;
  30. public class RBACExample {
  31. private static final MilvusServiceClient milvusClient;
  32. static {
  33. ConnectParam connectParam = ConnectParam.newBuilder()
  34. .withHost("localhost")
  35. .withPort(19530)
  36. .withAuthorization("root","Milvus")
  37. .build();
  38. milvusClient = new MilvusServiceClient(connectParam);
  39. }
  40. public static R<RpcStatus> createUser(String userName, String password) {
  41. return milvusClient.createCredential(CreateCredentialParam.newBuilder()
  42. .withUsername(userName)
  43. .withPassword(password)
  44. .build());
  45. }
  46. public static R<RpcStatus> grantUserRole(String userName, String roleName) {
  47. return milvusClient.addUserToRole(AddUserToRoleParam.newBuilder()
  48. .withUserName(userName)
  49. .withRoleName(roleName)
  50. .build());
  51. }
  52. public static R<RpcStatus> revokeUserRole(String userName, String roleName) {
  53. return milvusClient.removeUserFromRole(RemoveUserFromRoleParam.newBuilder()
  54. .withUserName(userName)
  55. .withRoleName(roleName)
  56. .build());
  57. }
  58. public static R<ListCredUsersResponse> listUsers() {
  59. return milvusClient.listCredUsers(ListCredUsersParam.newBuilder()
  60. .build());
  61. }
  62. public static R<SelectRoleResponse> selectRole(String roleName) {
  63. return milvusClient.selectRole(SelectRoleParam.newBuilder()
  64. .withRoleName(roleName)
  65. .build());
  66. }
  67. public static R<RpcStatus> createRole(String roleName) {
  68. return milvusClient.createRole(CreateRoleParam.newBuilder()
  69. .withRoleName(roleName)
  70. .build());
  71. }
  72. public static R<RpcStatus> dropRole(String roleName) {
  73. return milvusClient.dropRole(DropRoleParam.newBuilder()
  74. .withRoleName(roleName)
  75. .build());
  76. }
  77. public static R<RpcStatus> grantRolePrivilege(String roleName, String objectType, String objectName, String privilege) {
  78. return milvusClient.grantRolePrivilege(GrantRolePrivilegeParam.newBuilder()
  79. .withRoleName(roleName)
  80. .withObject(objectType)
  81. .withObjectName(objectName)
  82. .withPrivilege(privilege)
  83. .build());
  84. }
  85. public static R<RpcStatus> revokeRolePrivilege(String roleName, String objectType, String objectName, String privilege) {
  86. return milvusClient.revokeRolePrivilege(RevokeRolePrivilegeParam.newBuilder()
  87. .withRoleName(roleName)
  88. .withObject(objectType)
  89. .withObjectName(objectName)
  90. .withPrivilege(privilege)
  91. .build());
  92. }
  93. public static void main(String[] args) {
  94. // create a role
  95. R<RpcStatus> resp = createRole("role1");
  96. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "create role fail!");
  97. System.out.println("role1 created");
  98. //create user
  99. resp = createUser("user", "pwd123456");
  100. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "create user fail!");
  101. System.out.println("user created");
  102. // grant privilege to role.
  103. // grant object is all collections, grant object type is Collection, and the privilege is CreateCollection
  104. resp = grantRolePrivilege("role1","Global","*", "CreateCollection");
  105. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "bind privileges to role fail!");
  106. System.out.println("grant privilege to role1");
  107. // bind role to user
  108. resp = grantUserRole("user", "role1");
  109. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "bind role to user fail!");
  110. System.out.println("bind role1 to user");
  111. // revoke privilege from role
  112. resp = revokeRolePrivilege("role1","Global","*", "CreateCollection");
  113. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "revoke privileges to role fail!");
  114. System.out.println("revoke privilege from role1");
  115. // list role
  116. R<SelectRoleResponse> resp1 = selectRole("role1");
  117. Validate.isTrue(resp1.getStatus() == R.success().getStatus(), "select role information fail!");
  118. // delete a role
  119. resp = dropRole("role1");
  120. Validate.isTrue(resp.getStatus() == R.success().getStatus(), "drop role fail!");
  121. System.out.println("delete role1");
  122. }
  123. }