Home » Java » java – Dynamicly operate on application properties – vault settings (or any other values)-Exceptionshub

java – Dynamicly operate on application properties – vault settings (or any other values)-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I would like to be able to flexibly choose between 3rd party components my application is using. ATM I have hardcoded values

spring.cloud.vault.token=ffffffff-ffff-ffff-ffff-ffffffffffff
spring.cloud.vault.scheme=http
spring.cloud.vault.application-name=test
spring.cloud.vault.host=127.0.0.1
spring.cloud.vault.port=8200

Goal is to :

  • If spring.cloud.vault.host is set try to use it. If spring.cloud.vault.host is empty use
    local DB instead (with proper configuration)

My first thought was to use environment variables like:

spring.cloud.vault.token=${VAULT_TOKEN}
spring.cloud.vault.scheme=${VAULT_SCHEME}
spring.cloud.vault.application-name=${VAULT_APP_NAME}
spring.cloud.vault.host=${VAULT_HOST}
spring.cloud.vault.port=${VAULT_PORT}

and later use component class like:

@Component
public class VaultHelper {
    private final VaultOperations vaultOperations;
    @Value("${spring.cloud.vault.host}")
    String vaultHostname;

    @Autowired
    public VaultHelper(VaultOperations vaultOperations){
        this.vaultOperations = vaultOperations;
    }

    public void saveToVault(String password){
        if (!vaultHostname.equals("")) { 
            // Use vaultOperations to store password
        } else {
            // Use plaintext db storage
        }
    }

    public byte[] readFromVault(String passwordLoc){
        if (!vaultHostname.equals("")) {
            // Use vaultOperations to access password
        } else {
            // return plaintext password
        }
        return new byte[0];
    }


}

This way I could set ${VAULT_HOST} both from environment variable (running application from docker-compose) or just set --spring.cloud.vault.host=URL on local machine as java parameter.

Unfortunately I get exception while unparsing ${VAULT_PORT} field

Caused by: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [int] for value '${VAULT_PORT}'; nested exception is java.lang.NumberFormatException: For input string: "${VAULT_PORT}"
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:47) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:191) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.BindConverter$CompositeConversionService.convert(BindConverter.java:170) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:96) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.BindConverter.convert(BindConverter.java:88) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bindProperty(Binder.java:435) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bindObject(Binder.java:380) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    at org.springframework.boot.context.properties.bind.Binder.bind(Binder.java:320) ~[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]
    ... 83 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "${VAULT_PORT}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.8.0_231]
    at java.lang.Integer.parseInt(Integer.java:569) ~[na:1.8.0_231]
    at java.lang.Integer.valueOf(Integer.java:766) ~[na:1.8.0_231]
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:211) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:62) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.convert.support.StringToNumberConverterFactory$StringToNumber.convert(StringToNumberConverterFactory.java:49) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.convert.support.GenericConversionService$ConverterFactoryAdapter.convert(GenericConversionService.java:436) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    at org.springframework.core.convert.support.ConversionUtils.invokeConverter(ConversionUtils.java:41) ~[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
    ... 90 common frames omitted


Process finished with exit code 1

could anyone give me a hint how to make it work?

How to&Answers:

I guess its the matter of fact, that you do not provide a default / fallback. In this case, when it cannot find the corresponding environment variable it just declares the field PORT as the given string. try something with default using (:).

You can also provide another variable as default, which you know will be set.

spring.cloud.vault.port=${VAULT_PORT}:9000


spring.cloud.vault.port=${VAULT_PORT:8200}