|
该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Cloud Vault 5.0.0! |
快速入门
本节将介绍如何引导你开始使用 Vault 和 Spring Cloud Vault。
前提条件
要开始使用Vault和本指南,你需要一个类似*NIX的作系统,提供:
-
WGET,OpenSSL(开放SSL)和解 压缩 -
至少是Java 17,配置得当
JAVA_HOME环境变量
| 本指南从Spring Cloud Vault的角度解释了集成测试的Vault设置。 你可以直接在Vault项目网站上找到入门指南:developer.hashicorp.com/vault/tutorials |
安装 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
-
保险库证书(解密密钥)
work/ca/private/localhost.decrypted.key.pem以及证书work/ca/certs/localhost.cert.pem)
确保将根证书导入支持Java的truststore。
实现这一点最简单的方法是使用 OpenSSL。
create_certificates.sh创建 的证书工作/加州以及一个JKS信托存储器work/keystore.jks.
如果你想用这个快速入门指南运行 Spring Cloud Vault,你需要配置 truststoreSpring.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使用因梅姆存储和https.
开机时保险库是封闭的,没有初始化。
如果你想运行测试,可以让Vault保持初始化。
测试会初始化Vault并创建根Tokens00000000-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把钥匙,解封金库。
将金库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}
其中“应用”被注入为spring.application.name在SpringApplication(即普通 Spring Boot 应用中通常称为“应用程序”),“配置文件”是一个活跃配置文件(或逗号分隔的属性列表)。
从 Vault 检索的属性将按原样使用,不需进一步前缀属性名称。
客户端使用
要在应用中使用这些功能,只需构建为依赖于Spring-cloud-vault-config(例如,参见测试案例)。
示例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>5.0.1-SNAPSHOT</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如果它在运行的话。
要修改启动行为,可以用以下方式更改 Vault 服务器的位置application.properties例如
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证书验证 -
端口设置了Vault端口 -
方案将方案设为http将使用纯 HTTP 协议。 支持的方案有http和https. -
乌里用URI配置Vault端点。优先于主机/端口/方案配置 -
连接超时设置连接超时(毫秒级) -
读取超时将读取超时设置为毫秒级 -
spring.config.importMounts Vault 作为地产来源使用所有启用的秘密后端(默认启用键值)
如果应用程序导入Spring-启动-执行器项目中,Vault 服务器的状态将通过/健康端点。
保险库健康指示器可以通过属性启用或禁用management.health.vault.enabled(默认为true).
在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,引导式上下文初始化(bootstrap.yml,bootstrap.properties)的财产来源被弃用。
相反,Spring Cloud Vault 更倾向于 Spring Boot 的配置数据 API,允许从 Vault 导入配置。使用 Spring Boot 配置数据方法时,你需要设置spring.config.import财产以绑定于Vault。你可以在配置数据位置部分了解更多。
你可以通过设置配置属性来启用引导上下文spring.cloud.bootstrap.enabled=true或者通过包含依赖关系org.springframework.cloud:spring-cloud-starter-bootstrap. |