diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepository.java index f0836a2a..fd96e9f1 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepository.java @@ -77,11 +77,14 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde List applications = normalize(application, DEFAULT_APPLICATION); List profiles = normalize(profile, DEFAULT_PROFILE); + List labels = normalize(label, this.defaultLabel); Environment environment = new Environment(application, split(profile), label, null, null); - for (String prof : profiles) { - for (String app : applications) { - addPropertySource(environment, app, prof, label); + for (String l : labels) { + for (String prof : profiles) { + for (String app : applications) { + addPropertySource(environment, app, prof, l); + } } } diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepositoryTests.java index d8d15109..aee73a8c 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/CredhubEnvironmentRepositoryTests.java @@ -121,6 +121,28 @@ public class CredhubEnvironmentRepositoryTests { assertThat(environment.getPropertySources().get(1).getSource()).isEqualTo(Map.of("k1", "v1")); } + @Test + public void shouldRetrieveGivenLabelsProfiles() { + stubCredentials("/myApp/prod/myLabel", credential("c1", "k1", "v1")); + stubCredentials("/myApp/cloud/mySecondLabel", credential("c2", "k2", "v2")); + stubCredentials("/myApp/prod/myThirdLabel", credential("c3", "k3", "v3")); + + Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod,cloud", + "myLabel,mySecondLabel"); + + assertThat(environment.getName()).isEqualTo("myApp"); + assertThat(environment.getProfiles()).containsExactly("prod", "cloud"); + assertThat(environment.getLabel()).isEqualTo("myLabel,mySecondLabel"); + + assertThat(environment.getPropertySources()).hasSize(2); + + assertThat(environment.getPropertySources().get(0).getName()).isEqualTo("credhub-myApp-cloud-mySecondLabel"); + assertThat(environment.getPropertySources().get(0).getSource()).isEqualTo(Map.of("k2", "v2")); + + assertThat(environment.getPropertySources().get(1).getName()).isEqualTo("credhub-myApp-prod-myLabel"); + assertThat(environment.getPropertySources().get(1).getSource()).isEqualTo(Map.of("k1", "v1")); + } + @Test public void shouldRetrieveGivenMultipleApplicationNames() { stubCredentials("/app1/default/myLabel", credential("c1", "k1", "v1"));