快速入門
本節介紹如何開始使用 Vault 和 Spring Cloud Vault。
先決條件
要開始使用 Vault 並參考本指南,您需要一個類 UNIX 作業系統,該系統提供
-
wget、openssl和unzip -
至少 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
-
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://: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 訪問不同的資源。預設情況下,秘密後端已啟用,它透過 JSON 端點訪問秘密配置設定。
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 配置示例
<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.0</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://: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 端點獲取 vault 伺服器的狀態。
可以透過屬性 management.health.vault.enabled(預設為 true)啟用或停用 Vault 健康指標。
對於 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 來啟用引導上下文。 |