|
该版本仍在开发中,尚未被视为稳定。最新稳定版本请使用Spring Cloud Vault 5.0.0! |
ConfigData API
自2.4版本起,Spring Boot 提供了 ConfigData API,允许声明配置源并将其导入为属性源。
从 3.0 版本起,Spring Cloud Vault 使用 ConfigData API 将 Vault 的秘密后端挂载为属性源。 在之前的版本中,采用了 Bootstrap 上下文。 ConfigData API 更加灵活,因为它允许指定导入哪些配置系统及其导入顺序。
你可以通过设置配置属性来启用引导上下文spring.cloud.bootstrap.enabled=true或者通过包含依赖关系org.springframework.cloud:spring-cloud-starter-bootstrap.
使用boostrap上下文的使用通常很少需要,因此我们建议使用配置数据API,以获得属性源排序的更多灵活性。 |
ConfigData Locations
你可以通过一个或多个挂载Vault配置地产来源这些人是从避难所实体化出来的。
Spring Cloud Vault 支持两个配置位置:
-
库://(默认位置) -
vault:///<上下文路径>(上下文位置)
使用默认位置可以挂载所有启用的秘密后端的属性源。
无需进一步配置,Spring Cloud Vault 会挂载键值后端于/secret/${spring.application.name}.
每个激活的配置文件都会添加另一条上下文路径,遵循以下形式/secret/$\{spring.application.name}/${profile}.
向类路径添加更多模块,例如Spring-cloud-config-databases,提供了额外的秘密后端配置选项,如果启用这些选项,这些选项会挂载为属性源。
如果你想控制从Vault挂载哪些上下文路径,可以像地产来源,你可以选择使用上下文位置(vault:///my/context/path)或配置VaultConfigurer.
上下文位置被指定并单独安装。
春云宝库将每个地点作为唯一安装地产来源.
你可以将默认位置与上下文位置(或其他配置系统)混合使用,以控制属性源的顺序。
这种方法尤其适用于禁用默认键值路径计算,自己挂载每个键值后端时。
spring.config.import: vault://first/context/path, vault://other/path, vault://
泉水内的房产名称环境必须是独一无二的,才能避免被跟随。
如果你在不同的上下文路径中使用相同的秘密名称,并且想将它们作为单独的属性公开,可以通过添加前缀查询位置的参数。
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(默认位置) -
可选:vault:///<context-path>(上下文位置)
如果通过以下方式禁用了 Vault 支持,应用启动时会跳过可选位置Spring.cloud.vault.enabled=false.
| 找不到的保险库上下文路径(HTTP 状态 404)无论配置位置是否标记为可选,都会被跳过。Vault 客户端 Fail Fast 允许在因 HTTP 状态 404 找不到 Vault 上下文路径时在启动时失败。 |
基础设施定制化
Spring Cloud Vault 需要基础设施类来与 Vault 交互。当不使用 ConfigData API 时(也就是说你没有指定spring.config.import=vault://或上下文 Vault 路径),Spring Cloud Vault 通过以下方式定义其 beansVaultAutoConfiguration和VaultReactiveAutoConfiguration.
Spring Boot 在 Spring 上下文可用之前启动应用程序。因此VaultConfigDataLoader注册 Beans 自身,以便后续将这些数据传播到应用上下文中。
你可以通过注册自定义实例来自定义 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));
Rest模板构建器InstanceSupplier<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));
另见自定义哪些秘密后端要暴露为PropertySource,以及 的来源VaultConfigDataLoader用于定制钩子。