Managing Snowflake users, roles, and grants with Python¶
You can use Python to manage Snowflake users, roles, and grants. For more information about managing users and their privileges in
Snowflake, see User management.
The examples in this topic assume that you’ve added code to connect with Snowflake and to create a Root object from which to use the
Snowflake Python APIs.
For example, the following code uses connection parameters defined in a configuration file to create a connection to Snowflake:
from snowflake.core import Root
from snowflake.snowpark import Session
session = Session.builder.config("connection_name", "myconnection").create()
root = Root(session)
You can create a user by calling the UserCollection.create method and passing a User object that represents the user you
want to create. To create a user, first create a User object that specifies the user name.
Code in the following example creates a User object representing a user named my_user and then creates the user by passing
the User object to the UserCollection.create method:
from snowflake.core.user import User
my_user = User(name="my_user")
root.users.create(my_user)
You can set properties of a User object and pass it to the UserResource.create_or_alter method to create a user if it
doesn’t exist, or alter it according to the user definition if it does exist. The behavior of create_or_alter is intended to be
idempotent, which means that the resulting user object will be the same regardless of whether the user exists before you call the method.
create_or_alter uses default values for any User
properties that you don’t explicitly define. For example, if you don’t set snowflake_support, its value defaults to False even
if the user previously existed with a different value.
Note
The create_or_alter method currently does not support changing the password for an existing user. You can only set the
password when creating a new user.
Code in the following example updates the first name, last name, and must_change_password properties of the my_user user, and
then alters the user on Snowflake:
You can manage database roles in Snowflake. A database role is a database-level
object. The Snowflake Python APIs represents database roles with two separate types:
DatabaseRole: Exposes a database role’s properties, such as its name and a comment.
DatabaseRoleResource: Exposes methods you can use to grant and manage privileges on a corresponding DatabaseRole object,
and to drop the database role.
Code in the following example creates a database role named dr2 in the my_db_2 target database as a copy of the existing dr1
database role in the my_db database.
You can use the API to manage access privileges on a securable Snowflake object to an account role, database role, or user. For more
information about roles, securable objects, and the access control framework in Snowflake, see Overview of Access Control.
To grant privileges on a Snowflake object, you first create a Grant object that specifies the following attributes:
grantee: The role or user that is being granted the privileges.
securable: The Snowflake object that is being secured by the privileges.
privileges: The privileges that are being granted to a role.
Granting CREATE privileges in an account to a role¶
Code in the following example creates a Grant object representing a grant operation that grants the privileges create_database
and create_warehouse to the role my_role in the current Snowflake account. The code executes the operation using the
root.grants.grant method.
from snowflake.core.grant import Grant
from snowflake.core.grant._grantee import Grantees
from snowflake.core.grant._privileges import Privileges
from snowflake.core.grant._securables import Securables
root.grants.grant(
Grant(
grantee=Grantees.role(name='my_role'),
securable=Securables.current_account,
privileges=[Privileges.create_database,
Privileges.create_warehouse],
)
)
Code in the following example grants imported privileges on the database my_db
to the role my_role:
from snowflake.core.grant import Grant
from snowflake.core.grant._grantee import Grantees
from snowflake.core.grant._privileges import Privileges
from snowflake.core.grant._securables import Securables
root.grants.grant(
Grant(
grantee=Grantees.role('my_role'),
securable=Securables.database('my_db'),
privileges=[Privileges.imported_privileges],
)
)
You can assign a role to another role to create a “parent-child” relationship between the roles (also referred to as a role hierarchy).
Code in the following example grants the my_role user role to the ACCOUNTADMIN system role:
from snowflake.core.grant import Grant
from snowflake.core.grant._grantee import Grantees
from snowflake.core.grant._securables import Securables
root.grants.grant(
Grant(
grantee=Grantees.role('ACCOUNTADMIN'),
securable=Securables.role('my_role'),
)
)