Use secure objects to control data access¶
To ensure sensitive data in a shared database is not exposed to users in consumer accounts, Snowflake strongly recommends sharing secure views and/or secure UDFs instead of directly sharing tables.
In addition, for optimal performance, especially when sharing data in extremely large tables, we recommend defining clustering keys on the base table(s) for your secure objects.
This topic describes using clustering keys in base tables for shared secure objects and provides step-by-step instructions for sharing a secure view with a consumer account. It provides sample scripts for both data providers and consumers.
Note
The instructions for sharing a secure object are essentially the same as sharing a table, with the addition of the following objects:
A “private” schema containing the base table and a “public” schema containing the secure object. Only the public schema and secure object are shared.
A “mapping table” (also in the “private” schema), which is only required if you wish to share the data in the base table with multiple consumer accounts and share specific rows in the table with specific accounts.
Sample setup and tasks¶
These sample instructions assume a database named mydb exists in the data provider account and has two schemas, private
and public. If the database and schemas do not exist, you should create them before proceeding.
Step 1: Create data and mapping tables in private schema¶
Create the following two tables in the mydb.private schema and populate them with data:
sensitive_data — contains the data to share, and an access_id column for controlling data access by account.sharing_access — uses the access_id column to map the shared data and the accounts that can access the data.Step 2: Create secure view in public schema¶
Create the following secure view in the mydb.public schema:
paid_sensitive_data — displays data based on account.Note that the access_id column from the base table (sensitive_data) does not need to be included in the view.
Step 3: Validate tables and secure view¶
Validate the tables and secure view to ensure the data is filtered properly by account.
To enable validating secure views that will be shared with other accounts, Snowflake provides a session parameter, SIMULATED_DATA_SHARING_CONSUMER. Set this session parameter to the name of the consumer account you wish to simulate access for. You can then query the view and see the results that a user in the consumer account will see.
Sample script¶
The following script illustrates performing all the tasks described in the previous section:
Create two tables in the ‘private’ schema and populate the first one with stock data from three different companies (Apple, Microsoft, and IBM). You will then populate the second one with data that maps the stock data to individual accounts:
Create a secure view in the ‘public’ schema. This view filters the stock data from the first table by account, using the mapping information in the second table:
Create a share using the ACCOUNTADMIN role.
Add the objects to the share. You can choose to either add privileges on these objects to a share via a database role (Option 1), or grant privileges on the objects directly to the share (Option 2):
Add accounts to the share.
Sample script (for consumers)¶
The following script can be used by consumers to create a database (from the share created in the above script) and query the secure view in the resulting database:
Bring the shared database into your account by creating a database from the share.
Grant privileges on the database to other roles in your account (e.g. CUSTOM_ROLE1). The GRANT statement differs depending on whether the data consumer added objects to the share using database roles (Option 1) or by granting privileges on the objects directly to the share (Option 2):
Use the CUSTOM_ROLE1 role to query the view in the database you created. Note that there must be an active warehouse in use in the session to perform queries. In the USE WAREHOUSE command, replace <warehouse_name> with the name of one of the warehouses in your account. The CUSTOM_ROLE1 role must have the USAGE privilege on the warehouse: