Skip to content

Commit aed2c50

Browse files
committed
rename the placeholders
1 parent ba6c3d1 commit aed2c50

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

docs/reference/setup/configuration.asciidoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,15 @@ file which will resolve to an environment setting, for example:
265265
--------------------------------------------------
266266

267267
Additionally, for settings that you do not wish to store in the configuration
268-
file, you can use the value `__prompt:text__` or `__prompt:secret__` and start
269-
Elasticsearch in the foreground. `__prompt:secret__` has echoing disabled so
270-
that the value entered will not be shown in your terminal; `__prompt:text__`
268+
file, you can use the value `${prompt::text}` or `${prompt::secret}` and start
269+
Elasticsearch in the foreground. `${prompt::secret}` has echoing disabled so
270+
that the value entered will not be shown in your terminal; `${prompt::text}`
271271
will allow you to see the value as you type it in. For example:
272272

273273
[source,yaml]
274274
--------------------------------------------------
275275
node:
276-
name: __prompt:text__
276+
name: ${prompt::text}
277277
--------------------------------------------------
278278

279279
On execution of the `elasticsearch` command, you will be prompted to enter
@@ -284,7 +284,7 @@ the actual value like so:
284284
Enter value for [node.name]:
285285
--------------------------------------------------
286286

287-
NOTE: Elasticsearch will not start if `__prompt:text__` or `__prompt:secret__`
287+
NOTE: Elasticsearch will not start if `${prompt::text}` or `${prompt::secret}`
288288
is used in the settings and the process is run as a service or in the background.
289289

290290
The location of the configuration file can be set externally using a

