-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Enforce limitations on ILM policy names #35104
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
Merged
gwbrown
merged 19 commits into
elastic:master
from
gwbrown:ilm/enforce-policy-name-restrictions
Nov 9, 2018
Merged
Changes from 6 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
8370a0c
Enforce index-name rules for ILM policy names
gwbrown 782379d
Missed one "index"->"policy"
gwbrown 27ce24b
Add tests for valid policy names
gwbrown 4e3d989
Refactor name checks into a separate class
gwbrown d8cd5f7
Merge branch 'index-lifecycle' into ilm/enforce-policy-name-restrictions
gwbrown 18276bf
A bit of extra cleanup that I missed
gwbrown ebd551d
More cleanup
gwbrown 92db083
Back out changes to shared classes
gwbrown a95d940
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown ea7db68
Switch to looser set of name restrictions
gwbrown 1d5f20d
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown 246328f
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown 97e5701
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown bc8b1ed
Use UTF-8 Charset instead of "UTF-8"
gwbrown b2be88a
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown a0f7a92
Restrict spaces in policy names
gwbrown 6c58f5e
Fix error message with leftover constraints
gwbrown 0ff4c4f
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown a941312
Merge branch 'master' into ilm/enforce-policy-name-restrictions
gwbrown File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
65
server/src/test/java/org/elasticsearch/common/NamesTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.