Using Row Access Policies¶
This topic provides an introduction to implementing row access policies.
Implementing Row Access Policies¶
The following subsections provide two examples on how to implement row access policies:
A typical row access policy with a mapping table lookup.
Replacing existing row access policy subqueries with memoizable functions to increase query performance.
Example: Mapping Table Lookup¶
The following steps are a representative guide to configure row access policy privileges and add row access policies to tables and views.
These steps make the following assumptions:
The management approach is centralized.
If the row access policy use case includes a hybrid, or decentralized management approach, see Managing Row Access Policies for a representative distribution of roles and privileges.
A mapping table is necessary, similar to the Representative Use Case: Use a Mapping Table to Filter the Query Result.
The following steps use the CURRENT_ROLE context function to determine whether users see rows in a query result, while the representative use case focuses on the user’s first name (i.e. CURRENT_USER).
If role activation and role hierarchy are important, Snowflake recommends that the policy conditions use the IS_ROLE_IN_SESSION context function. For policy examples, see the Examples section in the IS_ROLE_IN_SESSION function.
The overall process to implement a row access policy with mapping tables remains the same even though the context functions are different.
The SECURITYADMIN system role grants privileges to custom roles to manage and implement row access policies.
If you do not want to use higher privileged roles (i.e. SECURITYADMIN or ACCOUNTADMIN) in a production environment in favor of less privileged custom roles (e.g.
database_admin
,finance_admin
), verify that the lower-privileged roles have the necessary privileges to manage and implement row access policies.For more information, see Row Access Policy Privileges and Summary of DDL Commands, Operations, and Privileges.
There are separate steps to create a table to be protected by a row access policy (step 1) and adding the row access policy to the table (step 5). It is possible to add row access policy to the table when the table is created, assuming that a row access policy already exists. For more information on the syntax, see CREATE TABLE.
Step 1: Create a Table for the Data¶
Create a table for the sales data.
CREATE TABLE sales (
customer varchar,
product varchar,
spend decimal(20, 2),
sale_date date,
region varchar
);
Step 2: Create a Mapping Table, Custom Role, and Grant the SELECT Privilege¶
In the security
schema, create a mapping table as shown in the
representative example. This table defines which rows in the sales
table sales
managers can see.
CREATE TABLE security.salesmanagerregions ( sales_manager varchar, region varchar );
Next, a security administrator creates the mapping_role
custom role and grants the SELECT privilege to the custom role. This grant
allows users with the custom role to query the mapping table.
USE ROLE SECURITYADMIN; CREATE ROLE mapping_role; GRANT SELECT ON TABLE security.salesmanagerregions TO ROLE mapping_role;
Step 3: Create a Row Access Policy¶
Using the schema owner role, create a row access policy with the following two conditions:
Users with the
sales_executive_role
custom role can view all rows.Users with the
sales_manager
custom role can view rows based on thesalesmanagerregions
mapping table.
Note that the schema owner role is automatically granted the CREATE ROW ACCESS POLICY privilege. If other roles should be able to create row access policies, the schema owner role can grant the CREATE ROW ACCESS policy privilege to other roles.
use role SCHEMA_OWNER_ROLE;
CREATE OR REPLACE ROW ACCESS POLICY security.sales_policy
AS (sales_region varchar) RETURNS BOOLEAN ->
'sales_executive_role' = CURRENT_ROLE()
OR EXISTS (
SELECT 1 FROM salesmanagerregions
WHERE sales_manager = CURRENT_ROLE()
AND region = sales_region
)
;
Where:
security.sales_policy
The name of the row access policy in the
Security
schema.AS (sales_region varchar)
The signature for the row access policy.
A signature specifies the mapping table attribute and data type. The returned value determines whether the user has access to a given row on the table or view to which the row access policy is added.
RETURNS BOOLEAN ->
Specifies the application of the row access policy.
Note that the
expression
of the row access policy immediately follows the right-arrow (i.e.->
).The expression can be any boolean-valued SQL expression. Snowflake supports expressions that invoke UDFs, External Functions, and expressions that use subqueries.
'sales_executive_role' = CURRENT_ROLE()
The first condition of the row access policy expression that allows users with the
sales_executive_role
custom role to view data.OR EXISTS (select 1 from salesmanagerregions WHERE sales_manager = CURRENT_ROLE() AND region = sales_region)
The second condition of the row access policy expression which uses a subquery.
The subquery requires the CURRENT_ROLE to be the
sales_manager
custom role with the executed query on the data to specify a region listed in the{salesmanagerregions}
mapping table.
Tip
To increase query performance on the policy-protected table, replace the mapping table lookup subquery in the EXISTS
clause with a
memoizable function.
For details, see the memoizable function example (in this topic).
Step 4: Grant Privileges to Custom Roles¶
Using the SECURITYADMIN system role, execute the following two statements:
GRANT OWNERSHIP ON ROW ACCESS POLICY security.sales_policy TO mapping_role;
GRANT APPLY ON ROW ACCESS POLICY security.sales_policy TO ROLE sales_analyst_role;
These two GRANT <privileges> statements have the following effects:
Ownership of the policy does not rest with the SECURITYADMIN system role. At query runtime, Snowflake uses the privileges granted to the custom role because policies are executed with owner’s rights, not the more privileged SECURITYADMIN system role. This approach supports the Principle of Least Privilege.
The
sales_analyst_role
custom role can add or drop the row access policy from a table as needed.
Step 5: Add the Row Access Policy to a Table¶
Any table or view in Snowflake can support up to one row access policy at a time.
Add (i.e. bind) the row access policy to the region column in the Sales
data table.
USE ROLE SECURITYADMIN;
ALTER TABLE sales ADD ROW ACCESS POLICY security.sales_policy ON (region);
Step 6: Allow a Role to Query the Protected Table Data¶
Grant the SELECT privilege on the protected sales
data to the sales_manager_role
custom role.
GRANT SELECT ON TABLE sales TO ROLE sales_manager_role;
Step 7: Test the Policy¶
After the sales data populates the Sales
data, test the row access policy.
USE ROLE sales_manager_role;
SELECT product, SUM(spend)
FROM sales
WHERE YEAR(sale_date) = 2020
GROUP BY product;
Example: Replacing Policy Subqueries with a Memoizable Function¶
The steps in this example create a memoizable function for each mapping table lookup in the row
access policy conditions. The subquery in each EXISTS
clause specifies the mapping table lookup, where the tables are named
regions
, customers
, and products
, respectively:
CREATE OR REPLACE ROW ACCESS POLICY rap_NO_memoizable_function AS (region_id number, customer_id number, product_id number) RETURNS BOOLEAN -> EXISTS(SELECT 1 FROM regions WHERE id = region_id) OR EXISTS(SELECT 1 FROM customers WHERE id = customer_id) OR EXISTS(SELECT 1 FROM products WHERE id = product_id) ;
For the following steps, assume that the rap_admin
custom role can create row access policies
(i.e. has the CREATE ROW ACCESS POLICY on SCHEMA privilege).
Complete the following steps to replace each of the row access policy mapping table lookups with a memoizable function:
Create a custom role named
functions_admin
to manage the memoizable function:USE ROLE USERADMIN; CREATE ROLE functions_admin;
Grant the following privileges to the
functions_admin
role to allow creating the memoizable function in an existing schema namedgovernance.functions
:USE ROLE SECURITYADMIN; GRANT USAGE ON DATABASE governance TO ROLE functions_admin; GRANT USAGE ON SCHEMA governance.functions TO ROLE functions_admin; GRANT CREATE FUNCTION ON SCHEMA governance.functions TO ROLE functions_admin;
Create a memoizable function for each of the
EXISTS
subquery clauses in the row access policy. Each memoizable function definition takes the same form. For brevity, only one function example is shown:USE ROLE functions_admin; USE SCHEMA governance.functions; CREATE OR REPLACE function allowed_regions() RETURNS array memoizable AS 'SELECT ARRAY_AGG(id) FROM regions';
Use a CREATE ROW ACCESS POLICY statement to define a new row access policy that replaces the subqueries with memoizable functions:
The new row access policy allows for testing queries on a protected table, when the policy uses or does not use the memoizable functions, to quantify the performance impact of using memoizable functions in the policy conditions:
USE ROLE rap_admin; CREATE OR REPLACE ROW ACCESS POLICY rap_with_memoizable_function AS (region_id number, customer_id number, product_id number) RETURNS BOOLEAN -> ARRAY_CONTAINS(region_id, allowed_regions()) OR ARRAY_CONTAINS(customer_id, allowed_customers()) OR ARRAY_CONTAINS(product_id, allowed_products()) ;