Google Secret Manager:不要通过 Spring Boot 从中获取价值

Google Secret Manager: don't get value from it with Spring Boot

我按照 post 的说明进行操作:https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-secretmanager-sample 启动应用程序后一切正常。

然后我在我的 Spring 引导应用程序中实现它,我得到 //application-secret 而不是秘密的值。

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.BUILD-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.flis</groupId>
    <artifactId>protein</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>protein</name>
    <description>Flis Protein Project</description>

    <properties>
        <maven.compiler.target>11</maven.compiler.target>
        <maven.compiler.source>11</maven.compiler.source>
        <start-class>com.flis.protein.ProteinApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

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

        <!-- used for secret manager -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gcp-starter-secretmanager</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-gcp-dependencies</artifactId>
                <version>1.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- match profiles from spring and maven -->
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>cloud</id>
            <properties>
                <activatedProperties>cloud</activatedProperties>
            </properties>
        </profile>
    </profiles>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>appengine-maven-plugin</artifactId>
                <version>2.2.0</version>
                <configuration>
                    <version>recruiter-wtf</version>
                    <projectId>GCLOUD_CONFIG</projectId>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

bootstrap.properties

spring.cloud.gcp.secretmanager.bootstrap.enabled=true

my-app-secret-1=${sm://application-secret}

控制器:

@RestController
public class DefaultController {

    @Value("${my-app-secret-1}")
    private String secret;

    @Value("${sm://application-secret}")
    private String appSecret;

    @GetMapping(value = "/")
    public ResponseEntity<String> start() {
        return ResponseEntity.ok("Worked");
    }

    @GetMapping(value = "/secret")
    public ResponseEntity<String> getSecret(){
        return ResponseEntity.ok(secret + " --- " + appSecret );
    }
}

知道我能做什么吗?

问题出在你的前缀上。我在您的 bootstrap.properties 文件中没有看到前缀提及。在文件中添加这个

spring.cloud.gcp.secretmanager.secret-name-prefix=sm://

对我有用