使用安全对象控制数据访问
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.
本主题介绍在基表中为共享安全对象使用群集密钥,并提供了与使用者账户共享安全视图的详细步骤说明。此外还提供了针对数据提供商和使用者的示例脚本。
Note
共享安全对象的说明与共享表的说明基本相同,只是增加了以下对象:
- 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.
为共享数据使用群集密钥
在超大(即大小为数 TB)的表上,群集密钥可提供显著的查询性能优势。通过在共享安全视图或安全 UDFs 中使用的基表上定义一个或多个群集密钥,即可确保使用者账户中的用户在使用这些对象时不会受到负面影响。
When choosing the columns to use as the clustering key for a table, please note some important considerations.
设置和任务示例
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.
第 1 步:在私有架构中创建数据和映射表¶
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.
第 2 步:在公共架构中创建安全视图¶
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.
第 3 步:验证表和安全视图¶
验证表和安全视图,以确保按账户正确筛选数据。
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.
第 4 步:创建共享¶
- Create a share.
要创建共享,必须使用 ACCOUNTADMIN 角色,或者获授全局 CREATE SHARE 权限的角色。要向共享授予对象,该角色还必须符合以下条件之一:
- 具有共享数据库 OWNERSHIP 权限的角色。
- A role with the USAGE privilege on the database WITH GRANT OPTION. For example:
-
Add the database (
mydb), schema (public), and secure view (paid_sensitive_data) to the share. You can choose to either add privileges on these objects to a share via a database role, or grant privileges on the objects directly to the share. For more information on these options, see How to share database objects. -
Confirm the contents of the share. At the most basic level, you should use the SHOW GRANTS command to confirm the objects in the share have the necessary privileges.
Note that the secure view
paid_sensitive_datais displayed in the command output as a table. -
向共享添加一个或多个账户。
脚本示例
以下脚本说明了如何执行上一节中介绍的所有任务:
-
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:
-
使用 ACCOUNTADMIN 角色创建共享。
-
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):
-
将账户添加到共享。
脚本示例(面向使用者)
使用者可使用以下脚本来创建数据库(从上述脚本中创建的共享),并在所获得的数据库中查询安全视图:
-
通过从共享创建数据库,将共享数据库引入您的账户。
-
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: