Using secret manager on micronaut and default values

Solution 1:

When using Micronaut and GCP Secret Manager, you will typically have a bootstrap.yml that may look like this

micronaut:
  config-client:
    enabled: true

gcp:
  project-id: my-gcp-project-id
  secret-manager:
    keys:
      - db_password

Now db_password is available as sm.db.password in the context, and you will have something like this in application.yml

datasources:
  default:
    url: jdbc:postgresql:my-db
    username: my-db-user
    password: ${sm.db.password}
    driverClassName: org.postgresql.Driver

When running with a different profile where you don't want to connect to GCP Secret Manager, define an env=localhost and add a bootstrap-localhost.yml like this

micronaut:
  config-client:
    enabled: false

Now you can do this in application-localhost.yml

datasources:
  default:
    password: ${ENV_VAR:myPassword} 

Documentation: https://micronaut-projects.github.io/micronaut-gcp/latest/guide/#secretManager