使用 Spring Cloud Starter Vault Config 访问 Docker Vault 机密无法解决

Accessing Docker Vault secrets using Spring Cloud Starter Vault Config Could Not Resolve

我是 运行 Docker Vault container 处于 dev 模式,我无法读取位于 /secret/mobsters/ 的一个名为 password 的秘密。

这里是Spring logs

运行 vault kv get secret/mobsters returns 密码键值对。我还可以在本地访问保险库服务器。

以下是我引用秘密的方式:

@Value("${password}")
String password;

@PostConstruct
private void postConstruct() {
    System.out.println("My password is: " + password);
}

Spring Cloud Vault 配置是使用 bootstrap.yml 文件设置的:

spring.application.name: mobsters
spring.cloud.vault:
host: localhost
port: 8200
scheme: http
authentication: TOKEN
token: ...

我收到消息异常(完全异常 here):

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'password' in value "${password}"`

来自保险库 UI:

将 Spring Vault/Spring Cloud Vault 与 HashiCorp Vault 0.10.0 一起使用不起作用,因为 key/value 后端安装时默认启用版本控制。这具有一定的意义,因为版本化的 API 已经完全改变并破坏了现有的客户端实现。上下文路径和响应结构不同。

您有两个选择:

  1. 使用较旧的 Vault 版本(例如 0.9.5)
  2. 尝试应对 API 变化,直到 Spring Cloud Vault 找到使用新 API 的方法。你需要:
    • 在您的 bootstrap 配置中设置 spring.cloud.vault.generic.backend=secret/data
    • 在 属性 名称前加上 data.,这样 @Value("${hello.world}") 就变成了 @Value("${data.hello.world}")

看来有办法解决这个问题。

在您的 bootstrap.yml 中,确保 generic.enabled 为假且 kv.enabled 为真。

spring:
  ...
  cloud.vault:
      ...
      kv.enabled: true
      generic.enabled: false

根据this answer on GitHub

The main difference between those two is that kv injects the data segment in the context path and unwraps nested data responses.

If you're running a [springboot] version before 2.0, then you need to implement an org.springframework.cloud.vault.config.VaultConfigurer bean that is exposed to the bootstrap context. SecretBackendConfigurer accepts a path and a PropertyTransformer that transforms properties before exposing these as PropertySource.