Search code examples
spring-bootkubernetesgoogle-secret-manager

How to inject secret from Google Secret Manager into Kubernetes Pod as environment variable with Spring Boot?


For the life of Bryan, how do I do this?

Terraform is used to create an SQL Server instance in GCP. Root password and user passwords are randomly generated, then put into the Google Secret Manager. The DB's IP is exposed via private DNS zone.

How can I now get the username and password to access the DB into my K8s cluster? Running a Spring Boot app here.

This was one option I thought of:

In my deployment I add an initContainer:

- name: secrets
  image: gcr.io/google.com/cloudsdktool/cloud-sdk
  args: 
  - echo "DB_PASSWORD=$(gcloud secrets versions access latest --secret=\"$NAME_OF_SECRET\")" >> super_secret.env

Okay, what now? How do I get it into my application container from here?

There are also options like bitnami/sealed-secrets, which I don't like since the setup is using Terraform already and saving the secrets in GCP. When using sealed-secrets I could skip using the secrets manager. Same with Vault IMO.


Solution

  • You can use spring-cloud-gcp-starter-secretmanager to load secrets from Spring application itself.

    Documentation - https://cloud.spring.io/spring-cloud-gcp/reference/html/#secret-manager