1

Plaintext value is available in /tmp/pass file , need to read this in application.yml spring boot component

Example Spring.datasource.password: FILE(/tmp/pass)

Spring.datasource.password: file:/tmp/pass

Both are not working , is there any other way to read the content from external file in application.yml ?

Bhuvan
  • 11
  • 1
  • i doubt it is possible with yaml only, and not that easy with spring...but your question points into the right direction, see also: - https://stackoverflow.com/q/45899911/592355 - https://spring.io/blog/2016/08/15/managing-your-database-secrets-with-vault - https://medium.com/@sun30nil/how-to-secure-secrets-and-passwords-in-springboot-90c952961d9 - https://dev.to/biplov/handling-passwords-and-secret-keys-using-environment-variables-2ei0 and also: - https://stackoverflow.com/q/56808232/592355 and https://docs.docker.com/compose/env-file/ – xerx593 Jun 03 '21 at 18:19
  • Is it possible to make FILE(/tmp/secrets) in deployment.yaml ? – Bhuvan Jun 03 '21 at 18:29
  • No! It is possible (e.g.) to take the content of `/tmp/secrets` and store it as a environment variable called (exactly) `SPRING_DATASOURCE_PASSWORD` ..this would override the value of `application.proeprties`. [See Chap.2](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config) – xerx593 Jun 03 '21 at 18:47

1 Answers1

1

There is a way to do it, however it might not be the best way. Suppose you have following application.yml:

spring:
    some-config:
        password: FILE(xxx)
        username: FILE(yyy)
        other-properties: FILE(zzz)

Create following config file:

@Configuration
@ConfigurationProperties("spring.some-config")
public class SomeConfigSettings {
    private String password;
    private String username;
    private String otherProperties;

    private String getPassword() {
        return this.password;
    }
    private void setPassword(String password) {
        this.password = FileUtil.read(password)
    }

    // Provide getter/setters for remaining fields
    .......
}

During application startup spring will call setter methods with values provided in application.yml. All you need is to implement your FileUtil.read method, so that it will use your format FILE(/file/path).

Then you may access spring.some-config properties in your beans:

@Autowire
private SomeConfigSettings someConfigSettings;
....

public void someMethod() {
    someConfigSettings.getPassword();
}
eparvan
  • 1,639
  • 1
  • 15
  • 26