首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使版本化KV存储与VaultPropertySource协同工作

如何使版本化KV存储与VaultPropertySource协同工作
EN

Stack Overflow用户
提问于 2019-06-06 23:44:48
回答 2查看 1.1K关注 0票数 2

我正在尝试使版本化的KV存储库与VaultPropertySource一起工作,以便可以使用@Value访问属性。然而,它并没有像预期的那样工作。我使用的是2.1.2版本的spring-vault-core。其目的是让它与spring vault和Spring MVC一起工作。

我已经尝试了@import(EnvironmentVaultConfiguration.class),但没有效果。

代码语言:javascript
复制
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,那么它就可以工作,并返回带有data.metadata.前缀的secrets。这意味着它正在使用通用方法来获取秘密,而不是kv one。

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

有没有人可以帮我在这段代码中使用spring-vault的版本API?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-06-07 17:32:09

使用VaultPropertySource的键值2支持尚未发布。它将与Spring Vault 2.2一起发布(参见this GitHub issue)。

在此之前,您可以使用快照构建来验证代码对您的用例是否有帮助。

票数 0
EN

Stack Overflow用户

发布于 2019-06-14 23:24:54

基于马克上面的回应,我决定将VaultPropertySource与PropertyTransformer结合使用,直到我们获得开箱即用的KV version2支持。

代码语言:javascript
复制
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;
    }
}

希望它能帮助那些正在寻找类似解决方案的人。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56481031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档