使用Spring云库连接到Vault时出现“机密ID丢失”错误

时间:2017-08-31 09:43:54

标签: spring-cloud hashicorp-vault spring-vault spring-cloud-vault-config

我正在尝试使用基于角色的身份验证(spring boot项目)连接到spring Vault。

根据文档,我应该只能使用approle(拉模式)连接到spring Vault。但是,我在应用程序启动时遇到了secretct-id缺失异常。

http://cloud.spring.io/spring-cloud-vault/single/spring-cloud-vault.html#_approle_authentication

当我通过时,secret-id也可以连接,并且属性/值正在自动连接。

有没有办法可以使用“token + role / role-id”连接Vault,并在运行时使用提到的信息为我自动生成secret-id。

spring.cloud.vault:
    scheme: http
    host: <host url>
    port: 80
    token : <token>
    generic.application-name: vault/abc/pqr/test
    generic.backend: <some value>
    generic.default-context: vault/abc/pqr/test
    token: <security token>
    authentication: approle
    app-role:
      role-id: <role-id>

POM:

<dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-vault-starter-config</artifactId>
        <version>1.0.0.BUILD-SNAPSHOT</version>
    </dependency>

如果需要任何其他信息,请告诉我。

更新

@ mp911de,我根据你的建议尝试过,但是spring-cloud-vault正在挑选bootstrap.yml中设置的属性而不是“onApplicationEvent”中的一个设置,因此解决方案无效。我尝试通过“System.setProperty”方法设置属性,但该事件不起作用。

但是,如果我在main before run方法中设置属性,它将按预期工作。但我需要首先加载application.properties(需要从那里选择一些配置),因此不想在那里编写逻辑。

我的做法有什么不对吗?

@Component public class LoadVaultProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private RestTemplate restTemplate = new RestTemplate();

@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
    try {
        String roleId = getRoleIdForRole(event); //helper method
        String secretId = getSecretIdForRoleId(event); //helper method

        Properties properties = new Properties();

        properties.put("spring.cloud.vault.app-role.secret-id", secretId);
        properties.put("spring.cloud.vault.app-role.role-id", roleId); 

        event.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource(
                PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME, properties));          
    } catch (Exception ex) {
        throw new IllegalStateException(ex);
    }
}

1 个答案:

答案 0 :(得分:1)

Spring Vault的AppRole身份验证支持两种模式,但不支持拉模式:

  1. 您需要提供secret_id
  2. 的推送模式
  3. 仅通过secret_id在没有role_id的情况下进行身份验证。此模式要求通过在角色创建时设置secret_id而无需bind_secret_id=false创建角色
  4. Vault文档中提到的拉模式要求客户端了解从包装响应中获取的secret_id。 Spring Vault不会获取包裹secret_id,但我认为这将是一个不错的增强。

    更新:在应用程序启动之前设置系统属性:

    @SpringBootApplication
    public class MyApplication {
    
        public static void main(String[] args) {
    
            System.setProperty("spring.cloud.vault.app-role.role-id", "…");
            System.setProperty("spring.cloud.vault.app-role.secret-id", "…");
    
            SpringApplication.run(MyApplication.class, args);
    }
    

    参考文献: