首页 > 解决方案 > 如何使版本化的 KV 存储与 VaultPropertySource 一起使用

问题描述

我正在尝试使 Vault 的版本化 KV 存储与 VaultPropertySource 一起使用,以便可以使用 @Value 访问属性。但是它没有按预期工作。我正在使用 spring-vault-core 的 2.1.2.RELEASE 版本。目的是让它与 spring vault 和 Spring MVC 一起工作。

我已经尝试过 @import(EnvironmentVaultConfiguration.class) 无济于事。

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.authentication.TokenAuthentication;
import org.springframework.vault.client.VaultEndpoint;
import org.springframework.vault.config.AbstractVaultConfiguration;
import org.springframework.vault.core.VaultTemplate;
import org.springframework.vault.core.env.VaultPropertySource;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.List;

@Configuration
@PropertySource("vault.properties")
public class AppConfig extends AbstractVaultConfiguration {

    @Value("${vault.uri}")
    private URI vaultUri;

    @Value("${vault.token}")
    private String token;

    @Value("#{'${vault.sources:}'.split(',')}")
    private List<String> vaultSources;

    @Autowired
    private ConfigurableEnvironment environment;

    @Autowired
    private VaultTemplate vaultTemplate;

    /**
     * Specify an endpoint for connecting to Vault.
     */
    @Override
    public VaultEndpoint vaultEndpoint() {
        return VaultEndpoint.from(vaultUri);
    }

    /**
     * Configure a client authentication.
     * Please consider a more secure authentication method
     * for production use.
     */
    @Override
    public ClientAuthentication clientAuthentication() {
        return new TokenAuthentication(token);
    }

    @PostConstruct
    public void setPropertySource() {
        MutablePropertySources sources = environment.getPropertySources();
        vaultSources.stream().forEach(vs -> {
            sources.addFirst(new VaultPropertySource(vaultTemplate, vs));
        });
    }
}

在给定的代码中,如果我提供, 那么它可以工作并返回带有和前缀的vault.sources=secret/data/abcd,secret/data/pqrs 秘密。这意味着它使用通用方法来获取秘密,而不是 kv 一。data.metadata.

如果我从路径 ie 中删除数据vault.sources=secret/abcd,secret/pqrs,它根本不会连接并抛出 403 异常。这意味着它不能使用 kv v2。

有人可以帮助我如何在这段代码中使用 spring-vault 的版本化 API 吗?

标签: spring-vault

解决方案


基于上述 Mark 的回复,我决定将 VaultPropertySource 与 PropertyTransformer 一起使用,直到我们获得开箱即用的 KV 版本 2 支持。

public class DataMetadataPrefixRemoverPropertyTransformer implements PropertyTransformer {

    private final String dataPrefix = "data.";
    private final String metadataPrefix = "metadata.";

    public Map<String, Object> transformProperties(Map<String, ? extends Object> inputProperties) {
        Map<String, Object> target = new LinkedHashMap(inputProperties.size(), 1.0F);
        Iterator propertiesIterator = inputProperties.entrySet().iterator();

        while(propertiesIterator.hasNext()) {

            Map.Entry<String, ? extends Object> entry = (Map.Entry)propertiesIterator.next();
            String key = entry.getKey();

            // do not add metadata properties to environment for now - do not see a use case for it as of now.
            if (StringUtils.startsWithIgnoreCase(key, metadataPrefix)) {
                continue;
            }

            if (StringUtils.startsWithIgnoreCase(key, dataPrefix)) {
                key = StringUtils.replace(key, dataPrefix, "");
            }

            target.put(key, entry.getValue());
        }
        return target;
    }
}

希望它可以帮助寻找类似解决方案的人。


推荐阅读