-
Notifications
You must be signed in to change notification settings - Fork 604
HDDS-12794. Improve Lifecycle rule validity checking #9593
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
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
131 changes: 131 additions & 0 deletions
131
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleUtils.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,131 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF 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.apache.hadoop.ozone.om.helpers; | ||
|
|
||
| import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER; | ||
| import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.isValidKeyPath; | ||
| import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.normalizePrefix; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.ozone.om.exceptions.OMException; | ||
|
|
||
| /** | ||
| * Utility class for Ozone Lifecycle. | ||
| */ | ||
| public final class OmLifecycleUtils { | ||
|
|
||
| // Ref: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-keys.html | ||
| public static final int MAX_PREFIX_LENGTH = 1024; | ||
|
|
||
| // Ref: https://docs.aws.amazon.com/AmazonS3/latest/userguide/object-tagging.html | ||
| public static final int MAX_TAG_KEY_LENGTH = 128; | ||
| public static final int MAX_TAG_VALUE_LENGTH = 256; | ||
|
|
||
| private OmLifecycleUtils() { | ||
| } | ||
|
|
||
| /** | ||
| * Check if the prefix is a Trash path. | ||
| * | ||
| * @param prefix the prefix to check | ||
| * @throws OMException if the prefix is a trash path | ||
| */ | ||
| public static void validateTrashPrefix(String prefix) throws OMException { | ||
| if (StringUtils.isEmpty(prefix)) { | ||
| return; | ||
| } | ||
| // Remove leading slash if present for validation | ||
| String p = prefix.startsWith(OZONE_URI_DELIMITER) ? prefix.substring(1) : prefix; | ||
|
|
||
| if (p.startsWith(FileSystem.TRASH_PREFIX + OZONE_URI_DELIMITER) || | ||
| p.equals(FileSystem.TRASH_PREFIX)) { | ||
| throw new OMException("Lifecycle rule prefix cannot be trash root " + | ||
| FileSystem.TRASH_PREFIX + OZONE_URI_DELIMITER, OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Normalize and validate the prefix for FILE_SYSTEM_OPTIMIZED layout. | ||
| * | ||
| * @param prefix the prefix to validate | ||
| * @throws OMException if the prefix is invalid | ||
| */ | ||
| public static void validateAndNormalizePrefix(String prefix) throws OMException { | ||
| String normalizedPrefix = normalizePrefix(prefix); | ||
| if (!normalizedPrefix.equals(prefix)) { | ||
| throw new OMException("Prefix format is not supported. Please use " + normalizedPrefix + | ||
| " instead of " + prefix + ".", OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
| try { | ||
| isValidKeyPath(normalizedPrefix); | ||
| } catch (OMException e) { | ||
| throw new OMException("Prefix is not a valid key path: " + prefix, OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate prefix length. | ||
| * | ||
| * @param prefix the prefix to validate | ||
| * @throws OMException if the prefix length exceeds 1024 | ||
| */ | ||
| public static void validatePrefixLength(String prefix) throws OMException { | ||
| if (prefix != null && prefix.getBytes(StandardCharsets.UTF_8).length > MAX_PREFIX_LENGTH) { | ||
| throw new OMException("The maximum size of a prefix is " + MAX_PREFIX_LENGTH, | ||
| OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Validate tag key, value length and the uniqueness of the key. | ||
| * | ||
| * @param tags the tags to validate | ||
| * @throws OMException if the tag key or value is invalid | ||
| */ | ||
| public static void validateTagUniqAndLength(Map<String, String> tags) throws OMException { | ||
| if (tags == null) { | ||
| return; | ||
| } | ||
| Set<String> keys = new HashSet<>(); | ||
| for (Map.Entry<String, String> entry : tags.entrySet()) { | ||
| String key = entry.getKey(); | ||
| String value = entry.getValue(); | ||
|
|
||
| if (StringUtils.isEmpty(key) || key.getBytes(StandardCharsets.UTF_8).length > MAX_TAG_KEY_LENGTH) { | ||
| throw new OMException("A Tag's Key must be a length between 1 and " + | ||
| MAX_TAG_KEY_LENGTH, OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
|
|
||
| if (!StringUtils.isEmpty(value) && value.getBytes(StandardCharsets.UTF_8).length > MAX_TAG_VALUE_LENGTH) { | ||
| throw new OMException("A Tag's Value must be a length between 0 and " + | ||
| MAX_TAG_VALUE_LENGTH, OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
|
|
||
| if (!keys.add(key)) { | ||
| throw new OMException("Duplicate Tag Keys are not allowed", | ||
| OMException.ResultCodes.INVALID_REQUEST); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
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 |
|---|---|---|
|
|
@@ -30,7 +30,9 @@ | |
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| import java.util.Collections; | ||
| import org.apache.commons.lang3.RandomStringUtils; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.ozone.om.exceptions.OMException; | ||
| import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.LifecycleFilter; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
@@ -90,6 +92,32 @@ public void testInValidFilter() { | |
|
|
||
| } | ||
|
|
||
| @Test | ||
| public void testFilterValidation() { | ||
| // 1. Prefix is Trash path | ||
| OmLCFilter.Builder trashPrefixFilter = getOmLCFilterBuilder(FileSystem.TRASH_PREFIX, null, null); | ||
| assertOMException(() -> trashPrefixFilter.build().valid(BucketLayout.DEFAULT), INVALID_REQUEST, | ||
| "Lifecycle rule prefix cannot be trash root"); | ||
|
|
||
| // 2. Prefix too long | ||
| String longPrefix = RandomStringUtils.randomAlphanumeric(1025); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can use (OmLifecycleUtils#MAX_PREFIX_LENGTH + 1 ) here and for below 1024 , so it will be very straightforward.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, more readable, updated |
||
| OmLCFilter.Builder longPrefixFilter = getOmLCFilterBuilder(longPrefix, null, null); | ||
| assertOMException(() -> longPrefixFilter.build().valid(BucketLayout.DEFAULT), INVALID_REQUEST, | ||
| "The maximum size of a prefix is 1024"); | ||
|
|
||
| // 3. Tag key too long | ||
| String longKey = RandomStringUtils.randomAlphanumeric(129); | ||
| OmLCFilter.Builder longKeyFilter = getOmLCFilterBuilder(null, Pair.of(longKey, "value"), null); | ||
| assertOMException(() -> longKeyFilter.build().valid(BucketLayout.DEFAULT), INVALID_REQUEST, | ||
| "A Tag's Key must be a length between 1 and 128"); | ||
|
|
||
| // 4. Tag value too long | ||
| String longValue = RandomStringUtils.randomAlphanumeric(257); | ||
| OmLCFilter.Builder longValueFilter = getOmLCFilterBuilder(null, Pair.of("key", longValue), null); | ||
| assertOMException(() -> longValueFilter.build().valid(BucketLayout.DEFAULT), INVALID_REQUEST, | ||
| "A Tag's Value must be a length between 0 and 256"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testProtobufConversion() throws OMException { | ||
| // Only prefix | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep these comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are currently multiple validation rules in place.
I'm concerned that if comments aren't properly maintained in the future, errors may occur. Therefore, it might be advisable to directly remove the information obtained from the method code.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that AWS will not change it's API spec and behavior frequently, so the validation logic is likely being stable. Besides, any future patch of this feature, likely at least one of us will help with the review, so we can guarantee that comments can get updated together with code change.
Let's keep these comments. It's very useful when I want to check that what's valid, and what's invalid of a configuration, than going through the code, as there are a few edge cases I would totally not sure about the conclusion without referring to these comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, updated