Parameterized secure views provide data security and row access control to applications. They filter query results based on the application user's identity.
This tutorial describes how to set up parameterized secure views in Cloud SQL, configure database roles and privileges to restrict access to base tables, and verify data security. The examples provided in this document are for demonstration purposes only.
What are parameterized secure views?
As a general best practice, your application should run using a service account with the minimal required access to your database. For example, if your application shouldn't write to your database, it should use a role with read-only access. You should configure these access controls at the database level.
When your application needs more granular security than standard database-level access permits, you can use parameterized secure views to make sure that users view only their authorized data. Parameterized secure views provide dynamic data security and row access control suitable for multi-tenant SaaS applications without requiring separate database roles for each application end user.
Using parameterized secure views provides three main benefits:
- Dynamic data security and row access control: Filters queries using end-user identifiers so users access only their authorized data regardless of query phrasing.
- Easier role management: Uses a single shared database role for all application users instead of managing separate database roles for each individual.
Objectives
- Create parameterized secure views with named view parameters.
- Create the database role that is used by the application to connect to the database and access parameterized secure views.
- Grant the new role permissions to the parameterized secure views and revoke access to the base tables.
- Connect using the new role and verify that the restricted tables can't be accessed.
- Run queries on the parameterized view using the
mysql.execute_parameterized_querystored procedure or using the QueryData API.
Costs
In this document, you use the following billable components of Google Cloud:
To generate a cost estimate based on your projected usage, use the pricing calculator.
New Google Cloud users might be eligible for a free trial.
To avoid continued billing, delete the resources you created when you finish the tasks in this document. For more information, see Clean up.
Before you begin
Before you create parameterized secure views, complete the following prerequisites.
Enable billing and required APIs
In the Google Cloud console, select a project.
Make sure that billing is enabled for your Google Cloud project.
Enable the Cloud APIs necessary to create and connect to Cloud SQL.
In the Confirm project step, click Next to confirm the name of the project you are going to make changes to.
In the Enable APIs step, click Enable to enable the following:
- Cloud SQL for MySQL API
- Knowledge Catalog API
Create and connect to a database
Prepare your environment
To prepare for running queries on a parameterized secure view, set up the database, database roles, and the application schema.
Enable the database flag
- Make sure that your Cloud SQL for MySQL instance is running Cloud SQL for MySQL 8.0.43 or later. If your Cloud SQL for MySQL 8.0 instance is running an earlier minor version, then you can perform a minor version upgrade.
- Enable the
cloudsql_parameterized_secure_viewdatabase flag for your instance. This flag change doesn't require a database restart. For more information, see Configure an instance's database flags.
Set up the database and roles
In the Google Cloud console, go to the Cloud SQL page.
Select an instance from the list.
In the navigation menu, click Cloud SQL Studio.
Sign in to Studio using administrative user account.
Click Authenticate. The Explorer pane displays a list of the objects in your database.
Click New SQL editor tab or New tab to open a new tab.
Create a database named
store_dbfor the application data and views:CREATE DATABASE IF NOT EXISTS store_db; USE store_db;Create a new database role for running queries against parameterized views. The application uses this role to connect to the database and run queries with access restricted strictly to authorized views:
CREATE ROLE 'psv_user';
Create the table and insert data
In the Google Cloud console, go to the Cloud SQL page.
Select a Cloud SQL for MySQL instance from the list.
In the navigation menu, click Cloud SQL Studio.
Sign in to Studio using database authentication.
Click New tab to open a new SQL editor tab.
Create the tables and insert data:
CREATE TABLE store_db.checked_items ( bag_id INT, timestamp TIMESTAMP, loc_code CHAR(3), scan_type CHAR(1), location TEXT, customer_id INT ); INSERT INTO store_db.checked_items (bag_id, 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);
Create parameterized secure views and set up access privileges
To create parameterized secure views, follow these steps:
In the Google Cloud console, go to the Cloud SQL page.
Select an instance from the list.
In the navigation menu, click Cloud SQL Studio.
Sign in to Studio and connect to the
databaseas thestore_dbuser account you used in the previous section.Click Authenticate. The Explorer pane displays a list of the objects in your database.
Click New SQL editor tab or New tab to open a new tab.
To provide limited access to the view, create a parameterized view:
CREATE VIEW store_db.secure_checked_items AS SELECT bag_id, timestamp, location FROM store_db.checked_items t WHERE customer_id = @app_end_userid;Grant SELECT access on the parameterized view to the
psv_userrole:GRANT SELECT ON store_db.secure_checked_items TO 'psv_user';Grant execution permission on the built-in system stored procedure used to execute parameterized queries safely:
GRANT EXECUTE ON PROCEDURE mysql.execute_parameterized_query TO 'psv_user';Optional. Explicitly revoke any direct privileges on the base table from
psv_userto ensure users cannot bypass the secure view:REVOKE ALL PRIVILEGES ON store_db.checked_items FROM 'psv_user';Grant the
psv_userrole to the IAM-authenticated database user or application login account:GRANT 'psv_user' TO 'IAM_USER_EMAIL'@'%'; SET DEFAULT ROLE 'psv_user' TO 'IAM_USER_EMAIL'@'%';Replace
IAM_USER_EMAILwith your IAM user email address or application login account.
Verify data security
To verify that you can query the parameterized secure views, follow these steps:
Sign in to Cloud SQL Studio using the restricted IAM email address or application login.
Click New tab to open a new SQL editor tab.
Query the base table and verify that access is denied.
SELECT * FROM store_db.checked_items;The database returns the
ERROR 1142 (42000): SELECT command denied to user 'IAM_USER_EMAIL'@'%' for table 'checked_items'error.Query the parameterized secure view using the built-in
mysql.execute_parameterized_querystored procedure.CALL mysql.execute_parameterized_query( 'SELECT * FROM store_db.secure_checked_items', 'app_end_userid=303' );Query the parameterized secure views using SQL syntax and the
QueryDatarequest with PSV parameters.curl -X POST \ "https://geminidataanalytics.googleapis.com/v1beta/projects/PROJECT_ID/locations/REGION:queryData" \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json; charset=utf-8" \ -d '{ "prompt": "Show me the checked items.", "context": { "datasource_references": { "cloud_sql_reference": { "database_reference": { "engine": "MYSQL", "project_id": "PROJECT_ID", "region": "REGION", "instance_id": "INSTANCE_ID", "database_id": "DATABASE_ID" } } }, "parameterized_secure_view_parameters": { "parameters": { "key": "app_end_userid" "app_end_userid": "303" } } }, "generation_options": { "generate_query_result": true, "generate_natural_language_answer": true, "generate_explanation": true } }'Replace the following values:
PROJECT_ID: Your Google Cloud project ID.REGION: The region where your Cloud SQL for MySQL instance is located.INSTANCE_ID: The ID of your Cloud SQL for MySQL instance.DATABASE_ID: The ID of your Cloud SQL for MySQL database.
Clean up
Delete the cluster
When you delete the cluster that you created in the before you begin section, you also delete all of the objects you created.
In the Google Cloud console, go to the Cloud SQL page.
Select an instance from the list.
Click Delete.
Confirm that you want to delete the instance by entering the instance name and clicking Delete.
What's next
Learn about parameterized secure views.
Learn how to use parameterized secure views.