Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception if index is null or missing when creating an alias #8240

Closed
wants to merge 8 commits into from
2 changes: 1 addition & 1 deletion docs/reference/indices/aliases.asciidoc
Expand Up @@ -185,7 +185,7 @@ An alias can also be added with the endpoint
where

[horizontal]
`index`:: The index the alias refers to. Can be any of `blank | * | _all | glob pattern | name1, name2, …`
`index`:: The index the alias refers to. Can be any of `* | _all | glob pattern | name1, name2, …`
`name`:: The name of the alias. This is a required option.
`routing`:: An optional routing that can be associated with an alias.
`filter`:: An optional filter that can be associated with an alias.
Expand Down
13 changes: 6 additions & 7 deletions rest-api-spec/test/indices.delete_alias/all_path_options.yaml
Expand Up @@ -16,11 +16,17 @@ setup:
- do:
indices.put_alias:
name: alias1
index:
- test_index1
- foo
body:
routing: "routing value"
- do:
indices.put_alias:
name: alias2
index:
- test_index2
- foo
body:
routing: "routing value"

Expand All @@ -31,14 +37,12 @@ setup:
name: alias1

- match: {test_index1.aliases.alias1.search_routing: "routing value"}
- match: {test_index2.aliases.alias1.search_routing: "routing value"}
- match: {foo.aliases.alias1.search_routing: "routing value"}

- do:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand All @@ -57,7 +61,6 @@ setup:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand All @@ -76,7 +79,6 @@ setup:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand All @@ -99,7 +101,6 @@ setup:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand All @@ -122,7 +123,6 @@ setup:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand Down Expand Up @@ -192,7 +192,6 @@ setup:
indices.get_alias:
name: alias2

- match: {test_index1.aliases.alias2.search_routing: "routing value"}
- match: {test_index2.aliases.alias2.search_routing: "routing value"}
- match: {foo.aliases.alias2.search_routing: "routing value"}

Expand Down
8 changes: 1 addition & 7 deletions rest-api-spec/test/indices.put_alias/all_path_options.yaml
Expand Up @@ -106,16 +106,10 @@ setup:


- do:
catch: param
indices.put_alias:
name: alias

- do:
indices.get_alias:
name: alias

- match: {test_index1.aliases.alias: {}}
- match: {test_index2.aliases.alias: {}}
- match: {foo.aliases.alias: {}}

---
"put alias with missing name":
Expand Down
Expand Up @@ -294,10 +294,6 @@ public ActionRequestValidationException validate() {
+ "]: [alias] may not be empty string", validationException);
}
}
if (CollectionUtils.isEmpty(aliasAction.indices)) {
validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
+ "]: indices may not be empty", validationException);
}
}
if (!CollectionUtils.isEmpty(aliasAction.indices)) {
for (String index : aliasAction.indices) {
Expand All @@ -306,6 +302,9 @@ public ActionRequestValidationException validate() {
+ "]: [index] may not be empty string", validationException);
}
}
} else {
validationException = addValidationError("Alias action [" + aliasAction.actionType().name().toLowerCase(Locale.ENGLISH)
+ "]: Property [index] was either missing or null", validationException);
}
}
return validationException;
Expand Down
36 changes: 31 additions & 5 deletions src/test/java/org/elasticsearch/aliases/IndexAliasesTests.java
Expand Up @@ -744,9 +744,35 @@ public void testIndicesGetAliases() throws Exception {
assertThat(existsResponse.exists(), equalTo(false));
}

@Test(expected = IndexMissingException.class)
public void testAddAliasNullIndex() {
admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, "alias1")).get();
@Test
public void testAddAliasNullWithoutExistingIndices() {
try {
assertAcked(admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, "alias1")));
fail("create alias should have failed due to null index");
} catch (ElasticsearchIllegalArgumentException e) {
assertThat("Exception text does not contain \"Property [index] was either missing or null\"",
e.getMessage().contains("Property [index] was either missing or null"),
equalTo(true));
}

}

@Test
public void testAddAliasNullWithExistingIndices() throws Exception {
logger.info("--> creating index [test]");
createIndex("test");
ensureGreen();

logger.info("--> aliasing index [null] with [empty-alias]");

try {
assertAcked(admin().indices().prepareAliases().addAlias((String) null, "empty-alias"));
fail("create alias should have failed due to null index");
} catch (ElasticsearchIllegalArgumentException e) {
assertThat("Exception text does not contain \"Property [index] was either missing or null\"",
e.getMessage().contains("Property [index] was either missing or null"),
equalTo(true));
}
}

@Test(expected = ActionRequestValidationException.class)
Expand All @@ -771,7 +797,7 @@ public void testAddAliasNullAliasNullIndex() {
assertTrue("Should throw " + ActionRequestValidationException.class.getSimpleName(), false);
} catch (ActionRequestValidationException e) {
assertThat(e.validationErrors(), notNullValue());
assertThat(e.validationErrors().size(), equalTo(1));
assertThat(e.validationErrors().size(), equalTo(2));
}
}

Expand Down Expand Up @@ -928,7 +954,7 @@ public void testAddAliasWithFilterNoMapping() throws Exception {
.addAlias("test", "a", FilterBuilders.matchAllFilter()) // <-- no fail, b/c no field mentioned
.get();
}

private void checkAliases() {
GetAliasesResponse getAliasesResponse = admin().indices().prepareGetAliases("alias1").get();
assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(1));
Expand Down