Skip to content

Commit 953f016

Browse files
author
Ali Beyad
committed
disallow node attributes with leading or trailing whitespace
1 parent 1ca2d47 commit 953f016

File tree

2 files changed

+47
-1
lines changed
  • core/src/main/java/org/elasticsearch/node
  • test/framework/src/main/java/org/elasticsearch/node

2 files changed

+47
-1
lines changed

core/src/main/java/org/elasticsearch/node/Node.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,16 @@ public class Node implements Closeable {
185185
*/
186186
public static final Setting<Boolean> NODE_LOCAL_STORAGE_SETTING = Setting.boolSetting("node.local_storage", true, Property.NodeScope);
187187
public static final Setting<String> NODE_NAME_SETTING = Setting.simpleString("node.name", Property.NodeScope);
188-
public static final Setting<Settings> NODE_ATTRIBUTES = Setting.groupSetting("node.attr.", Property.NodeScope);
188+
public static final Setting<Settings> NODE_ATTRIBUTES = Setting.groupSetting("node.attr.", (settings) -> {
189+
Map<String, String> settingsMap = settings.getAsMap();
190+
for (Map.Entry<String, String> entry : settingsMap.entrySet()) {
191+
String value = entry.getValue();
192+
if (Character.isWhitespace(value.charAt(0)) || Character.isWhitespace(value.charAt(value.length() - 1))) {
193+
throw new IllegalArgumentException("node.attr." + entry.getKey() + " cannot have leading or trailing whitespace " +
194+
"[" + value + "]");
195+
}
196+
}
197+
}, Property.NodeScope);
189198
public static final Setting<String> BREAKER_TYPE_KEY = new Setting<>("indices.breaker.type", "hierarchy", (s) -> {
190199
switch (s) {
191200
case "hierarchy":

test/framework/src/main/java/org/elasticsearch/node/NodeTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,41 @@ public void testWarnIfPreRelease() {
138138

139139
}
140140

141+
public void testNodeAttributes() throws IOException {
142+
String attr = randomAsciiOfLength(5);
143+
Settings.Builder settings = baseSettings().put(Node.NODE_ATTRIBUTES.getKey() + "test_attr", attr);
144+
try (Node node = new MockNode(settings.build(), Collections.singleton(MockTcpTransportPlugin.class))) {
145+
final Settings nodeSettings = randomBoolean() ? node.settings() : node.getEnvironment().settings();
146+
assertEquals(attr, Node.NODE_ATTRIBUTES.get(nodeSettings).getAsMap().get("test_attr"));
147+
}
148+
149+
// leading whitespace not allowed
150+
attr = " leading";
151+
settings = baseSettings().put(Node.NODE_ATTRIBUTES.getKey() + "test_attr", attr);
152+
try (Node node = new MockNode(settings.build(), Collections.singleton(MockTcpTransportPlugin.class))) {
153+
fail("should not allow a node attribute with leading whitespace");
154+
} catch (IllegalArgumentException e) {
155+
assertEquals("node.attr.test_attr cannot have leading or trailing whitespace [ leading]", e.getMessage());
156+
}
157+
158+
// trailing whitespace not allowed
159+
attr = "trailing ";
160+
settings = baseSettings().put(Node.NODE_ATTRIBUTES.getKey() + "test_attr", attr);
161+
try (Node node = new MockNode(settings.build(), Collections.singleton(MockTcpTransportPlugin.class))) {
162+
fail("should not allow a node attribute with trailing whitespace");
163+
} catch (IllegalArgumentException e) {
164+
assertEquals("node.attr.test_attr cannot have leading or trailing whitespace [trailing ]", e.getMessage());
165+
}
166+
}
167+
168+
private static Settings.Builder baseSettings() {
169+
final Path tempDir = createTempDir();
170+
return Settings.builder()
171+
.put(ClusterName.CLUSTER_NAME_SETTING.getKey(), InternalTestCluster.clusterName("single-node-cluster", randomLong()))
172+
.put(Environment.PATH_HOME_SETTING.getKey(), tempDir)
173+
.put(NetworkModule.HTTP_ENABLED.getKey(), false)
174+
.put("transport.type", "mock-socket-network")
175+
.put(Node.NODE_DATA_SETTING.getKey(), true);
176+
}
177+
141178
}

0 commit comments

Comments
 (0)