Isolate application identities for Cloud SQL
This guide explains how to configure separate IAM identities and database permissions for applications that share one DBO deployment. Each application pod uses its own Google service account and an IAM-injector sidecar. Cloud SQL Proxy remains in the DBO pod.
Applications authenticate with short-lived IAM credentials instead of static database passwords. Role separation prevents one application from accessing another application's objects.

Each application uses its own IAM identity while DBO and Cloud SQL Proxy provide the shared upstream connection.
The database request path is:
- The application connects to its local IAM-injector sidecar.
- The injector supplies the application's IAM database username and access token.
- The request passes through DBO and Cloud SQL Proxy to Cloud SQL.
- Cloud SQL authenticates and authorizes the request using the application's own PostgreSQL role.
Prerequisites
- Complete Configure Cloud SQL Proxy for DBO, including the DBO service account and Workload Identity configuration.
- An existing Cloud SQL PostgreSQL with IAM database authentication enabled.
- Applications and databases where you can demonstrate identity isolation. This guide uses
fooandbar, withfoo_dbandbar_db. - A GKE cluster with Workload Identity enabled and
kubectlconfigured to access it. - The
gcloudandhelmCLIs installed. - Permissions to create Google service accounts, grant IAM roles, create Cloud SQL users, and update the applications.
- Database administrator access to grant privileges in the application databases.
Create the application identities
Create one Google service account (GSA) and one Kubernetes service account (KSA) for each application. Workload Identity connects each KSA to its corresponding GSA.
| Application | GSA and KSA | Cloud SQL IAM database user | Database |
|---|---|---|---|
foo | foo-app | [email protected] | foo_db |
bar | bar-app | [email protected] | bar_db |
The DBO service account from Configure Cloud SQL Proxy for DBO continues to handle only the Cloud SQL Proxy connection. It does not need the Cloud SQL Instance User role or its own database user in this configuration.
Set the identity variables
export PROJECT_ID="your-project-id"
export FOO_SA_NAME="foo-app"
export FOO_SA_EMAIL="${FOO_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
export FOO_DB_USER="${FOO_SA_NAME}@${PROJECT_ID}.iam"
export BAR_SA_NAME="bar-app"
export BAR_SA_EMAIL="${BAR_SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
export BAR_DB_USER="${BAR_SA_NAME}@${PROJECT_ID}.iam"
export CLOUD_SQL_INSTANCE="your-cloud-sql-instance"variable "project_id" {
type = string
default = "your-project-id"
}
variable "cloud_sql_instance" {
type = string
default = "your-cloud-sql-instance"
}
variable "foo_service_account_id" {
type = string
default = "foo-app"
}
variable "bar_service_account_id" {
type = string
default = "bar-app"
}
locals {
foo_db_user = trimsuffix(
google_service_account.foo_app.email,
".gserviceaccount.com"
)
bar_db_user = trimsuffix(
google_service_account.bar_app.email,
".gserviceaccount.com"
)
}Replace the project ID and instance ID with your values. The PostgreSQL IAM database username omits the .gserviceaccount.com suffix from the service account email.
Create the Google service accounts
gcloud iam service-accounts create "${FOO_SA_NAME}" \
--project="${PROJECT_ID}" \
--display-name="Foo App" \
--description="Authenticates the foo application to Cloud SQL"
gcloud iam service-accounts create "${BAR_SA_NAME}" \
--project="${PROJECT_ID}" \
--display-name="Bar App" \
--description="Authenticates the bar application to Cloud SQL"resource "google_service_account" "foo_app" {
project = var.project_id
account_id = var.foo_service_account_id
display_name = "Foo App"
description = "Authenticates the foo application to Cloud SQL"
}
resource "google_service_account" "bar_app" {
project = var.project_id
account_id = var.bar_service_account_id
display_name = "Bar App"
description = "Authenticates the bar application to Cloud SQL"
}Grant the Cloud SQL Instance User role
Each application needs roles/cloudsql.instanceUser to authenticate to Cloud SQL. Applications do not need roles/cloudsql.client because they do not run Cloud SQL Proxy.
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${FOO_SA_EMAIL}" \
--role="roles/cloudsql.instanceUser"
gcloud projects add-iam-policy-binding "${PROJECT_ID}" \
--member="serviceAccount:${BAR_SA_EMAIL}" \
--role="roles/cloudsql.instanceUser"resource "google_project_iam_member" "foo_app_instance_user" {
project = var.project_id
role = "roles/cloudsql.instanceUser"
member = google_service_account.foo_app.member
}
resource "google_project_iam_member" "bar_app_instance_user" {
project = var.project_id
role = "roles/cloudsql.instanceUser"
member = google_service_account.bar_app.member
}Create the Cloud SQL IAM database users
Register each application GSA as a Cloud SQL IAM database user. These users become the PostgreSQL roles that receive application-specific privileges.
gcloud sql users create "${FOO_DB_USER}" \
--instance="${CLOUD_SQL_INSTANCE}" \
--project="${PROJECT_ID}" \
--type="CLOUD_IAM_SERVICE_ACCOUNT"
gcloud sql users create "${BAR_DB_USER}" \
--instance="${CLOUD_SQL_INSTANCE}" \
--project="${PROJECT_ID}" \
--type="CLOUD_IAM_SERVICE_ACCOUNT"resource "google_sql_user" "foo_app" {
project = var.project_id
name = local.foo_db_user
instance = var.cloud_sql_instance
type = "CLOUD_IAM_SERVICE_ACCOUNT"
}
resource "google_sql_user" "bar_app" {
project = var.project_id
name = local.bar_db_user
instance = var.cloud_sql_instance
type = "CLOUD_IAM_SERVICE_ACCOUNT"
}Grant database privileges
Use a database administrator account to grant privileges in the existing application databases. Do not route these administrative connections through DBO.
Connect to foo_db and grant privileges to the foo identity:
GRANT USAGE, CREATE ON SCHEMA public TO "[email protected]";
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO "[email protected]";Connect to bar_db and grant the equivalent privileges to the bar identity:
GRANT USAGE, CREATE ON SCHEMA public TO "[email protected]";
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO "[email protected]";Each application owns and has full privileges on objects that it creates in its own database, and cannot access another application's objects without explicit permission.
Configure application Workload Identity
Allow each application KSA to impersonate only its corresponding GSA.
If you used Terraform, make sure you have the environment variables from step 1 set before continuing.
gcloud iam service-accounts add-iam-policy-binding "${FOO_SA_EMAIL}" \
--project="${PROJECT_ID}" \
--role="roles/iam.workloadIdentityUser" \
--member="serviceAccount:${PROJECT_ID}.svc.id.goog[apps/foo-app]"
gcloud iam service-accounts add-iam-policy-binding "${BAR_SA_EMAIL}" \
--project="${PROJECT_ID}" \
--role="roles/iam.workloadIdentityUser" \
--member="serviceAccount:${PROJECT_ID}.svc.id.goog[apps/bar-app]"resource "google_service_account_iam_member" "foo_app_workload_identity" {
service_account_id = google_service_account.foo_app.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[apps/${var.foo_service_account_id}]"
}
resource "google_service_account_iam_member" "bar_app_workload_identity" {
service_account_id = google_service_account.bar_app.name
role = "roles/iam.workloadIdentityUser"
member = "serviceAccount:${var.project_id}.svc.id.goog[apps/${var.bar_service_account_id}]"
}These examples use the apps namespace and the ${PROJECT_ID}.svc.id.goog Workload Identity pool. Replace the namespace if your applications run elsewhere. If your cluster belongs to a different project, use that cluster's Workload Identity pool instead.
Create the application KSAs and annotate them with their corresponding GSAs:
kubectl create namespace apps --dry-run=client -o yaml | kubectl apply -f -
kubectl create serviceaccount foo-app --namespace apps \
--dry-run=client -o yaml | kubectl apply -f -
kubectl annotate serviceaccount foo-app --namespace apps \
"iam.gke.io/gcp-service-account=${FOO_SA_EMAIL}" --overwrite
kubectl create serviceaccount bar-app --namespace apps \
--dry-run=client -o yaml | kubectl apply -f -
kubectl annotate serviceaccount bar-app --namespace apps \
"iam.gke.io/gcp-service-account=${BAR_SA_EMAIL}" --overwriteConfirm the DBO configuration
Keep the DBO settings from Configure Cloud SQL Proxy for DBO:
serviceAccountName: db-proxy
cloudSqlProxy:
enabled: true
privateIp: true
autoIamAuthn: falseKeep the existing endpoint configuration unchanged. You do not need to redeploy DBO if these settings already match your deployment.
Each application's IAM-injector sidecar supplies the application's IAM credentials for database authentication.
Add IAM injectors to the applications
Add one IAM-injector sidecar to each application deployment. Configure each pod to use its corresponding KSA.
Open the endpoint settings
In the Cast AI console, open the DBO instance and go to Settings → Endpoints.
Copy the connection details
In Caching configuration, copy DBO HOST and DBO PORT.

