How to set spring.data.source.password with kubernetes secrets in spring boot >= 2.4

Viewed 599

Spring Boot 2.4 introduced volume mounted configuration as a popular feature of kubernetes.

For example, you can declare the following in your application.properties:

spring.config.import=configtree:/etc/config

If you have the following mounted content:

etc/
 +- config/
     +- my/
     |  +- application
     +- test

You end up with my.application and test properties in your Spring Environment.

Now I'm wondering what is the best approach regarding spring.datasource.password and spring.datasource.username and e.g. spring.something.else.password.

  1. Use spring.config.import=configtree:/etc/secrets. Create two secrets, postgres-secret and another-secret, mount the first one to /etc/secrets/spring/datasource and the second one to /etc/secrets/spring/something/else?
  2. Use spring.config.import=file:/etc/secrets/postgres.properties,file:/etc/secrets/somethingelse.properties, defining spring.datasource.password etc in it. So the whole .properties is base64 encoded as the secret.
  3. Mount all secrets in /etc/secrets/, making sure the filename matches the property name (spring.datasource.password).
  4. Introduce a new property (e.g. pg.password), using it in spring.datasource.password and do one of [1-3] for that (etc/secrets/pg/password, /etc/secrets/postgres.properties).

So basically, how can I best integrate e.g. db secret properties and other secret properties to be picked up by spring boot, while also keeping the configuration low. E.g. mapping secret password of secret pg-secret to map to /etc/secrets/spring/datasource/password is a lot to type for every single key in a secret.

0 Answers
Related