diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java index 5719803b94c..4c0df91e1a6 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/ozone/OzoneConfigKeys.java @@ -568,11 +568,6 @@ public final class OzoneConfigKeys { "ozone.https.client.need-auth"; public static final boolean OZONE_CLIENT_HTTPS_NEED_AUTH_DEFAULT = false; - public static final String OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY = - "ozone.om.keyname.character.check.enabled"; - public static final boolean OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT = - false; - public static final int OZONE_INIT_DEFAULT_LAYOUT_VERSION_DEFAULT = -1; public static final String OZONE_CLIENT_KEY_PROVIDER_CACHE_EXPIRY = "ozone.client.key.provider.cache.expiry"; diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMDirectoryCreateRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMDirectoryCreateRequest.java index 802cfa54e60..0b938cb1e6e 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMDirectoryCreateRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMDirectoryCreateRequest.java @@ -32,7 +32,6 @@ import org.apache.hadoop.hdds.client.ReplicationConfig; import org.apache.hadoop.hdds.protocol.proto.HddsProtos; import org.apache.ratis.server.protocol.TermIndex; -import org.apache.hadoop.ozone.OmUtils; import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfoGroup; import org.apache.hadoop.ozone.om.helpers.OzoneFSUtils; import org.apache.hadoop.ozone.om.helpers.BucketLayout; @@ -117,8 +116,10 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { super.preExecute(ozoneManager).getCreateDirectoryRequest(); Preconditions.checkNotNull(createDirectoryRequest); - OmUtils.verifyKeyNameWithSnapshotReservedWord( - createDirectoryRequest.getKeyArgs().getKeyName()); + KeyArgs keyArgs = createDirectoryRequest.getKeyArgs(); + ValidateKeyArgs validateArgs = new ValidateKeyArgs.Builder() + .setSnapshotReservedWord(keyArgs.getKeyName()).build(); + validateKey(ozoneManager, validateArgs); KeyArgs.Builder newKeyArgs = createDirectoryRequest.getKeyArgs() .toBuilder().setModificationTime(Time.now()); diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMFileCreateRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMFileCreateRequest.java index c13af319c5c..5d1922cdefb 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMFileCreateRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/file/OMFileCreateRequest.java @@ -30,9 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.hdds.client.ReplicationConfig; import org.apache.ratis.server.protocol.TermIndex; -import org.apache.hadoop.ozone.OmUtils; import org.apache.hadoop.ozone.OzoneConsts; -import org.apache.hadoop.ozone.om.OMConfigKeys; import org.apache.hadoop.ozone.om.OzoneConfigUtil; import org.apache.hadoop.ozone.om.helpers.BucketLayout; import org.apache.hadoop.ozone.om.request.util.OmResponseUtil; @@ -93,16 +91,11 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { Preconditions.checkNotNull(createFileRequest); KeyArgs keyArgs = createFileRequest.getKeyArgs(); - - // Verify key name - OmUtils.verifyKeyNameWithSnapshotReservedWord(keyArgs.getKeyName()); - final boolean checkKeyNameEnabled = ozoneManager.getConfiguration() - .getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, - OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT); - if (checkKeyNameEnabled) { - OmUtils.validateKeyName(StringUtils.removeEnd(keyArgs.getKeyName(), - OzoneConsts.FS_FILE_COPYING_TEMP_SUFFIX)); - } + ValidateKeyArgs validateArgs = new ValidateKeyArgs.Builder() + .setSnapshotReservedWord(keyArgs.getKeyName()) + .setKeyName(StringUtils.removeEnd(keyArgs.getKeyName(), + OzoneConsts.FS_FILE_COPYING_TEMP_SUFFIX)).build(); + validateKey(ozoneManager, validateArgs); UserInfo userInfo = getUserInfo(); if (keyArgs.getKeyName().length() == 0) { diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCommitRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCommitRequest.java index 378e0cb12ce..87d126de98a 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCommitRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCommitRequest.java @@ -30,9 +30,7 @@ import org.apache.commons.lang3.StringUtils; import org.apache.hadoop.ozone.OzoneManagerVersion; import org.apache.ratis.server.protocol.TermIndex; -import org.apache.hadoop.ozone.OmUtils; import org.apache.hadoop.ozone.OzoneConsts; -import org.apache.hadoop.ozone.om.OMConfigKeys; import org.apache.hadoop.ozone.om.helpers.KeyValueUtil; import org.apache.hadoop.ozone.om.helpers.OmBucketInfo; import org.apache.hadoop.ozone.om.helpers.BucketLayout; @@ -100,14 +98,11 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { ozoneManager.checkFeatureEnabled(OzoneManagerVersion.ATOMIC_REWRITE_KEY); } - // Verify key name - final boolean checkKeyNameEnabled = ozoneManager.getConfiguration() - .getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, - OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT); - if (checkKeyNameEnabled) { - OmUtils.validateKeyName(StringUtils.removeEnd(keyArgs.getKeyName(), - OzoneConsts.FS_FILE_COPYING_TEMP_SUFFIX)); - } + ValidateKeyArgs validateArgs = new ValidateKeyArgs.Builder() + .setKeyName(StringUtils.removeEnd(keyArgs.getKeyName(), + OzoneConsts.FS_FILE_COPYING_TEMP_SUFFIX)).build(); + validateKey(ozoneManager, validateArgs); + boolean isHsync = commitKeyRequest.hasHsync() && commitKeyRequest.getHsync(); boolean isRecovery = commitKeyRequest.hasRecovery() && commitKeyRequest.getRecovery(); boolean enableHsync = OzoneFSUtils.canEnableHsync(ozoneManager.getConfiguration(), false); diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCreateRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCreateRequest.java index dee5bb0fe0e..6ee2f719800 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCreateRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyCreateRequest.java @@ -30,8 +30,6 @@ import org.apache.hadoop.hdds.client.ReplicationConfig; import org.apache.hadoop.ozone.OzoneManagerVersion; import org.apache.ratis.server.protocol.TermIndex; -import org.apache.hadoop.ozone.OmUtils; -import org.apache.hadoop.ozone.om.OMConfigKeys; import org.apache.hadoop.ozone.om.OzoneConfigUtil; import org.apache.hadoop.ozone.om.exceptions.OMException; import org.apache.hadoop.ozone.om.helpers.BucketLayout; @@ -98,14 +96,10 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { ozoneManager.checkFeatureEnabled(OzoneManagerVersion.ATOMIC_REWRITE_KEY); } - // Verify key name - OmUtils.verifyKeyNameWithSnapshotReservedWord(keyArgs.getKeyName()); - final boolean checkKeyNameEnabled = ozoneManager.getConfiguration() - .getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, - OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT); - if (checkKeyNameEnabled) { - OmUtils.validateKeyName(keyArgs.getKeyName()); - } + ValidateKeyArgs validateArgs = new ValidateKeyArgs.Builder() + .setSnapshotReservedWord(keyArgs.getKeyName()) + .setKeyName(keyArgs.getKeyName()).build(); + validateKey(ozoneManager, validateArgs); String keyPath = keyArgs.getKeyName(); keyPath = validateAndNormalizeKey(ozoneManager.getEnableFileSystemPaths(), diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRenameRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRenameRequest.java index 804e536d21f..35940f5a770 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRenameRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRenameRequest.java @@ -24,9 +24,7 @@ import com.google.common.base.Preconditions; import org.apache.ratis.server.protocol.TermIndex; -import org.apache.hadoop.ozone.OmUtils; import org.apache.hadoop.ozone.OzoneConsts; -import org.apache.hadoop.ozone.om.OMConfigKeys; import org.apache.hadoop.ozone.om.helpers.BucketLayout; import org.apache.hadoop.ozone.om.request.util.OmResponseUtil; import org.apache.hadoop.ozone.om.request.validation.RequestFeatureValidator; @@ -83,15 +81,10 @@ public OMRequest preExecute(OzoneManager ozoneManager) throws IOException { .getRenameKeyRequest(); Preconditions.checkNotNull(renameKeyRequest); - // Verify key name - final boolean checkKeyNameEnabled = ozoneManager.getConfiguration() - .getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, - OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT); - if (checkKeyNameEnabled) { - OmUtils.validateKeyName(renameKeyRequest.getToKeyName()); - } - KeyArgs renameKeyArgs = renameKeyRequest.getKeyArgs(); + ValidateKeyArgs validateArgs = new ValidateKeyArgs.Builder() + .setKeyName(renameKeyRequest.getToKeyName()).build(); + validateKey(ozoneManager, validateArgs); String srcKey = extractSrcKey(renameKeyArgs); String dstKey = extractDstKey(renameKeyRequest); diff --git a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java index 88c5ad91405..8eba6b2e928 100644 --- a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java +++ b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/key/OMKeyRequest.java @@ -47,6 +47,7 @@ import org.apache.hadoop.ozone.om.OMMetrics; import org.apache.hadoop.ozone.om.PrefixManager; import org.apache.hadoop.ozone.om.ResolvedBucket; +import org.apache.hadoop.ozone.om.OMConfigKeys; import org.apache.hadoop.ozone.om.helpers.BucketEncryptionKeyInfo; import org.apache.hadoop.ozone.om.helpers.BucketLayout; import org.apache.hadoop.ozone.om.helpers.KeyValueUtil; @@ -176,6 +177,80 @@ protected KeyArgs resolveBucketAndCheckOpenKeyAcls(KeyArgs keyArgs, return resolvedArgs; } + /** + * Define the parameters carried when verifying the Key. + */ + public static class ValidateKeyArgs { + private String snapshotReservedWord; + private String keyName; + private boolean validateSnapshotReserved; + private boolean validateKeyName; + + ValidateKeyArgs(String snapshotReservedWord, String keyName, + boolean validateSnapshotReserved, boolean validateKeyName) { + this.snapshotReservedWord = snapshotReservedWord; + this.keyName = keyName; + this.validateSnapshotReserved = validateSnapshotReserved; + this.validateKeyName = validateKeyName; + } + + public String getSnapshotReservedWord() { + return snapshotReservedWord; + } + + public String getKeyName() { + return keyName; + } + + public boolean isValidateSnapshotReserved() { + return validateSnapshotReserved; + } + + public boolean isValidateKeyName() { + return validateKeyName; + } + + /** + * Tools for building {@link ValidateKeyArgs}. + */ + public static class Builder { + private String snapshotReservedWord; + private String keyName; + private boolean validateSnapshotReserved; + private boolean validateKeyName; + + public Builder setSnapshotReservedWord(String snapshotReservedWord) { + this.snapshotReservedWord = snapshotReservedWord; + this.validateSnapshotReserved = true; + return this; + } + + public Builder setKeyName(String keyName) { + this.keyName = keyName; + this.validateKeyName = true; + return this; + } + + public ValidateKeyArgs build() { + return new ValidateKeyArgs(snapshotReservedWord, keyName, + validateSnapshotReserved, validateKeyName); + } + } + } + + protected void validateKey(OzoneManager ozoneManager, ValidateKeyArgs validateKeyArgs) + throws OMException { + if (validateKeyArgs.isValidateSnapshotReserved()) { + OmUtils.verifyKeyNameWithSnapshotReservedWord(validateKeyArgs.getSnapshotReservedWord()); + } + final boolean checkKeyNameEnabled = ozoneManager.getConfiguration() + .getBoolean(OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_KEY, + OMConfigKeys.OZONE_OM_KEYNAME_CHARACTER_CHECK_ENABLED_DEFAULT); + if (validateKeyArgs.isValidateKeyName() && checkKeyNameEnabled) { + OmUtils.validateKeyName(validateKeyArgs.getKeyName()); + } + } + /** * This methods avoids multiple rpc calls to SCM by allocating multiple blocks * in one rpc call. diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequest.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequest.java index 7af60c18d94..2701aa22db6 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequest.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequest.java @@ -97,6 +97,7 @@ public void setup() throws Exception { folder.toAbsolutePath().toString()); omMetadataManager = new OmMetadataManagerImpl(ozoneConfiguration, ozoneManager); + when(ozoneManager.getConfiguration()).thenReturn(ozoneConfiguration); when(ozoneManager.getMetrics()).thenReturn(omMetrics); when(ozoneManager.getMetadataManager()).thenReturn(omMetadataManager); AuditLogger auditLogger = mock(AuditLogger.class); diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequestWithFSO.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequestWithFSO.java index e0460ba81a9..795e6e3c534 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequestWithFSO.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/file/TestOMDirectoryCreateRequestWithFSO.java @@ -97,6 +97,7 @@ public void setup() throws Exception { OMRequestTestUtils.configureFSOptimizedPaths(ozoneConfiguration, true); omMetadataManager = new OmMetadataManagerImpl(ozoneConfiguration, ozoneManager); + when(ozoneManager.getConfiguration()).thenReturn(ozoneConfiguration); when(ozoneManager.getMetrics()).thenReturn(omMetrics); when(ozoneManager.getMetadataManager()).thenReturn(omMetadataManager); auditLogger = mock(AuditLogger.class); diff --git a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/key/TestOMKeyRequest.java b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/key/TestOMKeyRequest.java index e2219d5fcc1..c18e1ee7c3f 100644 --- a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/key/TestOMKeyRequest.java +++ b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/request/key/TestOMKeyRequest.java @@ -60,6 +60,7 @@ import jakarta.annotation.Nonnull; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.apache.hadoop.hdds.client.ContainerBlockID; @@ -85,6 +86,10 @@ import static org.apache.hadoop.ozone.om.request.OMRequestTestUtils.setupReplicationConfigValidation; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyLong; @@ -322,4 +327,21 @@ protected SnapshotInfo createSnapshot(String snapshotName) throws Exception { return snapshotInfo; } + @Test + public void testValidateKeyArgs() { + OMKeyRequest.ValidateKeyArgs validateKeyArgs1 = new OMKeyRequest.ValidateKeyArgs.Builder() + .setKeyName("tmpKey").setSnapshotReservedWord("tmpSnapshotReservedWord").build(); + assertEquals("tmpSnapshotReservedWord", validateKeyArgs1.getSnapshotReservedWord()); + assertEquals("tmpKey", validateKeyArgs1.getKeyName()); + assertTrue(validateKeyArgs1.isValidateKeyName()); + assertTrue(validateKeyArgs1.isValidateSnapshotReserved()); + + OMKeyRequest.ValidateKeyArgs validateKeyArgs2 = new OMKeyRequest.ValidateKeyArgs.Builder() + .setKeyName("tmpKey2").build(); + assertNull(validateKeyArgs2.getSnapshotReservedWord()); + assertEquals("tmpKey2", validateKeyArgs2.getKeyName()); + assertTrue(validateKeyArgs2.isValidateKeyName()); + assertFalse(validateKeyArgs2.isValidateSnapshotReserved()); + } + }