You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Back port of PR:
cyrilgdn#448
---
Add support for GCP IAM service account impersonation
### Use cases
The company has a centralized service account that is used for Terraform
automation. However, such GSA should not be used to access the database
directly where each database will have its own IAM DB users.
This added an option to impersonate the database IAM user via the
centralized GSA. As long as the centralized GSA has sufficient
permissions to impersonate as the database IAM DB user, it can be used
to perform database automation in Terraform.
### Testing
```hcl
resource "google_sql_database_instance" "self" {}
resource "google_sql_user" "admin" {}
resource "google_service_account" "db_iam_admin" {}
resource "google_sql_user" "iam_admin" {
name = trimsuffix(google_service_account.db_iam_admin.email, ".gserviceaccount.com")
instance = google_sql_database_instance.self.name
type = "CLOUD_IAM_SERVICE_ACCOUNT"
}
resource "google_project_iam_member" "iam_admin_project_iam_members" {
for_each = toset(["roles/cloudsql.client", "roles/cloudsql.instanceUser"])
member = google_service_account.db_iam_admin.member
role = each.key
}
provider "postgresql" {
scheme = "gcppostgres"
host = google_sql_database_instance.self.connection_name
username = trimsuffix(google_service_account.db_iam_admin.email, ".gserviceaccount.com")
gcp_iam_impersonate_service_account = google_service_account.db_iam_admin.email
port = 5432
superuser = false
alias = "iamAdmin"
}
# it should work and able to apply resources using the IAM db user
resource "postgresql_*" "*" {
provider = postgresql.iamAdmin
// *
}
```
Co-authored-by: Michael Lin <mlzc@hey.com>
Copy file name to clipboardExpand all lines: website/docs/index.html.markdown
+22-1
Original file line number
Diff line number
Diff line change
@@ -213,7 +213,28 @@ To enable GoCloud for GCP SQL, set `scheme` to `gcppostgres` and `host` to the c
213
213
For GCP, GoCloud also requires the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to be set to the service account credentials file.
214
214
These credentials can be created here: https://console.cloud.google.com/iam-admin/serviceaccounts
215
215
216
-
See also: https://cloud.google.com/docs/authentication/production
216
+
In addition, the provider supports service account impersonation with the `gcp_iam_impersonate_service_account` option. You must ensure:
217
+
218
+
- The IAM database user has sufficient permissions to connect to the database, e.g., `roles/cloudsql.instanceUser`
219
+
- The principal (IAM user or IAM service account) behind the `GOOGLE_APPLICATION_CREDENTIALS` has sufficient permissions to impersonate the provided service account. Learn more from [roles for service account authentication](https://cloud.google.com/iam/docs/service-account-permissions).
0 commit comments