配置数据API

Spring Boot 自 2.4 版本起提供了 ConfigData API,允许声明配置源并将其作为属性源导入。

Spring Cloud Vault 自 3.0 版本起使用 ConfigData API 将 Vault 的秘密后端挂载为属性源。在之前的版本中,使用的是 Bootstrap 上下文。ConfigData API 更加灵活,因为它允许指定要导入哪些配置系统以及导入的顺序。

您可以通过设置配置属性 spring.cloud.bootstrap.enabled=true 或包含依赖 org.springframework.cloud:spring-cloud-starter-bootstrap 来启用引导上下文。使用引导上下文应该很少需要,因此我们建议使用 Config Data API 以便在属性源排序方面获得更大的灵活性。

ConfigData 位置

您可以通过一个或多个从 Vault 具象化的 PropertySource 来挂载 Vault 配置。Spring Cloud Vault 支持两种配置位置:

  • vault://(默认位置)

  • vault:///<context-path>(上下文位置)

使用默认位置会为所有启用的秘密后端挂载属性源。如果没有进一步配置,Spring Cloud Vault 会将键值后端挂载到 /secret/${spring.application.name}。每个激活的配置文件都会添加另一个上下文路径,形式为 /secret/$\{spring.application.name}/${profile}。在类路径中添加更多模块,例如 spring-cloud-config-databases,可以提供额外的秘密后端配置选项,如果启用,这些选项将作为属性源挂载。

如果您想控制从 Vault 挂载哪些上下文路径作为 PropertySource,您可以使用上下文位置 (vault:///my/context/path) 或配置一个VaultConfigurer

上下文位置是单独指定和挂载的。Spring Cloud Vault 将每个位置挂载为唯一的 PropertySource。您可以将默认位置与上下文位置(或其他配置系统)混合使用,以控制属性源的顺序。这种方法特别有用,如果您想禁用默认的键值路径计算并自行挂载每个键值后端。

application.yml
spring.config.import: vault://first/context/path, vault://other/path, vault://

Spring Environment 中的属性名称必须是唯一的,以避免覆盖。如果您在不同的上下文路径中使用相同的秘密名称,并且希望将它们作为单独的属性公开,您可以通过向位置添加 prefix 查询参数来区分它们。

示例 1. application.yml
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 客户端快速失败 允许在找不到 Vault 上下文路径(因为 HTTP 状态 404)时启动失败。

基础设施定制

Spring Cloud Vault 需要基础设施类才能与 Vault 交互。当不使用 ConfigData API(意味着您没有指定 spring.config.import=vault:// 或上下文 Vault 路径)时,Spring Cloud Vault 通过 VaultAutoConfigurationVaultReactiveAutoConfiguration 定义其 Bean。Spring Boot 在 Spring 上下文可用之前引导应用程序。因此 VaultConfigDataLoader 自身注册 Bean,以便稍后将这些 Bean 传播到应用程序上下文中。

您可以通过使用 Bootstrapper API 注册自定义实例来定制 Spring Cloud Vault 使用的基础设施

自定义 ClientHttpRequestFactory
ClientOptions 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));
自定义 RestTemplateBuilder
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 的源以获取定制钩子。

© . This site is unofficial and not affiliated with VMware.