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

Cloud Run, Secret Manager and Spring Boot


I want to load whole .properties file from GCP Secret Manager to my Spring Boot application.

Secret is "mounted as volume" in Cloud Run (whole .properties file, in path /secrets/secret.properties), but I cannot manage to load it to Spring Boot using spring.config.import

I was trying:

spring.config.import=optional:configtree:/secrets/
spring.config.import=optional:classpath:/secrets/secret.properties
spring.config.import=optional:/secrets/secret.properties

but nothing works. Values are not visible in Spring Boot application. (java.lang.IllegalArgumentException: Could not resolve placeholder 'x' in value "${x}") When putting file on resource classpath, everything works.


Solution

  • Here the set up that I have tested and which works (don't forget to grant the correct permissions)

    JIB configuration

    ...
               <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>com.google.cloud.tools</groupId>
                    <artifactId>jib-maven-plugin</artifactId>
                    <version>3.1.1</version>
                    <configuration>
                        <container>
                            <args>--spring.config.location=file:///secret/my.properties</args>
                        </container>
                        <to>
                            <image>gcr.io/<PROJECT_ID>/springboot</image>
                            <credHelper>gcr</credHelper>
                        </to>
                    </configuration>
                </plugin>
    ...
    

    My Cloud Run deployment

    gcloud beta run deploy --image=gcr.io/<PROJECT_ID>/springboot \
     --region=us-central1 --allow-unauthenticated --platform=managed \
     --set-secrets=/secret/my.properties=projects/<PROJECT_Number>/secrets/springboot:1 \
    secret-springboot