快速入門
本節將指導您如何開始使用 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 證書
接下來,您需要生成一套證書
-
根 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 訪問不同的資源。預設情況下,Secret 後端是啟用的,它透過 JSON 端點訪問 Secret 配置設定。
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>4.2.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://: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
使用所有啟用的 Secret 後端(預設啟用 key-value)將 Vault 掛載為PropertySource
如果應用程式匯入了 spring-boot-starter-actuator
專案,Vault 伺服器的狀態將可以透過 /health
端點獲取。
Vault 健康指示器可以透過屬性 management.health.vault.enabled
來啟用或停用(預設為 true
)。
在 Spring Cloud Vault 3.0 和 Spring Boot 2.4 中,屬性源的 bootstrap 上下文初始化(bootstrap.yml ,bootstrap.properties )已被棄用。相反,Spring Cloud Vault 更傾向於使用 Spring Boot 的 Config Data API,該 API 允許從 Vault 匯入配置。使用 Spring Boot Config Data 方法,您需要設定 spring.config.import 屬性才能繫結到 Vault。您可以在 Config Data 位置 部分中閱讀更多相關內容。 |