快速入门

本节将指导您如何开始使用 Vault 和 Spring Cloud Vault。

先决条件

要开始使用 Vault 和本指南,您需要一个类 *NIX 操作系统,该系统提供

  • wget, opensslunzip

  • 至少 Java 8,并且正确配置 JAVA_HOME 环境变量

本指南从 Spring Cloud Vault 的角度解释 Vault 的设置,以便进行集成测试。您可以在 Vault 项目网站上直接找到入门指南:learn.hashicorp.com/vault

安装 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 证书

接下来,您需要生成一套证书

  • 根 CA

  • Vault 证书(解密的密钥 work/ca/private/localhost.decrypted.key.pem 和证书 work/ca/certs/localhost.cert.pem

请务必将根证书导入 Java 兼容的信任库。

实现此目的最简单的方法是使用 OpenSSL。

create_certificates.sh 会在 work/ca 中创建证书,并在 work/keystore.jks 中创建 JKS 信任库。如果您希望使用此快速入门指南运行 Spring Cloud Vault,需要将信任库的 spring.cloud.vault.ssl.trust-store 属性配置为 file:work/keystore.jks

启动 Vault 服务器

接下来,创建一个类似如下的配置文件

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 已启动,使用 inmem 存储和 https 监听 0.0.0.0:8200。启动时,Vault 是密封的,尚未初始化。

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

如果您想将 Vault 用于您的应用程序或尝试一下,则需要先对其进行初始化。

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

您应该会看到类似如下内容

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 环境变量中。

$ 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 访问不同的资源。默认情况下,Secret 后端是启用的,它通过 JSON 端点访问 Secret 配置设置。

HTTP 服务具有以下形式的资源

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

其中 "application" 作为 spring.application.name 注入到 SpringApplication 中(即普通 Spring Boot 应用中通常的 "application"),"profile" 是活动配置文件(或逗号分隔的属性列表)。从 Vault 中检索到的属性将按“原样”使用,不再对属性名称进行进一步前缀处理。

客户端用法

要在应用程序中使用这些特性,只需将其构建为依赖于 spring-cloud-vault-config 的 Spring Boot 应用程序即可(例如,参见测试用例)。Maven 配置示例

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.1</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 服务器

@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 更改 Vault 服务器的位置,例如

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://
  • host 设置 Vault 主机的主机名。主机名将用于 SSL 证书验证

  • port 设置 Vault 端口

  • scheme 将方案设置为 http 将使用纯 HTTP。支持的方案为 httphttps

  • uri 使用 URI 配置 Vault 端点。优先级高于 host/port/scheme 配置

  • connection-timeout 设置连接超时时间(毫秒)

  • read-timeout 设置读取超时时间(毫秒)

  • spring.config.import 使用所有启用的 Secret 后端(默认启用 key-value)将 Vault 挂载为 PropertySource

启用进一步的集成需要额外的依赖和配置。根据您设置 Vault 的方式,您可能需要额外的配置,例如 SSL认证

如果应用程序导入了 spring-boot-starter-actuator 项目,Vault 服务器的状态将可以通过 /health 端点获取。

Vault 健康指示器可以通过属性 management.health.vault.enabled 来启用或禁用(默认为 true)。

在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,属性源的 bootstrap 上下文初始化(bootstrap.ymlbootstrap.properties)已被弃用。相反,Spring Cloud Vault 更倾向于使用 Spring Boot 的 Config Data API,该 API 允许从 Vault 导入配置。使用 Spring Boot Config Data 方法,您需要设置 spring.config.import 属性才能绑定到 Vault。您可以在 Config Data 位置 部分中阅读更多相关内容。

认证

Vault 需要一个认证机制授权客户端请求

Spring Cloud Vault 支持多种认证机制来验证应用程序与 Vault 的身份。

为了快速入门,请使用由Vault 初始化打印出的根令牌。

application.yml
spring.cloud.vault:
    token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
spring.config.import: vault://
请仔细考虑您的安全要求。如果您只是想快速入门使用 Vault,静态令牌认证是可以的,但静态令牌没有进一步的保护。任何泄露给未经授权的方都可能导致使用关联令牌角色访问 Vault。