快速開始
本節說明如何開始使用 Vault 和 Spring Cloud Vault。
先決條件
若要開始使用 Vault 和本指南,您需要提供下列功能的類 *NIX 作業系統:
-
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 憑證
接下來,您需要產生一組憑證
-
Root CA
-
Vault 憑證 (解密金鑰
work/ca/private/localhost.decrypted.key.pem
和憑證work/ca/certs/localhost.cert.pem
)
請務必將 Root 憑證匯入符合 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 並建立 root token 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 將初始化並傳回一組解封金鑰和 root token。選取 3 個金鑰並解封 Vault。將 Vault token 儲存在 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 backend,它透過 JSON 端點存取 secret config 設定。
HTTP 服務的資源形式如下
/secret/{application}/{profile} /secret/{application} /secret/{defaultContext}/{profile} /secret/{defaultContext}
其中 "application" 作為 SpringApplication
中的 spring.application.name
注入 (即一般 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.1.3</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 端點。優先於 host/port/scheme 設定 -
connection-timeout
設定連線逾時時間,單位為毫秒 -
read-timeout
設定讀取逾時時間,單位為毫秒 -
spring.config.import
將 Vault 掛載為 PropertySource,使用所有已啟用的 secret backend (預設啟用 key-value)。
如果應用程式匯入 spring-boot-starter-actuator
專案,則 Vault 伺服器的狀態將可透過 /health
端點取得。
可以透過屬性 management.health.vault.enabled
啟用或停用 Vault 健康指示器 (預設為 true
)。
在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,bootstrap context 初始化 (bootstrap.yml 、bootstrap.properties ) 的屬性來源已被棄用。相反地,Spring Cloud Vault 偏好 Spring Boot 的 Config Data API,它允許從 Vault 匯入設定。使用 Spring Boot Config Data 方法,您需要設定 spring.config.import 屬性才能繫結至 Vault。您可以在 Config Data Locations 區段 中閱讀更多相關資訊。您可以透過設定組態屬性 spring.cloud.bootstrap.enabled=true 或包含依賴項 org.springframework.cloud:spring-cloud-starter-bootstrap 來啟用 bootstrap context。 |