Search code examples
spring-bootspring-data-jpaspring-cloud-gcp

Enhance DataSourceProperties configuration bean with KMS provided secret


I want to set the password for the datasource programatically and still use auto configuration of Spring GCP and Spring Data.

Background

I want to obtain my password as encrypted blob and decrypt it via Google KMS. I read that spring gcp auto configuration mutates the DataSourceProperties bean to apply gcp secific configurations (https://docs.spring.io/spring-cloud-gcp/docs/1.0.0.RELEASE/reference/html/_spring_jdbc.html#_literal_datasource_literal_creation_flow).

Now I would like to mutate the DataSourceProperties Bean exposed by GcpCloudSqlAutoConfiguration once more. Exposing the DataSourceProperties as follows does not work because GcpCloudSqlAutoConfiguration then doesn't know which Bean to take: the one configured by spring boot auto configuration or mine. But mine should be applied in the very last step of configuration.

@Bean
public DataSourceProperties dataSourceProperties(DataSourceProperties dataSourceProperties) {
    dataSourceProperties.setPassword(getDecryptedSecret());
    return dataSourceProperties;
}

private String getDecryptedSecret() {
    // get encrypted password and decrypt it
    return "myDecryptedPassword"
}

Solution

  • You should be able to provide your own bootstrap property source that contains a value for spring.datasource.password.

    See: https://cloud.spring.io/spring-cloud-commons/multi/multi__spring_cloud_context_application_context_services.html#customizing-bootstrap-property-sources

    Full answer is on project GitHub: https://github.com/spring-cloud/spring-cloud-gcp/issues/2330