cloud-sql-proxy icon indicating copy to clipboard operation
cloud-sql-proxy copied to clipboard

Support Workload Identity Federation principal:// for IAM auth

Open forestoden opened this issue 2 weeks ago • 2 comments

Feature Description

This might be impossible, but I figured I'd ask. I'd love to be able to allow a Kubernetes Service Account to use IAM Auth with WIF Principals where KSAs are bound directly to GCP permissions.

A KSA might be granted the name of the KSA, or something like <sa>-<namespace>

It seems like there might be a way to have a KSA impersonate a service account. Maybe some docs on how to execute that would be enough here.

This is similar to this feature request on the operator: https://github.com/GoogleCloudPlatform/cloud-sql-proxy-operator/issues/706

Sample code

// sample code here

Alternatives Considered

No response

Additional Details

No response

forestoden avatar Nov 20 '26 01:11 forestoden

Yes. KSA impersonation of service account is supported via https://docs.cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds#impersonation.

schen59 avatar Nov 24 '26 22:11 schen59

Thanks for the link! I think I explained the request badly, so here's what I mean.

I know impersonation works. That's what we do today. The request is to not need the Google service account (GSA) at all. GKE Workload Identity Federation now lets a Kubernetes service account (KSA) be granted IAM roles directly as a principal://, and that works for almost every Google API. Cloud SQL IAM database auth seems to be the one place where we still need a GSA for each workload.

What we have to do today (per workload):

# 1. Create a GSA that exists only so this pod can log in to MySQL
gcloud iam service-accounts create orders-api-db \
  --project=my-project

# 2. Grant the GSA Cloud SQL access
gcloud projects add-iam-policy-binding my-project \
  --member="serviceAccount:[email protected]" \
  --role="roles/cloudsql.client"
gcloud projects add-iam-policy-binding my-project \
  --member="serviceAccount:[email protected]" \
  --role="roles/cloudsql.instanceUser"

# 3. Let the KSA principal mint tokens for the GSA
gcloud iam service-accounts add-iam-policy-binding \
  [email protected] \
  --role="roles/iam.serviceAccountTokenCreator" \
  --member="principal://iam.googleapis.com/projects/123456789/locations/global/workloadIdentityPools/my-project.svc.id.goog/subject/ns/orders/sa/orders-api"

# 4. Create the DB user from the GSA's email
gcloud sql users create [email protected] \
  --instance=my-instance --type=cloud_iam_service_account
# 5. Have the proxy sidecar impersonate the GSA
apiVersion: v1
kind: Pod
metadata:
  name: orders-api
  namespace: orders
spec:
  serviceAccountName: orders-api   # plain KSA, no iam.gke.io annotation
  initContainers:
    - name: cloud-sql-proxy
      image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:latest
      restartPolicy: Always        # native sidecar
      args:
        - --auto-iam-authn
        - --impersonate-service-account=orders-api-db@my-project.iam.gserviceaccount.com
        - --port=3306
        - --structured-logs
        - my-project:us-central1:my-instance
      securityContext:
        runAsNonRoot: true
  containers:
    - name: app
      image: us-docker.pkg.dev/my-project/apps/orders-api:latest
      env:
        - name: DB_HOST
          value: "127.0.0.1"
        - name: DB_PORT
          value: "3306"
        - name: DB_USER
          value: "orders-api-db"   # still the GSA, not the workload
-- 6. The DB username is the truncated GSA email, not anything tied to the KSA
GRANT 'orders_rw' TO 'orders-api-db'@'%';

That's one GSA, three IAM bindings and an --impersonate-service-account flag on every sidecar, per workload. It all has to be kept in sync across Terraform and Helm. The DB username also points to a GSA rather than the workload that's really connecting. Across N workloads this adds up quickly.

What I'd like to do (per workload):

# 1. Grant the KSA principal Cloud SQL access directly (no GSA)
PRINCIPAL="principal://iam.googleapis.com/projects/123456789/locations/global/workloadIdentityPools/my-project.svc.id.goog/subject/ns/orders/sa/orders-api"

gcloud projects add-iam-policy-binding my-project \
  --member="$PRINCIPAL" --role="roles/cloudsql.client"
gcloud projects add-iam-policy-binding my-project \
  --member="$PRINCIPAL" --role="roles/cloudsql.instanceUser"

# 2. Create a DB user for the principal (hypothetical: no such user type exists today)
gcloud sql users create orders-api-orders \
  --instance=my-instance --type=cloud_iam_principal \
  --principal="$PRINCIPAL"
-- 3. The DB username identifies the workload that's actually connecting
GRANT 'orders_rw' TO 'orders-api-orders'@'%';
# 4. Proxy sidecar runs as the pod's own identity: no impersonation flag
apiVersion: v1
kind: Pod
metadata:
  name: orders-api
  namespace: orders
spec:
  serviceAccountName: orders-api
  initContainers:
    - name: cloud-sql-proxy
      image: gcr.io/cloud-sql-connectors/cloud-sql-proxy:latest
      restartPolicy: Always        # native sidecar
      args:
        - --auto-iam-authn
        - --port=3306
        - --structured-logs
        - my-project:us-central1:my-instance
  containers:
    - name: app
      image: us-docker.pkg.dev/my-project/apps/orders-api:latest
      env:
        - name: DB_USER
          value: "orders-api-orders"

This uses two IAM bindings on an identity Kubernetes already gives us. There's no extra service account to create and keep in sync, and the DB username matches the workload.

Today this doesn't work for two reasons:

  1. No user type for principals. gcloud sql users create --type=cloud_iam_user / cloud_iam_service_account only takes an email. There's no way to create a DB user for a principal:// identity.
  2. No email in the token. Without the KSA annotation, the GKE metadata server returns a federated token for the KSA principal. That token has no email, so Cloud SQL has nothing to match a DB user against.

So I understand this may need Cloud SQL backend changes and not just proxy changes. If so, could this be routed to the right team, or tracked here alongside GoogleCloudPlatform/cloud-sql-proxy-operator#706? I think the proxy repo is still a good home for it: the proxy's --auto-iam-authn is the main way people use IAM DB auth from GKE, and principal:// is where GKE identity is heading.

forestoden avatar Dec 03 '26 11:12 forestoden