I am currently trying to replace hardcoded secrets on my Spring (non SpringBoot) application using the Spring Cloud Azure library as documented in here.
Currently I have managed to retrieve the secrets after the application startup:
@Configuration
public class KeyVaultConfig {
@Value("${spring.cloud.azure.keyvault.secret.endpoint}")
private String keyVaultUri;
private SecretClient secretClient;
public KeyVaultConfig() {}
@PostConstruct
private void setKeyVaultProvider() {
this.secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
}
public SecretClient getSecretClient() {
return secretClient;
}
}
And then I succesfully retrieve the secret in another class as:
@PostConstruct
public void getTest() {
System.out.println("lul");
SecretClient secretClient = keyVaultConfig.getSecretClient();
secretClient.getSecret("secret");
}
Now, the problem is that I need to use the secret as soon as the context is created because my app-context.xml is defined as this:
<bean id="dataSourceMysql" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${db.driverClassName}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
<property name="minPoolSize" value="${c3p0.minPoolSize}" />
<property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
<property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
<property name="preferredTestQuery" value="SELECT 1234567890" />
<property name="testConnectionOnCheckout" value="${c3p0.testConnectionOnCheckout}" />
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
</bean>
Any advice or suggestion? Thanks!
To use the secret retrieved from Azure Key Vault as the password for your MySQL DataSource in the Spring application.
Firstly, Add Spring Cloud Azure Dependencies.
pom.xml:If you use any certificates the add
Azure-identitydependency also.KeyVaultConfig:
SecretInitializer:
Its better to configure in
application.properties, configure your MySQL DataSource, including the password placeholder.properties:
DataSourceConfig:
Secret retrieved: