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 629bf54e..90c77d39 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,10 +18,10 @@ 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 java.util.stream.Collectors; import software.amazon.awssdk.core.ResponseInputStream; import software.amazon.awssdk.services.s3.S3Client; @@ -73,16 +73,17 @@ public class AwsS3EnvironmentRepository implements EnvironmentRepository, Ordere @Override public Environment findOne(String specifiedApplication, String specifiedProfiles, String specifiedLabel) { - final String application = !StringUtils.hasText(specifiedApplication) + final String application = ObjectUtils.isEmpty(specifiedApplication) ? serverProperties.getDefaultApplicationName() : specifiedApplication; - final String profiles = !StringUtils.hasText(specifiedProfiles) ? serverProperties.getDefaultProfile() + final String profiles = ObjectUtils.isEmpty(specifiedProfiles) ? serverProperties.getDefaultProfile() : specifiedProfiles; - final String label = !StringUtils.hasText(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); @@ -90,34 +91,36 @@ 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 (ObjectUtils.isEmpty(profiles)) { - return new String[] { "" }; + 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)); } - List parsedProfiles = Arrays.stream(profiles.split(",")) - .collect(Collectors.collectingAndThen(Collectors.toList(), p -> { - p.add(""); - return p; - })); - return parsedProfiles.toArray(new String[0]); + } + + private String[] parseProfiles(String profiles) { + + return StringUtils.commaDelimitedListToStringArray(profiles); } private S3ConfigFile getS3ConfigFile(String application, String profile, String label) { 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 6a9b7112..fc47e0f9 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 @@ -90,6 +90,10 @@ public class AwsS3IntegrationTests { RequestBody.fromString("this is a test in main")); s3Client.putObject((request) -> request.bucket("test-bucket").key("application.properties"), RequestBody.fromString("foo=1")); + s3Client.putObject((request) -> request.bucket("test-bucket").key("data.properties"), + RequestBody.fromString("bar=1")); + s3Client.putObject((request) -> request.bucket("test-bucket").key("data-dev.properties"), + RequestBody.fromString("bar=1")); } } @@ -107,6 +111,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 c2c44bdd..3002da3d 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 @@ -145,7 +145,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar"); } @Test @@ -154,7 +154,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar"); } @Test @@ -163,7 +163,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar"); } @Test @@ -172,7 +172,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar"); } @Test @@ -181,7 +181,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", null, null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "default", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "default"); } @Test @@ -190,7 +190,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", null, null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "default", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "default"); } @Test @@ -200,7 +200,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "profile1,profile2", null); - assertExpectedEnvironment(env, "foo", null, versionId, 2, "profile1", "profile2", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 2, "profile1", "profile2"); } @Test @@ -209,7 +209,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "profile1,profile2", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "profile1", "profile2", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "profile1", "profile2"); } @Test @@ -219,7 +219,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "profile1", null); - assertExpectedEnvironment(env, "foo", null, versionId, 2, "profile1", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 2, "profile1"); } @Test @@ -229,7 +229,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", null, null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1); } @@ -239,7 +239,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", "label1"); - assertExpectedEnvironment(env, "foo", "label1", versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", "label1", versionId, 1, "bar"); } @Test @@ -248,7 +248,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo", "bar", null); - assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar", ""); + assertExpectedEnvironment(env, "foo", null, versionId, 1, "bar"); } @Test @@ -258,7 +258,7 @@ public class AwsS3EnvironmentRepositoryTests { final Environment env = envRepo.findOne("foo,bar", "profile1", null); - assertExpectedEnvironment(env, "foo,bar", null, versionId, 2, "profile1", ""); + assertExpectedEnvironment(env, "foo,bar", null, versionId, 2, "profile1"); }