From f59f8b5816c407978878340a3a0645113ed73a03 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Wed, 3 May 2023 16:26:48 -0400 Subject: [PATCH] Include property sources without profile and include application property source (#2261) Fixes #1911 Co-authored-by: Ryan Baxter <524254+ryanjbaxter@users.noreply.github.com> --- .../AwsS3EnvironmentRepository.java | 58 +++++++++++-------- .../config/server/AwsS3IntegrationTests.java | 13 +++++ .../AwsS3EnvironmentRepositoryTests.java | 4 +- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepository.java b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepository.java index c96a297c..28357f41 100644 --- a/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepository.java +++ b/spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepository.java @@ -18,6 +18,9 @@ package org.springframework.cloud.config.server.environment; import java.io.IOException; import java.io.InputStream; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Properties; import com.amazonaws.services.s3.AmazonS3; @@ -31,6 +34,7 @@ import org.springframework.cloud.config.environment.PropertySource; import org.springframework.cloud.config.server.config.ConfigServerProperties; import org.springframework.core.Ordered; import org.springframework.core.io.InputStreamResource; +import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; /** @@ -68,16 +72,17 @@ public class AwsS3EnvironmentRepository implements EnvironmentRepository, Ordere @Override public Environment findOne(String specifiedApplication, String specifiedProfiles, String specifiedLabel) { - final String application = StringUtils.isEmpty(specifiedApplication) + final String application = ObjectUtils.isEmpty(specifiedApplication) ? serverProperties.getDefaultApplicationName() : specifiedApplication; - final String profiles = StringUtils.isEmpty(specifiedProfiles) ? serverProperties.getDefaultProfile() + final String profiles = ObjectUtils.isEmpty(specifiedProfiles) ? serverProperties.getDefaultProfile() : specifiedProfiles; - final String label = StringUtils.isEmpty(specifiedLabel) ? serverProperties.getDefaultLabel() : specifiedLabel; + final String label = ObjectUtils.isEmpty(specifiedLabel) ? serverProperties.getDefaultLabel() : specifiedLabel; String[] profileArray = parseProfiles(profiles); - String[] apps = new String[] { application }; - if (application != null) { - apps = StringUtils.commaDelimitedListToStringArray(application.replace(" ", "")); + List apps = Arrays.asList(StringUtils.commaDelimitedListToStringArray(application.replace(" ", ""))); + if (!apps.contains(serverProperties.getDefaultApplicationName())) { + apps = new ArrayList<>(apps); + apps.add(serverProperties.getDefaultApplicationName()); } final Environment environment = new Environment(application, profileArray); @@ -85,28 +90,35 @@ public class AwsS3EnvironmentRepository implements EnvironmentRepository, Ordere for (String profile : profileArray) { for (String app : apps) { - S3ConfigFile s3ConfigFile = getS3ConfigFile(app, profile, label); - if (s3ConfigFile != null) { - environment.setVersion(s3ConfigFile.getVersion()); - - final Properties config = s3ConfigFile.read(); - config.putAll(serverProperties.getOverrides()); - StringBuilder propertySourceName = new StringBuilder().append("s3:").append(app); - if (profile != null) { - propertySourceName.append("-").append(profile); - } - environment.add(new PropertySource(propertySourceName.toString(), config)); - } + addPropertySource(environment, app, profile, label); } } + // Add propertysources without profiles as well + for (String app : apps) { + addPropertySource(environment, app, null, label); + } + return environment; } - private String[] parseProfiles(String profiles) { - if (profiles.equals(serverProperties.getDefaultProfile())) { - return new String[] { profiles, null }; + private void addPropertySource(Environment environment, String app, String profile, String label) { + S3ConfigFile s3ConfigFile = getS3ConfigFile(app, profile, label); + if (s3ConfigFile != null) { + environment.setVersion(s3ConfigFile.getVersion()); + + final Properties config = s3ConfigFile.read(); + config.putAll(serverProperties.getOverrides()); + StringBuilder propertySourceName = new StringBuilder().append("s3:").append(app); + if (profile != null) { + propertySourceName.append("-").append(profile); + } + environment.add(new PropertySource(propertySourceName.toString(), config)); } + } + + private String[] parseProfiles(String profiles) { + return StringUtils.commaDelimitedListToStringArray(profiles); } @@ -120,11 +132,11 @@ public class AwsS3EnvironmentRepository implements EnvironmentRepository, Ordere private String buildObjectKeyPrefix(String application, String profile, String label) { StringBuilder objectKeyPrefix = new StringBuilder(); - if (!StringUtils.isEmpty(label)) { + if (!ObjectUtils.isEmpty(label)) { objectKeyPrefix.append(label).append(PATH_SEPARATOR); } objectKeyPrefix.append(application); - if (!StringUtils.isEmpty(profile)) { + if (!ObjectUtils.isEmpty(profile)) { objectKeyPrefix.append("-").append(profile); } return objectKeyPrefix.toString(); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/AwsS3IntegrationTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/AwsS3IntegrationTests.java index 5a14613d..b558eab5 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/AwsS3IntegrationTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/AwsS3IntegrationTests.java @@ -86,6 +86,8 @@ public class AwsS3IntegrationTests { s3Client.putObject("test-bucket", "data.txt", "this is a test"); s3Client.putObject("test-bucket", "main/data.txt", "this is a test in main"); s3Client.putObject("test-bucket", "application.properties", "foo=1"); + s3Client.putObject("test-bucket", "data.properties", "bar=1"); + s3Client.putObject("test-bucket", "data-dev.properties", "bar=1"); } @@ -101,6 +103,17 @@ public class AwsS3IntegrationTests { .isEqualTo("this is a test"); } + @Test + public void defaultApplicationAndProfileIncluded() throws IOException { + RestTemplate rest = new RestTemplateBuilder().build(); + String configServerUrl = "http://localhost:" + configServerPort; + Environment env = rest.getForObject(configServerUrl + "/data/dev", Environment.class); + assertThat(env.getPropertySources().size()).isEqualTo(3); + assertThat(env.getPropertySources().get(0).getName()).isEqualTo("s3:data-dev"); + assertThat(env.getPropertySources().get(1).getName()).isEqualTo("s3:data"); + assertThat(env.getPropertySources().get(2).getName()).isEqualTo("s3:application"); + } + @AfterAll public static void after() { server.close(); diff --git a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepositoryTests.java b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepositoryTests.java index 787ab5d4..e9a617cf 100644 --- a/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepositoryTests.java +++ b/spring-cloud-config-server/src/test/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepositoryTests.java @@ -135,7 +135,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", null, null); - assertExpectedEnvironment(env, "foo", null, null, 1, "default", null); + assertExpectedEnvironment(env, "foo", null, null, 1, "default"); } @Test @@ -144,7 +144,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", null, null); - assertExpectedEnvironment(env, "foo", null, null, 1, "default", null); + assertExpectedEnvironment(env, "foo", null, null, 1, "default"); } @Test