spring-vault - 如何使版本化的 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 吗?
解决方案
基于上述 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;
}
}
希望它可以帮助寻找类似解决方案的人。
推荐阅读
- nlog - 归档文件大小超过指定大小,archiveAboveSize 与 NLog
- sql - 仅当值存在时打印引号
- node.js - Firebase Promise 向用户返回 null,但在云功能测试中返回正常数据
- java - Integrating Cloudinary with Adobe AEM
- excel - 如何使用 VBA 突出显示散点图中的最后一个数据点
- r - R:使用 for 循环创建一个新的数据表,其中包含给定多列组合的最小和最大变量
- python - Python - 加快嵌套字典之间的迭代
- angular - onPush 更改检测的问题
- reactjs - how to programmatically clear TextInput using useRef
- javascript - 拖动屏幕滚动