本节介绍如何开始使用 Vault 和 Spring Cloud Vault。

先决条件

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

  • wgetopensslunzip

  • 至少是 Java 8 和正确配置的环境变量JAVA_HOME

本指南从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来实现。
本指南从Spring Cloud Vault的角度解释了用于集成测试的Vault设置。 您可以直接在 Vault 项目网站上找到入门指南:learn.hashicorp.com/vault
这些步骤可以通过下载并运行install_vault.sh来实现。

为保险柜创建SSL证书

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

  • 根 CA

  • 保险柜证书(解密的密钥和证书)work/ca/private/localhost.decrypted.key.pemwork/ca/certs/localhost.cert.pem)

确保将根证书导入到符合 Java 的信任库中。

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

create_certificates.sh 在 和 JKS 信任库中创建证书。 如果要使用本快速入门指南运行Spring Cloud Vault,则需要将信任库配置为。work/cawork/keystore.jksspring.cloud.vault.ssl.trust-storefile:work/keystore.jks
create_certificates.sh 在 和 JKS 信任库中创建证书。 如果要使用本快速入门指南运行Spring Cloud Vault,则需要将信任库配置为。work/cawork/keystore.jksspring.cloud.vault.ssl.trust-storefile: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 已开始侦听使用存储和 . Vault 已密封,启动时未初始化。0.0.0.0:8200inmemhttps

如果要运行测试,请保持保管库未初始化状态。 测试将初始化 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.

保险柜将初始化并返回一组解封密钥和根令牌。 选择 3 把钥匙并解封保险库。 将 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 访问不同的资源。 默认情况下,密钥后端处于启用状态,该后端通过 JSON 端点访问密钥配置设置。

HTTP 服务包含以下形式的资源:

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

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

您可以在 vault.conf 中找到示例配置文件。
如果要运行测试,请保持保管库未初始化状态。 测试将初始化 Vault 并创建根令牌。00000000-0000-0000-0000-000000000000

客户端使用情况

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

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.1.2</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 服务器中获取外部配置(如果它正在运行)。 要修改启动行为,您可以使用 更改 Vault 服务器的位置,例如8200application.properties

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。 支持的方案是 和 。httphttphttps

  • uri使用 URI 配置 Vault 端点。优先于主机/端口/方案配置

  • connection-timeout设置连接超时(以毫秒为单位)

  • read-timeout设置读取超时(以毫秒为单位)

  • spring.config.import使用所有已启用的密钥后端装载 Vault(默认启用键值)PropertySource

启用进一步的集成需要额外的依赖项和配置。 根据您设置保险柜的方式,您可能需要其他配置,例如 SSL身份验证

如果应用程序导入项目,则库服务器的状态将通过端点提供。spring-boot-starter-actuator/health

可以通过属性启用或禁用保管库运行状况指示器(默认为 )。management.health.vault.enabledtrue

在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,不推荐使用属性源的引导上下文初始化 (, )。 相反,Spring Cloud Vault 更喜欢 Spring Boot 的 Config Data API,它允许从 Vault 导入配置。使用Spring Boot配置数据方法,您需要设置属性才能绑定到Vault。您可以在“配置数据位置”部分阅读有关它的更多信息。 您可以通过设置 configuration 属性或包含依赖项来启用引导上下文。bootstrap.ymlbootstrap.propertiesspring.config.importspring.cloud.bootstrap.enabled=trueorg.springframework.cloud:spring-cloud-starter-bootstrap
在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,不推荐使用属性源的引导上下文初始化 (, )。 相反,Spring Cloud Vault 更喜欢 Spring Boot 的 Config Data API,它允许从 Vault 导入配置。使用Spring Boot配置数据方法,您需要设置属性才能绑定到Vault。您可以在“配置数据位置”部分阅读有关它的更多信息。 您可以通过设置 configuration 属性或包含依赖项来启用引导上下文。bootstrap.ymlbootstrap.propertiesspring.config.importspring.cloud.bootstrap.enabled=trueorg.springframework.cloud:spring-cloud-starter-bootstrap

认证

Spring Cloud Vault 支持多种身份验证机制,以使用 Vault 对应用程序进行身份验证。

对于快速入门,请使用保管库初始化打印的根令牌。

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