Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.cluster.metadata;

import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.common.Names;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.component.AbstractComponent;
Expand Down Expand Up @@ -105,7 +106,7 @@ void validateAliasStandalone(String alias, String indexRouting) {
if (!Strings.hasText(alias)) {
throw new IllegalArgumentException("alias name is required");
}
MetaDataCreateIndexService.validateIndexOrAliasName(alias, InvalidAliasNameException::new);
Names.validateNameWithIndexNameRules(alias, InvalidAliasNameException::new);
if (indexRouting != null && indexRouting.indexOf(',') != -1) {
throw new IllegalArgumentException("alias [" + alias + "] has several index routing values associated with it");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.elasticsearch.cluster.routing.ShardRoutingState;
import org.elasticsearch.cluster.routing.allocation.AllocationService;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Names;
import org.elasticsearch.common.Priority;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.UUIDs;
Expand Down Expand Up @@ -145,7 +146,7 @@ public MetaDataCreateIndexService(
* Validate the name for an index against some static rules and a cluster state.
*/
public static void validateIndexName(String index, ClusterState state) {
validateIndexOrAliasName(index, InvalidIndexNameException::new);
Names.validateNameWithIndexNameRules(index, InvalidIndexNameException::new);
if (!index.toLowerCase(Locale.ROOT).equals(index)) {
throw new InvalidIndexNameException(index, "must be lowercase");
}
Expand Down
66 changes: 66 additions & 0 deletions server/src/main/java/org/elasticsearch/common/Names.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common;

import org.elasticsearch.ElasticsearchException;

import java.io.UnsupportedEncodingException;
import java.util.function.BiFunction;

/**
* A set of utilities for names.
*/
public class Names {

public static final int MAX_INDEX_NAME_BYTES = 255;

private Names() {}

/**
* Validates a name by the rules used for Index names. Used for some things that are not indexes for consistency.
*/
public static void validateNameWithIndexNameRules(String name, BiFunction<String, String, ? extends RuntimeException> exceptionCtor) {
if (!Strings.validFileName(name)) {
throw exceptionCtor.apply(name, "must not contain the following characters " + Strings.INVALID_FILENAME_CHARS);
}
if (name.contains("#")) {
throw exceptionCtor.apply(name, "must not contain '#'");
}
if (name.contains(":")) {
throw exceptionCtor.apply(name, "must not contain ':'");
}
if (name.charAt(0) == '_' || name.charAt(0) == '-' || name.charAt(0) == '+') {
throw exceptionCtor.apply(name, "must not start with '_', '-', or '+'");
}
int byteCount = 0;
try {
byteCount = name.getBytes("UTF-8").length;
} catch (UnsupportedEncodingException e) {
// UTF-8 should always be supported, but rethrow this if it is not for some reason
throw new ElasticsearchException("Unable to determine length of name", e);
}
if (byteCount > MAX_INDEX_NAME_BYTES) {
throw exceptionCtor.apply(name, "name is too long, (" + byteCount + " > " + MAX_INDEX_NAME_BYTES + ")");
}
if (name.equals(".") || name.equals("..")) {
throw exceptionCtor.apply(name, "must not be '.' or '..'");
}
}
}
65 changes: 65 additions & 0 deletions server/src/test/java/org/elasticsearch/common/NamesTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.common;

import org.elasticsearch.indices.InvalidAliasNameException;
import org.elasticsearch.indices.InvalidIndexNameException;
import org.elasticsearch.test.ESTestCase;
import org.junit.Before;

import java.util.function.BiFunction;

public class NamesTests extends ESTestCase {

private BiFunction<String, String, ? extends RuntimeException> exceptionCtor;

@Before
public void setExceptionCtor() {
exceptionCtor = randomFrom(InvalidIndexNameException::new,
InvalidAliasNameException::new,
(name, msg) -> new IllegalArgumentException(name + " is invalid: " + msg));
}

public void testValidateTopLevelName() {
for (Character badChar : Strings.INVALID_FILENAME_CHARS) {
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(0, 10) +
badChar + randomAlphaOfLengthBetween(0, 10), exceptionCtor));
}
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(0, 10) +
"#" + randomAlphaOfLengthBetween(0, 10), exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(0, 10) +
":" + randomAlphaOfLengthBetween(0, 10), exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules("_" + randomAlphaOfLengthBetween(1, 20), exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules("-" + randomAlphaOfLengthBetween(1, 20), exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules("+" + randomAlphaOfLengthBetween(1, 20), exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules(".", exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules("..", exceptionCtor));
expectThrows(RuntimeException.class, () -> Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(256, 1000), exceptionCtor));

Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(1, 10) + "_" + randomAlphaOfLengthBetween(0, 10), exceptionCtor);
Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(1, 10) + "-" + randomAlphaOfLengthBetween(0, 10), exceptionCtor);
Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(1, 10) + "+" + randomAlphaOfLengthBetween(0, 10), exceptionCtor);

Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(0, 10) + "." + randomAlphaOfLengthBetween(1, 10), exceptionCtor);
Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(0, 10) + ".." + randomAlphaOfLengthBetween(1, 10), exceptionCtor);

Names.validateNameWithIndexNameRules(randomAlphaOfLengthBetween(1, 255), exceptionCtor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,15 @@ public void testCreateIndexWithLongName() {
fail("exception should have been thrown on too-long index name");
} catch (InvalidIndexNameException e) {
assertThat("exception contains message about index name too long: " + e.getMessage(),
e.getMessage().contains("index name is too long,"), equalTo(true));
e.getMessage().contains("name is too long,"), equalTo(true));
}

try {
client().prepareIndex(randomAlphaOfLengthBetween(min, max).toLowerCase(Locale.ROOT), "mytype").setSource("foo", "bar").get();
fail("exception should have been thrown on too-long index name");
} catch (InvalidIndexNameException e) {
assertThat("exception contains message about index name too long: " + e.getMessage(),
e.getMessage().contains("index name is too long,"), equalTo(true));
e.getMessage().contains("name is too long,"), equalTo(true));
}

