From 97421f0bf893d1675ac22da5793c2584b73d287d Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 30 Mar 2022 12:14:05 -0400 Subject: [PATCH] Use path to key property in SpringVaultEnvironmentRepository. Fixes #2072 (#2073) --- .../SpringVaultEnvironmentRepository.java | 8 ++++- ...SpringVaultEnvironmentRepositoryTests.java | 29 ++++++++++++++++--- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepository.java index 6336a3ce..3e220fda 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepository.java @@ -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 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()); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepositoryTests.java index df0c3627..0a354cf3 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/vault/SpringVaultEnvironmentRepositoryTests.java @@ -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");