Skip to content

Commit

Permalink
Disallow creating indices starting with '-' or '+'
Browse files Browse the repository at this point in the history
Previously this was possible, which was problematic when issuing a
request like `DELETE /-myindex`, which was interpretted as "delete
everything except for myindex".

Resolves #19800
  • Loading branch information
dakrone committed Aug 17, 2016
1 parent 066afcf commit 6030acb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public MetaDataCreateIndexService(Settings settings, ClusterService clusterServi
this.activeShardsObserver = new ActiveShardsObserver(settings, clusterService, threadPool);
}

public void validateIndexName(String index, ClusterState state) {
public static void validateIndexName(String index, ClusterState state) {
if (state.routingTable().hasIndex(index)) {
throw new IndexAlreadyExistsException(state.routingTable().index(index).getIndex());
}
Expand All @@ -157,8 +157,8 @@ public void validateIndexName(String index, ClusterState state) {
if (index.contains("#")) {
throw new InvalidIndexNameException(index, "must not contain '#'");
}
if (index.charAt(0) == '_') {
throw new InvalidIndexNameException(index, "must not start with '_'");
if (index.charAt(0) == '_' || index.charAt(0) == '-' || index.charAt(0) == '+') {
throw new InvalidIndexNameException(index, "must not start with '_', '-', or '+'");
}
if (!index.toLowerCase(Locale.ROOT).equals(index)) {
throw new InvalidIndexNameException(index, "must be lowercase");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public ClusterState execute(ClusterState currentState) {
if (currentIndexMetaData == null) {
// Index doesn't exist - create it and start recovery
// Make sure that the index we are about to create has a validate name
createIndexService.validateIndexName(renamedIndexName, currentState);
MetaDataCreateIndexService.validateIndexName(renamedIndexName, currentState);
createIndexService.validateIndexSettings(renamedIndexName, snapshotIndexMetaData.getSettings());
IndexMetaData.Builder indexMdBuilder = IndexMetaData.builder(snapshotIndexMetaData).state(IndexMetaData.State.OPEN).index(renamedIndexName);
indexMdBuilder.settings(Settings.builder().put(snapshotIndexMetaData.getSettings()).put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ public void testValidateIndexName() throws Exception {

validateIndexName("index#name", "must not contain '#'");

validateIndexName("_indexname", "must not start with '_'");
validateIndexName("_indexname", "must not start with '_', '-', or '+'");
validateIndexName("-indexname", "must not start with '_', '-', or '+'");
validateIndexName("+indexname", "must not start with '_', '-', or '+'");

validateIndexName("INDEXNAME", "must be lowercase");

Expand All @@ -201,7 +203,7 @@ public void testValidateIndexName() throws Exception {

private void validateIndexName(String indexName, String errorMessage) {
InvalidIndexNameException e = expectThrows(InvalidIndexNameException.class,
() -> getCreateIndexService().validateIndexName(indexName, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING
() -> MetaDataCreateIndexService.validateIndexName(indexName, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING
.getDefault(Settings.EMPTY)).build()));
assertThat(e.getMessage(), endsWith(errorMessage));
}
Expand Down
8 changes: 8 additions & 0 deletions docs/reference/migration/migrate_5_0/index-apis.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ CPU usage can be obtained from `OsStats.Cpu#getPercent`.

Suggest stats exposed through `suggest` in indices stats has been merged
with `search` stats. `suggest` stats is exposed as part of `search` stats.

==== Creating indices starting with '-' or '+'

Elasticsearch no longer allows indices to be created started with '-' or '+', so
that the multi-index matching and expansion is not confused. It was previously
possible (but a really bad idea) to create indices starting with a hyphen or
plus sign. Any index already existing with these preceding characters will
continue to work normally.

0 comments on commit 6030acb

Please sign in to comment.