-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Remove potential NPE while resolving concrete indices from Meta…
…Data Prevents a current edge case resolving concrete aliases or index names in cluster MetaData that could potentialy lead to NullPointerException when the IndicesOptions don't allow wildcard expansion and the method is called with aliasesOrIndices argument null or emtpy list. This change adds a check for that and introduces randomized test that catches this. Closes #10342 Closes #10339
- Loading branch information
Christoph Büscher
committed
Apr 1, 2015
1 parent
7d08940
commit fc04967
Showing
2 changed files
with
139 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
package org.elasticsearch.cluster.metadata; | ||
|
||
import com.google.common.collect.Sets; | ||
|
||
import org.apache.tools.ant.taskdefs.condition.IsTrue; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
cbuescher
Member
|
||
import org.elasticsearch.ElasticsearchIllegalArgumentException; | ||
import org.elasticsearch.Version; | ||
import org.elasticsearch.action.support.IndicesOptions; | ||
|
@@ -107,9 +109,15 @@ public void testIndexOptions_strict() { | |
String[] results = md.concreteIndices(IndicesOptions.strictExpandOpen(), Strings.EMPTY_ARRAY); | ||
assertEquals(3, results.length); | ||
|
||
results = md.concreteIndices(IndicesOptions.strictExpandOpen(), null); | ||
assertEquals(3, results.length); | ||
|
||
results = md.concreteIndices(IndicesOptions.strictExpand(), Strings.EMPTY_ARRAY); | ||
assertEquals(4, results.length); | ||
|
||
results = md.concreteIndices(IndicesOptions.strictExpand(), null); | ||
assertEquals(4, results.length); | ||
|
||
results = md.concreteIndices(IndicesOptions.strictExpandOpen(), "foofoo*"); | ||
assertEquals(3, results.length); | ||
assertThat(results, arrayContainingInAnyOrder("foo", "foobar", "foofoo")); | ||
|
@@ -320,6 +328,12 @@ public void testIndexOptions_noExpandWildcards() { | |
results = md.concreteIndices(noExpandLenient, "foofoobar"); | ||
assertEquals(2, results.length); | ||
assertThat(results, arrayContainingInAnyOrder("foo", "foobar")); | ||
|
||
results = md.concreteIndices(noExpandLenient, null); | ||
assertEquals(0, results.length); | ||
|
||
results = md.concreteIndices(noExpandLenient, Strings.EMPTY_ARRAY); | ||
assertEquals(0, results.length); | ||
} | ||
|
||
//ignore unavailable but don't allow no indices | ||
|
@@ -478,7 +492,7 @@ public void testIndexOptions_emptyCluster() { | |
} | ||
|
||
@Test | ||
public void convertWildcardsJustIndicesTests() { | ||
public void testConvertWildcardsJustIndicesTests() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX")) | ||
.put(indexBuilder("testXYY")) | ||
|
@@ -494,7 +508,7 @@ public void convertWildcardsJustIndicesTests() { | |
} | ||
|
||
@Test | ||
public void convertWildcardsTests() { | ||
public void testConvertWildcardsTests() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX").putAlias(AliasMetaData.builder("alias1")).putAlias(AliasMetaData.builder("alias2"))) | ||
.put(indexBuilder("testXYY").putAlias(AliasMetaData.builder("alias2"))) | ||
|
@@ -509,7 +523,7 @@ public void convertWildcardsTests() { | |
} | ||
|
||
@Test | ||
public void convertWildcardsOpenClosedIndicesTests() { | ||
public void testConvertWildcardsOpenClosedIndicesTests() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX").state(State.OPEN)) | ||
.put(indexBuilder("testXXY").state(State.OPEN)) | ||
|
@@ -529,7 +543,7 @@ private IndexMetaData.Builder indexBuilder(String index) { | |
} | ||
|
||
@Test(expected = IndexMissingException.class) | ||
public void concreteIndicesIgnoreIndicesOneMissingIndex() { | ||
public void testConcreteIndicesIgnoreIndicesOneMissingIndex() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX")) | ||
.put(indexBuilder("kuku")); | ||
|
@@ -538,7 +552,7 @@ public void concreteIndicesIgnoreIndicesOneMissingIndex() { | |
} | ||
|
||
@Test | ||
public void concreteIndicesIgnoreIndicesOneMissingIndexOtherFound() { | ||
public void testConcreteIndicesIgnoreIndicesOneMissingIndexOtherFound() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX")) | ||
.put(indexBuilder("kuku")); | ||
|
@@ -547,7 +561,7 @@ public void concreteIndicesIgnoreIndicesOneMissingIndexOtherFound() { | |
} | ||
|
||
@Test(expected = IndexMissingException.class) | ||
public void concreteIndicesIgnoreIndicesAllMissing() { | ||
public void testConcreteIndicesIgnoreIndicesAllMissing() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX")) | ||
.put(indexBuilder("kuku")); | ||
|
@@ -556,7 +570,7 @@ public void concreteIndicesIgnoreIndicesAllMissing() { | |
} | ||
|
||
@Test | ||
public void concreteIndicesIgnoreIndicesEmptyRequest() { | ||
public void testConcreteIndicesIgnoreIndicesEmptyRequest() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX")) | ||
.put(indexBuilder("kuku")); | ||
|
@@ -565,7 +579,7 @@ public void concreteIndicesIgnoreIndicesEmptyRequest() { | |
} | ||
|
||
@Test | ||
public void concreteIndicesWildcardExpansion() { | ||
public void testConcreteIndicesWildcardExpansion() { | ||
MetaData.Builder mdBuilder = MetaData.builder() | ||
.put(indexBuilder("testXXX").state(State.OPEN)) | ||
.put(indexBuilder("testXXY").state(State.OPEN)) | ||
|
@@ -579,6 +593,113 @@ public void concreteIndicesWildcardExpansion() { | |
assertThat(newHashSet(md.concreteIndices(IndicesOptions.fromOptions(true, true, true, true), "testX*")), equalTo(newHashSet("testXXX", "testXXY", "testXYY"))); | ||
} | ||
|
||
/** | ||
* test resolving _all pattern (null, empty array or "_all") for random IndicesOptions | ||
*/ | ||
@Test | ||
public void testConcreteIndicesAllPatternRandom() { | ||
for (int i = 0; i < 10; i++) { | ||
String[] allIndices = null; | ||
switch (randomIntBetween(0, 2)) { | ||
case 0: | ||
break; | ||
case 1: | ||
allIndices = new String[0]; | ||
break; | ||
case 2: | ||
allIndices = new String[] { MetaData.ALL }; | ||
break; | ||
} | ||
|
||
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); | ||
MetaData metadata = MetaData.builder().build(); | ||
|
||
// with no indices, asking for all indices should return empty list or exception, depending on indices options | ||
if (indicesOptions.allowNoIndices()) { | ||
String[] concreteIndices = metadata.concreteIndices(indicesOptions, allIndices); | ||
assertThat(concreteIndices, notNullValue()); | ||
assertThat(concreteIndices.length, equalTo(0)); | ||
} else { | ||
checkCorrectException(metadata, indicesOptions, allIndices); | ||
} | ||
|
||
// with existing indices, asking for all indices should return all open/closed indices depending on options | ||
metadata = MetaData.builder() | ||
.put(indexBuilder("aaa").state(State.OPEN).putAlias(AliasMetaData.builder("aaa_alias1"))) | ||
.put(indexBuilder("bbb").state(State.OPEN).putAlias(AliasMetaData.builder("bbb_alias1"))) | ||
.put(indexBuilder("ccc").state(State.CLOSE).putAlias(AliasMetaData.builder("ccc_alias1"))) | ||
.build(); | ||
if (indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed() || indicesOptions.allowNoIndices()) { | ||
String[] concreteIndices = metadata.concreteIndices(indicesOptions, allIndices); | ||
assertThat(concreteIndices, notNullValue()); | ||
int expectedNumberOfIndices = 0; | ||
if (indicesOptions.expandWildcardsOpen()) { | ||
expectedNumberOfIndices += 2; | ||
} | ||
if (indicesOptions.expandWildcardsClosed()) { | ||
expectedNumberOfIndices += 1; | ||
} | ||
assertThat(concreteIndices.length, equalTo(expectedNumberOfIndices)); | ||
} else { | ||
checkCorrectException(metadata, indicesOptions, allIndices); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* check for correct exception type depending on indicesOptions and provided index name list | ||
*/ | ||
private void checkCorrectException(MetaData metadata, IndicesOptions indicesOptions, String[] allIndices) { | ||
// two different exception types possible | ||
if (!(indicesOptions.expandWildcardsOpen() || indicesOptions.expandWildcardsClosed()) | ||
&& (allIndices == null || allIndices.length == 0)) { | ||
try { | ||
metadata.concreteIndices(indicesOptions, allIndices); | ||
fail("no wildcard expansion and null or empty list argument should trigger ElasticsearchIllegalArgumentException"); | ||
} catch (ElasticsearchIllegalArgumentException e) { | ||
// expected | ||
} | ||
} else { | ||
try { | ||
metadata.concreteIndices(indicesOptions, allIndices); | ||
fail("wildcard expansion on should trigger IndexMissingException"); | ||
} catch (IndexMissingException e) { | ||
// expected | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* test resolving wildcard pattern that matches no index of alias for random IndicesOptions | ||
*/ | ||
@Test | ||
public void testConcreteIndicesWildcardNoMatch() { | ||
for (int i = 0; i < 10; i++) { | ||
IndicesOptions indicesOptions = IndicesOptions.fromOptions(randomBoolean(), randomBoolean(), randomBoolean(), randomBoolean()); | ||
MetaData metadata = MetaData.builder().build(); | ||
|
||
metadata = MetaData.builder() | ||
.put(indexBuilder("aaa").state(State.OPEN).putAlias(AliasMetaData.builder("aaa_alias1"))) | ||
.put(indexBuilder("bbb").state(State.OPEN).putAlias(AliasMetaData.builder("bbb_alias1"))) | ||
.put(indexBuilder("ccc").state(State.CLOSE).putAlias(AliasMetaData.builder("ccc_alias1"))) | ||
.build(); | ||
|
||
// asking for non existing wildcard pattern should return empty list or exception | ||
if (indicesOptions.allowNoIndices()) { | ||
String[] concreteIndices = metadata.concreteIndices(indicesOptions, "Foo*"); | ||
assertThat(concreteIndices, notNullValue()); | ||
assertThat(concreteIndices.length, equalTo(0)); | ||
} else { | ||
try { | ||
metadata.concreteIndices(indicesOptions, "Foo*"); | ||
fail("expecting exeption when result empty and allowNoIndicec=false"); | ||
} catch (IndexMissingException e) { | ||
// expected exception | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsAllIndices_null() throws Exception { | ||
assertThat(MetaData.isAllIndices(null), equalTo(true)); | ||
|
@@ -636,7 +757,6 @@ public void testIsExplicitAllIndices_normalIndexes() throws Exception { | |
|
||
@Test | ||
public void testIsExplicitAllIndices_wildcard() throws Exception { | ||
MetaData metaData = MetaData.builder().build(); | ||
assertThat(MetaData.isExplicitAllPattern(new String[]{"*"}), equalTo(false)); | ||
} | ||
|
||
|
@cbuescher I think this is the wrong import here? Should be hamcrest maybe?