diff --git a/core/src/main/java/org/elasticsearch/common/settings/Settings.java b/core/src/main/java/org/elasticsearch/common/settings/Settings.java index 2ceafc376c5..15554e5ccaa 100644 --- a/core/src/main/java/org/elasticsearch/common/settings/Settings.java +++ b/core/src/main/java/org/elasticsearch/common/settings/Settings.java @@ -602,8 +602,7 @@ public final class Settings implements ToXContent { private final Map<String, String> map = new LinkedHashMap<>(); - // visible for testing - Builder() { + private Builder() { } @@ -961,33 +960,38 @@ public final class Settings implements ToXContent { * another setting already set on this builder. */ public Builder replacePropertyPlaceholders() { + return replacePropertyPlaceholders(System::getenv); + } + + // visible for testing + Builder replacePropertyPlaceholders(Function<String, String> getenv) { PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false); PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() { - @Override - public String resolvePlaceholder(String placeholderName) { - final String value = getenv(placeholderName); - if (value != null) { - return value; - } - return map.get(placeholderName); + @Override + public String resolvePlaceholder(String placeholderName) { + final String value = getenv.apply(placeholderName); + if (value != null) { + return value; } + return map.get(placeholderName); + } - @Override - public boolean shouldIgnoreMissing(String placeholderName) { - if (placeholderName.startsWith("prompt.")) { - return true; - } - return false; - } - - @Override - public boolean shouldRemoveMissingPlaceholder(String placeholderName) { - if (placeholderName.startsWith("prompt.")) { - return false; - } + @Override + public boolean shouldIgnoreMissing(String placeholderName) { + if (placeholderName.startsWith("prompt.")) { return true; } - }; + return false; + } + + @Override + public boolean shouldRemoveMissingPlaceholder(String placeholderName) { + if (placeholderName.startsWith("prompt.")) { + return false; + } + return true; + } + }; for (Map.Entry<String, String> entry : new HashMap<>(map).entrySet()) { String value = propertyPlaceholder.replacePlaceholders(entry.getKey(), entry.getValue(), placeholderResolver); // if the values exists and has length, we should maintain it in the map @@ -1001,11 +1005,6 @@ public final class Settings implements ToXContent { return this; } - // visible for testing - String getenv(String placeholderName) { - return System.getenv(placeholderName); - } - /** * Checks that all settings in the builder start with the specified prefix. * diff --git a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java index 5b3a698d328..346c5bc60de 100644 --- a/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java +++ b/core/src/test/java/org/elasticsearch/common/settings/SettingsTests.java @@ -63,15 +63,9 @@ public class SettingsTests extends ESTestCase { public void testReplacePropertiesPlaceholderByEnvironmentVariables() { final String hostname = randomAsciiOfLength(16); - final Settings.Builder builder = new Settings.Builder() { - @Override - protected String getenv(String placeholderName) { - return "HOSTNAME".equals(placeholderName) ? hostname : null; - } - }; - final Settings implicitEnvSettings = builder + final Settings implicitEnvSettings = Settings.builder() .put("setting1", "${HOSTNAME}") - .replacePropertyPlaceholders() + .replacePropertyPlaceholders(name -> "HOSTNAME".equals(name) ? hostname : null) .build(); assertThat(implicitEnvSettings.get("setting1"), equalTo(hostname)); }