try {
Expand All @@ -217,7 +217,7 @@ public void testCreateIndexWithLongName() {
fail("exception should have been thrown on too-long index name");
} catch (InvalidIndexNameException e) {
assertThat("exception contains message about index name too long: " + e.getMessage(),
e.getMessage().contains("index name is too long,"), equalTo(true));
e.getMessage(), containsString("name is too long"));
}

// we can create an index of max length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -44,6 +46,7 @@

import static java.util.Collections.singletonMap;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.not;
Expand Down Expand Up @@ -365,6 +368,39 @@ public void testNonexistentPolicy() throws Exception {

}

public void testInvalidPolicyNames() throws UnsupportedEncodingException {
ResponseException ex;
for (Character badChar : Strings.INVALID_FILENAME_CHARS) {
policy = URLEncoder.encode(randomAlphaOfLengthBetween(0,10) + badChar + randomAlphaOfLengthBetween(0,10), "UTF-8");
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getCause().getMessage(), containsString("invalid policy name"));
}

policy = randomAlphaOfLengthBetween(0,10) + ":" + randomAlphaOfLengthBetween(0,10);
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));
policy = "_" + randomAlphaOfLengthBetween(1, 20);
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));
policy = "-" + randomAlphaOfLengthBetween(1, 20);
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));
policy = "+" + randomAlphaOfLengthBetween(1, 20);
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));

policy = ".";
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));
policy = "..";
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));

policy = randomAlphaOfLengthBetween(256, 1000);
ex = expectThrows(ResponseException.class, () -> createNewSingletonPolicy("delete", new DeleteAction()));
assertThat(ex.getMessage(), containsString("invalid policy name"));
}

private void createFullPolicy(TimeValue hotTime) throws IOException {
Map<String, LifecycleAction> warmActions = new HashMap<>();
warmActions.put(ForceMergeAction.NAME, new ForceMergeAction(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.metadata.MetaData;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.Names;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;
import org.elasticsearch.xpack.core.ClientHelper;
import org.elasticsearch.xpack.core.indexlifecycle.IndexLifecycleMetadata;
import org.elasticsearch.xpack.core.indexlifecycle.LifecyclePolicyMetadata;
import org.elasticsearch.xpack.core.indexlifecycle.OperationMode;
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction;
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Request;
import org.elasticsearch.xpack.core.indexlifecycle.action.PutLifecycleAction.Response;

import java.time.Instant;
import java.util.Locale;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Expand Down Expand Up @@ -66,6 +68,8 @@ protected void masterOperation(Request request, ClusterState state, ActionListen
Map<String, String> filteredHeaders = threadPool.getThreadContext().getHeaders().entrySet().stream()
.filter(e -> ClientHelper.SECURITY_HEADER_FILTERS.contains(e.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
Names.validateNameWithIndexNameRules(request.getPolicy().getName(),
(name, message) -> new IllegalArgumentException(String.format(Locale.ROOT, "invalid policy name [%s]: %s", name, message)));
clusterService.submitStateUpdateTask("put-lifecycle-" + request.getPolicy().getName(),
new AckedClusterStateUpdateTask<Response>(request, listener) {
@Override
Expand Down