${VAULT_SCHEME} 在 bootstrap.properties 中不工作

${VAULT_SCHEME} not working in bootstrap.properties

我已将 spring 引导应用程序配置为从我的环境中获取属性,但奇怪的是我在启动我的应用程序时遇到错误。 我已经在我的 ~/.bash_profile 中添加了这些属性,并且在将它们添加到配置文件之后也做了 source ~/.bash_profile

这就是我的 bootstrap.properties 的样子:

spring.application.name=gamification
spring.cloud.vault.enabled=${VAULT_ENABLE:true}
spring.cloud.vault.fail-fast=false
spring.cloud.vault.token=${VAULT_TOKEN}
spring.cloud.vault.scheme=${VAULT_SCHEME}
spring.cloud.vault.host=${VAULT_HOST}
spring.cloud.vault.port=${VAULT_PORT:8200}

我收到此错误:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cloud.vault.config.VaultReactiveBootstrapConfiguration]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Scheme must be http or https
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:216) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 30 common frames omitted
Caused by: java.lang.IllegalArgumentException: Scheme must be http or https
    at org.springframework.util.Assert.isTrue(Assert.java:118) ~[spring-core-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    at org.springframework.vault.client.VaultEndpoint.setScheme(VaultEndpoint.java:167) ~[spring-vault-core-2.2.0.RELEASE.jar:2.2.0.RELEASE]
    at org.springframework.cloud.vault.config.VaultConfigurationUtil.createVaultEndpoint(VaultConfigurationUtil.java:91) ~[spring-cloud-vault-config-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at org.springframework.cloud.vault.config.VaultReactiveBootstrapConfiguration.<init>(VaultReactiveBootstrapConfiguration.java:110) ~[spring-cloud-vault-config-2.2.2.RELEASE.jar:2.2.2.RELEASE]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_231]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_231]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_231]
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:203) ~[spring-beans-5.2.4.RELEASE.jar:5.2.4.RELEASE]
    ... 32 common frames omitted

我在 Vault Endpoint 中添加了一个调试点并发现了这个:

如您所见,VAULT_HOST 被视为 VAULT_HOST 而不是该环境变量的值,与 VAULT_SCHEME

相同

[编辑] 添加 bash_profile 保险库配置:

export VAULT_ENABLE=true
export VAULT_SCHEME=http
export VAULT_HOST=vault-1.dev.lokal
export VAULT_PORT=8200
export VAULT_TOKEN=5F97X

[编辑 #2]

尝试了@Gopinath 建议的解决方案 尝试自动装配时,我将环境设置为 null

问题的根本原因可以从这条错误消息中找到:

org.springframework.core.convert.ConverterNotFoundException: 

No converter found capable of converting 
from type [java.lang.String] 
to type [org.springframework.cloud.vault.config.VaultProperties$Config]

以上消息表明无法使用提供的字符串参数初始化 VaultProperties 对象。

这里是 link 配置 VaultProperties 的文档和说明:

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

一些有助于理解保险库的更多信息:

参考文献:

Spring 云保险库:https://cloud.spring.io/spring-cloud-vault/ Hashicorp 保险库:https://www.vaultproject.io

什么是保管库?

保管库是一种安全存储方式space,用于存储秘密信息。 Hashicorp Vault 是一种为云应用程序提供保险库功能的工具。

什么是 Spring 启动库?

Spring 引导应用程序通常需要机密信息才能正常工作。 秘密信息的一些例子是:

  1. 数据库密码
  2. 私钥
  3. API键

通常,输入参数通过 Spring 引导应用程序传递 "application.properties" 文件或 "bootstrap.properties" 文件。 如果在文件中直接提及秘密数据,使用此类属性文件会带来安全风险。

Spring Boot Vault 解决了这个风险。 它在启动时从保管库中提取秘密信息并提供给应用程序。

.properties 文件只会告诉应用程序它可以从 Vault 获得的参数名称。 参数的实际值将从保险库中获取。

如何设置保险柜?

步骤 1: 安装并启动 HashiCorp Vault https://www.vaultproject.io/downloads.html:

第 2 步: 安装 Vault 后,测试它是否有效,方法是启动 它在控制台中 window.

> vault server --dev --dev-root-token-id="spring-boot-vault-demo"

==> Vault server configuration:
    Api Address: http://127.0.0.1:8200
                         Cgo: disabled
             Cluster Address: https://127.0.0.1:8201
                  Listener 1: tcp (addr: "127.0.0.1:8200", cluster address: "127.0.0.1:8201", max_request_duration: "1m30s", max_request_size: "33554432", tls: "disabled")
                   Log Level: info
                       Mlock: supported: false, enabled: false
               Recovery Mode: false
                     Storage: inmem
                     Version: Vault v1.4.1

    WARNING! dev mode is enabled! 
    .....

    You may need to set the following environment variable:

    PowerShell:
        $env:VAULT_ADDR="http://127.0.0.1:8200"
    cmd.exe:
        set VAULT_ADDR=http://127.0.0.1:8200

    The unseal key and root token are displayed below in case you want to
    seal/unseal the Vault or re-authenticate.

    Unseal Key: +Dihvgj/oRN2zo6/97ZqpWt086/CFRZEPkuauDu4uQo=
    Root Token: spring-boot-vault-demo

第 3 步: 在保险库中存储一些秘密数据, 通过 运行 这些命令在单独的命令中 window:

> set VAULT_ADDR=http://127.0.0.1:8200

> set VAULT_TOKEN=spring-boot-vault-demo

> vault kv put secret/spring-boot-vault-demo password=££££$$$$%%%%
    Key              Value
    ---              -----
    created_time     2020-05-02T09:59:41.2233332Z
    deletion_time    n/a
    destroyed        false
    version          1

我这样做了: 我制作了一个名为 setenv.sh 的 shell 脚本并将其放在下面:

#!/bin/bash

launchctl setenv VAULT_ENABLE true
launchctl setenv VAULT_SCHEME http
launchctl setenv VAULT_HOST vault-1.dev.lokal
launchctl setenv VAULT_PORT 8200
launchctl setenv VAULT_TOKEN 5F97X

然后,在启动应用程序之前,我 运行 shell 脚本

sudo sh setenv.sh

应用程序似乎运行良好,没有任何错误。 St运行gely 如果我用我以前在 .bash_profile 中添加环境变量的方法来做到这一点,它是行不通的。