此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Cloud Config 4.1.4spring-doc.cadn.net.cn

Vault 后端

Spring Cloud Config Server 还支持将 Vault 作为后端。spring-doc.cadn.net.cn

Vault 是一种用于安全访问 secret 的工具。 密钥是您想要严格控制访问的任何内容,例如 API 密钥、密码、证书和其他敏感信息。Vault 为任何密钥提供统一的接口,同时提供严格的访问控制并记录详细的审计日志。spring-doc.cadn.net.cn

有关 Vault 的更多信息,请参阅 Vault 快速入门指南spring-doc.cadn.net.cn

要使配置服务器能够使用 Vault 后端,您可以使用vault轮廓。 例如,在您的配置服务器的application.properties中,您可以添加spring.profiles.active=vault.spring-doc.cadn.net.cn

默认情况下,Spring Cloud Config Server 使用基于令牌的身份验证从 Vault 中获取配置。 Vault 还支持其他身份验证方法,如 AppRole、LDAP、JWT、CloudFoundry、Kubernetes Auth。 为了使用除TOKEN或X-Config-Token头之外的任何身份验证方法,我们需要在 Classpath 上具有 Spring Vault Core,以便 Config Server 可以将身份验证委托给该库。请将以下依赖项添加到您的 Config Server 应用程序。spring-doc.cadn.net.cn

Maven (pom.xml)spring-doc.cadn.net.cn

<dependencies>
	<dependency>
		<groupId>org.springframework.vault</groupId>
		<artifactId>spring-vault-core</artifactId>
	</dependency>
</dependencies>

Gradle (build.gradle)spring-doc.cadn.net.cn

dependencies {
    implementation "org.springframework.vault:spring-vault-core"
}

默认情况下,配置服务器假定您的 Vault 服务器在127.0.0.1:8200. 它还假定 backend 的名称为secret关键是application. 所有这些默认值都可以在配置服务器的application.properties. 下表描述了可配置的 Vault 属性:spring-doc.cadn.net.cn

名字 默认值

主机spring-doc.cadn.net.cn

127.0.0.1spring-doc.cadn.net.cn

港口spring-doc.cadn.net.cn

8200spring-doc.cadn.net.cn

方案spring-doc.cadn.net.cn

httpspring-doc.cadn.net.cn

后端spring-doc.cadn.net.cn

秘密spring-doc.cadn.net.cn

defaultKeyspring-doc.cadn.net.cn

应用spring-doc.cadn.net.cn

defaultLabelspring-doc.cadn.net.cn

main(仅在enableLabel设置为true)spring-doc.cadn.net.cn

启用标签spring-doc.cadn.net.cn

spring-doc.cadn.net.cn

profileSeparator 的spring-doc.cadn.net.cn

,spring-doc.cadn.net.cn

kvVersionspring-doc.cadn.net.cn

1spring-doc.cadn.net.cn

skipSslValidationspring-doc.cadn.net.cn

spring-doc.cadn.net.cn

超时spring-doc.cadn.net.cn

5spring-doc.cadn.net.cn

Namespacespring-doc.cadn.net.cn

spring-doc.cadn.net.cn

上表中的所有属性都必须以spring.cloud.config.server.vault或放置在复合配置的正确 Vault 部分中。

所有可配置的属性都可以在org.springframework.cloud.config.server.environment.VaultEnvironmentProperties.spring-doc.cadn.net.cn

Vault 0.10.0 引入了一个版本化的键值后端(k/v 后端版本 2),它公开了与早期版本不同的 API,现在它需要一个data/在挂载路径和实际上下文路径之间,并将 Secret 包装在data对象。设置spring.cloud.config.server.vault.kv-version=2将考虑到这一点。

(可选)支持 Vault EnterpriseX-Vault-Namespace页眉。要将其发送到 Vault,请将namespace财产。spring-doc.cadn.net.cn

在您的配置服务器运行的情况下,您可以向服务器发出 HTTP 请求以检索 值。 为此,您需要 Vault 服务器的令牌。spring-doc.cadn.net.cn

首先,将一些数据放入 Vault 中,如以下示例所示:spring-doc.cadn.net.cn

$ vault kv put secret/application foo=bar baz=bam
$ vault kv put secret/myapp foo=myappsbar

