Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@
log(result);

// result.getPropertySources() can be null if using xml
Map<PropertySource<?>, String> propertySourceProfiles = new HashMap<>();
if (result.getPropertySources() != null) {
for (org.springframework.cloud.config.environment.PropertySource source : result
.getPropertySources()) {
@SuppressWarnings("unchecked")
Map<String, Object> map = translateOrigins(source.getName(),
(Map<String, Object>) source.getSource());
propertySources.add(0,
new OriginTrackedMapPropertySource("configserver:" + source.getName(), map, true));
OriginTrackedMapPropertySource propertySource = new OriginTrackedMapPropertySource(
"configserver:" + source.getName(), map, true);
propertySources.add(0, propertySource);
propertySourceProfiles.put(propertySource, source.getProfile());
}
}

Expand All @@ -165,31 +168,37 @@
// boot 2.4.5+
return new ConfigData(propertySources, propertySource -> {
String propertySourceName = propertySource.getName();
String profile = propertySourceProfiles.get(propertySource);
List<Option> options = new ArrayList<>();
options.add(Option.IGNORE_IMPORTS);
// 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
// When we see the overrides property source name we
// should always prioritize those
// properties over everything else, even profile specific
// property sources so also
// label this property source profile specific.
if (OVERRIDES_NAME.equals(propertySourceName) || (!DEFAULT_PROFILE.equals(profile)
&& propertySourceName.matches(".*[-,]" + profile + "\\b.*"))) {
// // TODO: switch to Options.with() when implemented
options.add(Option.PROFILE_SPECIFIC);
options.add(Option.IGNORE_PROFILES);

if (OVERRIDES_NAME.equals(propertySourceName)) {
options.add(Option.PROFILE_SPECIFIC);
options.add(Option.IGNORE_PROFILES);
}
else if (profile != null) {
for (String acceptedProfile : resource.getAcceptedProfiles()) {
if (!DEFAULT_PROFILE.equals(profile) && profile.equals(acceptedProfile)) {
options.add(Option.PROFILE_SPECIFIC);
options.add(Option.IGNORE_PROFILES);
break;
}
}
}
else {
// Backward compatibility with Config Server versions that
// do not
// provide profile metadata.
for (String acceptedProfile : resource.getAcceptedProfiles()) {
if (!DEFAULT_PROFILE.equals(acceptedProfile)
&& propertySourceName.matches(".*[-,]" + acceptedProfile + "\\b.*")) {
options.add(Option.PROFILE_SPECIFIC);
options.add(Option.IGNORE_PROFILES);
break;
}
}
}

return ConfigData.Options.of(options.toArray(new Option[0]));
});
}
Expand Down Expand Up @@ -276,7 +285,7 @@
protected Environment getRemoteEnvironment(ConfigDataLoaderContext context, ConfigServerConfigDataResource resource,
String label, String state) {
ConfigClientProperties properties = resource.getProperties();
RestTemplate restTemplate = context.getBootstrapContext().get(RestTemplate.class);

Check warning on line 288 in spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java

View workflow job for this annotation

GitHub Actions / build / pr

org.springframework.web.client.RestTemplate in org.springframework.web.client has been deprecated and marked for removal

Check warning on line 288 in spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoader.java

View workflow job for this annotation

GitHub Actions / build / pr

org.springframework.web.client.RestTemplate in org.springframework.web.client has been deprecated and marked for removal

String path = "/{name}/{profile}";
String name = properties.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,34 @@ public class PropertySource {

private Map<?, ?> source;

private String profile;

private org.springframework.core.env.PropertySource<?> originalPropertySource;

@JsonCreator
public PropertySource(@JsonProperty("name") String name, @JsonProperty("source") Map<?, ?> source) {
public PropertySource(@JsonProperty("name") String name, @JsonProperty("source") Map<?, ?> source,
@JsonProperty("profile") String profile) {
this.name = name;
this.source = source;
this.profile = profile;
}

public PropertySource(String name, Map<?, ?> source) {
this(name, source, (String) null);
}

@JsonIgnore
public PropertySource(String name, Map<?, ?> source,
org.springframework.core.env.PropertySource<?> originalPropertySource) {
this(name, source, null, originalPropertySource);
}

@JsonIgnore
public PropertySource(String name, Map<?, ?> source, String profile,
org.springframework.core.env.PropertySource<?> originalPropertySource) {
this.name = name;
this.source = source;
this.profile = profile;
this.originalPropertySource = originalPropertySource;
}

Expand All @@ -59,6 +74,10 @@ public String getName() {
return this.source;
}

public String getProfile() {
return this.profile;
}

@JsonIgnore
public org.springframework.core.env.PropertySource<?> getOriginalPropertySource() {
return this.originalPropertySource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,37 @@ void testProfileSpecificPropertySources() {
.contains(ConfigData.Option.IGNORE_PROFILES)).isTrue();
}

@Test
void testProfileSpecificPropertySourceWithExplicitProfile() {
PropertySource profileSpecific = new PropertySource("some-unrelated-name",
Collections.singletonMap("foo", "bar"), "dev");
PropertySource nonProfileSpecific = new PropertySource("application.properties",
Collections.singletonMap("foo", "bar"));

ConfigData configData = setupConfigServerConfigDataLoader(Arrays.asList(profileSpecific, nonProfileSpecific),
"application", "dev");

assertThat(configData.getPropertySources()).hasSize(3);

ConfigData.Options profileSpecificOptions = configData.getOptions(configData.getPropertySources()
.stream()
.filter(propertySource -> propertySource.getName().equals("configserver:some-unrelated-name"))
.findFirst()
.orElseThrow());

assertThat(profileSpecificOptions.contains(ConfigData.Option.PROFILE_SPECIFIC)).isTrue();
assertThat(profileSpecificOptions.contains(ConfigData.Option.IGNORE_PROFILES)).isTrue();

ConfigData.Options nonProfileSpecificOptions = configData.getOptions(configData.getPropertySources()
.stream()
.filter(propertySource -> propertySource.getName().equals("configserver:application.properties"))
.findFirst()
.orElseThrow());

assertThat(nonProfileSpecificOptions.contains(ConfigData.Option.PROFILE_SPECIFIC)).isFalse();
assertThat(nonProfileSpecificOptions.contains(ConfigData.Option.IGNORE_PROFILES)).isFalse();
}

@Test
void testProfileSpecificPropertySourcesWithDefaultProfile() {
PropertySource p1 = new PropertySource("overrides", Collections.singletonMap("foo", "bar"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,15 @@ protected Environment clean(Environment env,
continue;
}
String[] locations = null;
String profile = null;

PropertySourceConfigData configData = propertySourceToConfigData.get(source.getOriginalPropertySource());
// try and get information directly from ConfigData
if (configData != null && configData.resource instanceof StandardConfigDataResource) {
StandardConfigDataResource configDataResource = (StandardConfigDataResource) configData.resource;
// use StandardConfigDataResource as that format is expected still
name = configDataResource.toString();
profile = configDataResource.getProfile();
locations = configDataLocations(configData.location.split());
}
else {
Expand All @@ -302,9 +304,10 @@ protected Environment clean(Environment env,
locations = new String[] { matcher.group(2) };
}
}

name = name.replace("\\", "/"); // change windows path '\' into '/'
name = name.replaceAll("\\[(?=\\w:)", "[/"); // change [D:/path] into
// [/D:/path]
// [/D:/path]
name = name.replace("applicationConfig: [", "");
name = name.replace("file [", "file:");
name = name.replace("class path resource [", "classpath:/");
Expand All @@ -325,11 +328,12 @@ protected Environment clean(Environment env,
logger.info("Adding property source: " + originalName);
if (originalName.contains("document #")) {
// this is a multi-document file, use originalName for uniqueness.
result.add(new PropertySource(originalName, source.getSource()));
result.add(new PropertySource(originalName, source.getSource(), profile,
source.getOriginalPropertySource()));
}
else {
// many other file tests rely on the mangled name
result.add(new PropertySource(name, source.getSource()));
result.add(new PropertySource(name, source.getSource(), profile, source.getOriginalPropertySource()));
}
}
return result;
Expand Down
Loading