Skip to content

Commit 6075e15

Browse files
original-brownbearjasontedor
authored andcommitted
Validate list values for settings (#33503)
When we see a settings value, it could be a list. Yet this should only happen if the underlying setting type is a list setting type. This commit adds validation that when we get a setting value that is a list, that the setting that we are getting is a list setting. And similarly, if we get a value for a list setting, the underlying value should be a list.
1 parent 624b6bb commit 6075e15

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

server/src/main/java/org/elasticsearch/common/settings/Setting.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,11 @@ boolean isGroupSetting() {
345345
return false;
346346
}
347347

348+
349+
final boolean isListSetting() {
350+
return this instanceof ListSetting;
351+
}
352+
348353
boolean hasComplexMatcher() {
349354
return isGroupSetting();
350355
}
@@ -453,7 +458,7 @@ public final String getRaw(final Settings settings) {
453458
* @return the raw string representation of the setting value
454459
*/
455460
String innerGetRaw(final Settings settings) {
456-
return settings.get(getKey(), defaultValue.apply(settings));
461+
return settings.get(getKey(), defaultValue.apply(settings), isListSetting());
457462
}
458463

459464
/** Logs a deprecation warning if the setting is deprecated and used. */
@@ -1305,7 +1310,6 @@ public void diff(Settings.Builder builder, Settings source, Settings defaultSett
13051310
}
13061311
}
13071312
}
1308-
13091313
}
13101314

13111315
static void logSettingUpdate(Setting setting, Settings current, Settings previous, Logger logger) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,30 @@ public String get(String setting, String defaultValue) {
245245
return retVal == null ? defaultValue : retVal;
246246
}
247247

248+
/**
249+
* Returns the setting value associated with the setting key. If it does not exists,
250+
* returns the default value provided.
251+
*/
252+
String get(String setting, String defaultValue, boolean isList) {
253+
Object value = settings.get(setting);
254+
if (value != null) {
255+
if (value instanceof List) {
256+
if (isList == false) {
257+
throw new IllegalArgumentException(
258+
"Found list type value for setting [" + setting + "] but but did not expect a list for it."
259+
);
260+
}
261+
} else if (isList) {
262+
throw new IllegalArgumentException(
263+
"Expected list type value for setting [" + setting + "] but found [" + value.getClass() + ']'
264+
);
265+
}
266+
return toString(value);
267+
} else {
268+
return defaultValue;
269+
}
270+
}
271+
248272
/**
249273
* Returns the setting value (as float) associated with the setting key. If it does not exists,
250274
* returns the default value provided.

server/src/test/java/org/elasticsearch/common/settings/SettingTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ public void testSimpleUpdate() {
180180
}
181181
}
182182

183+
public void testValidateStringSetting() {
184+
Settings settings = Settings.builder().putList("foo.bar", Arrays.asList("bla-a", "bla-b")).build();
185+
Setting<String> stringSetting = Setting.simpleString("foo.bar", Property.NodeScope);
186+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> stringSetting.get(settings));
187+
assertEquals("Found list type value for setting [foo.bar] but but did not expect a list for it.", e.getMessage());
188+
}
189+
183190
private static final Setting<String> FOO_BAR_SETTING = new Setting<>(
184191
"foo.bar",
185192
"foobar",

0 commit comments

Comments
 (0)