Vault 后端
Spring Cloud Config Server 也支持使用 Vault 作为后端。
有关 Vault 的更多信息,请参阅 Vault 快速入门指南。
要使 Config Server 使用 Vault 后端,您可以使用 vault
profile 运行 Config Server。例如,在您的 Config Server 的 application.properties
中,您可以添加 spring.profiles.active=vault
。
默认情况下,Config Server 假设您的 Vault 服务器运行在 127.0.0.1:8200
。它还假设后端名称为 secret
,键为 application
。所有这些默认值都可以在 Config Server 的 application.properties
中配置。下表描述了可配置的 Vault 属性。
名称 | 默认值 |
---|---|
host |
127.0.0.1 |
port |
8200 |
scheme |
http |
backend |
secret |
defaultKey |
application |
defaultLabel |
main(仅在 |
enableLabel |
false |
profileSeparator |
, |
kvVersion |
1 |
skipSslValidation |
false |
timeout |
5 |
namespace |
null |
前述表格中的所有属性必须以 spring.cloud.config.server.vault 为前缀,或者放在复合配置中正确的 Vault 部分下。 |
所有可配置属性可以在 org.springframework.cloud.config.server.environment.VaultEnvironmentProperties
中找到。
Vault 0.10.0 引入了版本化的键值后端(k/v 后端版本 2),它提供了与早期版本不同的 API。现在,它要求在挂载路径和实际上下文路径之间有一个 data/ ,并将秘密包装在 data 对象中。设置 spring.cloud.config.server.vault.kv-version=2 将考虑此特性。 |
可选地,支持 Vault Enterprise 的 X-Vault-Namespace
请求头。要将其发送到 Vault,请设置 namespace
属性。
Config Server 启动后,您可以向服务器发出 HTTP 请求以从 Vault 后端检索值。为此,您需要 Vault 服务器的令牌。
首先,在您的 Vault 中放置一些数据,示例如下:
$ vault kv put secret/application foo=bar baz=bam
$ vault kv put secret/myapp foo=myappsbar
其次,向您的 Config Server 发出 HTTP 请求以检索值,示例如下:
$ curl -X "GET" "http://localhost:8888/myapp/default" -H "X-Config-Token: yourtoken"
您应该看到类似以下的响应:
{
"name":"myapp",
"profiles":[
"default"
],
"label":null,
"version":null,
"state":null,
"propertySources":[
{
"name":"vault:myapp",
"source":{
"foo":"myappsbar"
}
},
{
"name":"vault:application",
"source":{
"baz":"bam",
"foo":"bar"
}
}
]
}
客户端向 Config Server 提供必要身份验证以使其与 Vault 通信的默认方法是设置 X-Config-Token 请求头。但是,您可以省略此请求头,并在服务器端配置身份验证,方法是设置与 Spring Cloud Vault 相同的配置属性。需要设置的属性是 spring.cloud.config.server.vault.authentication
。应将其设置为受支持的身份验证方法之一。您可能还需要设置特定于您使用的身份验证方法的其他属性,方法是使用与 spring.cloud.vault
文档中相同的属性名称,但改用 spring.cloud.config.server.vault
前缀。有关更多详细信息,请参阅 Spring Cloud Vault 参考指南。
如果您省略 X-Config-Token 请求头并使用服务器属性来设置身份验证,则 Config Server 应用程序需要额外依赖 Spring Vault 以启用额外的身份验证选项。有关如何添加该依赖项的信息,请参阅 Spring Vault 参考指南。 |
多个属性源
使用 Vault 时,您可以为应用程序提供多个属性源。例如,假设您已将数据写入 Vault 中的以下路径:
secret/myApp,dev
secret/myApp
secret/application,dev
secret/application
写入 secret/application
的属性可供使用 Config Server 的所有应用程序使用。名为 myApp
的应用程序将可以使用写入 secret/myApp
和 secret/application
的任何属性。当 myApp
启用了 dev
profile 时,写入上述所有路径的属性都可供其使用,列表中的第一个路径中的属性优先于其他路径中的属性。
启用按标签搜索
默认情况下,Vault 后端在搜索秘密时不使用标签。您可以通过将 enableLabel
特性标志设置为 true
,并可选地设置 defaultLabel
来更改此行为。如果未提供 defaultLabel
,将使用 main
。
当 enableLabel
特性标志开启时,Vault 中的秘密在其路径中应始终包含所有三个部分(应用程序名称、profile 和标签)。因此,上一节中的示例在启用此特性标志后将类似于:
secret/myApp,dev,myLabel
secret/myApp,default,myLabel # default profile
secret/application,dev,myLabel # default application name
secret/application,default,myLabel # default application name and default profile.
解密属性源中的 Vault 秘密
Spring Cloud Config Server 支持通过利用特殊的占位符前缀 {vault}
来解密 Vault 中的属性。此功能允许在运行时直接从 Vault 动态解析敏感配置属性。
配置步骤
所有与 Vault 集成的配置设置应放置在您的 application.yml
或 application.properties
中。以下是激活 Vault profile、连接到您的 Vault 服务器以及使用 {vault}
前缀格式化属性所需的具体配置。