Skip to content

Commit daa088a

Browse files
committed
Refactor S3 env repo path/profile handling (#2812)
Fixes #2812 Rework AwsS3EnvironmentRepository path resolution and profile handling. Reverse the app list before checking/appending the default application and only append the default when it is missing. Simplify property-source iteration and invoke negated-profile property source logic only when searchPaths is empty to avoid duplicate sources. Normalize search path patterns by resolving null label/profile vars, collapsing duplicate slashes and trimming leading '/', and optimize extension probing so existing extensions aren't re-probed. Signed-off-by: Geonwook Ham <tomy8964@naver.com>
1 parent 4a2c386 commit daa088a

1 file changed

Lines changed: 24 additions & 23 deletions

File tree

spring-cloud-config-server/src/main/java/org/springframework/cloud/config/server/environment/AwsS3EnvironmentRepository.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ public Environment findOne(String specifiedApplication, String specifiedProfiles
120120

121121
String[] profileArray = parseProfiles(profiles);
122122
List<String> apps = Arrays.asList(StringUtils.commaDelimitedListToStringArray(application.replace(" ", "")));
123-
if (searchPaths.isEmpty() && !apps.contains(serverProperties.getDefaultApplicationName())) {
124-
Collections.reverse(apps);
123+
Collections.reverse(apps);
124+
if (!apps.contains(serverProperties.getDefaultApplicationName())) {
125125
apps = new ArrayList<>(apps);
126126
apps.add(serverProperties.getDefaultApplicationName());
127127
}
@@ -149,22 +149,10 @@ public Environment findOne(String specifiedApplication, String specifiedProfiles
149149

150150
private void addPropertySources(Environment environment, List<String> apps, String[] profiles,
151151
List<String> labels) {
152-
if (!this.searchPaths.isEmpty()) {
153-
for (String label : labels) {
154-
for (String profile : profiles) {
155-
for (String app : apps) {
156-
List<S3ConfigFile> s3ConfigFiles = getS3ConfigFileWithSearchPaths(app, profile, label);
157-
addPropertySource(environment, s3ConfigFiles);
158-
}
159-
}
160-
}
161-
return;
162-
}
163152
for (String label : labels) {
164-
// If we have profiles, add property sources with those profiles
165153
for (String profile : profiles) {
166154
addPropertySourcesForApps(apps,
167-
app -> addProfileSpecificPropertySource(environment, app, profile, label));
155+
app -> addProfileSpecificPropertySource(environment, app, profile, label));
168156
}
169157
}
170158

@@ -176,8 +164,10 @@ private void addPropertySources(Environment environment, List<String> apps, Stri
176164
// Even with no profiles, negated profile documents (e.g. on-profile:
177165
// "!my-profile") should be included because no profile is active,
178166
// so all negations are satisfied
179-
addPropertySourcesForApps(apps,
167+
if (this.searchPaths.isEmpty()) {
168+
addPropertySourcesForApps(apps,
180169
app -> addNegatedProfilePropertySource(environment, app, profiles, label));
170+
}
181171
}
182172
}
183173
else {
@@ -196,8 +186,10 @@ private void addPropertySources(Environment environment, List<String> apps, Stri
196186
// Handle documents with negated profile expressions (e.g. on-profile:
197187
// "!my-profile")
198188
// once per label rather than once per profile to avoid duplicates
199-
addPropertySourcesForApps(apps,
189+
if (this.searchPaths.isEmpty()) {
190+
addPropertySourcesForApps(apps,
200191
app -> addNegatedProfilePropertySource(environment, app, profiles, label));
192+
}
201193
}
202194
}
203195
}
@@ -297,16 +289,25 @@ private List<S3ConfigFile> getS3ConfigFileWithSearchPaths(
297289
List<S3ConfigFile> result = new ArrayList<>();
298290
Set<String> seenKeys = new LinkedHashSet<>();
299291

300-
for (String template : this.searchPaths) {
292+
for (String template : this.searchPaths) {String resolvedLabel = (label == null ? "" : label);
293+
String resolvedProfile = (profile == null ? "" : profile);
294+
301295
String pattern = template
302296
.replace("{application}", application)
303-
.replace("{profile}", profile == null ? "" : profile)
304-
.replace("{label}", label == null ? "" : label);
297+
.replace("{profile}", resolvedProfile)
298+
.replace("{label}", resolvedLabel);
299+
300+
pattern = StringUtils.trimLeadingCharacter(pattern.replaceAll("/{2,}", "/"), '/');
305301

306302
if (!pathMatcher.isPattern(pattern)) {
307303
boolean fileFound = false;
308-
for (String ext : List.of(".properties", ".json", ".yml", ".yaml")) {
309-
String key = pattern.endsWith(ext) ? pattern : pattern + ext;
304+
List<String> extensionsToProbe = (pattern.endsWith(".properties") || pattern.endsWith(".json")
305+
|| pattern.endsWith(".yml") || pattern.endsWith(".yaml"))
306+
? List.of("")
307+
: List.of(".properties", ".json", ".yml", ".yaml");
308+
309+
for (String ext : extensionsToProbe) {
310+
String key = pattern + ext;
310311
if (!seenKeys.add(key)) {
311312
continue;
312313
}
@@ -991,4 +992,4 @@ protected String buildObjectKeyPrefix() {
991992
public boolean isShouldIncludeWithEmptyProperties() {
992993
return false;
993994
}
994-
}
995+
}

0 commit comments

Comments
 (0)