|
对于最新稳定版本,请使用 Spring Cloud Vault 5.0.1! |
快速开始
这一部分将向您介绍如何开始使用 Vault 和 Spring Cloud Vault。
前置条件
为了开始使用 Vault 和本指南,你需要一个 *NIX-like 操作系统,该系统提供:
-
wget,openssl和unzip -
至少需要 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 开始在 0.0.0.0:8200 上监听,使用 inmem 存储和 https。
Vault 在启动时被封存且未初始化。
如果要运行测试,请不初始化 Vault。
测试将初始化 Vault 并创建一个根Tokens 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 将初始化并返回一组解密密钥和根Tokens。
选择 3 个密钥进行解密。
将 Vault Tokens存储在 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}
其中 "application" 作为 spring.application.name 在
SpringApplication(即在常规 Spring Boot 应用中通常为 "application" 的部分)中被注入,"profile" 为一个活动的 profile(或用逗号分隔的多个属性)。从 Vault 获取的属性值将直接使用,不会对属性名进行进一步的前缀处理。
客户端使用
要在应用程序中使用这些功能,只需将其构建为依赖于 spring-cloud-vault-config 的 Spring Boot 应用程序(例如,请参阅测试用例)。
Maven 配置示例:
<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.3.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 服务器的位置,例如
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。支持的协议有http和https。 -
uri使用URI配置Vault端点。优先于主机/端口/方案配置 -
connection-timeout设置连接超时时间(毫秒) -
read-timeout设置读取超时时间(毫秒) -
spring.config.import将 Vault 挂载为PropertySource,并使用所有已启用的密钥后端(默认情况下启用了键值对)
如果应用程序导入了 spring-boot-starter-actuator 项目,则可以通过 /health 端点获取保险箱服务器的状态。
机密存储库健康指标可以通过属性 management.health.vault.enabled 启用或禁用(默认为 true)。
使用 Spring Cloud Vault 3.0 和 Spring Boot 2.4,属性源的引导上下文初始化(bootstrap.yml,bootstrap.properties)已弃用。
相反,Spring Cloud Vault 倾向于使用 Spring Boot 的配置数据 API,该 API 允许从 Vault 导入配置。使用 Spring Boot 配置数据方法时,您需要设置 spring.config.import 属性才能绑定到 Vault。有关更多信息,请参阅配置数据位置部分。
您可以设置配置属性 spring.cloud.bootstrap.enabled=true 或包含依赖项 org.springframework.cloud:spring-cloud-starter-bootstrap 来启用引导上下文。 |
身份验证
Spring Cloud Vault 支持多种身份验证机制,用于将应用程序与 Vault 进行身份验证。
快速入门时,请使用 Vault 初始化过程中打印的根Tokens (Vault初始化)。
spring.cloud.vault:
token: 19aefa97-cccc-bbbb-aaaa-225940e63d76
spring.config.import: vault://
| 仔细考虑您的安全需求。</p><p>如果您想快速开始使用Vault,静态Tokens身份验证是可以接受的,但是静态Tokens没有得到进一步保护。</p><p>任何向非预期方披露都会允许使用与该Tokens角色关联的Vault。 |