Core: Validates bool values in yaml for node settings
- Added parseBooleanExact in booleans which throws exception in case of parse failure - Used ParseExact in static's to make it consistent + code review fixes - Added unit test - Changed Exception Type to ElasticSearchIllegalArg from Parse - used isExplicit* Closes #8097
This commit is contained in:
parent
7f14674ff5
commit
b8d2e2cd29
|
@ -23,6 +23,7 @@ import com.google.common.collect.ImmutableList;
|
|||
import com.google.common.collect.ImmutableMap;
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.common.Booleans;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.io.stream.StreamInput;
|
||||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
|
@ -72,7 +73,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
|
||||
public static boolean clientNode(Settings settings) {
|
||||
String client = settings.get("node.client");
|
||||
return client != null && client.equals("true");
|
||||
return Booleans.isExplicitTrue(client);
|
||||
}
|
||||
|
||||
public static boolean masterNode(Settings settings) {
|
||||
|
@ -80,7 +81,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
if (master == null) {
|
||||
return !clientNode(settings);
|
||||
}
|
||||
return master.equals("true");
|
||||
return Booleans.isExplicitTrue(master);
|
||||
}
|
||||
|
||||
public static boolean dataNode(Settings settings) {
|
||||
|
@ -88,7 +89,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
if (data == null) {
|
||||
return !clientNode(settings);
|
||||
}
|
||||
return data.equals("true");
|
||||
return Booleans.isExplicitTrue(data);
|
||||
}
|
||||
|
||||
public static final ImmutableList<DiscoveryNode> EMPTY_LIST = ImmutableList.of();
|
||||
|
@ -244,7 +245,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
if (data == null) {
|
||||
return !clientNode();
|
||||
}
|
||||
return data.equals("true");
|
||||
return Booleans.parseBooleanExact(data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -259,7 +260,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
*/
|
||||
public boolean clientNode() {
|
||||
String client = attributes.get("client");
|
||||
return client != null && client.equals("true");
|
||||
return client != null && Booleans.parseBooleanExact(client);
|
||||
}
|
||||
|
||||
public boolean isClientNode() {
|
||||
|
@ -274,7 +275,7 @@ public class DiscoveryNode implements Streamable, Serializable {
|
|||
if (master == null) {
|
||||
return !clientNode();
|
||||
}
|
||||
return master.equals("true");
|
||||
return Booleans.parseBooleanExact(master);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.common;
|
||||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
@ -78,6 +79,26 @@ public class Booleans {
|
|||
return false;
|
||||
}
|
||||
|
||||
/***
|
||||
*
|
||||
* @param value
|
||||
* @return true/false
|
||||
* throws exception if string cannot be parsed to boolean
|
||||
*/
|
||||
public static Boolean parseBooleanExact(String value){
|
||||
|
||||
boolean isFalse = isExplicitFalse(value);
|
||||
if (isFalse) {
|
||||
return false;
|
||||
}
|
||||
boolean isTrue = isExplicitTrue(value);
|
||||
if (isTrue) {
|
||||
return true;
|
||||
}
|
||||
|
||||
throw new ElasticsearchIllegalArgumentException("value cannot be parsed to boolean [ true/1/on/yes OR false/0/off/no ] ");
|
||||
}
|
||||
|
||||
public static Boolean parseBoolean(String value, Boolean defaultValue) {
|
||||
if (value == null) { // only for the null case we do that here!
|
||||
return defaultValue;
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
|
||||
package org.elasticsearch.common;
|
||||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Test;
|
||||
|
@ -69,6 +70,18 @@ public class BooleansTests extends ElasticsearchTestCase {
|
|||
assertThat(Booleans.parseBoolean(chars,0, chars.length, randomBoolean()), is(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseBooleanExact() {
|
||||
assertThat(Booleans.parseBooleanExact(randomFrom("true", "on", "yes", "1")), is(true));
|
||||
assertThat(Booleans.parseBooleanExact(randomFrom("false", "off", "no", "0")), is(false));
|
||||
try {
|
||||
Booleans.parseBooleanExact(randomFrom(null, "fred", "foo", "barney"));
|
||||
fail("Expected exception while parsing invalid boolean value ");
|
||||
} catch (Exception ex) {
|
||||
assertTrue(ex instanceof ElasticsearchIllegalArgumentException);
|
||||
}
|
||||
}
|
||||
|
||||
public void testIsExplict() {
|
||||
assertThat(Booleans.isExplicitFalse(randomFrom("true", "on", "yes", "1", "foo", null)), is(false));
|
||||
assertThat(Booleans.isExplicitFalse(randomFrom("false", "off", "no", "0")), is(true));
|
||||
|
|
Loading…
Reference in New Issue