Copy the DBO host and port from the Read/Write endpoint's Caching configuration.
Configure the IAM injectors
Use these values for the --upstream argument of the db-iam-injector.
For example, the Deployment of the foo application could include:
spec:
template:
spec:
serviceAccountName: foo-app
containers:
- name: foo-container
env:
- name: DB_HOST
value: "127.0.0.1"
- name: DB_PORT
value: "5432"
- name: DB_NAME
value: "foo_db"
- name: db-iam-injector
image: ghcr.io/castai/images/db-iam-injector:v0.2.0
args:
- "--listen=127.0.0.1:5432"
- "--upstream=db-proxy-gcp-d3238.castai-db-optimizer:5432"serviceAccountNamespecifies the application KSA you created and annotated earlier. Workload Identity maps that KSA to the application's GSA.--listenspecifies the local address and port where the injector listens inside the application pod. Set your application'sDB_HOSTandDB_PORTto the same values.--upstreamspecifies the DBO proxy address. Use the DBO HOST and DBO PORT values copied from the console.
The application connects to the injector on 127.0.0.1. The applications do not need a static database password.
Deploy and verify the applications
Deploy the updated applications and verify that they can still access their databases.
Verify identity isolation
Use a temporary foo-db-client pod to verify authentication and authorization independently of your application code. The pod has the same KSA and injector configuration as the foo application.
Create the client pod
Create the client pod using the DBO host and port shown in your configuration:
kubectl apply -f - <<EOF
apiVersion: v1
kind: Pod
metadata:
name: foo-db-client
namespace: apps
spec:
serviceAccountName: foo-app
containers:
- name: postgres-client
image: postgres:17
command: ["/bin/sh", "-c"]
args: ["sleep infinity"]
- name: db-iam-injector
image: ghcr.io/castai/images/db-iam-injector:v0.2.0
args:
- "--listen=127.0.0.1:5432"
- "--upstream=db-proxy-gcp-d3238.castai-db-optimizer:5432"
EOF
kubectl wait --namespace apps --for=condition=Ready \
pod/foo-db-client --timeout=120sVerify foo database access
Verify that foo can create and access objects in foo_db:
kubectl exec foo-db-client --namespace apps --container postgres-client -- \
psql "host=127.0.0.1 port=5432 dbname=foo_db sslmode=disable" \
--command="CREATE TABLE IF NOT EXISTS app_records (id integer PRIMARY KEY, message text NOT NULL);" \
--command="INSERT INTO app_records VALUES (1, 'created by foo') ON CONFLICT (id) DO UPDATE SET message = EXCLUDED.message;" \
--command="SELECT current_user, current_database();" \
--command="SELECT * FROM app_records;"The output confirms that the connection authenticated as foo-app, selected foo_db, and returned the inserted row:
CREATE TABLE
INSERT 0 1
current_user | current_database
-----------------------------+------------------
[email protected] | foo_db
(1 row)
id | message
----+----------------
1 | created by foo
(1 row)Test cross-database isolation
Attempt to create a table in bar_db as foo:
kubectl exec foo-db-client --namespace apps --container postgres-client -- \
psql "host=127.0.0.1 port=5432 dbname=bar_db sslmode=disable" \
--command="CREATE TABLE IF NOT EXISTS app_records (id integer PRIMARY KEY, message text NOT NULL);"The connection can succeed, but PostgreSQL rejects the operation because foo does not have the CREATE privilege on the public schema in bar_db:
ERROR: permission denied for schema publicDelete the temporary client pod
kubectl delete pod/foo-db-client --namespace appsYour applications now authenticate with separate IAM identities and PostgreSQL roles while sharing the centralized DBO and Cloud SQL Proxy connection path.
Updated 21 hours ago
