Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -17,10 +17,13 @@

package org.apache.hadoop.ozone.om.helpers;

import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.isValidKeyPath;
import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.normalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateAndNormalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validatePrefixLength;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateTagUniqAndLength;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateTrashPrefix;

import jakarta.annotation.Nullable;
import java.util.Collections;
import net.jcip.annotations.Immutable;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.hadoop.ozone.OzoneConsts;
Expand Down Expand Up @@ -59,12 +62,17 @@ private OmLCFilter(Builder builder) {

/**
* Validates the OmLCFilter.
* Ensures that only one of prefix, tag, or andOperator is set.
* You can specify an empty filter, in which case the rule applies to all objects in the bucket.
* Prefix can be "", in which case the rule applies to all objects in the bucket.
* Ref: <a href="https://docs.aws.amazon.com/AmazonS3/latest/userguide/intro-lifecycle-filters.html#filter-examples">...</a>
* If the validation fails, an OMException is thrown.
* - Only one of prefix, tag, or andOperator is set.
* - You can specify an empty filter, in which case the rule applies to all objects in the bucket.
* - Prefix can be "", in which case the rule applies to all objects in the bucket.
* - Prefix length must be a length between 0 and 1024.
* - Tag's key must be a length between 1 and 128.
* - Tag's value must be a length between 0 and 256.
* - For FSO bucket, the prefix must be normalized and valid path.
* - Prefix cannot be the Trash directory or any of its subdirectories.
*
* @param layout The bucket layout for validation
* @throws OMException if the filter is invalid.
*/
public void valid(BucketLayout layout) throws OMException {
Expand All @@ -78,17 +86,17 @@ public void valid(BucketLayout layout) throws OMException {
OMException.ResultCodes.INVALID_REQUEST);
}

if (hasPrefix) {
validatePrefixLength(prefix);
validateTrashPrefix(prefix);
}

if (hasTag()) {
validateTagUniqAndLength(Collections.singletonMap(tagKey, tagValue));
}

if (hasPrefix && layout == BucketLayout.FILE_SYSTEM_OPTIMIZED) {
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);
}
validateAndNormalizePrefix(prefix);
}

if (andOperator != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package org.apache.hadoop.ozone.om.helpers;

import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.isValidKeyPath;
import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.normalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateAndNormalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validatePrefixLength;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateTrashPrefix;

import jakarta.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -142,14 +143,17 @@ public boolean isTagEnable() {

/**
* Validates the lifecycle rule.
* - ID length should not exceed the allowed limit
Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

@ChenSammi ChenSammi Jan 13, 2026

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, updated

* - At least one action must be specified
* - Filter and Prefix cannot be used together
* - Filter and prefix cannot both be null
* - ID length should not exceed the allowed limit.
* - At least one action must be specified, and the expiration type Action can have at most one.
* - Filter and Prefix cannot be used together.
* - Filter and prefix cannot both be null.
* - Prefix can be "", in which case the rule applies to all objects in the bucket.
* - Actions must be valid
* - Filter must be valid
* - There must be at most one Expiration action per rule
* - Prefix length must be a length between 0 and 1024.
* - For FSO bucket, the prefix must be normalized and valid path.
* - Prefix cannot be the Trash directory or any of its subdirectories.
* - Actions must be valid.
* - Filter must be valid.
* - There must be at most one Expiration action per rule.
*
* @param bucketLayout The bucket layout for validation
* @param creationTime The creation time of the lifecycle configuration in milliseconds since epoch
Expand Down Expand Up @@ -189,17 +193,13 @@ public void valid(BucketLayout bucketLayout, Long creationTime) throws OMExcepti
OMException.ResultCodes.INVALID_REQUEST);
}

if (prefix != null) {
validatePrefixLength(prefix);
validateTrashPrefix(prefix);
}

if (prefix != null && bucketLayout == BucketLayout.FILE_SYSTEM_OPTIMIZED) {
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);
}
validateAndNormalizePrefix(prefix);
}

if (filter != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

package org.apache.hadoop.ozone.om.helpers;

import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.isValidKeyPath;
import static org.apache.hadoop.ozone.om.helpers.OzoneFSUtils.normalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateAndNormalizePrefix;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validatePrefixLength;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateTagUniqAndLength;
import static org.apache.hadoop.ozone.om.helpers.OmLifecycleUtils.validateTrashPrefix;

import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
Expand Down Expand Up @@ -77,7 +79,14 @@ public boolean isDirectoryStylePrefix() {
* - If there are tags and no prefix, the tags should be more than one.
* - Prefix can be "".
* - Prefix alone is not allowed.
* - Prefix length must be a length between 0 and 1024.
* - The key of a tag must be unique.
* - Tag's key must be a length between 1 and 128.
* - Tag's value must be a length between 0 and 256.
* - Prefix cannot be the Trash directory or any of its subdirectories.
* - For FSO bucket, the prefix must be normalized and valid path
*
* @param layout The bucket layout for validation
* @throws OMException if the validation fails.
*/
public void valid(BucketLayout layout) throws OMException {
Expand All @@ -102,17 +111,17 @@ public void valid(BucketLayout layout) throws OMException {
OMException.ResultCodes.INVALID_REQUEST);
}

if (hasTags) {
validateTagUniqAndLength(tags);
}

if (hasPrefix) {
validatePrefixLength(prefix);
validateTrashPrefix(prefix);
}

if (hasPrefix && layout == BucketLayout.FILE_SYSTEM_OPTIMIZED) {
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);
}
validateAndNormalizePrefix(prefix);
}
}

Expand Down
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);
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Copy Markdown
Contributor

@ChenSammi ChenSammi Jan 8, 2026

Choose a reason for hiding this comment

The 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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
Expand Down
Loading