Skip to content
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

HDDS-10133. Add a method to check key name in OMKeyRequest #6012

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -85,6 +86,7 @@

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.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyLong;
Expand Down Expand Up @@ -322,4 +324,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();
assertTrue("tmpSnapshotReservedWord".equals(validateKeyArgs1.getSnapshotReservedWord()));
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be better to use assertEquals, assertFalse, and assertThat. Can you please change them accordingly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks @hemantk-12 for the comment and review.
I have updated it.

assertTrue("tmpKey".equals(validateKeyArgs1.getKeyName()));
assertTrue(validateKeyArgs1.isValidateKeyName());
assertTrue(validateKeyArgs1.isValidateSnapshotReserved());

OMKeyRequest.ValidateKeyArgs validateKeyArgs2 = new OMKeyRequest.ValidateKeyArgs.Builder()
.setKeyName("tmpKey2").build();
assertTrue(null == validateKeyArgs2.getSnapshotReservedWord());
assertTrue("tmpKey2".equals(validateKeyArgs2.getKeyName()));
assertTrue(validateKeyArgs2.isValidateKeyName());
assertTrue(!validateKeyArgs2.isValidateSnapshotReserved());
}

}