This page explains how to configure fine-grained access control for Spanner for GoogleSQL-dialect databases and PostgreSQL-dialect databases.
To learn about fine-grained access control, see About fine-grained access control .
Configure fine-grained access control by following these steps:
Fine-grained access control users must then specify a database role to perform queries, DML, or row operations against the database.
Before you begin
Ensure that each principal who is to be a fine-grained access control user is granted
the Cloud Spanner Viewer
IAM role ( roles/spanner.viewer
).
This role is recommended at the project level for users who must interact with
Spanner resources in the Google Cloud console.
For instructions, see Grant permissions to principals .
Create database roles and grant privileges
A database role is a collection of fine-grained access privileges. You can create up to 100 database roles for each database.
Decide on roles and role hierarchies in your database and encode them in DDL. As with other schema changes in Spanner, we strongly recommend issuing schema changes in a batch rather than separately. For more information, see Limit the frequency of schema updates .
Console
To create a database role and grant fine-grained access privileges to it, follow these steps:
-
Go to the Instancespage in the Google Cloud console.
-
Select the instance containing the database for which you want to add the role.
-
Select the database.
-
On the Overviewpage, click Spanner Studio.
-
On the Spanner Studiopage, for each database role that you want to create and grant privileges to, follow these steps:
-
To create the role, enter the following statement:
CREATE ROLE ROLE_NAME ;
Don't click Submityet.
-
To grant privileges to the role, enter a
GRANT
statement on the next line after theCREATE ROLE
statement.For syntax details for the
GRANT
statement, see GoogleSQL data definition language . For information about privileges, see Fine-grained access control privileges .For example, to grant
SELECT
,INSERT
, andUPDATE
on tablesemployees
andcontractors
to the database rolehr_manager
, enter the following statement:GoogleSQL
GRANT SELECT , INSERT , UPDATE ON TABLE employees , contractors TO ROLE hr_manager ;
PostgreSQL
GRANT SELECT , INSERT , UPDATE ON TABLE employees , contractors TO hr_manager ;
You can use a DDL template for the
GRANT
statement. In the Explorerpane, navigate to the role for which you want to grant privileges. Click View actionsand select the type of privilege you want to grant access to for this role. TheGRANT
template statement populates in a new editor tab.
-
-
Click Submit.
If there are errors in your DDL, the Google Cloud console returns an error.
gcloud
To create a database role and grant fine-grained access privileges to it,
use the gcloud spanner databases ddl update
command with CREATE ROLE
and GRANT
statements.
For syntax details on the CREATE ROLE
and GRANT
statements, see GoogleSQL data definition language
.
For example, use the following command to create a database role and grant privileges to it on one or more tables.
GoogleSQL
gcloud spanner databases ddl update DATABASE_NAME --instance = INSTANCE_NAME \ --ddl = 'CREATE ROLE ROLE_NAME ; GRANT PRIVILEGES ON TABLE TABLES TO ROLE ROLE_NAME ;'
PostgreSQL
gcloud spanner databases ddl update DATABASE_NAME --instance = INSTANCE_NAME \ --ddl = 'CREATE ROLE ROLE_NAME ; GRANT PRIVILEGES ON TABLE TABLES TO ROLE_NAME ;'
Replace the following:
-
PRIVILEGES
is a comma-delimited list of fine-grained access control privileges. For information about privileges, see Fine-grained access control privileges . -
TABLES
is a comma-delimited list of tables.
For example, to grant SELECT
, INSERT
, and UPDATE
on the employees
and contractors
tables to the
database role hr_analyst
in the database hrdb1
in the instance hr
,
enter the following statement:
GoogleSQL
gcloud spanner databases ddl update hrdb1 --instance = hr \ --ddl = 'CREATE ROLE hr_analyst; GRANT SELECT, INSERT, UPDATE ON TABLE employees, contractors TO ROLE hr_analyst;'
PostgreSQL
gcloud spanner databases ddl update hrdb1 --instance = hr \ --ddl = 'CREATE ROLE hr_analyst; GRANT SELECT, INSERT, UPDATE ON TABLE employees, contractors TO hr_analyst;'
Client libraries
These code samples both create and drop a database role.
C++
C#
Go
Java
Node.js
PHP
Python
Ruby
Create a hierarchy of roles with inheritance
You can create a hierarchy of database roles by granting one database role to another. Child roles (known as member roles ) inherit privileges from the parent.
To grant a database role to another database role, use the following statement:
GoogleSQL
GRANT
ROLE
role1
TO
ROLE
role2
;
PostgreSQL
GRANT
role1
TO
role2
;
For more information, see Database role hierarchies and inheritance .
Grant database roles to principals
To access Spanner resources, a principal must be granted the necessary database role using one of the following options:
Console
-
On the database Overviewpage, click SHOW INFO PANELif the Info panelis not already open.
-
Click ADD PRINCIPAL.
-
Under Add principals, in New principals, enter one or more principals.
-
Under Assign roles, in Select a role, select Cloud Spanner > Cloud Spanner Fine-grained Access User.
You need to grant this role only once to each principal. It makes the principal a fine-grained access control user.
-
Click ADD ANOTHER ROLE.
-
In Select a role, select Cloud Spanner > Cloud Spanner Database Role User.
-
Follow these steps to create the IAM condition that specifies the roles to grant.
-
Next to the Cloud Spanner Database Role User role, click ADD IAM CONDITION.
-
In the Add conditionpanel, enter a title and optional description for the condition.
If you're granting a single database role, you typically include the role name in the condition title. If you're granting multiple roles, you can indicate something about the set of roles.
-
Click Condition editor.
-
In the Expressionfield, enter the following code:
resource.type == "spanner.googleapis.com/DatabaseRole" && resource.name.endsWith ( "/ ROLE " )
Replace
ROLE
with your role name.To grant more than one role to the principal, add more conditions with the or(
||
) operator, as shown in the following example:resource.type == "spanner.googleapis.com/DatabaseRole" && ( resource.name.endsWith ( "/ ROLE1 " ) || resource.name.endsWith ( "/ ROLE2 " ))
This code grants two roles. Replace
ROLE1
andROLE2
with your role names. To grant more than two roles, add more orconditions.You can use any condition expression that's supported by IAM. For more information, see Overview of IAM conditions .
-
Click Save.
-
Verify that the condition appears in the IAM Conditioncolumn next to the Rolefield.
-
Click Save.
Back on the Info panel, under Role/Principal, notice that Cloud Spanner Database Role Userappears for each defined condition.
The number in parentheses next to the condition indicates the number of principals who are granted the database role by that condition. You can click the expander arrow to view the list of principals.
-
To correct errors in database role names or conditions, or to add additional database roles for a principal, follow these steps:
-
Expand the Cloud Spanner Database Role Userentry that lists the condition that you want.
-
Click the Edit(pencil) icon next to a principal.
-
On the Edit access to database_name panel, do one of the following:
-
Click ADD ANOTHER ROLE.
-
To edit the condition, click the Edit(pencil) icon adjacent to the condition name. Then on the Edit conditionpage, click Condition editor, make corrections, and click Savetwice.
-
-
-
gcloud
-
Enable fine-grained access control for the principal by using the
gcloud spanner databases add-iam-policy-binding
command as follows:gcloud spanner databases add-iam-policy-binding DATABASE_NAME \ --instance = INSTANCE_NAME \ --role = roles/spanner.fineGrainedAccessUser \ --member = MEMBER_NAME \ --condition = None
-
MEMBER_NAME
is the identifier for the principal. It must use one of the following syntaxes:user|group|serviceAccount: email
ordomain: domain
. -
This command makes the principal a fine-grained access control user. Submit this command only once for each principal.
-
If successful, the command outputs the entire policy for the database.
-
-
Grant permission to use one or more database roles by using the
gcloud spanner databases add-iam-policy-binding
command as follows:gcloud spanner databases add-iam-policy-binding DATABASE_NAME \ --instance = INSTANCE_NAME \ --role = roles/spanner.databaseRoleUser \ --member = MEMBER_NAME \ --condition = CONDITION
-
MEMBER_NAME
is the identifier for the principal. It must use one of the following syntaxes:user|group|serviceAccount: email
ordomain: domain
. -
CONDITION
is an IAM condition expression that specifies the roles to grant to the principal.CONDITION
has the following form:--condition = 'expression=(resource.type == "spanner.googleapis.com/DatabaseRole" && resource.name.endsWith("/ ROLE1 ")),title= TITLE ,description= DESCRIPTION '
Or, to grant access to the principal to more than one role, add more conditions with the or(
||
) operator, as shown in the following example:--condition = 'expression=(resource.type == "spanner.googleapis.com/DatabaseRole" && (resource.name.endsWith("/ ROLE1 ") || resource.name.endsWith("/ ROLE2 "))),title= TITLE ,description= DESCRIPTION '
This code grants two roles. Replace
ROLE1
andROLE2
with your role names. To grant more than two roles, add more orconditions with the||
operator.You can use any condition expression that's supported by IAM. For more information, see Overview of IAM conditions .
If successful, the command outputs the entire policy for the database.
The following example grants the database roles
hr_rep
andhr_manager
to the principaljsmith@example.com
.gcloud spanner databases add - iam - policy - binding myDatabase \ -- instance = myInstance \ -- role = roles / spanner . databaseRoleUser \ -- member = user : jsmith @ example . com \ -- condition = ' expression =( resource . type == "spanner.googleapis.com/DatabaseRole" && ( resource . name . endsWith ( "/hr_rep" ) || resource . name . endsWith ( "/hr_manager" ))), title = HR roles , description = Grant permissions on HR roles '
-
Client libraries
C++
C#
Go
Java
Node.js