如何使用 java/spring boot 读取 Vault kv [英] How to read Vault kv with java/spring boot

查看:22
本文介绍了如何使用 java/spring boot 读取 Vault kv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试弄清楚如何将 Hashicorp's Vault 与 spring boot 一起使用.

I'm trying to figure out how to use Hashicorp's Vault with spring boot.

最初,我尝试遵循指南:

Initially, I have tried to follow the guide:

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

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

But due to api changes I used following command in the vault CLI:

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

它保存了两者,我可以使用以下命令检索它

which saved both and I'm able to retrieve it with the following command

vault kv get secret/gs-vault-config

然后我按照指南中的描述创建了 Application.javaMyConfiguration.java.起初,我在没有运行 Vault 服务器的情况下运行程序,这导致了 连接被拒绝.然后我启动了保管库服务器并从 CLI 输入用户名和密码.从日志中我可以看到它实际上进入了应用程序并写出了Here we goooo

Then I created the Application.java and MyConfiguration.java as described in the guide. At first, I ran the program without having the vault server running which resulted in a connection refused. Then I started the vault server and entered the username and password from the CLI. From the log I can see it actually enters the Application and writes out 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 的问题

But im unable to retrieve any data from Vault - probably due to the V1 vs V2 issues

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 中检索数据?

Does anyone know if there is a similar guide to a spring-boot code snippet where data is retrieved from vault which has been entered with the kv engine?

推荐答案

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

I stumbled a single note in this page : https://cloud.spring.io/spring-cloud-vault/multi/multi_vault.config.backends.html

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

In which i says : Spring Cloud Vault adds the data/ context between the mount path and the actual context path

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

So i tried to change the code to :

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

然后它起作用了.

这篇关于如何使用 java/spring boot 读取 Vault kv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