src/main/java/org/elasticsearch/common/settings/ImmutableSettings.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ public Builder putProperties(String prefix, Properties properties, String[] igno
10571057
* tries and resolve it against an environment variable ({@link System#getenv(String)}), and last, tries
10581058
* and replace it with another setting already set on this builder.
10591059
*/
1060-
public Builder replacePropertyPlaceholders() {
1060+
public Builder replacePropertyPlaceholders(String... ignoredValues) {
10611061
PropertyPlaceholder propertyPlaceholder = new PropertyPlaceholder("${", "}", false);
10621062
PropertyPlaceholder.PlaceholderResolver placeholderResolver = new PropertyPlaceholder.PlaceholderResolver() {
10631063
@Override
@@ -1087,7 +1087,19 @@ public boolean shouldIgnoreMissing(String placeholderName) {
10871087
}
10881088
};
10891089
for (Map.Entry<String, String> entry : Maps.newHashMap(map).entrySet()) {
1090-
String value = propertyPlaceholder.replacePlaceholders(entry.getValue(), placeholderResolver);
1090+
String possiblePlaceholder = entry.getValue();
1091+
boolean ignored = false;
1092+
for (String ignoredValue : ignoredValues) {
1093+
if (ignoredValue.equals(possiblePlaceholder)) {
1094+
ignored = true;
1095+
break;
1096+
}
1097+
}
1098+
if (ignored) {
1099+
continue;
1100+
}
1101+
1102+
String value = propertyPlaceholder.replacePlaceholders(possiblePlaceholder, placeholderResolver);
10911103
// if the values exists and has length, we should maintain it in the map
10921104
// otherwise, the replace process resolved into removing it
10931105
if (Strings.hasLength(value)) {

src/main/java/org/elasticsearch/node/internal/InternalSettingsPreparer.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
*/
4141
public class InternalSettingsPreparer {
4242

43-
public static final String SECRET_PROMPT_VALUE = "__prompt:secret__";
44-
public static final String TEXT_PROMPT_VALUE = "__prompt:text__";
43+
public static final String SECRET_PROMPT_VALUE = "${prompt::secret}";
44+
public static final String TEXT_PROMPT_VALUE = "${prompt::text}";
4545
public static final String IGNORE_SYSTEM_PROPERTIES_SETTING = "config.ignore_system_properties";
4646

4747
/**
@@ -68,6 +68,9 @@ public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, b
6868
public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, boolean loadConfigSettings, Terminal terminal) {
6969
// ignore this prefixes when getting properties from es. and elasticsearch.
7070
String[] ignorePrefixes = new String[]{"es.default.", "elasticsearch.default."};
71+
// ignore the special prompt placeholders since they have the same format as property placeholders and will be resolved
72+
// as having a default value because of the ':' in the format
73+
String[] ignoredPlaceholders = new String[] { SECRET_PROMPT_VALUE, TEXT_PROMPT_VALUE };
7174
boolean useSystemProperties = !pSettings.getAsBoolean(IGNORE_SYSTEM_PROPERTIES_SETTING, false);
7275
// just create enough settings to build the environment
7376
ImmutableSettings.Builder settingsBuilder = settingsBuilder().put(pSettings);
@@ -77,7 +80,7 @@ public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, b
7780
.putProperties("elasticsearch.", System.getProperties(), ignorePrefixes)
7881
.putProperties("es.", System.getProperties(), ignorePrefixes);
7982
}
80-
settingsBuilder.replacePropertyPlaceholders();
83+
settingsBuilder.replacePropertyPlaceholders(ignoredPlaceholders);
8184

8285
Environment environment = new Environment(settingsBuilder.build());
8386

@@ -125,7 +128,7 @@ public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, b
125128
settingsBuilder.putProperties("elasticsearch.", System.getProperties(), ignorePrefixes)
126129
.putProperties("es.", System.getProperties(), ignorePrefixes);
127130
}
128-
settingsBuilder.replacePropertyPlaceholders();
131+
settingsBuilder.replacePropertyPlaceholders(ignoredPlaceholders);
129132

130133
// allow to force set properties based on configuration of the settings provided
131134
for (Map.Entry<String, String> entry : pSettings.getAsMap().entrySet()) {
@@ -135,7 +138,7 @@ public static Tuple<Settings, Environment> prepareSettings(Settings pSettings, b
135138
settingsBuilder.put(setting.substring("force.".length()), entry.getValue());
136139
}
137140
}
138-
settingsBuilder.replacePropertyPlaceholders();
141+
settingsBuilder.replacePropertyPlaceholders(ignoredPlaceholders);
139142

140143
// generate the name
141144
if (settingsBuilder.get("name") == null) {

src/test/java/org/elasticsearch/common/settings/ImmutableSettingsTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,17 @@ public void testReplacePropertiesPlaceholderIgnoreEnvUnset() {
137137
assertThat(settings.get("setting1"), is(nullValue()));
138138
}
139139

140+
@Test
141+
public void testReplacePropertiesPlaceholderIgnores() {
142+
Settings settings = settingsBuilder()
143+
.put("setting1", "${foo.bar}")
144+
.put("setting2", "${foo.bar1}")
145+
.replacePropertyPlaceholders("${foo.bar}", "${foo.bar1}")
146+
.build();
147+
assertThat(settings.get("setting1"), is("${foo.bar}"));
148+
assertThat(settings.get("setting2"), is("${foo.bar1}"));
149+
}
150+
140151
@Test
141152
public void testUnFlattenedSettings() {
142153
Settings settings = settingsBuilder()

src/test/java/org/elasticsearch/node/internal/InternalSettingsPreparerTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import static org.hamcrest.Matchers.is;
4141

4242
public class InternalSettingsPreparerTests extends ElasticsearchTestCase {
43+
4344
@Before
4445
public void setupSystemProperties() {
4546
System.setProperty("es.node.zone", "foo");
@@ -124,7 +125,7 @@ public void testReplaceSecretPromptPlaceholderWithNullTerminal() {
124125
InternalSettingsPreparer.replacePromptPlaceholders(builder.build(), null);
125126
fail("an exception should have been thrown since no terminal was provided!");
126127
} catch (UnsupportedOperationException e) {
127-
assertThat(e.getMessage(), containsString("with value [__prompt:secret__]"));
128+
assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.SECRET_PROMPT_VALUE + "]"));
128129
}
129130
}
130131

@@ -136,7 +137,7 @@ public void testReplaceTextPromptPlaceholderWithNullTerminal() {
136137
InternalSettingsPreparer.replacePromptPlaceholders(builder.build(), null);
137138
fail("an exception should have been thrown since no terminal was provided!");
138139
} catch (UnsupportedOperationException e) {
139-
assertThat(e.getMessage(), containsString("with value [__prompt:text__]"));
140+
assertThat(e.getMessage(), containsString("with value [" + InternalSettingsPreparer.TEXT_PROMPT_VALUE + "]"));
140141
}
141142
}
142143
}

0 commit comments

Comments
 (0)