Skip to content

Commit caa63ad

Browse files
Fix environment variable substitutions in list setting (#28106)
Since Elasticsearch 6.1.0 environment variable substitutions in lists do not work. Environment variables in a list setting were not resolved, because settings with a list type were skipped during variables resolution. This commit fixes by processing list settings as well. Closes #27926
1 parent 4182e9e commit caa63ad

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

core/src/main/java/org/elasticsearch/common/settings/Settings.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import java.util.NoSuchElementException;
6565
import java.util.Set;
6666
import java.util.TreeMap;
67+
import java.util.ListIterator;
6768
import java.util.concurrent.TimeUnit;
6869
import java.util.function.Function;
6970
import java.util.function.Predicate;
@@ -414,7 +415,7 @@ public List<String> getAsList(String key, List<String> defaultValue, Boolean com
414415
final Object valueFromPrefix = settings.get(key);
415416
if (valueFromPrefix != null) {
416417
if (valueFromPrefix instanceof List) {
417-
return ((List<String>) valueFromPrefix); // it's already unmodifiable since the builder puts it as a such
418+
return Collections.unmodifiableList((List<String>) valueFromPrefix);
418419
} else if (commaDelimited) {
419420
String[] strings = Strings.splitStringByCommaToArray(get(key));
420421
if (strings.length > 0) {
@@ -1042,7 +1043,7 @@ public Builder putList(String setting, String... values) {
10421043
*/
10431044
public Builder putList(String setting, List<String> values) {
10441045
remove(setting);
1045-
map.put(setting, Collections.unmodifiableList(new ArrayList<>(values)));
1046+
map.put(setting, new ArrayList<>(values));
10461047
return this;
10471048
}
10481049

@@ -1210,10 +1211,20 @@ public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
12101211
Iterator<Map.Entry<String, Object>> entryItr = map.entrySet().iterator();
12111212
while (entryItr.hasNext()) {
12121213
Map.Entry<String, Object> entry = entryItr.next();
1213-
if (entry.getValue() == null || entry.getValue() instanceof List) {
1214+
if (entry.getValue() == null) {
12141215
// a null value obviously can't be replaced
12151216
continue;
12161217
}
1218+
if (entry.getValue() instanceof List) {
1219+
final ListIterator<String> li = ((List<String>) entry.getValue()).listIterator();
1220+
while (li.hasNext()) {
1221+
final String settingValueRaw = li.next();
1222+
final String settingValueResolved = propertyPlaceholder.replacePlaceholders(settingValueRaw, placeholderResolver);
1223+
li.set(settingValueResolved);
1224+
}
1225+
continue;
1226+
}
1227+
12171228
String value = propertyPlaceholder.replacePlaceholders(Settings.toString(entry.getValue()), placeholderResolver);
12181229
// if the values exists and has length, we should maintain it in the map
12191230
// otherwise, the replace process resolved into removing it

core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public void testReplacePropertiesPlaceholderSystemProperty() {
6868
assertThat(settings.get("setting1"), equalTo(value));
6969
}
7070

71+
public void testReplacePropertiesPlaceholderSystemPropertyList() {
72+
final String hostname = randomAlphaOfLength(16);
73+
final String hostip = randomAlphaOfLength(16);
74+
final Settings settings = Settings.builder()
75+
.putList("setting1", "${HOSTNAME}", "${HOSTIP}")
76+
.replacePropertyPlaceholders(name -> name.equals("HOSTNAME") ? hostname : name.equals("HOSTIP") ? hostip : null)
77+
.build();
78+
assertThat(settings.getAsList("setting1"), contains(hostname, hostip));
79+
}
80+
7181
public void testReplacePropertiesPlaceholderSystemVariablesHaveNoEffect() {
7282
final String value = System.getProperty("java.home");
7383
assertNotNull(value);

0 commit comments

Comments
 (0)