IAM Conditions allows you to define and enforce conditional, attribute-based access control for Google Cloud resources, including Cloud SQL instances. For more information about IAM Conditions, see the Overview of IAM Conditions page.
IntroductionIn Cloud SQL, you can enforce conditional access based on the following attributes:
Use cases include:
Allowing users to connect to specific instances.
Allowing users to create instances with specific prefixes or suffixes (for example, "test").
Limiting access to backup operations for test instances
Allowing users to delete development and test instances, but not production instances.
Allowing users to perform administrative operations on certain dates or at certain times.
Suppose you want to let a user or service account have permission to connect to one specific Cloud SQL instance only. You can include an IAM Condition in the IAM policy binding that grants that account the permissions of a Cloud SQL role.
By default, the predefined Cloud SQL Client role (roles/cloudsql.client
), which contains the cloudsql.instances.connect
permission, authorizes its member to connect to all Cloud SQL instances in a project. By introducing an IAM Condition into the policy binding, you can grant permission to just the named instance.
This example shows how to modify the existing IAM binding for the project to give a service account a Cloud SQL Client role for a specific instance.
This example uses the following variables:
In the Google Cloud console, go to the IAM page.
projects/PROJECT_ID/instances/INSTANCE_ID
sqladmin.googleapis.com
.This example shows how to modify the existing IAM policy binding for the project to give a specific service account the Cloud SQL Client role, but only for a specific instance.
This example uses the following variables:
bindings.json
:gcloud projects get-iam-policy PROJECT_ID --format=json > bindings.json
bindings.json
file:
{ "bindings": [ { "role": "roles/cloudsql.client", "members": [ "serviceAccount:SERVICE_ACCOUNT_EMAIL" ], "condition": { "expression": "resource.name == 'projects/PROJECT_ID/instances/INSTANCE_ID' && resource.service == 'sqladmin.googleapis.com'" } } ], "etag": "BwWKmjvelug=", "version": 3 }
bindings.json
file.
gcloud projects set-iam-policy PROJECT_ID bindings.json
To allow users to connect to specific instances, use a Terraform google_iam_policy
data resource and a google_project_iam_policy
Terraform resource.
Caution: If you create a google_project_iam_policy
resource, then you override both the existing policy and all access in your Google Cloud project.
If you delete this resource, then anyone who doesn't have organization-level access to your Google Cloud project is locked out of the project. As a result, they can't access any resources associated with the project.
Use the resource only for Google Cloud projects that are fully managed by Terraform. If you use the resource, we strongly recommend that you import the policy before deleting it. This way, if any issues occur, you can reinstate the policy.
Apply the changesTo apply your Terraform configuration in a Google Cloud project, complete the steps in the following sections.
Prepare Cloud ShellSet the default Google Cloud project where you want to apply your Terraform configurations.
You only need to run this command once per project, and you can run it in any directory.
export GOOGLE_CLOUD_PROJECT=PROJECT_ID
Environment variables are overridden if you set explicit values in the Terraform configuration file.
Each Terraform configuration file must have its own directory (also called a root module).
.tf
extension—for example main.tf
. In this tutorial, the file is referred to as main.tf
.
mkdir DIRECTORY && cd DIRECTORY && touch main.tf
If you are following a tutorial, you can copy the sample code in each section or step.
Copy the sample code into the newly created main.tf
.
Optionally, copy the code from GitHub. This is recommended when the Terraform snippet is part of an end-to-end solution.
terraform init
Optionally, to use the latest Google provider version, include the -upgrade
option:
terraform init -upgrade
terraform plan
Make corrections to the configuration as necessary.
yes
at the prompt:
terraform apply
Wait until Terraform displays the "Apply complete!" message.
To delete your changes, do the following:
deletion_protection
argument to false
.
deletion_protection = "false"
yes
at the prompt:
terraform apply
Remove resources previously applied with your Terraform configuration by running the following command and entering yes
at the prompt:
terraform destroy
Suppose your service's topology is configured so that all test instances have a prefix of test
(for example, test-instance-1
), and all production instances have a prefix of prod
(for example, prod-instance-1
).
You can limit access to backup operations to your test instances for a user or a service account. Limiting access includes restricting CREATE
, GET
, LIST
, or DELETE
operations to backups for your test instances.
In the Google Cloud console, go to the IAM page.
In the Filter field of the subsequent dialog box, enter Cloud SQL Admin
. Then, select the Cloud SQL Admin role that appears.
The Edit permissions dialog box is active, and the Cloud SQL Admin role now appears in the dialog box.
Limit access to backup operations
.Click the CONDITION EDITOR tab, and then add the following condition:
resource.type == "sqladmin.googleapis.com/BackupRun" && resource.name.startsWith("projects/PROJECT_ID/instances/test")This condition limits the scope of the Cloud SQL Admin role for the principal that you selected to those resources that have resource names that start with
projects/PROJECT_ID/instances/test
. Also, replace the PROJECT_ID placeholder variable with the name of your Google Cloud project.This example uses the following variables:
Limit the scope of the cloudsql.admin
role for a user who has an email address of USER_EMAIL.
The scope of the role is limited to those resources that have resource names that start with projects/PROJECT_ID/instances/test
.
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=user:USER_EMAIL \ --role=roles/cloudsql.admin \ --condition=expression="resource.type == \"sqladmin.googleapis.com/BackupRun\" && resource.name.startsWith(\"projects/PROJECT_ID/instances/test-instance-1\")",title="test"
OR
Limit the scope of the cloudsql.admin
role for a user who's logged in with a service account of SERVICE_ACCOUNT_EMAIL.
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL \ --role=roles/cloudsql.admin \ --condition=expression="resource.type == \"sqladmin.googleapis.com/BackupRun\" && resource.name.startsWith(\"projects/PROJECT_ID/instances/test-instance-1\")",title="test"
test
prefix, then you can see the final backups only for instances with this prefix. All IAM conditions that exist for the BackupRuns
resource in the format of either the BackupRun
URI (projects/PROJECT_ID/instances/INSTANCE_ID/backupRuns
) or the Instance
resource (projects/PROJECT_ID/instances/var>INSTANCE_ID
) also apply to final backups. We don't support conditions for the backup
URI.
Suppose you want to allow a service account to delete test instances, but not production instances. You can do this by using tags, and by adding the following two policy bindings for the service account:
cloudsql.instances.delete
permission.test
tag.In the Google Cloud console, go to the IAM page.
sqladmin.googleapis.com
.matches
and the value is 815471563813/env/test
.This example uses the following variables:
gcloud alpha resource-manager tags keys create env \ --parent=organizations/ORGANIZATION_ID gcloud alpha resource-manager tags values create prod \ --parent=env gcloud alpha resource-manager tags values create test \ --parent=env
gcloud alpha resource-manager tags bindings create \ --tag-value=test \ --parent=//sqladmin.googleapis.com/projects/PROJECT_ID/instances/INSTANCE_ID \ --location=REGION
bindings.json
:
gcloud projects get-iam-policy PROJECT_ID --format=json >> bindings.json
bindings.json
file:
{ "bindings": [ { "role": "roles/cloudsql.editor", "members": [ "serviceAccount:SERVICE_ACCOUNT_EMAIL" ] }, { "role": "roles/cloudsql.admin", "members": [ "serviceAccount:SERVICE_ACCOUNT_EMAIL" ], "condition": { "expression": "resource.matchTag('ORGANIZATION_ID/env', 'test')" } } ], "etag": "BwWKmjvelug=" "version": 3 }
bindings.json
file.
gcloud projects set-iam-policy PROJECT_ID bindings.json
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4