In this post we are going to cover row access policies in Snowflake.
A row access policy is a named object with a body that returns true or false. You attach it to a table, and from then on every query against that table gets only the rows the body said true to. A masking policy changes what a value looks like. This one changes whether the row is in the answer at all.
We will start with a table of nine sales rows and build it up step by step. The first thing it hides is the whole table, from the role that owns it.
Every customer name and amount below is invented.
Episode 166 of the Snowflake SQL: Zero to Hero series on TechBrothersIT.
- Let's create some sample data
- Let's make three roles and check what they can see
- Now let's write the policy and attach it
- And now the owner gets nothing
- Now let's run that same statement as three roles
- What happens when we delete a row we cannot see?
- Now the pattern you will actually use
- Can a policy go on a view?
- So where would you look to find out it is there?
- And now six more refusals
- One last thing: let's take the policies off again
Let's create some sample data
We need a table whose rows belong to different owners. The script below makes a schema for this post and a sales table with three regions in it.
-- Sample data for this post. Every customer and amount is invented.
USE DATABASE DEMO_DB;
CREATE OR REPLACE SCHEMA DEMO_DB.ROWS_T166
COMMENT = 'episode 166 - row access policies, dropped at the end';
USE SCHEMA DEMO_DB.ROWS_T166;
CREATE OR REPLACE TABLE DEMO_DB.ROWS_T166.SALES_T166 (
SALE_ID NUMBER,
REGION VARCHAR(10),
CUSTOMER VARCHAR(40),
AMOUNT NUMBER(10,2)
);
INSERT INTO DEMO_DB.ROWS_T166.SALES_T166
(SALE_ID, REGION, CUSTOMER, AMOUNT)
VALUES (1, 'NORTH', 'Northwind Trading', 1200.00),
(2, 'NORTH', 'Borealis Supplies', 840.50),
(3, 'NORTH', 'Tundra Logistics', 2310.75),
(4, 'SOUTH', 'Meridian Foods', 1550.00),
(5, 'SOUTH', 'Equator Imports', 620.25),
(6, 'SOUTH', 'Palm Coast Retail', 1890.00),
(7, 'EAST', 'Sunrise Hardware', 735.00),
(8, 'EAST', 'Harbour Stationery', 1105.40),
(9, 'EAST', 'Lantern Electrical', 1460.00);Let's look at what we just created.
SELECT * FROM DEMO_DB.ROWS_T166.SALES_T166 ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 1 | NORTH | Northwind Trading | 1200.00 |
| 2 | NORTH | Borealis Supplies | 840.50 |
| 3 | NORTH | Tundra Logistics | 2310.75 |
| 4 | SOUTH | Meridian Foods | 1550.00 |
| 5 | SOUTH | Equator Imports | 620.25 |
| 6 | SOUTH | Palm Coast Retail | 1890.00 |
| 7 | EAST | Sunrise Hardware | 735.00 |
| 8 | EAST | Harbour Stationery | 1105.40 |
| 9 | EAST | Lantern Electrical | 1460.00 |
Nine rows and four columns. And here is the shape of it per region, which is the truth of this table and worth keeping in view.
SELECT REGION, COUNT(*) AS HOW_MANY, SUM(AMOUNT) AS TOTAL
FROM DEMO_DB.ROWS_T166.SALES_T166
GROUP BY REGION
ORDER BY REGION;| REGION | HOW_MANY | TOTAL |
|---|---|---|
| EAST | 3 | 3300.40 |
| NORTH | 3 | 4351.25 |
| SOUTH | 3 | 4060.25 |
Three regions, three rows each, and a total for each one.
And the control, before anything exists
This post is entirely about roles getting different answers, so USE ROLE has to actually change what the session can do.
USE SECONDARY ROLES NONE;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| ACCOUNTADMIN | {"roles":"","value":""} |
Empty, and run before a single policy existed. The demo proves this again inside every role excursion, and those grids are below where they happened.
Here is the account before we start.
SHOW ROW ACCESS POLICIES IN ACCOUNT;| created_on | name | database_name | schema_name | kind | owner | comment | owner_role_type | options |
|---|
SELECT COUNT(*) AS POLICIES_ALREADY_HERE
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| POLICIES_ALREADY_HERE |
|---|
| 0 |
Let's make three roles and check what they can see
Two of these roles will be named in the policy. The third one never will be, and it is there to answer a question at the end of the next section.
-- Three roles. Two of them the policy will name, one it never will.
-- The GRANT ROLE lines name a user; YOUR_USER_NAME stands in for mine.
CREATE OR REPLACE ROLE RNORTH_T166 COMMENT = 'should see NORTH rows only';
CREATE OR REPLACE ROLE RSOUTH_T166 COMMENT = 'should see SOUTH rows only';
CREATE OR REPLACE ROLE RNOBODY_T166 COMMENT = 'mentioned nowhere in any policy';
GRANT USAGE ON DATABASE DEMO_DB TO ROLE RNORTH_T166;
GRANT USAGE ON SCHEMA DEMO_DB.ROWS_T166 TO ROLE RNORTH_T166;
GRANT SELECT ON TABLE DEMO_DB.ROWS_T166.SALES_T166 TO ROLE RNORTH_T166;
GRANT USAGE ON WAREHOUSE COMPUTE_WH TO ROLE RNORTH_T166;
-- ... and the same four grants again for RSOUTH_T166 and for RNOBODY_T166.
GRANT ROLE RNORTH_T166 TO USER YOUR_USER_NAME;
GRANT ROLE RSOUTH_T166 TO USER YOUR_USER_NAME;
GRANT ROLE RNOBODY_T166 TO USER YOUR_USER_NAME;Let's see what one of them holds.
SELECT "privilege", "granted_on", "name"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
ORDER BY "granted_on", "privilege";| privilege | granted_on | name |
|---|---|---|
| USAGE | DATABASE | DEMO_DB |
| USAGE | SCHEMA | DEMO_DB.ROWS_T166 |
| SELECT | TABLE | DEMO_DB.ROWS_T166.SALES_T166 |
| USAGE | WAREHOUSE | COMPUTE_WH |
Four grants. Database usage, schema usage, select on the table and a warehouse to run on. Nothing clever.
And with no policy anywhere, let's check that it can read the whole table.
USE ROLE RNORTH_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RNORTH_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 1 | NORTH | Northwind Trading | 1200.00 |
| 2 | NORTH | Borealis Supplies | 840.50 |
| 3 | NORTH | Tundra Logistics | 2310.75 |
| 4 | SOUTH | Meridian Foods | 1550.00 |
| 5 | SOUTH | Equator Imports | 620.25 |
| 6 | SOUTH | Palm Coast Retail | 1890.00 |
| 7 | EAST | Sunrise Hardware | 735.00 |
| 8 | EAST | Harbour Stationery | 1105.40 |
| 9 | EAST | Lantern Electrical | 1460.00 |
All nine rows. So the role is not restricted by anything yet, and whatever changes later is the policy and not the grants.
USE ROLE ACCOUNTADMIN;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| ACCOUNTADMIN | {"roles":"","value":""} |
Now let's write the policy and attach it
A row access policy takes an argument, returns a boolean, and the body decides. Let's name two of our three roles in it.
CREATE OR REPLACE ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166
AS (THE_REGION VARCHAR) RETURNS BOOLEAN ->
(CURRENT_ROLE() = 'RNORTH_T166' AND THE_REGION = 'NORTH')
OR (CURRENT_ROLE() = 'RSOUTH_T166' AND THE_REGION = 'SOUTH');| status |
|---|
| Row access policy 'RAP_REGION_T166' is successfully created. |
Read that body. The north role gets north rows, the south role gets south rows, and nothing else is mentioned at all.
SELECT "name", "kind", "owner", "options", "comment"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| name | kind | owner | options | comment |
|---|---|---|---|---|
| RAP_REGION_T166 | ROW_ACCESS_POLICY | ACCOUNTADMIN |
DESCRIBE gives us the same shape of object as a masking policy did last post: a signature, a return type and a body.
SELECT "name", "signature", "return_type", "body"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| name | signature | return_type | body |
|---|---|---|---|
| RAP_REGION_T166 | (THE_REGION VARCHAR) | BOOLEAN | (CURRENT_ROLE() = 'RNORTH_T166' AND THE_REGION = 'NORTH') OR (CURRENT_ROLE() = 'RSOUTH_T166' AND THE_REGION = 'SOUTH') |
Nothing is filtered yet, because the policy is not attached to anything.
SELECT COUNT(*) AS ROWS_STILL_VISIBLE_TO_THE_OWNER
FROM DEMO_DB.ROWS_T166.SALES_T166;| ROWS_STILL_VISIBLE_TO_THE_OWNER |
|---|
| 9 |
Now let's attach it.
ALTER TABLE DEMO_DB.ROWS_T166.SALES_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166 ON (REGION);| status |
|---|
| Statement executed successfully. |
SELECT POLICY_NAME, POLICY_KIND, REF_COLUMN_NAME, POLICY_STATUS
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'DEMO_DB.ROWS_T166.SALES_T166',
REF_ENTITY_DOMAIN => 'TABLE'));| POLICY_NAME | POLICY_KIND | REF_COLUMN_NAME | POLICY_STATUS |
|---|---|---|---|
| RAP_REGION_T166 | ROW_ACCESS_POLICY | NULL | ACTIVE |
The table carries the policy and its status reads ACTIVE. We will come back to that NULL under REF_COLUMN_NAME later, because it is not the typo it looks like.
And now the owner gets nothing
Let's run the same SELECT as before, as ACCOUNTADMIN, which owns the table and owns the policy.
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|
Column headings and nothing underneath. Not an error, not stars, not a refusal. An empty answer.
SELECT COUNT(*) AS ROWS_THE_OWNER_CAN_SEE
FROM DEMO_DB.ROWS_T166.SALES_T166;| ROWS_THE_OWNER_CAN_SEE |
|---|
| 0 |
SELECT REGION, COUNT(*) AS HOW_MANY
FROM DEMO_DB.ROWS_T166.SALES_T166
GROUP BY REGION
ORDER BY REGION;| REGION | HOW_MANY |
|---|
The reason is in the body. It names RNORTH_T166 and RSOUTH_T166, and ACCOUNTADMIN is neither of those, so the body returns false for every row in the table.
This is the difference from a masking policy in one line. A masked row is still a row. A row this policy says no to is simply not in the answer, and the owner of the table is not exempt from it.
Now let's run that same statement as three roles
Nothing below changes by one character except the role in the session.
The north role
USE ROLE RNORTH_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RNORTH_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 1 | NORTH | Northwind Trading | 1200.00 |
| 2 | NORTH | Borealis Supplies | 840.50 |
| 3 | NORTH | Tundra Logistics | 2310.75 |
Three rows, all north. The policy let through exactly the rows its body said yes to. And the count agrees with the select, which is worth checking rather than assuming.
SELECT COUNT(*) AS WHAT_THE_COUNT_SAYS
FROM DEMO_DB.ROWS_T166.SALES_T166;| WHAT_THE_COUNT_SAYS |
|---|
| 3 |
And now the part that is quiet about itself
Let's ask that same role for some aggregates.
SELECT COUNT(*) AS ROWS_COUNTED,
COUNT(DISTINCT REGION) AS REGIONS_VISIBLE,
MIN(SALE_ID) AS LOWEST_ID,
MAX(SALE_ID) AS HIGHEST_ID,
SUM(AMOUNT) AS TOTAL_VISIBLE
FROM DEMO_DB.ROWS_T166.SALES_T166;| ROWS_COUNTED | REGIONS_VISIBLE | LOWEST_ID | HIGHEST_ID | TOTAL_VISIBLE |
|---|---|---|---|---|
| 3 | 1 | 1 | 3 | 4351.25 |
One region visible, the lowest and highest id it can see, and a total. Compare that total with the per-region grid in the first section: it is the real north total, to the penny.
That number is correct, and nothing in the grid says it is a subset. An aggregate over a table with a row access policy on it is computed over the rows you can see, and it comes back looking exactly like an aggregate over the whole table. It is a right answer to a question nobody asked.
And asking for one specific row by its key does not help.
SELECT SALE_ID, REGION, CUSTOMER
FROM DEMO_DB.ROWS_T166.SALES_T166
WHERE SALE_ID = 4;| SALE_ID | REGION | CUSTOMER |
|---|
Empty. Sale 4 is in the table, as the first section shows. The policy does not refuse the question. It makes it unanswerable.
The south role
USE ROLE RSOUTH_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RSOUTH_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 4 | SOUTH | Meridian Foods | 1550.00 |
| 5 | SOUTH | Equator Imports | 620.25 |
| 6 | SOUTH | Palm Coast Retail | 1890.00 |
SELECT COUNT(*) AS WHAT_THE_SOUTH_ROLE_COUNTS
FROM DEMO_DB.ROWS_T166.SALES_T166;| WHAT_THE_SOUTH_ROLE_COUNTS |
|---|
| 3 |
Three rows, all south. So that one statement is three different tables so far, depending on who runs it.
And the role the policy never mentions
USE ROLE RNOBODY_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RNOBODY_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|
SELECT COUNT(*) AS ROWS_FOR_A_ROLE_NOT_IN_THE_POLICY
FROM DEMO_DB.ROWS_T166.SALES_T166;| ROWS_FOR_A_ROLE_NOT_IN_THE_POLICY |
|---|
| 0 |
Nothing, the same as the owner. It does hold SELECT on the table, and it can still read the table's shape.
SELECT "name", "type", "null?"
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| name | type | null? |
|---|---|---|
| SALE_ID | NUMBER(38,0) | Y |
| REGION | VARCHAR(10) | Y |
| CUSTOMER | VARCHAR(40) | Y |
| AMOUNT | NUMBER(10,2) | Y |
All four column names. So a role can know everything about the structure of a table and see none of its rows.
USE ROLE ACCOUNTADMIN;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| ACCOUNTADMIN | {"roles":"","value":""} |
What happens when we delete a row we cannot see?
This is the part I would put a warning on. The sales table is never written to, so let's make a second table for this and put the same policy on it.
-- A second table, so the sales table is never written to.
CREATE OR REPLACE TABLE DEMO_DB.ROWS_T166.WRITABLE_T166 (
SALE_ID NUMBER,
REGION VARCHAR(10),
CUSTOMER VARCHAR(40)
);
INSERT INTO DEMO_DB.ROWS_T166.WRITABLE_T166 (SALE_ID, REGION, CUSTOMER)
VALUES (1, 'NORTH', 'Northwind Trading'),
(2, 'NORTH', 'Borealis Supplies'),
(3, 'SOUTH', 'Meridian Foods'),
(4, 'SOUTH', 'Equator Imports');
GRANT SELECT, INSERT, UPDATE, DELETE
ON TABLE DEMO_DB.ROWS_T166.WRITABLE_T166 TO ROLE RNORTH_T166;SELECT * FROM DEMO_DB.ROWS_T166.WRITABLE_T166 ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER |
|---|---|---|
| 1 | NORTH | Northwind Trading |
| 2 | NORTH | Borealis Supplies |
| 3 | SOUTH | Meridian Foods |
| 4 | SOUTH | Equator Imports |
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166 ON (REGION);| status |
|---|
| Statement executed successfully. |
Four rows in it, two north and two south. Now let's see what the north role makes of it.
USE ROLE RNORTH_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RNORTH_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER
FROM DEMO_DB.ROWS_T166.WRITABLE_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER |
|---|---|---|
| 1 | NORTH | Northwind Trading |
| 2 | NORTH | Borealis Supplies |
Two rows, as expected. Now let's delete one it cannot see.
DELETE FROM DEMO_DB.ROWS_T166.WRITABLE_T166 WHERE SALE_ID = 3;| number of rows deleted |
|---|
| 0 |
SELECT "number of rows deleted" AS ROWS_DELETED
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| ROWS_DELETED |
|---|
| 0 |
Nought rows deleted. No error, no warning, no refusal of any kind.
Now let's delete one it can see.
DELETE FROM DEMO_DB.ROWS_T166.WRITABLE_T166 WHERE SALE_ID = 2;| number of rows deleted |
|---|
| 1 |
SELECT "number of rows deleted" AS ROWS_DELETED
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| ROWS_DELETED |
|---|
| 1 |
One. Same statement shape, same role, same table. The only difference between those two runs is the number in the WHERE clause, and the only thing that tells them apart afterwards is the row count.
SELECT SALE_ID, REGION, CUSTOMER
FROM DEMO_DB.ROWS_T166.WRITABLE_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER |
|---|---|---|
| 1 | NORTH | Northwind Trading |
And now let's take the policy off as the owner and look at the actual table.
USE ROLE ACCOUNTADMIN;| status |
|---|
| Statement executed successfully. |
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166;| status |
|---|
| Statement executed successfully. |
SELECT SALE_ID, REGION, CUSTOMER
FROM DEMO_DB.ROWS_T166.WRITABLE_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER |
|---|---|---|
| 1 | NORTH | Northwind Trading |
| 3 | SOUTH | Meridian Foods |
| 4 | SOUTH | Equator Imports |
SELECT COUNT(*) AS ROWS_LEFT_IN_THE_WRITABLE_TABLE
FROM DEMO_DB.ROWS_T166.WRITABLE_T166;| ROWS_LEFT_IN_THE_WRITABLE_TABLE |
|---|
| 3 |
Three rows survived, and sale 3 is one of them. That is the row the first delete named by key. The delete did not fail. It matched nothing, because the row was not in scope.
A row you cannot see is a row you cannot delete, and the row count is the only thing that tells you. If your code checks for an error rather than checking how many rows it changed, it will think that delete worked.
Now the pattern you will actually use
Naming roles in a body does not scale, and every change means editing the policy. The usual pattern is to look the roles up in a table instead.
-- A mapping table, and one role deliberately mapped to two regions.
CREATE OR REPLACE TABLE DEMO_DB.ROWS_T166.MAP_T166 (
ROLE_NAME VARCHAR(40),
REGION_NAME VARCHAR(10)
);
INSERT INTO DEMO_DB.ROWS_T166.MAP_T166 (ROLE_NAME, REGION_NAME)
VALUES ('RNORTH_T166', 'NORTH'),
('RSOUTH_T166', 'SOUTH'),
('RSOUTH_T166', 'EAST');SELECT ROLE_NAME, REGION_NAME
FROM DEMO_DB.ROWS_T166.MAP_T166
ORDER BY ROLE_NAME, REGION_NAME;| ROLE_NAME | REGION_NAME |
|---|---|
| RNORTH_T166 | NORTH |
| RSOUTH_T166 | EAST |
| RSOUTH_T166 | SOUTH |
Three rows, and the south role is mapped to two regions.
Now a policy whose body is a subquery against that table.
CREATE OR REPLACE ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166
AS (THE_REGION VARCHAR) RETURNS BOOLEAN ->
EXISTS (SELECT 1
FROM DEMO_DB.ROWS_T166.MAP_T166
WHERE ROLE_NAME = CURRENT_ROLE()
AND REGION_NAME = THE_REGION);| status |
|---|
| Row access policy 'RAP_MAPPED_T166' is successfully created. |
Before we can attach it, one question. Can a table hold two of these?
ALTER TABLE DEMO_DB.ROWS_T166.SALES_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166 ON (REGION);003549 (23505): Object SALES_T166 already has a ROW_ACCESS_POLICY. Only one ROW_ACCESS_POLICY is allowed at a time.
003549. Only one row access policy at a time, so one table holds one. Let's swap them over.
ALTER TABLE DEMO_DB.ROWS_T166.SALES_T166
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166;| status |
|---|
| Statement executed successfully. |
ALTER TABLE DEMO_DB.ROWS_T166.SALES_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166 ON (REGION);| status |
|---|
| Statement executed successfully. |
SELECT POLICY_NAME, POLICY_KIND, REF_COLUMN_NAME, POLICY_STATUS
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'DEMO_DB.ROWS_T166.SALES_T166',
REF_ENTITY_DOMAIN => 'TABLE'));| POLICY_NAME | POLICY_KIND | REF_COLUMN_NAME | POLICY_STATUS |
|---|---|---|---|
| RAP_MAPPED_T166 | ROW_ACCESS_POLICY | NULL | ACTIVE |
Now let's wear the south role, which the mapping gives two regions.
USE ROLE RSOUTH_T166;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| RSOUTH_T166 | {"roles":"","value":""} |
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 4 | SOUTH | Meridian Foods | 1550.00 |
| 5 | SOUTH | Equator Imports | 620.25 |
| 6 | SOUTH | Palm Coast Retail | 1890.00 |
| 7 | EAST | Sunrise Hardware | 735.00 |
| 8 | EAST | Harbour Stationery | 1105.40 |
| 9 | EAST | Lantern Electrical | 1460.00 |
Six rows, south and east. Two regions out of one role, and the policy body never changed.
SELECT REGION, COUNT(*) AS HOW_MANY
FROM DEMO_DB.ROWS_T166.SALES_T166
GROUP BY REGION
ORDER BY REGION;| REGION | HOW_MANY |
|---|---|
| EAST | 3 |
| SOUTH | 3 |
And here is the fact that makes the pattern work
Let's ask that same role to read the mapping table directly.
SELECT ROLE_NAME, REGION_NAME
FROM DEMO_DB.ROWS_T166.MAP_T166
ORDER BY ROLE_NAME;002003 (42S02): SQL compilation error: Object 'DEMO_DB.ROWS_T166.MAP_T166' does not exist or not authorized. Your primary role RSOUTH_T166 must have at least one privilege granted on TABLE DEMO_DB.ROWS_T166.MAP_T166.
002003. It cannot. It was never granted anything on that table.
So the body read a table the caller cannot see at all. A policy body runs with the policy's rights, not the caller's. That is the whole reason a mapping table is usable: the mapping itself stays private while it decides what everybody gets.
USE ROLE ACCOUNTADMIN;| status |
|---|
| Statement executed successfully. |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| ACCOUNTADMIN | {"roles":"","value":""} |
Can a policy go on a view?
Yes. Let's make a view and a third policy for it.
CREATE OR REPLACE VIEW DEMO_DB.ROWS_T166.SALES_VIEW_T166 AS
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166;| status |
|---|
| View SALES_VIEW_T166 successfully created. |
CREATE OR REPLACE ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_VIEW_T166
AS (THE_REGION VARCHAR) RETURNS BOOLEAN ->
CURRENT_ROLE() = 'ACCOUNTADMIN';| status |
|---|
| Row access policy 'RAP_VIEW_T166' is successfully created. |
ALTER VIEW DEMO_DB.ROWS_T166.SALES_VIEW_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_VIEW_T166 ON (REGION);| status |
|---|
| Statement executed successfully. |
And the catalogue reports it with the entity domain reading VIEW.
SELECT POLICY_NAME, POLICY_KIND, REF_ENTITY_NAME, REF_ENTITY_DOMAIN, POLICY_STATUS
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
POLICY_NAME => 'DEMO_DB.ROWS_T166.RAP_VIEW_T166'));| POLICY_NAME | POLICY_KIND | REF_ENTITY_NAME | REF_ENTITY_DOMAIN | POLICY_STATUS |
|---|---|---|---|---|
| RAP_VIEW_T166 | ROW_ACCESS_POLICY | SALES_VIEW_T166 | VIEW | ACTIVE |
So where would you look to find out it is there?
Last post, DESCRIBE TABLE grew a policy name column and named the masking policy. Let's try the same thing here, with a row access policy attached.
DESCRIBE TABLE DEMO_DB.ROWS_T166.SALES_T166;policy name is the eleventh — scroll right| name | type | kind | null? | default | primary key | unique key | check | expression | comment | policy name | privacy domain | write default |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| SALE_ID | NUMBER(38,0) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL | NULL |
| REGION | VARCHAR(10) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL | NULL |
| CUSTOMER | VARCHAR(40) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL | NULL |
| AMOUNT | NUMBER(10,2) | COLUMN | Y | NULL | N | N | NULL | NULL | NULL | NULL | NULL | NULL |
A real NULL under policy name on all four columns. DESCRIBE TABLE does not mention a row access policy at all.
POLICY_REFERENCES does know about it.
SELECT POLICY_NAME, POLICY_KIND, REF_ENTITY_NAME, REF_ENTITY_DOMAIN, REF_COLUMN_NAME
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
POLICY_NAME => 'DEMO_DB.ROWS_T166.RAP_MAPPED_T166'));| POLICY_NAME | POLICY_KIND | REF_ENTITY_NAME | REF_ENTITY_DOMAIN | REF_COLUMN_NAME |
|---|---|---|---|---|
| RAP_MAPPED_T166 | ROW_ACCESS_POLICY | SALES_T166 | TABLE | NULL |
There it is, against the table. But look at REF_COLUMN_NAME: a real NULL again.
That is not an omission. When we wrote ON (REGION) we were passing REGION to the policy as an argument, not attaching the policy to that column. The policy is on the table, so there is no column name to report.
The NULL here comes from an INFORMATION_SCHEMA function, which returns a real SQL NULL. A missing value in SHOW output comes back as an empty string instead, which is why some grids in this series show an empty cell where others show the word NULL.
And now six more refusals
Two refusals have already happened on this page: the second policy on one table, and the role that could not read the mapping table. Six more are worth running on purpose, and the first two differ in when they arrive.
First, a policy whose argument is a number.
CREATE OR REPLACE ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_WRONGTYPE_T166
AS (THE_AMOUNT NUMBER) RETURNS BOOLEAN ->
THE_AMOUNT > 1000;| status |
|---|
| Row access policy 'RAP_WRONGTYPE_T166' is successfully created. |
Created, happily. Now let's attach it to a text column.
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_WRONGTYPE_T166 ON (REGION);003554 (42601): SQL compilation error: Column 'REGION' data type 'VARCHAR(10)' does not match with Row access policy data type 'NUMBER(38,0)'.
003554, and the message names both types. So the type check happens when you attach, not when you create.
SELECT COUNT(*) AS POLICIES_ON_THE_WRITABLE_TABLE
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'DEMO_DB.ROWS_T166.WRITABLE_T166',
REF_ENTITY_DOMAIN => 'TABLE'));| POLICIES_ON_THE_WRITABLE_TABLE |
|---|
| 0 |
And nothing landed. Now a body that returns a number instead of a boolean.
CREATE OR REPLACE ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_NOTBOOL_T166
AS (THE_REGION VARCHAR) RETURNS BOOLEAN ->
LENGTH(THE_REGION);090207 (42601): Declared return type 'BOOLEAN' is incompatible with actual return type 'NUMBER(18,0)'
090207, and this one is refused at CREATE. The object never existed.
SELECT COUNT(*) AS NOTBOOL_POLICY_ROWS
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| NOTBOOL_POLICY_ROWS |
|---|
| 0 |
Next, dropping a policy while a table still carries it.
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166;003531 (23001): SQL compilation error: Policy RAP_MAPPED_T166 cannot be dropped/replaced as it is associated with one or more entities.
003531, the same code a masking policy gave last post for the same mistake.
SELECT COUNT(*) AS MAPPED_POLICY_STILL_THERE
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| MAPPED_POLICY_STILL_THERE |
|---|
| 1 |
Still there. Now three smaller ones. A policy that does not exist.
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.NO_SUCH_POLICY_T166 ON (REGION);002003 (02000): SQL compilation error: Row access policy 'DEMO_DB.ROWS_T166.NO_SUCH_POLICY_T166' does not exist or not authorized. Your primary role ACCOUNTADMIN must have at least one privilege granted on POLICY DEMO_DB.ROWS_T166.NO_SUCH_POLICY_T166.
Detaching a policy a table never had.
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_VIEW_T166;003548 (55000): Policy RAP_VIEW_T166 is not attached to TABLE WRITABLE_T166.
003548, and it names both objects, which makes it easy to see what you got wrong.
And a column that does not exist.
ALTER TABLE DEMO_DB.ROWS_T166.WRITABLE_T166
ADD ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_REGION_T166 ON (NO_SUCH_COLUMN_T166);000904 (42000): SQL compilation error: error line 2 at position 62 invalid identifier 'NO_SUCH_COLUMN_T166'
000904, invalid identifier. That one is a typo, not a rule.
One last thing: let's take the policies off again
Let's detach both of them.
ALTER TABLE DEMO_DB.ROWS_T166.SALES_T166
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166;| status |
|---|
| Statement executed successfully. |
ALTER VIEW DEMO_DB.ROWS_T166.SALES_VIEW_T166
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_VIEW_T166;| status |
|---|
| Statement executed successfully. |
SELECT COUNT(*) AS REFERENCES_LEFT
FROM TABLE(DEMO_DB.INFORMATION_SCHEMA.POLICY_REFERENCES(
REF_ENTITY_NAME => 'DEMO_DB.ROWS_T166.SALES_T166',
REF_ENTITY_DOMAIN => 'TABLE'));| REFERENCES_LEFT |
|---|
| 0 |
And read the table one more time.
SELECT SALE_ID, REGION, CUSTOMER, AMOUNT
FROM DEMO_DB.ROWS_T166.SALES_T166
ORDER BY SALE_ID;| SALE_ID | REGION | CUSTOMER | AMOUNT |
|---|---|---|---|
| 1 | NORTH | Northwind Trading | 1200.00 |
| 2 | NORTH | Borealis Supplies | 840.50 |
| 3 | NORTH | Tundra Logistics | 2310.75 |
| 4 | SOUTH | Meridian Foods | 1550.00 |
| 5 | SOUTH | Equator Imports | 620.25 |
| 6 | SOUTH | Palm Coast Retail | 1890.00 |
| 7 | EAST | Sunrise Hardware | 735.00 |
| 8 | EAST | Harbour Stationery | 1105.40 |
| 9 | EAST | Lantern Electrical | 1460.00 |
All nine rows, exactly as the first section had them. Nothing in the table ever changed except the one row we deleted out of the other table on purpose.
With nothing attached, the drop that was refused goes through.
DROP ROW ACCESS POLICY DEMO_DB.ROWS_T166.RAP_MAPPED_T166;| status |
|---|
| RAP_MAPPED_T166 successfully dropped. |
And here is the account at the end, after the roles, the tables, the view and the schema have gone too.
SHOW ROW ACCESS POLICIES IN ACCOUNT;| created_on | name | database_name | schema_name | kind | owner | comment | owner_role_type | options |
|---|
SELECT COUNT(*) AS MY_POLICIES_LEFT
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()))
WHERE "name" LIKE '%\\_T166' ESCAPE '\\';| MY_POLICIES_LEFT |
|---|
| 0 |
SELECT COUNT(*) AS MY_ROLES_LEFT
FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));| MY_ROLES_LEFT |
|---|
| 0 |
SELECT CURRENT_ROLE() AS THE_ROLE, CURRENT_SECONDARY_ROLES() AS THE_SECONDARY_ROLES;| THE_ROLE | THE_SECONDARY_ROLES |
|---|---|
| ACCOUNTADMIN | {"roles":"ORGADMIN","value":"ALL"} |
Counts of 0, and the control put back the way we found it.
Key points
- A row access policy has a boolean body and is attached to a table with
ALTER TABLE ... ADD ROW ACCESS POLICY ... ON (col). - A row the body says no to is absent, not redacted, and the owner of the table is not exempt.
- One unchanged statement gave three roles three different tables, and a role the policy never mentions gets nothing.
- Aggregates are computed over the rows you can see and never say so. The restricted total on this page is the real regional total.
DELETEon a row you cannot see reports 0 rows deleted and raises nothing. The count is the only tell.- A body can be an
EXISTSsubquery against a mapping table, and the body runs with the policy's rights, not the caller's. - One table holds one row access policy. A second one is
003549. - A policy can go on a view as well, and
POLICY_REFERENCESreports the entity domain asVIEW. DESCRIBE TABLEsays nothing about it. OnlyPOLICY_REFERENCESknows, and even thereREF_COLUMN_NAMEisNULL, becauseON (col)names an argument.
In the next post we look at tags and object tagging, which label an object rather than filtering it — including a value that stayed on an object after its own tag stopped allowing it.
Every query and every result grid on this page was captured from a live Snowflake account (version 10.36.101) running the demo script for this episode. USE SECONDARY ROLES NONE was set and proved off before the first policy existed and again inside every role excursion, and those proofs are on the page where they happened. Every customer name and amount is invented. The sales table is never written to; a separate table takes the one delete. The grants that name a user are written here with a stand-in name. Everything created here was dropped here and the closing counts are on the page. Nothing is timed and no performance claims are made anywhere in this series. Behaviour follows the Snowflake CREATE ROW ACCESS POLICY documentation.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.