|
对于最新稳定版本,请使用 Spring Cloud Vault 5.0.1! |
配置数据 API
Spring Boot 自 2.4 版本起提供了一套 ConfigData API,允许声明配置源,并将这些源作为属性源导入。
Spring Cloud Vault 使用自 3.0 版本起采用 ConfigData API 将 Vault 的 secret 后端作为 property sources 挂载。 在先前版本中,使用的是 Bootstrap context。 ConfigData API 要求更为灵活,允许指定要导入的配置系统及其顺序。
您可以通过设置配置属性spring.cloud.bootstrap.enabled=true或包括依赖项org.springframework.cloud:spring-cloud-starter-bootstrap来启用bootstrap上下文。使用boostrap上下文应非常罕见,因此我们推荐使用Config Data API,以获得更灵活的属性源顺序控制。 |
配置数据位置
你可以通过一个或多个 PropertySource 将 Vault 配置挂载上,这些 PropertySource 来自于 materialized 从 Vault。
Spring Cloud Vault 支持两种配置位置:
-
vault://(默认位置) -
vault:///<context-path>(上下文位置)
使用默认的location mounts属性源为所有启用的Secret后端挂载。
在不进行进一步配置的情况下,Spring Cloud Vault会在/secret/${spring.application.name}处挂载键值后端。
每个激活的profile都会添加一个上下文路径,形式为/secret/$\{spring.application.name}/${profile}。
将更多模块添加到类路径中,如spring-cloud-config-databases,会提供额外的secret后端配置选项,如果启用则会作为属性源挂载。
如果你希望控制Vault挂载为PropertySource的上下文路径,你可以使用上下文位置(vault:///my/context/path)或配置一个">VaultConfigurer。
上下文位置可以单独指定和挂载。
Spring Cloud Vault 会将每个位置挂载为一个唯一的 PropertySource。
你可以混合使用默认位置与上下文位置(或其他配置系统)来控制属性源的顺序。
这种方法特别适用于你想要禁用默认的键值路径计算,并手动挂载每个键值后端的情况。
spring.config.import: vault://first/context/path, vault://other/path, vault://
在Spring中,Environment内的属性名必须唯一,以避免遮蔽。
如果你在不同的上下文路径使用相同的秘密名称,并且希望将它们作为独立的属性暴露,可以通过在位置中添加prefix查询参数来区分。
spring.config.import: vault://my/path?prefix=foo., vault://my/other/path?prefix=bar.
secret: ${foo.secret}
other.secret: ${bar.secret}
| 前缀会被直接添加到由Vault返回的所有属性名称前。如果你希望在前缀和键名称之间使用点号分隔,请在前缀后添加一个尾随点号。 |
有条件启用/禁用 Vault 配置
在某些情况下,可能需要在不使用Vault的情况下启动应用程序。可以通过位置字符串表达是否应将Vault配置位置设置为可选或强制(默认):
-
optional:vault://(默认位置) -
optional:vault:///<context-path>(上下文位置)
在应用程序启动期间,如果通过 spring.cloud.vault.enabled=false 禁用了 Vault 支持,则会跳过可选的位置。
| Vault 无法找到的上下文路径(HTTP 状态 404)会被忽略,无论配置位置是否标记为可选。 Vault 客户端立即失败 允许在启动时因上下文路径无法找到(HTTP 状态 404)而立即失败。 |
基础设施自定义
Spring Cloud Vault 需要基础设施类来与 Vault 交互。当未使用 ConfigData API(即未指定 spring.config.import=vault:// 或上下文 Vault 路径)时,Spring Cloud Vault 通过 VaultAutoConfiguration 和 VaultReactiveAutoConfiguration 定义其 beans。
Spring Boot 在 Spring 上下文可用之前会预启动应用。因此 VaultConfigDataLoader 会自己注册 beans,以便将它们后续传播到应用上下文中。
您可以通过使用 Bootstrapper API 注册自定义实例来定制 Spring Cloud Vault 所使用的基础设施:
ClientHttpRequestFactoryClientOptions options = new ClientOptions();
SslConfiguration sslConfiguration = SslConfiguration.unconfigured();
HttpClientBuilder builder = HttpComponents.getHttpClientBuilder(options, sslConfiguration);
InstanceSupplier<ClientFactoryWrapper> supplier = context ->
new ClientFactoryWrapper(new HttpComponentsClientHttpRequestFactory(builder.build()));
SpringApplication application = new SpringApplication(MyApplication.class);
application.addBootstrapRegistryInitializer(registry -> registry.register(ClientFactoryWrapper.class, supplier));
RestTemplateBuilderInstanceSupplier<RestTemplateBuilder> supplier = context -> {
return RestTemplateBuilder
.builder()
.requestFactory(context.get(ClientFactoryWrapper.class).getClientHttpRequestFactory())
.defaultHeader("X-Vault-Namespace", "my-namespace");
};
SpringApplication application = new SpringApplication(MyApplication.class);
application.addBootstrapRegistryInitializer(registry -> registry.register(RestTemplateBuilder.class, supplier));
见 also 自定义要作为 PropertySource 暴露的 secret 后端 以及 VaultConfigDataLoader 的来源用于自定义钩子。