该版本仍在开发中,尚未被视为稳定版本。如需获取最新的稳定版本,请使用Spring Vault 4.0.1spring-doc.cadn.net.cn

属性源

Vault 可以通过多种方式使用。一个特定的用例是使用 Vault 来存储加密属性。Spring Vault 支持将 Vault 作为属性源,通过 Spring 的 PropertySource 抽象 获取配置属性。spring-doc.cadn.net.cn

您可以引用存储在 Vault 中的属性到其他属性源,或者使用 @Value(…) 进行值注入。在引导需要 Vault 内部数据的 Bean 时需要特别注意。此时必须初始化一个 VaultPropertySource 以从 Vault 中检索属性。
Spring Boot/Spring Cloud 用户可以受益于 Spring Cloud Vault 的配置集成,该集成在应用程序启动期间初始化各种属性源。
Vault 通过 Vault 的 sys/internal/ui/mounts/… 端点确定挂载路径。确保您的策略允许访问该路径,否则您将无法使用 Vault 属性源。

注册中VaultPropertySource

Spring Vault 提供了一个 VaultPropertySource,用于与 Vault 一起获取属性。它使用嵌套的 data 元素来暴露存储在 Vault 中并加密的属性。spring-doc.cadn.net.cn

ConfigurableApplicationContext ctx = new GenericApplicationContext();
MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
sources.addFirst(new VaultPropertySource(vaultTemplate, "secret/my-application"));

在上面的代码中,VaultPropertySource 已被添加为搜索中的最高优先级。如果它包含一个 ´foo` 属性,该属性将被检测到并优先于任何其他 foo 中的 PropertySource 属性返回。MutablePropertySources 暴露了一些方法,允许对属性源集合进行精确操作。spring-doc.cadn.net.cn

@VaultPropertySource

@VaultPropertySource 注解为向 Spring 的 Environment 添加 PropertySource 提供了一种便捷且声明式的机制,可以与 @Configuration 类结合使用。spring-doc.cadn.net.cn

@VaultPropertySource 接受一个 Vault 路径,例如 secret/my-application 并将存储在节点中的数据暴露在一个 PropertySource 中。 @VaultPropertySource 支持对与租约关联的秘密(即来自 mysql 机密引擎的凭证)进行租约续期,并在租约最终到期时进行凭证轮换。默认情况下,租约续期是禁用的。spring-doc.cadn.net.cn

示例 1. 存储在 Vault 中的属性
{
  // …

  "data": {
    "database": {
      "password": ...
    },
    "user.name": ...,
  }

  // …
}
Example 2. Declaring a @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;
    }
}
示例 3. 声明一个带有凭据轮换和前缀的 @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 secrets engines获取的密钥与TTL(refresh_interval)相关联,但没有租约Id。 Spring Vault的PropertySource在达到其TTL时会轮换通用密钥。
您可以使用 @VaultPropertySource 从版本化的 Key-Value secrets 后端获取最新的密钥版本。 请确保不要在路径中包含 data/ 段。

任何在 @VaultPropertySource 路径中存在的 ${…} 占位符都会根据已经注册到环境中的属性源集合进行解析,如下例所示:spring-doc.cadn.net.cn

示例 4. 使用占位符声明 @VaultPropertySource 路径
@Configuration
@VaultPropertySource(value = "aws/creds/${my.placeholder:fallback/value}",
                     propertyNamePrefix = "aws.",
                     renewal = Renewal.ROTATE)
public class AppConfig {
}

假设my.placeholder已经存在于某个已注册的属性源中(例如,系统属性或环境变量),占位符将解析为相应的值。 如果不存在,则使用fallback/value作为默认值。 如果没有指定默认值且无法解析属性,则会抛出IllegalArgumentExceptionspring-doc.cadn.net.cn

在某些情况下,使用 @VaultPropertySource 注解时可能无法或不切实际地严格控制属性源的顺序。 例如,如果上面的 @Configuration 类是通过组件扫描注册的,那么顺序将很难预测。 在这种情况下——如果覆盖属性很重要——建议用户回退到使用编程式的 PropertySource API。 详情请参见 ConfigurableEnvironmentMutablePropertySourcesspring-doc.cadn.net.cn