Skip to content

Commit 7268c10

Browse files
committed
Allow wildcards for shard IP filtering (#26187)
Fixes the broken usage of wildcards for IP-based allocation filtering (introduced by PR #22591), which is documented at https://www.elastic.co/guide/en/elasticsearch/reference/current/shard-allocation-filtering.html Closes #26184
1 parent e01c90b commit 7268c10

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

core/src/main/java/org/elasticsearch/cluster/node/DiscoveryNodeFilters.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public enum OpType {
4141
/**
4242
* Validates the IP addresses in a group of {@link Settings} by looking for the keys
4343
* "_ip", "_host_ip", and "_publish_ip" and ensuring each of their comma separated values
44-
* is a valid IP address.
44+
* that has no wildcards is a valid IP address.
4545
*/
4646
public static final Consumer<Settings> IP_VALIDATOR = (settings) -> {
4747
Map<String, String> settingsMap = settings.getAsMap();
@@ -52,7 +52,7 @@ public enum OpType {
5252
}
5353
if ("_ip".equals(propertyKey) || "_host_ip".equals(propertyKey) || "_publish_ip".equals(propertyKey)) {
5454
for (String value : Strings.tokenizeToStringArray(entry.getValue(), ",")) {
55-
if (InetAddresses.isInetAddress(value) == false) {
55+
if (Regex.isSimpleMatchPattern(value) == false && InetAddresses.isInetAddress(value) == false) {
5656
throw new IllegalArgumentException("invalid IP address [" + value + "] for [" + propertyKey + "]");
5757
}
5858
}

core/src/test/java/org/elasticsearch/cluster/node/DiscoveryNodeFiltersTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ public void testIpPublishFilteringNotMatchingOr() {
245245
assertThat(filters.match(node), equalTo(true));
246246
}
247247

248+
public void testIpPublishFilteringMatchingWildcard() {
249+
boolean matches = randomBoolean();
250+
Settings settings = shuffleSettings(Settings.builder()
251+
.put("xxx._publish_ip", matches ? "192.1.*" : "192.2.*")
252+
.build());
253+
DiscoveryNodeFilters filters = DiscoveryNodeFilters.buildFromSettings(OR, "xxx.", settings);
254+
255+
DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, emptyMap(), emptySet(), null);
256+
assertThat(filters.match(node), equalTo(matches));
257+
}
258+
248259
public void testCommaSeparatedValuesTrimmed() {
249260
DiscoveryNode node = new DiscoveryNode("", "", "", "", "192.1.1.54", localAddress, singletonMap("tag", "B"), emptySet(), null);
250261

core/src/test/java/org/elasticsearch/cluster/routing/allocation/decider/FilterAllocationDeciderTests.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,22 @@ public void testInvalidIPFilter() {
185185
String ipKey = randomFrom("_ip", "_host_ip", "_publish_ip");
186186
Setting<Settings> filterSetting = randomFrom(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING,
187187
IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING, IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING);
188+
String invalidIP = randomFrom("192..168.1.1", "192.300.1.1");
188189
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
189190
IndexScopedSettings indexScopedSettings = new IndexScopedSettings(Settings.EMPTY, IndexScopedSettings.BUILT_IN_INDEX_SETTINGS);
190-
indexScopedSettings.updateDynamicSettings(Settings.builder().put(filterSetting.getKey() + ipKey, "192..168.1.1").build(),
191+
indexScopedSettings.updateDynamicSettings(Settings.builder().put(filterSetting.getKey() + ipKey, invalidIP).build(),
191192
Settings.builder().put(Settings.EMPTY), Settings.builder(), "test ip validation");
192193
});
193-
assertEquals("invalid IP address [192..168.1.1] for [" + ipKey + "]", e.getMessage());
194+
assertEquals("invalid IP address [" + invalidIP + "] for [" + ipKey + "]", e.getMessage());
195+
}
196+
197+
public void testWildcardIPFilter() {
198+
String ipKey = randomFrom("_ip", "_host_ip", "_publish_ip");
199+
Setting<Settings> filterSetting = randomFrom(IndexMetaData.INDEX_ROUTING_REQUIRE_GROUP_SETTING,
200+
IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING, IndexMetaData.INDEX_ROUTING_EXCLUDE_GROUP_SETTING);
201+
String wildcardIP = randomFrom("192.168.*", "192.*.1.1");
202+
IndexScopedSettings indexScopedSettings = new IndexScopedSettings(Settings.EMPTY, IndexScopedSettings.BUILT_IN_INDEX_SETTINGS);
203+
indexScopedSettings.updateDynamicSettings(Settings.builder().put(filterSetting.getKey() + ipKey, wildcardIP).build(),
204+
Settings.builder().put(Settings.EMPTY), Settings.builder(), "test ip validation");
194205
}
195206
}

0 commit comments

Comments
 (0)