I want to use docker-swarm secrets in my Dockerized Spring-Boot application. These secrets are mounted as special files into the docker container: the secret text is accessible by reading the file at /run/secrets/<secret_name>. This is deemed best-practice in secret handling regarding containers (as opposed to injecting the secret text as environment variable, which isn't even supported by docker-swarm).

It would be great if Spring-Boot offered a way to set a property's value from the content of a file so I can write something like this:

spring.data.mongodb.password=${fileContent./var/run/secrets/mongo_db_password}

This would be similar to how RandomValuePropertySource works. Looking at its source it should be straight forward to provide a PropertySource implementation for reading file content. However, I wasn't able to properly inject my custom PropertySource into the Spring-Boot application start up process.

I also wonder if there might be already some spring magic that would allow to set a property's value from file?

If this is a valid feature request, I'd be happy to help out with a pull request and a PropertySource implementation thats handled similar to the RandomValuePropertySource. Otherwise it would be great to provide some information on how I could properly inject my PropertySource into the start up process.

Comment From: bclozel

Could be a duplicate of https://github.com/spring-projects/spring-framework/issues/21961 or #19990.

Comment From: wilkinsona

However, I wasn't able to properly inject my custom PropertySource into the Spring-Boot application start up process.

You can do this using an EnvironmentPostProcessor registered in META-INF/spring.factories.

I agree with @bclozel that this is a duplicate of #19990 or spring-projects/spring-framework#21961.

Comment From: skuzzle

I'm sorry for raising the duplicate.

I actually tried using the EnvironmentPostProcessor but just now I found an embarassing copy/ paste failure which prevented it from functioning properly (I registered the new RandomPropertySource() instead of my custom implementation ...) So, this works for me and I happily look forward to having an official solution in the future

Comment From: skuzzle

To be complete, this is how I solved it: https://github.com/skuzzle/cmp/pull/61/files#diff-19d3d9baef38df0d452bd263f28a5985

Usage examples: https://github.com/skuzzle/cmp/pull/61/files#diff-4305ea58e6cba01279868fe00283c931 https://github.com/skuzzle/cmp/pull/61/files#diff-bf6bc5f90da75034128c40d3349118b8R61