From ce3b9847c49b993531dae8f1858a67172ffb0348 Mon Sep 17 00:00:00 2001 From: Kaveh Shamsi Date: Wed, 9 Oct 2024 16:50:38 +0200 Subject: [PATCH] Default-label in CredhubEnviornmentRepository should be used only if label is not provided (#2581) Signed-off-by: kvmw --- .../environment/CredhubEnvironmentRepository.java | 15 +++++++++++---- .../CredhubEnvironmentRepositoryTests.java | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) 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 fd96e9f1..83090dc3 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 @@ -22,7 +22,6 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import java.util.stream.Stream; import org.springframework.cloud.config.environment.Environment; import org.springframework.cloud.config.environment.PropertySource; @@ -77,7 +76,7 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde List applications = normalize(application, DEFAULT_APPLICATION); List profiles = normalize(profile, DEFAULT_PROFILE); - List labels = normalize(label, this.defaultLabel); + List labels = normalize(label); Environment environment = new Environment(application, split(profile), label, null, null); for (String l : labels) { @@ -92,12 +91,20 @@ public class CredhubEnvironmentRepository implements EnvironmentRepository, Orde } /** - * Splits the comma delimited items and returns the reversed distinct items with given + * Splits the comma-delimited items and returns the reversed distinct items with given * default item at the end. */ private List normalize(String commaDelimitedItems, String defaultItem) { - var items = Stream.concat(Stream.of(defaultItem), Arrays.stream(split(commaDelimitedItems))) + return normalize(defaultItem + "," + commaDelimitedItems); + } + + /** + * Splits the comma-delimited items and returns the reversed distinct items. + */ + private List normalize(String commaDelimitedItems) { + var items = Arrays.stream(split(commaDelimitedItems)) .distinct() + .filter(StringUtils::hasText) .collect(Collectors.toList()); Collections.reverse(items); 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 aee73a8c..75226843 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 @@ -125,7 +125,7 @@ public class CredhubEnvironmentRepositoryTests { 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")); + stubCredentials("/myApp/prod/master", credential("c3", "k3", "v3")); Environment environment = this.credhubEnvironmentRepository.findOne("myApp", "prod,cloud", "myLabel,mySecondLabel");