Can you reference Azure Key Vault secrets inside application.properties?

Viewed 59

I have a record:

@ConfigurationProperties("service.api")
@ConstructorBinding
public record ServiceApiConfig(String clientId, String clientSecret) {}

and an application.properties file:

service.api.client_id=client_id
service.api.client_secret=client_secret

and the binding works when I'm doing local development.

What I want to do is to use Azure services (Azure Spring Apps, Key Vault, etc.) and my question is whether I can, inside application.properties file, reference secrets stored in the Key Vault. Something like this:

service.api.client_id=<name-of-the-id-kept-in-the-vault>
service.api.client_secret=<name-of-the-secret-kept-in-the-vault>

Tutorials I've seen make me think that the only way to do the binding is by using @Value("${nameOfTheSecret}") annotation on fields in java classes. Is it possible to do it in a way that I want?

1 Answers

You can reference Azure Key Vault secrets inside the application.properties file by adding the below properties of azure key vault which you want to connect from the spring boot application:

spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-id=<your client ID>
spring.cloud.azure.keyvault.secret.property-sources[0].credential.client-secret=<your client key>
spring.cloud.azure.keyvault.secret.property-sources[0].endpoint=https://contosokv.vault.azure.net/
spring.cloud.azure.keyvault.secret.property-sources[0].profile.tenant-id=<your tenant ID>
Related