This tutorial shows how to use parameterized views in Spanner to restrict access to data based on parameters that an application passes securely.
For more information about parameterized views, see Parameterized views overview.
Objectives
- Create a table with sample data.
- Create a parameterized view that filters data based on a secure context parameter.
- Create a database role for fine-grained access control that the application uses to connect to the database and access the parameterized view.
- Grant the new role permissions to query the parameterized view without granting it access to the base table.
- Query the parameterized view from an application by passing a parameter in the secure context.
Before you begin
You need a Spanner instance and database to complete this tutorial. If you don't have one, see Create and query a database using the Google Cloud console to create an instance and a Spanner database.
Prepare sample data
In the Google Cloud console, go to the Spanner page.
Click the name of the instance that you want to use, then select the database to use for this tutorial.
On the Database overview page, in the navigation menu, click Spanner Studio.
Enter the following DDL statement to create the
checked_itemstable:CREATE TABLE checked_items( bag_id INT64, item_timestamp TIMESTAMP, loc_code STRING(3), scan_type STRING(1), location STRING(MAX), customer_id INT64 ) PRIMARY KEY(bag_id);Click Run.
After Spanner creates the table, enter the following statement to insert sample data into the table:
INSERT INTO checked_items (bag_id, item_timestamp, loc_code, scan_type, location, customer_id) VALUES (101, '2023-10-26 10:00:00', 'ABC', 'I', 'Warehouse A', 123), (102, '2023-10-26 10:15:30', 'DEF', 'O', 'Loading Dock B', 456), (103, '2023-10-26 10:30:45', 'GHI', 'I', 'Conveyor Belt 1', 789), (104, '2023-10-26 11:00:00', 'JKL', 'O', 'Shipping Area C', 101), (105, '2023-10-26 11:45:15', 'MNO', 'I', 'Sorting Station D', 202), (106, '2023-10-26 12:00:00', 'PQR', 'O', 'Truck Bay E', 303);Click Run.
Create a parameterized view
Create a view with SQL SECURITY DEFINER that filters rows from
checked_items based on the value of app_end_userid provided by
SECURE_CONTEXT(). This function retrieves parameter values that are
securely passed from the application.
In the query editor, enter the following statement to create the
secure_checked_itemsview:CREATE VIEW secure_checked_items SQL SECURITY DEFINER AS (SELECT bag_id, item_timestamp, location FROM checked_items t WHERE customer_id = CAST(SECURE_CONTEXT('app_end_userid'), INT64));Click Run.
Set up access privileges
To ensure that users can only access data through the parameterized
view, create a fine-grained access control role and
grant that role SELECT permission on the view. Ensure that neither the role
nor the public system
role has privileges on the underlying base table.
In the query editor, enter the following statement to create the
pv_userrole:CREATE ROLE pv_user;Grant access to the view:
GRANT SELECT ON VIEW secure_checked_items TO ROLE pv_user;Click Run.
However, if you need to call SECURE_CONTEXT() directly to debug your
queries, grant yourself the spanner_secure_context_reader role. You
must not grant this role to untrusted agents.
Query data using the parameterized view
Your application queries a parameterized view by first setting parameters
in the secure context of the query request, and then executing a standard
SELECT query against the view. The view uses the SECURE_CONTEXT() function
to filter results based on parameter values.
The following code samples demonstrate how to set the app_end_userid secure
parameter to 303 and query the secure_checked_items view:
C#
Go
Java
Python
This query returns only rows from checked_items where customer_id is 303:
+--------+---------------------------+-------------+
| bag_id | item_timestamp | location |
+--------+---------------------------+-------------+
| 106 | 2023-10-26 12:00:00+00:00 | Truck Bay E |
+--------+---------------------------+-------------+
1 row.
If your application connects to Spanner using a principal that
has been granted the pv_user role, any attempt to query the checked_items
table directly results in a permission denied error.
What's next
- Learn more about Parameterized views overview.
- Learn how to Create and manage views.
- Learn more about Fine-grained access control.