Connecting to a gcp k8s Postgres db with a local spring boot service using iam authentication and port forwarding

12/7/2021

I have a spring boot app running in a gcp k8s cluster using iam-auth to connect to postgres.

Spring boot settings when the service is deployed on the cluster that work without problem:

spring:  
  cloud:
    gcp:
      sql:
        database-name: my_database
        instance-connection-name: my-project-12345:europe-north1:mydb-instance-56aaf6d6
        enable-iam-auth: true
  datasource:
    username: myiam@my-project-12345.iam

So now for dev reasons I want to run my app locally from my IDE using port forwarding to reach the postgres database:

kubectl port-forward svc/cloudsql-proxy-whatever -n whatevernamespace 5432:5432

Is there any configuration that can be used here? I have tried different combinations of the properties below, commented and uncommented, of the properties below without success.

spring:
  cloud:
    gcp:
      sql:
        database-name: my_database
        instance-connection-name: my-project-12345:europe-north1:mydb-instance-56aaf6d6
        enable-iam-auth: true
  datasource:
    username: myiam@my-project-12345.iam
    url: jdbc:postgresql://localhost:5432/

Somehow, I need to square the instance-connection-name with the fact that port forwarding (localhost) is being used.

I know I can do this with username and password auth...no problems...but now user/password is disabled and iam-auth is the secure way to go. Is it even possible to portforward using iam-auth?

(Names have been changed to protect the innocent.)

-- Englishbob
amazon-iam
google-cloud-platform
kubernetes
postgresql
spring-boot

0 Answers