Skip to content

Commit 2e64dbc

Browse files
committed
Forbid index names over 100 characters in length
Fixes #4417
1 parent d73a1ff commit 2e64dbc

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/main/java/org/elasticsearch/cluster/metadata/MetaDataCreateIndexService.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
3737
import org.elasticsearch.cluster.block.ClusterBlock;
3838
import org.elasticsearch.cluster.block.ClusterBlocks;
39-
import org.elasticsearch.cluster.metadata.IndexMetaData.Custom;
40-
import org.elasticsearch.cluster.metadata.IndexMetaData.State;
39+
import org.elasticsearch.cluster.metadata.IndexMetaData.*;
4140
import org.elasticsearch.cluster.node.DiscoveryNodes;
4241
import org.elasticsearch.cluster.routing.RoutingTable;
4342
import org.elasticsearch.cluster.routing.allocation.AllocationService;
@@ -72,6 +71,7 @@
7271
import java.io.File;
7372
import java.io.FileInputStream;
7473
import java.io.InputStreamReader;
74+
import java.io.UnsupportedEncodingException;
7575
import java.util.Comparator;
7676
import java.util.List;
7777
import java.util.Locale;
@@ -87,6 +87,8 @@
8787
*/
8888
public class MetaDataCreateIndexService extends AbstractComponent {
8989

90+
public final static int MAX_INDEX_NAME_BYTES = 100;
91+
9092
private final Environment environment;
9193
private final ThreadPool threadPool;
9294
private final ClusterService clusterService;
@@ -172,6 +174,18 @@ public void validateIndexName(String index, ClusterState state) throws Elasticse
172174
if (!index.toLowerCase(Locale.ROOT).equals(index)) {
173175
throw new InvalidIndexNameException(new Index(index), index, "must be lowercase");
174176
}
177+
int byteCount = 0;
178+
try {
179+
byteCount = index.getBytes("UTF-8").length;
180+
} catch (UnsupportedEncodingException e) {
181+
// UTF-8 should always be supported, but rethrow this if it is not for some reason
182+
throw new ElasticsearchException("Unable to determine length of index name", e);
183+
}
184+
if (byteCount > MAX_INDEX_NAME_BYTES) {
185+
throw new InvalidIndexNameException(new Index(index), index,
186+
"index name is too long, (" + byteCount +
187+
" > " + MAX_INDEX_NAME_BYTES + ")");
188+
}
175189
if (state.metaData().aliases().containsKey(index)) {
176190
throw new InvalidIndexNameException(new Index(index), index, "already exists as alias");
177191
}

src/test/java/org/elasticsearch/indexing/IndexActionTests.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
import org.elasticsearch.action.bulk.BulkResponse;
2222
import org.elasticsearch.action.index.IndexRequestBuilder;
2323
import org.elasticsearch.action.index.IndexResponse;
24+
import org.elasticsearch.cluster.metadata.MetaDataCreateIndexService;
2425
import org.elasticsearch.index.VersionType;
26+
import org.elasticsearch.indices.InvalidIndexNameException;
2527
import org.elasticsearch.test.ElasticsearchIntegrationTest;
2628
import org.elasticsearch.test.junit.annotations.TestLogging;
2729
import org.junit.Test;
2830

2931
import java.util.ArrayList;
3032
import java.util.List;
33+
import java.util.Locale;
3134
import java.util.Random;
3235
import java.util.concurrent.Callable;
3336
import java.util.concurrent.ExecutorService;
@@ -177,4 +180,39 @@ public void testCreateFlagWithBulk() {
177180
IndexResponse indexResponse = bulkResponse.getItems()[0].getResponse();
178181
assertTrue(indexResponse.isCreated());
179182
}
183+
184+
@Test
185+
public void testCreateIndexWithLongName() {
186+
int min = MetaDataCreateIndexService.MAX_INDEX_NAME_BYTES + 1;
187+
int max = MetaDataCreateIndexService.MAX_INDEX_NAME_BYTES * 2;
188+
try {
189+
createIndex(randomAsciiOfLengthBetween(min, max).toLowerCase(Locale.ROOT));
190+
fail("exception should have been thrown on too-long index name");
191+
} catch (InvalidIndexNameException e) {
192+
assertThat("exception contains message about index name too long: " + e.getMessage(),
193+
e.getMessage().contains("index name is too long,"), equalTo(true));
194+
}
195+
196+
try {
197+
client().prepareIndex(randomAsciiOfLengthBetween(min, max).toLowerCase(Locale.ROOT), "mytype").setSource("foo", "bar").get();
198+
fail("exception should have been thrown on too-long index name");
199+
} catch (InvalidIndexNameException e) {
200+
assertThat("exception contains message about index name too long: " + e.getMessage(),
201+
e.getMessage().contains("index name is too long,"), equalTo(true));
202+
}
203+
204+
try {
205+
// Catch chars that are more than a single byte
206+
client().prepareIndex(randomAsciiOfLength(MetaDataCreateIndexService.MAX_INDEX_NAME_BYTES -1).toLowerCase(Locale.ROOT) +
207+
"Ϟ".toLowerCase(Locale.ROOT),
208+
"mytype").setSource("foo", "bar").get();
209+
fail("exception should have been thrown on too-long index name");
210+
} catch (InvalidIndexNameException e) {
211+
assertThat("exception contains message about index name too long: " + e.getMessage(),
212+
e.getMessage().contains("index name is too long,"), equalTo(true));
213+
}
214+
215+
// we can create an index of max length
216+
createIndex(randomAsciiOfLength(MetaDataCreateIndexService.MAX_INDEX_NAME_BYTES).toLowerCase(Locale.ROOT));
217+
}
180218
}

0 commit comments

Comments
 (0)