diff --git a/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java b/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java index 633e6846958..19a47283e48 100644 --- a/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java +++ b/src/main/java/org/elasticsearch/action/admin/indices/alias/IndicesAliasesRequest.java @@ -24,6 +24,7 @@ import org.elasticsearch.ElasticSearchGenerationException; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.support.master.MasterNodeOperationRequest; import org.elasticsearch.cluster.metadata.AliasAction; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.unit.TimeValue; @@ -35,6 +36,7 @@ import org.elasticsearch.index.query.FilterBuilder; import java.io.IOException; import java.util.List; +import java.util.Locale; import java.util.Map; import static org.elasticsearch.action.ValidateActions.addValidationError; @@ -170,7 +172,15 @@ public class IndicesAliasesRequest extends MasterNodeOperationRequest indices = Maps.newHashMap(); try { for (AliasAction aliasAction : request.actions) { + if (!Strings.hasText(aliasAction.alias()) || !Strings.hasText(aliasAction.index())) { + listener.onFailure(new ElasticSearchIllegalArgumentException("Index name and alias name are required")); + return currentState; + } if (!currentState.metaData().hasIndex(aliasAction.index())) { listener.onFailure(new IndexMissingException(new Index(aliasAction.index()))); return currentState; diff --git a/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/RestIndicesAliasesAction.java b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/RestIndicesAliasesAction.java index dc6f775ceee..91728162e0b 100644 --- a/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/RestIndicesAliasesAction.java +++ b/src/main/java/org/elasticsearch/rest/action/admin/indices/alias/RestIndicesAliasesAction.java @@ -117,12 +117,7 @@ public class RestIndicesAliasesAction extends BaseRestHandler { } } } - if (index == null) { - throw new ElasticSearchIllegalArgumentException("Alias action [" + action + "] requires an [index] to be set"); - } - if (alias == null) { - throw new ElasticSearchIllegalArgumentException("Alias action [" + action + "] requires an [alias] to be set"); - } + if (type == AliasAction.Type.ADD) { AliasAction aliasAction = newAddAliasAction(index, alias).filter(filter); if (routingSet) { diff --git a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java index 7ef8095d36a..984437bf7b4 100644 --- a/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java +++ b/src/test/java/org/elasticsearch/test/integration/aliases/IndexAliasesTests.java @@ -19,6 +19,7 @@ package org.elasticsearch.test.integration.aliases; +import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse; import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus; import org.elasticsearch.action.admin.indices.alias.IndicesAliasesResponse; @@ -57,6 +58,7 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde import static org.elasticsearch.index.query.FilterBuilders.termFilter; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; +import static org.testng.AssertJUnit.assertTrue; import static org.testng.AssertJUnit.fail; /** @@ -795,6 +797,106 @@ public class IndexAliasesTests extends AbstractSharedClusterTest { assertThat(existsResponse.exists(), equalTo(false)); } + @Test(expectedExceptions = ActionRequestValidationException.class) + public void testAddAliasNullIndex() { + + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, "alias1")) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void testAddAliasEmptyIndex() { + + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("", "alias1")) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void testAddAliasNullAlias() { + + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("index1", null)) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void testAddAliasEmptyAlias() { + + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("index1", "")) + .execute().actionGet(); + } + + @Test + public void testAddAliasNullAliasNullIndex() { + try { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, null)) + .execute().actionGet(); + assertTrue("Should throw " + ActionRequestValidationException.class.getSimpleName(), false); + } catch(ActionRequestValidationException e) { + assertThat(e.validationErrors(), notNullValue()); + assertThat(e.validationErrors().size(), equalTo(2)); + } + } + + @Test + public void testAddAliasEmptyAliasEmptyIndex() { + try { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("", "")) + .execute().actionGet(); + assertTrue("Should throw " + ActionRequestValidationException.class.getSimpleName(), false); + } catch(ActionRequestValidationException e) { + assertThat(e.validationErrors(), notNullValue()); + assertThat(e.validationErrors().size(), equalTo(2)); + } + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void tesRemoveAliasNullIndex() { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newRemoveAliasAction(null, "alias1")) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void tesRemoveAliasEmptyIndex() { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newRemoveAliasAction("", "alias1")) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void tesRemoveAliasNullAlias() { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newRemoveAliasAction("index1", null)) + .execute().actionGet(); + } + + @Test(expectedExceptions = ActionRequestValidationException.class) + public void tesRemoveAliasEmptyAlias() { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newRemoveAliasAction("index1", "")) + .execute().actionGet(); + } + + @Test + public void testRemoveAliasNullAliasNullIndex() { + try { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction(null, null)) + .execute().actionGet(); + assertTrue("Should throw " + ActionRequestValidationException.class.getSimpleName(), false); + } catch(ActionRequestValidationException e) { + assertThat(e.validationErrors(), notNullValue()); + assertThat(e.validationErrors().size(), equalTo(2)); + } + } + + @Test + public void testRemoveAliasEmptyAliasEmptyIndex() { + try { + client().admin().indices().prepareAliases().addAliasAction(AliasAction.newAddAliasAction("", "")) + .execute().actionGet(); + assertTrue("Should throw " + ActionRequestValidationException.class.getSimpleName(), false); + } catch(ActionRequestValidationException e) { + assertThat(e.validationErrors(), notNullValue()); + assertThat(e.validationErrors().size(), equalTo(2)); + } + } + private void assertHits(SearchHits hits, String... ids) { assertThat(hits.totalHits(), equalTo((long) ids.length)); Set hitIds = newHashSet();