AI

PyMilvus List_Grants()

In Milvus, a role is a predefined set of permissions that determines what actions and operations does a user or group can perform within the Milvus system.

As the database administrator, you often need to control certain functionalities and data within the Milvus system by assigning specific roles to the users or groups.

Roles then allow you to define the granular permissions for different users and groups based on responsibilities and groups.

Requirements:

  1. Access to a Milvus server
  2. Python 3.10 and above
  3. Installed PyMilvus SDK

PyMilvus Role() Method

In Milvus, the Role() is a constructor method that allows us to define a new role in the Milvus cluster.

The method parameters are expressed as follows:

Role(name, using="default", **kwargs)

The “name” parameter determines the name of the role to create. The “using” parameters specify the alias of the Milvus connection to be attached to.

The method returns a new role object.

PyMilvus Grant() Method

Once we defined a new role, we need to allocate the various privileges to that role. This determines what actions do the users that are assigned to that role can perform.

The method syntax is as follows:

grant(object, object_name, privilege)

The parameters are as explained in the following:

Object – This refers to the operation objects that the privilege applies to—for example, a Collection, an Index, a Partition, etc. Keep in mind that the value of this parameter is case-sensitive.

Object_name – It specifies the object’s name to which the role is granted the specified privilege.

Privilege – It defines the actual name of the privilege to be granted to the role. Similarly, the actual value of this parameter is case-sensitive.

The following table describes the supported objects and the privileges that you can assign to them:

Object name Privilege name
Collection CreateIndex
Collection DropIndex
Collection IndexDetail
Collection Load
Collection Release
Collection Insert
Collection Delete
Collection Search
Collection Flush
Collection Query
Collection GetStatistics
Collection Compaction
Collection Alias
Collection Import
Collection LoadBalance
Global *(All)
Global CreateCollection
Global DropCollection
Global DescribeCollection
Global ShowCollections
Global CreateOwnership
Global DropOwnership
Global SelectOwnership
Global ManageOwnership
Global CreateResourceGroup
Global DropResourceGroup
Global DescribeResourceGroup
Global ListResourceGroups
Global TransferNode
Global TransferReplica
Global RenameCollection
Global CreateDatabase
Global ListDatabases
User UpdateUser
User SelectUser

For example, suppose we wish to allow the admin user to create an index on a collection called “films”. We can run the code as follows:

from pymilvus import connections

from pymilvus.orm.role import Role

connections.connect()

role = Role("admin")

role.grant("Collection", "films", "CreateIndex")

The previous code should allow any admin user to create the indexes on the “films” collection.

PyMilvus Add_User() Method

Unfortunately, we cannot use the role() method by itself. Hence, we pair it with the add_user() method which associates a user with a given role. After this, the user can access the privileges that are assigned to that role.

The method syntax is as follows:

add_user(username)

Example Usage:

Let us explore a basic example that demonstrates how to combine the role() method and the add_user() method to add a new role and assign a new user to it.

from pymilvus import connections

from pymilvus.orm.role import Role

connections.connect()

role = Role(name=admin)

role.add_user("linuxhint")

users = role.get_users()

print(f"users added to the role: {users}")

The previous code creates a new role called “admin” and assigns the role to the “linuxhint” user.

PyMilvus List_Grants() Method

The list_grants() method allows you to list all privileges that are granted to the current role. The method syntax is as follows:

list_grants()

The method returns a “GrantInfo” object that comprises a list of “GrantItem” objects.

Example:

from pymilvus import connections

from pymilvus.orm.role import Role

connections.connect()

role = Role("admin")

print(role.list_grants())

This should print the privileges that are assigned to the admin role.

Conclusion

This post covered the basics of working with roles and role privileges in Milvus using the PyMilvus SDK. You can explore the docs and source code for a more detailed implementation.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list