Merge branch '4.0.x'

This commit is contained in:
Ryan Baxter
2023-06-16 16:18:32 -04:00
3 changed files with 73 additions and 42 deletions

View File

@@ -24,7 +24,6 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
@@ -50,7 +49,6 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
@@ -154,15 +152,28 @@ public class ConfigServerConfigDataLoader implements ConfigDataLoader<ConfigServ
}
else if (ALL_OPTIONS.size() > 2) {
// boot 2.4.5+
if (resource.isProfileSpecific()) {
List<PropertySource<?>> filteredSources = propertySources.stream()
.filter(propertySource -> resource.getAcceptedProfiles().stream()
.anyMatch(profile -> isProfileSpecificPropertySource(
resource.getProperties().getName(), propertySource, profile)))
.collect(Collectors.toList());
return new ConfigData(filteredSources, Option.IGNORE_IMPORTS, Option.PROFILE_SPECIFIC);
}
return new ConfigData(propertySources, Option.IGNORE_IMPORTS);
return new ConfigData(propertySources, propertySource -> {
String propertySourceName = propertySource.getName();
List<Option> options = new ArrayList<>();
options.add(Option.IGNORE_IMPORTS);
options.add(Option.IGNORE_PROFILES);
// TODO: the profile is now available on the backend
// in a future minor, add the profile associated with a
// PropertySource see
// https://github.com/spring-cloud/spring-cloud-config/issues/1874
for (String profile : resource.getAcceptedProfiles()) {
// TODO: switch to match
// , is used as a profile-separator for property sources
// from vault
// - is the default profile-separator for property sources
// TODO This is error prone logic see https://github.com/spring-cloud/spring-cloud-config/issues/2291
if (propertySourceName.matches(".*[-,]" + profile + ".*")) {
// // TODO: switch to Options.with() when implemented
options.add(Option.PROFILE_SPECIFIC);
}
}
return ConfigData.Options.of(options.toArray(new Option[0]));
});
}
}
}
@@ -193,36 +204,6 @@ public class ConfigServerConfigDataLoader implements ConfigDataLoader<ConfigServ
return null;
}
private boolean isProfileSpecificPropertySource(String applicationName, PropertySource<?> propertySource,
String profile) {
// Application names can have - so before checking if the application name
// contains a -
// check to see if the application name matches the property source name, if so
// its not a profile specific property source.
// TODO: switch to match
// , is used as a profile-separator for property sources
// from vault
// - is the default profile-separator for property sources
return !applicationName.equals(extractApplicationName(propertySource))
&& propertySource.getName().matches(".*[-,]" + profile + ".*");
}
private String extractApplicationName(PropertySource<?> propertySource) {
if (ObjectUtils.isEmpty(propertySource.getName())) {
return "";
}
int lastSlash = propertySource.getName().lastIndexOf("/");
int lastPeriod = propertySource.getName().lastIndexOf(".");
if (lastSlash == -1) {
lastSlash = 0;
}
if (lastPeriod == -1) {
lastPeriod = propertySource.getName().length();
}
return propertySource.getName().substring(lastSlash + 1, lastPeriod);
}
protected void log(Environment result) {
if (logger.isInfoEnabled()) {
logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",

View File

@@ -124,7 +124,7 @@ public class ConfigServerConfigDataCustomizationIntegrationTests {
assertThat(options.contains(Option.PROFILE_SPECIFIC)).isFalse();
}
else {
assertThat(configData.getPropertySources()).hasSize(0);
assertThat(configData.getPropertySources()).hasSize(1);
}
return configData;
}

View File

@@ -29,6 +29,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
@@ -411,7 +412,10 @@ public class ConfigServerConfigDataLoaderTests {
}
@Disabled
@Test
// TODO Enable once we have
// https://github.com/spring-cloud/spring-cloud-config/issues/2291
void filterPropertySourcesThatAreNotProfileSpecific() {
PropertySource p1 = new PropertySource("p1", Collections.singletonMap("foo", "bar"));
PropertySource p2 = new PropertySource("p2", Collections.singletonMap("hello", "world"));
@@ -420,7 +424,10 @@ public class ConfigServerConfigDataLoaderTests {
}
@Disabled
@Test
// TODO Enable once we have
// https://github.com/spring-cloud/spring-cloud-config/issues/2291
void returnPropertySourcesThatAreProfileSpecific() {
PropertySource p1 = new PropertySource("p1-dev", Collections.singletonMap("foo", "bar"));
PropertySource p2 = new PropertySource("p2-dev", Collections.singletonMap("hello", "world"));
@@ -434,6 +441,49 @@ public class ConfigServerConfigDataLoaderTests {
}
@Disabled
@Test
// TODO Enable once we have
// https://github.com/spring-cloud/spring-cloud-config/issues/2291
void filterPropertySourcesWithDocuments() {
PropertySource p1 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/application.yml",
Collections.singletonMap("foo", "bar"));
PropertySource p2 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/application-foo.yml",
Collections.singletonMap("foo", "bar"));
PropertySource p3 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application.properties' via location 'commons/' (document #0)",
Collections.singletonMap("hello", "world"));
PropertySource p4 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application-foo.properties' via location 'commons/' (document #0)",
Collections.singletonMap("hello", "world"));
Map<String, Object> activatesOnProfileCamelCase = new HashMap<>();
activatesOnProfileCamelCase.put("spring.config.activate.onProfile", "foo");
PropertySource p5 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application.properties' via location 'commons/' (document #1)",
activatesOnProfileCamelCase);
Map<String, Object> activatesOnProfile = new HashMap<>();
activatesOnProfile.put("spring.config.activate.on-profile", "foo");
PropertySource p6 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application.properties' via location 'commons/' (document #2)",
activatesOnProfile);
PropertySource p7 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/application-foo.yaml",
Collections.singletonMap("hello", "world"));
PropertySource p8 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application-foo.yaml' via location 'commons/' (document #0)",
Collections.singletonMap("hello", "world"));
PropertySource p9 = new PropertySource(
"configserver:git@github.com:demo/support-configuration-repo.git/Config resource 'file [/var/folders/k3/zv8hzdm17vv69j485fv3cf9r0000gp/T/config-repo-14772121892716396795/commons/application-foo.yaml' via location 'commons/' (document #1)",
Collections.singletonMap("hello", "world"));
ConfigData configData = setupConfigServerConfigDataLoader(Arrays.asList(p1, p2, p3, p4, p5, p6, p7, p8, p9),
"application-slash", "foo");
assertThat(configData.getPropertySources().size()).isEqualTo(7);
}
private ConfigData setupConfigServerConfigDataLoader(List<PropertySource> propertySources, String applicationName,
String... profileList) {
RestTemplate rest = mock(RestTemplate.class);