Search code examples
spring-bootgoogle-cloud-sqlgoogle-secret-manager

Secret Manager with Spring Boot issue


I am working on a Spring Boot project where I need to store secrets in the GCP secret manager. I have set the secrets in the GCP project but to access the secrets in spring boot code whenever I add this dependency

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
        </dependency>

I get the error

java.lang.IllegalArgumentException: An instance connection name must be provided in the format <PROJECT_ID>:<REGION>:<INSTANCE_ID>.
    at org.springframework.util.Assert.hasText(Assert.java:289) ~[spring-core-5.3.16.jar:5.3.16]
    at com.google.cloud.spring.autoconfigure.sql.DefaultCloudSqlJdbcInfoProvider.<init>(DefaultCloudSqlJdbcInfoProvider.java:38) ~[spring-cloud-gcp-autoconfigure-3.3.0.jar:3.3.0]

I have Cloud SQL excluded in application-dev.properties file

spring.autoconfigure.exclude=org.springframework.cloud.gcp.autoconfigure.sql.GcpCloudSqlAutoConfiguration

I am not sure why it still gives this error mentioning DefaultCloudSqlJdbcInfoProvider. Did anyone encounter such an issue with Secret Manager? Could there be a dependency conflict?

How to reproduce

To reproduce the issue, I have following dependencies in the pom.xml file that might be conflicting:

    
    <dependencies>        
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.http-client</groupId>
            <artifactId>google-http-client-appengine</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-tasks</artifactId>
        </dependency>
    </dependencies>

I'm using parent version 2.6.4.

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.6.4</version>
</parent>

and org.springframework.cloud version 2021.0.4

So whenever I have these dependencies in the file it breaks. I need all of these so i could not remove any and run the project. You might not need any GCP API enabled since this issue occurs before any kind of billing check.


Solution

  • After hours of debugging I finally identified the issue that was conflicting dependencies of com.google.cloud and org.springframework.cloud. By changing

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
            </dependency>
    

    to

            <dependency>
                <groupId>com.google.cloud</groupId>
                <artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
            </dependency>
    

    The org.springframework.cloud was triggering the mysql autoconfiguration which needed an instance name even with exclusion.