spring-boot - 如何使用 java/spring boot 读取 Vault kv

标签 spring-boot hashicorp-vault

我想弄清楚如何使用 Hashicorp's Vault带 Spring Boot 。

最初,我尝试按照指南进行操作:

https://spring.io/guides/gs/vault-config/#scratch

但是由于 api 更改,我在 vault CLI 中使用了以下命令:

vault kv put secret/gs-vault-config example.username=demouser example.password=demopassword

它保存了两者,我可以使用以下命令检索它
vault kv get secret/gs-vault-config

然后我创建了 Application.javaMyConfiguration.java如指南中所述。起初,我在没有运行 vault 服务器的情况下运行了程序,这导致了 connection refused .
然后我启动了保管库服务器并从 CLI 输入用户名和密码。从日志中我可以看到它实际上进入了应用程序并写出了Here we goooo
@SpringBootApplication
public class Application implements CommandLineRunner {

@Autowired
private VaultTemplate vaultTemplate;

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Override
public void run(String... strings) throws Exception {

    // You usually would not print a secret to stdout
    System.out.println("Here we gooooo");
    VaultResponse response = vaultTemplate.read("secret/gs-vault-config");
    System.out.println("Value of username");
    System.out.println("-------------------------------");
    System.out.println(response.getData().get("example.username"));
    System.out.println("-------------------------------");
    System.out.println();

但我无法从 Vault 检索任何数据 - 可能是由于 V1 与 V2 的问题
2018-08-30 17:10:07.375 ERROR 21582 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1255) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1243) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    at hello.Application.main(Application.java:23) [classes!/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
    at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [gs-vault-config-0.1.0.jar:na]
    at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [gs-vault-config-0.1.0.jar:na]
Caused by: java.lang.NullPointerException: null
    at hello.Application.run(Application.java:34) [classes!/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:797) [spring-boot-2.0.3.RELEASE.jar!/:2.0.3.RELEASE]
    ... 13 common frames omitted

有谁知道是否有类似的指南 spring-boot从使用 kv 引擎输入的 vault 中检索数据的代码片段?

最佳答案

我在此页面上偶然发现了一个注释:
https://cloud.spring.io/spring-cloud-vault/multi/multi_vault.config.backends.html

我在其中说:
Spring Cloud Vault 在挂载路径和实际上下文路径之间添加数据/上下文

所以我尝试将代码更改为:

VaultResponse response = vaultTemplate.read("/secret/data/gs-vault-config");

然后它起作用了。

关于spring-boot - 如何使用 java/spring boot 读取 Vault kv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52100532/

相关文章:

javascript - Spring Boot REST - 所需的字符串参数不存在

spring-boot - 无法访问Minikube中已部署的Spring-boot RESTful API

kubernetes - 如何注入(inject)保险库并使用 hashicorp 保险库 secret ?

consul - 如何重置或 "un-initialize"保管库?

amazon-dynamodb - 是否可以使用 DynamoDB 存储后端扩展 Hashicorp Vault?

spring-boot - 在Spring Boot中使用不同的弹性指数进行测试

java - Collection 类的 Spring Boot 自定义序列化器

salt-stack - 如何使用 Hashicorp Vault PKI 后端为 minions 生成动态证书

amazon-web-services - 当我尝试使用 AWS IAM 角色连接 HashiCorp Vault 时如何修复 "Vault location [kv/my-client-service] not resolvable: Not found"?

java - 无法使用 Junit5 控制台启动器从终端运行 junit5 测试