其次,向配置服务器发出 HTTP 请求以检索值,如以下示例所示:spring-doc.cadn.net.cn

$ curl -X "GET" "http://localhost:8888/myapp/default" -H "X-Config-Token: yourtoken"spring-doc.cadn.net.cn

您应该会看到类似于以下内容的响应:spring-doc.cadn.net.cn

{
   "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"
         }
      }
   ]
}

Client 端提供必要的身份验证以让 Config Server 与 Vault 通信的默认方法是设置 X-Config-Token 标头。 但是,您可以通过设置与 Spring Cloud Vault 相同的配置属性来省略 Headers 并在服务器中配置身份验证。 要设置的属性是spring.cloud.config.server.vault.authentication. 它应设置为受支持的身份验证方法之一。 您可能还需要设置特定于您使用的身份验证方法的其他属性,方法是使用与spring.cloud.vault而是使用spring.cloud.config.server.vault前缀。 有关更多详细信息,请参阅 Spring Cloud Vault 参考指南spring-doc.cadn.net.cn

如果省略X-Config-Token标头并使用服务器属性来设置身份验证,则 Config Server 应用程序需要对 Spring Vault 的额外依赖才能启用其他身份验证选项。 有关如何添加该依赖项,请参见 Spring Vault 参考指南

多个属性源

使用 Vault 时,您可以为应用程序提供多个属性源。 例如,假设您已将数据写入 Vault 中的以下路径:spring-doc.cadn.net.cn

secret/myApp,dev
secret/myApp
secret/application,dev
secret/application

写入secret/application可用于使用 Config Server 的所有应用程序。 名称为myApp将写入任何属性secret/myAppsecret/application可用。 什么时候myApp具有devprofile 启用,则写入上述所有路径的属性将可供其使用,列表中第一个路径中的属性优先于其他路径。spring-doc.cadn.net.cn

启用按标签搜索

默认情况下,Vault 后端在搜索 secret 时不使用标签。您可以通过以下方式更改此设置 设置enableLabelfeature 标志设置为true以及(可选)设置defaultLabel. 什么时候defaultLabel未提供main将被使用。spring-doc.cadn.net.cn

什么时候enableLabelfeature 标志为 on,则 Vault 中的 secret 的路径中应始终包含所有三个段(应用程序名称、配置文件和标签)。 因此,上一节中启用了功能标志的示例将如下所示:spring-doc.cadn.net.cn

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.

解密 Property Sources 中的 Vault Secret

Spring Cloud Config Server 支持通过使用特殊的占位符前缀来解密 Vault 中的属性{vault}.此功能允许在运行时直接从 Vault 动态解析敏感的配置属性。spring-doc.cadn.net.cn

配置步骤

与 Vault 集成的所有配置设置都应放在application.ymlapplication.properties.以下是激活 Vault 配置文件、连接到 Vault 服务器以及使用{vault}前缀。spring-doc.cadn.net.cn

启用 Vault 配置文件

为您的 Spring Cloud Config Server 激活 Vault 配置文件:spring-doc.cadn.net.cn

spring:
  profiles:
    active: vault

Vault 配置

使用必要的身份验证详细信息设置与 Vault 服务器的连接:spring-doc.cadn.net.cn

spring:
  cloud:
    config:
      server:
        vault:
          host: vault.example.com
          port: 8200
          scheme: https
          backend: secret
          defaultKey: application
          kvVersion: 2
          authentication: TOKEN
          token: ${VAULT_TOKEN}
          skipSslValidation: true

这些设置指定 Vault 服务器地址、身份验证方法和访问 Vault 所需的令牌。spring-doc.cadn.net.cn

属性格式

使用{vault}前缀以指定用于检索密钥的 Vault 路径和密钥:spring-doc.cadn.net.cn

some:
  sensitive:
    value: '{vault}:path/to/secret#key'

此格式直接映射到 Vault 中存储密钥的位置 (path/to/secret) 和特定密钥 (key) 进行检索。spring-doc.cadn.net.cn

错误处理

如果 Config Server 在解密过程中遇到任何问题,例如路径不正确、访问问题或缺少密钥,则受影响的属性将带有前缀invalid.,其值将设置为<n/a>.此方法类似于处理前缀为{cipher},但它是专门为与 Vault 集成而定制的,在解密失败时提供明确的反馈。spring-doc.cadn.net.cn