This commit is contained in:
@@ -25,6 +25,7 @@ import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.config.server.environment.AbstractVaultEnvironmentRepository;
|
||||
import org.springframework.cloud.config.server.environment.EnvironmentWatch;
|
||||
import org.springframework.cloud.config.server.environment.VaultEnvironmentProperties;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.vault.core.VaultKeyValueOperations;
|
||||
import org.springframework.vault.support.VaultResponse;
|
||||
@@ -39,15 +40,20 @@ public class SpringVaultEnvironmentRepository extends AbstractVaultEnvironmentRe
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
private String path = "";
|
||||
|
||||
public SpringVaultEnvironmentRepository(ObjectProvider<HttpServletRequest> request, EnvironmentWatch watch,
|
||||
VaultEnvironmentProperties properties, VaultKeyValueOperations keyValueTemplate) {
|
||||
super(request, watch, properties);
|
||||
this.keyValueTemplate = keyValueTemplate;
|
||||
if (properties.getKvVersion() == 2 && StringUtils.hasText(properties.getPathToKey())) {
|
||||
path = "data/" + properties.getPathToKey() + "/";
|
||||
}
|
||||
this.objectMapper = new ObjectMapper();
|
||||
}
|
||||
|
||||
protected String read(String key) {
|
||||
VaultResponse response = this.keyValueTemplate.get(key);
|
||||
VaultResponse response = this.keyValueTemplate.get(this.path + key);
|
||||
if (response != null) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(response.getData());
|
||||
|
||||
@@ -27,6 +27,7 @@ import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.cloud.config.environment.Environment;
|
||||
import org.springframework.cloud.config.server.environment.EnvironmentWatch;
|
||||
import org.springframework.cloud.config.server.environment.VaultEnvironmentProperties;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.vault.core.VaultKeyValueOperations;
|
||||
import org.springframework.vault.support.VaultResponse;
|
||||
|
||||
@@ -48,12 +49,32 @@ public class SpringVaultEnvironmentRepositoryTests {
|
||||
|
||||
@Test
|
||||
public void testFindOneNoDefaultKey() {
|
||||
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
|
||||
when(keyValueTemplate.get("myapp")).thenReturn(withVaultResponse("foo", "bar"));
|
||||
when(keyValueTemplate.get("application")).thenReturn(withVaultResponse("def-foo", "def-bar"));
|
||||
defaultKeyTest("", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathKey() {
|
||||
defaultKeyTest("mypath", 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPathKeyNotUsedWithVersionOne() {
|
||||
defaultKeyTest("mypath", 1);
|
||||
}
|
||||
|
||||
private void defaultKeyTest(String myPathKey, int version) {
|
||||
String path = "";
|
||||
if (StringUtils.hasText(myPathKey) && version == 2) {
|
||||
path = "data/" + myPathKey + "/";
|
||||
}
|
||||
VaultKeyValueOperations keyValueTemplate = mock(VaultKeyValueOperations.class);
|
||||
when(keyValueTemplate.get(path + "myapp")).thenReturn(withVaultResponse("foo", "bar"));
|
||||
when(keyValueTemplate.get(path + "application")).thenReturn(withVaultResponse("def-foo", "def-bar"));
|
||||
VaultEnvironmentProperties properties = new VaultEnvironmentProperties();
|
||||
properties.setPathToKey(myPathKey);
|
||||
properties.setKvVersion(version);
|
||||
SpringVaultEnvironmentRepository repo = new SpringVaultEnvironmentRepository(mockHttpRequest(),
|
||||
new EnvironmentWatch.Default(), new VaultEnvironmentProperties(), keyValueTemplate);
|
||||
new EnvironmentWatch.Default(), properties, keyValueTemplate);
|
||||
|
||||
Environment e = repo.findOne("myapp", null, null);
|
||||
assertThat(e.getName()).as("Name should be the same as the application argument").isEqualTo("myapp");
|
||||
|
||||
Reference in New Issue
Block a user