此版本仍在开发中,尚未被视为稳定版本。如需最新的稳定版本,请使用 Spring Cloud Vault 4.1.4spring-doc.cadn.net.cn

快速开始

本节介绍如何开始使用 Vault 和 Spring Cloud Vault。spring-doc.cadn.net.cn

先决条件

要开始使用 Vault 和本指南,您需要一个类似 *NIX 的作系统,该作系统提供:spring-doc.cadn.net.cn

本指南从 Spring Cloud Vault 的角度解释了 Vault 设置以进行集成测试。 您可以直接在 Vault 项目站点上找到快速入门指南:learn.hashicorp.com/vault
$ wget https://releases.hashicorp.com/vault/${vault_version}/vault_${vault_version}_${platform}.zip
$ unzip vault_${vault_version}_${platform}.zip
这些步骤可以通过下载和运行install_vault.sh.

为 Vault 创建 SSL 证书

接下来,您需要生成一组证书:spring-doc.cadn.net.cn

确保将根证书导入到符合 Java 的信任库中。spring-doc.cadn.net.cn

实现这一目标的最简单方法是使用 OpenSSL。spring-doc.cadn.net.cn

create_certificates.sh在 中创建证书work/ca和 JKS 信任库work/keystore.jks. 如果您想使用本快速入门指南运行 Spring Cloud Vault,则需要配置信任库spring.cloud.vault.ssl.trust-storeproperty 设置为file:work/keystore.jks.

启动 Vault 服务器

接下来,按照以下行创建一个配置文件:spring-doc.cadn.net.cn

backend "inmem" {
}

listener "tcp" {
  address = "0.0.0.0:8200"
  tls_cert_file = "work/ca/certs/localhost.cert.pem"
  tls_key_file = "work/ca/private/localhost.decrypted.key.pem"
}

disable_mlock = true
您可以在vault.conf.
$ vault server -config=vault.conf

Vault 已开始侦听0.0.0.0:8200使用inmemstorage 和https. Vault 是密封的,并且在启动时未初始化。spring-doc.cadn.net.cn

如果要运行测试,请保持 Vault 未初始化状态。 这些测试将初始化 Vault 并创建根令牌00000000-0000-0000-0000-000000000000.

如果您想将 Vault 用于您的应用程序或尝试一下,那么您需要先初始化它。spring-doc.cadn.net.cn

$ export VAULT_ADDR="https://localhost:8200"
$ export VAULT_SKIP_VERIFY=true # Don't do this for production
$ vault operator init

您应该会看到如下内容:spring-doc.cadn.net.cn

Key 1: 7149c6a2e16b8833f6eb1e76df03e47f6113a3288b3093faf5033d44f0e70fe701
Key 2: 901c534c7988c18c20435a85213c683bdcf0efcd82e38e2893779f152978c18c02
Key 3: 03ff3948575b1165a20c20ee7c3e6edf04f4cdbe0e82dbff5be49c63f98bc03a03
Key 4: 216ae5cc3ddaf93ceb8e1d15bb9fc3176653f5b738f5f3d1ee00cd7dccbe926e04
Key 5: b2898fc8130929d569c1677ee69dc5f3be57d7c4b494a6062693ce0b1c4d93d805
Initial Root Token: 19aefa97-cccc-bbbb-aaaa-225940e63d76

Vault initialized with 5 keys and a key threshold of 3. Please
securely distribute the above keys. When the Vault is re-sealed,
restarted, or stopped, you must provide at least 3 of these keys
to unseal it again.

Vault does not store the master key. Without at least 3 keys,
your Vault will remain permanently sealed.

Vault 将初始化并返回一组解封密钥和根令牌。 选择 3 个密钥并解封 Vault。 将 Vault 令牌存储在VAULT_TOKEN环境变量。spring-doc.cadn.net.cn

$ vault operator unseal (Key 1)
$ vault operator unseal (Key 2)
$ vault operator unseal (Key 3)
$ export VAULT_TOKEN=(Root token)
# Required to run Spring Cloud Vault tests after manual initialization
$ vault token create -id="00000000-0000-0000-0000-000000000000" -policy="root"

Spring Cloud Vault 访问不同的资源。 默认情况下,密钥后端处于启用状态,该后端通过 JSON 终端节点访问密钥配置设置。spring-doc.cadn.net.cn

HTTP 服务具有以下形式的资源:spring-doc.cadn.net.cn

/secret/{application}/{profile}
/secret/{application}
/secret/{defaultContext}/{profile}
/secret/{defaultContext}

其中 “application” 作为spring.application.nameSpringApplication(即常规 Spring Boot 应用程序中通常的“应用程序”),“profile”是一个活动的配置文件(或逗号分隔的属性列表)。 从 Vault 检索到的属性将“按原样”使用,而无需为属性名称添加进一步的前缀。spring-doc.cadn.net.cn

客户端使用

要在应用程序中使用这些功能,只需将其构建为依赖于spring-cloud-vault-config(例如,参见测试用例)。 Maven 配置示例:spring-doc.cadn.net.cn

pom.xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>${springBootVersion}</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-vault-config</artifactId>
        <version>4.2.0-RC1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<!-- repositories also needed for snapshots and milestones -->

然后,您可以创建一个标准的 Spring Boot 应用程序,例如这个简单的 HTTP 服务器:spring-doc.cadn.net.cn

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

当它运行时,它将从端口上的默认本地 Vault 服务器获取外部配置8200如果它正在运行。 要修改启动行为,可以使用application.properties例如spring-doc.cadn.net.cn

application.yml
spring.cloud.vault:
    host: localhost
    port: 8200
    scheme: https
    uri: https://localhost:8200
    connection-timeout: 5000
    read-timeout: 15000
spring.config.import: vault://

启用更多集成需要额外的依赖项和配置。 根据您设置 Vault 的方式,您可能需要其他配置,例如 SSL身份验证spring-doc.cadn.net.cn

如果应用程序导入spring-boot-starter-actuatorproject 中,Vault 服务器的状态将通过/health端点。spring-doc.cadn.net.cn

可以通过属性management.health.vault.enabled(默认为true).spring-doc.cadn.net.cn

在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,引导上下文初始化(bootstrap.yml,bootstrap.properties) 已弃用。 相反,Spring Cloud Vault 偏爱 Spring Boot 的 Config Data API,它允许从 Vault 导入配置。使用 Spring Boot Config Data 方法,您需要将spring.config.import属性以绑定到 Vault 中。您可以在 Config Data Locations 部分中阅读更多相关信息。 您可以通过设置 configuration 属性来启用 bootstrap 上下文spring.cloud.bootstrap.enabled=true或通过包含依赖项org.springframework.cloud:spring-cloud-starter-bootstrap.

认证

Spring Cloud Vault 支持多种身份验证机制,以使用 Vault 对应用程序进行身份验证。spring-doc.cadn.net.cn

对于快速入门,请使用 Vault 初始化打印的根令牌。spring-doc.cadn.net.cn

application.yml
spring.cloud.vault:
    token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
spring.config.import: vault://
请仔细考虑您的安全要求。 如果您想快速开始使用 Vault,静态令牌身份验证很好,但静态令牌不再受到保护。 任何向非预期方披露都允许 Vault 与关联的令牌角色一起使用。