Skip to content

Commit d12793a

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 6a86ac7 commit d12793a

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;
@@ -444,7 +445,7 @@ public List<String> getAsList(String key, List<String> defaultValue, Boolean com
444445
final Object valueFromPrefix = settings.get(key);
445446
if (valueFromPrefix != null) {
446447
if (valueFromPrefix instanceof List) {
447-
return ((List<String>) valueFromPrefix); // it's already unmodifiable since the builder puts it as a such
448+
return Collections.unmodifiableList((List<String>) valueFromPrefix);
448449
} else if (commaDelimited) {
449450
String[] strings = Strings.splitStringByCommaToArray(get(key));
450451
if (strings.length > 0) {
@@ -1072,7 +1073,7 @@ public Builder putList(String setting, String... values) {
10721073
*/
10731074
public Builder putList(String setting, List<String> values) {
10741075
remove(setting);
1075-
map.put(setting, Collections.unmodifiableList(new ArrayList<>(values)));
1076+
map.put(setting, new ArrayList<>(values));
10761077
return this;
10771078
}
10781079

@@ -1240,10 +1241,20 @@ public boolean shouldRemoveMissingPlaceholder(String placeholderName) {
12401241
Iterator<Map.Entry<String, Object>> entryItr = map.entrySet().iterator();
12411242
while (entryItr.hasNext()) {
12421243
Map.Entry<String, Object> entry = entryItr.next();
1243-
if (entry.getValue() == null || entry.getValue() instanceof List) {
1244+
if (entry.getValue() == null) {
12441245
// a null value obviously can't be replaced
12451246
continue;
12461247
}
1248+
if (entry.getValue() instanceof List) {
1249+
final ListIterator<String> li = ((List<String>) entry.getValue()).listIterator();
1250+
while (li.hasNext()) {
1251+
final String settingValueRaw = li.next();
1252+
final String settingValueResolved = propertyPlaceholder.replacePlaceholders(settingValueRaw, placeholderResolver);
1253+
li.set(settingValueResolved);
1254+
}
1255+
continue;
1256+
}
1257+
12471258
String value = propertyPlaceholder.replacePlaceholders(Settings.toString(entry.getValue()), placeholderResolver);
12481259
// if the values exists and has length, we should maintain it in the map
12491260
// 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
@@ -73,6 +73,16 @@ public void testReplacePropertiesPlaceholderSystemProperty() {
7373
assertThat(settings.get("setting1"), equalTo(value));
7474
}
7575

76+
public void testReplacePropertiesPlaceholderSystemPropertyList() {
77+
final String hostname = randomAlphaOfLength(16);
78+
final String hostip = randomAlphaOfLength(16);
79+
final Settings settings = Settings.builder()
80+
.putList("setting1", "${HOSTNAME}", "${HOSTIP}")
81+
.replacePropertyPlaceholders(name -> name.equals("HOSTNAME") ? hostname : name.equals("HOSTIP") ? hostip : null)
82+
.build();
83+
assertThat(settings.getAsList("setting1"), contains(hostname, hostip));
84+
}
85+
7686
public void testReplacePropertiesPlaceholderSystemVariablesHaveNoEffect() {
7787
final String value = System.getProperty("java.home");
7888
assertNotNull(value);

0 commit comments

Comments
 (0)