diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepository.java index 9bf420cd..e90fb316 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepository.java @@ -44,6 +44,7 @@ import static org.springframework.cloud.config.client.ConfigClientProperties.TOK * @author Spencer Gibb * @author Mark Paluch * @author Haroun Pacquee + * @author Haytham Mohamed */ @Validated public class VaultEnvironmentRepository implements EnvironmentRepository, Ordered { @@ -145,8 +146,12 @@ public class VaultEnvironmentRepository implements EnvironmentRepository, Ordere addProfiles(keys, this.defaultKey, profiles); } - keys.add(application); - addProfiles(keys, application, profiles); + // application may have comma-separated list of names + String[] applications = StringUtils.commaDelimitedListToStringArray(application); + for (String app : applications) { + keys.add(app); + addProfiles(keys, app, profiles); + } Collections.reverse(keys); return keys; diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepositoryTests.java index 64c14505..21ecd1d8 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/VaultEnvironmentRepositoryTests.java @@ -44,6 +44,7 @@ import static org.mockito.Mockito.when; * @author Ryan Baxter * @author Haroun Pacquee * @author Mark Paluch + * @author Haytham Mohamed */ public class VaultEnvironmentRepositoryTests { @@ -197,6 +198,71 @@ public class VaultEnvironmentRepositoryTests { secondResult, e.getPropertySources().get(1).getSource()); } + @Test + @SuppressWarnings("unchecked") + public void testFindOneDefaultKeySetAndDifferentToMultipleApplications() { + MockHttpServletRequest configRequest = new MockHttpServletRequest(); + configRequest.addHeader("X-CONFIG-TOKEN", "mytoken"); + RestTemplate rest = mock(RestTemplate.class); + + ResponseEntity myAppResp = mock(ResponseEntity.class); + when(myAppResp.getStatusCode()).thenReturn(HttpStatus.OK); + VaultResponse myAppVaultResp = mock(VaultResponse.class); + when(myAppVaultResp.getData()).thenReturn("{\"myapp-foo\":\"myapp-bar\"}"); + when(myAppResp.getBody()).thenReturn(myAppVaultResp); + when(rest.exchange(eq("http://127.0.0.1:8200/v1/secret/{key}"), + eq(HttpMethod.GET), any(HttpEntity.class), eq(VaultResponse.class), + eq("myapp"))).thenReturn(myAppResp); + + ResponseEntity yourAppResp = mock(ResponseEntity.class); + when(yourAppResp.getStatusCode()).thenReturn(HttpStatus.OK); + VaultResponse yourAppVaultResp = mock(VaultResponse.class); + when(yourAppVaultResp.getData()).thenReturn("{\"yourapp-foo\":\"yourapp-bar\"}"); + when(yourAppResp.getBody()).thenReturn(yourAppVaultResp); + when(rest.exchange(eq("http://127.0.0.1:8200/v1/secret/{key}"), + eq(HttpMethod.GET), any(HttpEntity.class), eq(VaultResponse.class), + eq("yourapp"))).thenReturn(yourAppResp); + + ResponseEntity myDefaultKeyResp = mock(ResponseEntity.class); + when(myDefaultKeyResp.getStatusCode()).thenReturn(HttpStatus.OK); + VaultResponse myDefaultKeyVaultResp = mock(VaultResponse.class); + when(myDefaultKeyVaultResp.getData()).thenReturn("{\"def-foo\":\"def-bar\"}"); + when(myDefaultKeyResp.getBody()).thenReturn(myDefaultKeyVaultResp); + when(rest.exchange(eq("http://127.0.0.1:8200/v1/secret/{key}"), + eq(HttpMethod.GET), any(HttpEntity.class), eq(VaultResponse.class), + eq("mydefaultkey"))).thenReturn(myDefaultKeyResp); + + VaultEnvironmentRepository repo = new VaultEnvironmentRepository( + mockProvide(configRequest), new EnvironmentWatch.Default(), rest, + new VaultEnvironmentProperties()); + repo.setDefaultKey("mydefaultkey"); + + Environment e = repo.findOne("myapp,yourapp", null, null); + assertThat(e.getName()).as("Name should be the same as the application argument") + .isEqualTo("myapp,yourapp"); + assertThat(e.getPropertySources().size()).as( + "Properties for specified applications and default application with key 'mydefaultkey' should be returned") + .isEqualTo(3); + + Map firstResult = new HashMap(); + firstResult.put("yourapp-foo", "yourapp-bar"); + assertThat(e.getPropertySources().get(0).getSource()).as( + "Properties for first specified application should be returned in priority position") + .isEqualTo(firstResult); + + Map secondResult = new HashMap(); + secondResult.put("myapp-foo", "myapp-bar"); + assertThat(e.getPropertySources().get(1).getSource()).as( + "Properties for second specified application should be returned in priority position") + .isEqualTo(secondResult); + + Map thirdResult = new HashMap(); + thirdResult.put("def-foo", "def-bar"); + assertThat(e.getPropertySources().get(2).getSource()).as( + "Properties for default application with key 'mydefaultkey' should be returned in second position") + .isEqualTo(thirdResult); + } + @Test @SuppressWarnings("unchecked") public void testFindOneDefaultKeySetAndEqualToApplication() {