此版本仍在开发中,尚不被认为是稳定的。对于最新的稳定版本,请使用 Spring Vault 3.2.0! |
属性来源
Vault 可以以多种不同的方式使用。一个特定的用例是使用 Vault 以存储加密属性。Spring Vault 支持 Vault 作为属性 source 使用 Spring 的 PropertySource 抽象获取配置属性。
您可以在其他属性源中引用存储在 Vault 中的属性,也可以将值注入与@Value(…) .引导需要存储在 Vault 内部的数据的 Bean 时需要特别注意。一个VaultPropertySource 必须在该时初始化才能从 Vault 检索属性。 |
Spring Boot/Spring Cloud 用户可以从 Spring Cloud Vault 的 在应用程序启动期间初始化各种属性源的配置集成。 |
Vault 确定通过 Vault 的sys/internal/ui/mounts/… 端点。请确保您的政策允许访问该路径,否则您将无法使用保险柜媒体资源来源。 |
注册VaultPropertySource
Spring Vault 提供了一个VaultPropertySource
与 Vault 一起使用以获取
性能。它使用嵌套的data
元素来公开存储的属性和
在 Vault 中加密。
ConfigurableApplicationContext ctx = new GenericApplicationContext();
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.addFirst(new VaultPropertySource(vaultTemplate, "secret/my-application"));
在上面的代码中,VaultPropertySource
已以最高优先级添加
在搜索中。如果它包含“foo”属性,则将检测并返回它
领先于任何foo
属性PropertySource
.MutablePropertySources
公开了许多允许精确
对属性源集的作。
@VaultPropertySource
这@VaultPropertySource
注释提供了方便的声明性
添加PropertySource
到 Spring 的Environment
与@Configuration
类。
@VaultPropertySource
采用 Vault 路径,例如secret/my-application
并将存储在节点上的数据公开在PropertySource
.@VaultPropertySource
支持与租约关联的机密的租约续订
(即来自mysql
backend)和终端上的凭证轮换
租约到期。默认情况下,租约续订处于禁用状态。
{
// …
"data": {
"database": {
"password": ...
},
"user.name": ...,
}
// …
}
@VaultPropertySource
@Configuration
@VaultPropertySource("secret/my-application")
public class AppConfig {
@Autowired Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setUser(env.getProperty("user.name"));
testBean.setPassword(env.getProperty("database.password"));
return testBean;
}
}
@VaultPropertySource
使用凭据轮换和前缀@Configuration
@VaultPropertySource(value = "aws/creds/s3-access",
propertyNamePrefix = "aws.",
renewal = Renewal.ROTATE)
public class AppConfig {
// provides aws.access_key and aws.secret_key properties
}
从中获得的秘密generic 秘密后端与 TTL (refresh_interval ),但不是租约 Id。Spring Vault 的PropertySource 在达到其 TTL 时轮换通用密钥。 |
您可以使用@VaultPropertySource 从版本控制的键值后端获取最新的机密版本。确保不包含data/ 路径中的段。 |
任何${…}
占位符@VaultPropertySource
path 是针对已针对环境注册的属性源集进行解析的,如以下示例所示:
@VaultPropertySource
使用占位符的路径@Configuration
@VaultPropertySource(value = "aws/creds/${my.placeholder:fallback/value}",
propertyNamePrefix = "aws.",
renewal = Renewal.ROTATE)
public class AppConfig {
}
假设my.placeholder
存在于已注册的属性源之一(例如,系统属性或环境变量)中,则占位符将解析为相应的值。
如果没有,那么fallback/value
用作默认值。
如果未指定默认值并且无法解析属性,则IllegalArgumentException
被抛出。
在某些情况下,严格控制可能是不可能的,也不切实际的
使用时的属性源排序@VaultPropertySource
附注。
例如,如果@Configuration
上述课程是通过
组件扫描,顺序难以预测。
在这种情况下,如果覆盖很重要,建议将
用户回退到使用编程 PropertySource API。
看ConfigurableEnvironment
和MutablePropertySources
了解详情。