谷歌秘密管理器:不要用Spring Boot从中获取价值

问题描述 投票:0回答:1

我按照这个帖子里的说明。https:/github.comspring-cloudspring-cloud-gcptreemasterspring-cloud-gcp-samplesspring-cloud-gcp-secretmanager-sample启动应用程序后,一切正常。

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

pom.xml.bootstrap.属性。

<?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 );
    }
}

任何想法,我可以做什么?

spring-boot google-cloud-platform google-secret-manager
1个回答
3
投票

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

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

对我有用

© www.soinside.com 2019 - 2024. All rights reserved.