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 @@ -67,27 +67,26 @@ private HddsClientUtils() {
.add(NotReplicatedException.class)
.build();

private static void doNameChecks(String resName) {
private static void doNameChecks(String resName, String resType) {
if (resName == null) {
throw new IllegalArgumentException("Bucket or Volume name is null");
throw new IllegalArgumentException(resType + " name is null");
}

if (resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH ||
resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH) {
throw new IllegalArgumentException(
"Bucket or Volume length is illegal, "
+ "valid length is 3-63 characters");
throw new IllegalArgumentException(resType +
" length is illegal, " + "valid length is 3-63 characters");
}

if (resName.charAt(0) == '.' || resName.charAt(0) == '-') {
throw new IllegalArgumentException(
"Bucket or Volume name cannot start with a period or dash");
throw new IllegalArgumentException(resType +
" name cannot start with a period or dash");
}

if (resName.charAt(resName.length() - 1) == '.' ||
resName.charAt(resName.length() - 1) == '-') {
throw new IllegalArgumentException("Bucket or Volume name "
+ "cannot end with a period or dash");
throw new IllegalArgumentException(resType +
" name cannot end with a period or dash");
}
}

Expand All @@ -108,27 +107,27 @@ private static boolean isSupportedCharacter(char c, boolean isStrictS3) {
return false;
}

private static void doCharacterChecks(char currChar, char prev,
private static void doCharacterChecks(char currChar, char prev, String resType,
boolean isStrictS3) {
if (Character.isUpperCase(currChar)) {
throw new IllegalArgumentException(
"Bucket or Volume name does not support uppercase characters");
throw new IllegalArgumentException(resType +
" name does not support uppercase characters");
}
if (!isSupportedCharacter(currChar, isStrictS3)) {
throw new IllegalArgumentException("Bucket or Volume name has an " +
"unsupported character : " + currChar);
throw new IllegalArgumentException(resType +
" name has an unsupported character : " + currChar);
}
if (prev == '.' && currChar == '.') {
throw new IllegalArgumentException("Bucket or Volume name should not " +
"have two contiguous periods");
throw new IllegalArgumentException(resType +
" name should not have two contiguous periods");
}
if (prev == '-' && currChar == '.') {
throw new IllegalArgumentException(
"Bucket or Volume name should not have period after dash");
throw new IllegalArgumentException(resType +
" name should not have period after dash");
}
if (prev == '.' && currChar == '-') {
throw new IllegalArgumentException(
"Bucket or Volume name should not have dash after period");
throw new IllegalArgumentException(resType +
" name should not have dash after period");
}
}

Expand All @@ -140,7 +139,11 @@ private static void doCharacterChecks(char currChar, char prev,
* @throws IllegalArgumentException
*/
public static void verifyResourceName(String resName) {
verifyResourceName(resName, true);
verifyResourceName(resName, "resource", true);
}

public static void verifyResourceName(String resName, String resType) {
verifyResourceName(resName, resType, true);
}

/**
Expand All @@ -150,9 +153,9 @@ public static void verifyResourceName(String resName) {
*
* @throws IllegalArgumentException
*/
public static void verifyResourceName(String resName, boolean isStrictS3) {
public static void verifyResourceName(String resName, String resType, boolean isStrictS3) {

doNameChecks(resName);
doNameChecks(resName, resType);

boolean isIPv4 = true;
char prev = (char) 0;
Expand All @@ -162,13 +165,13 @@ public static void verifyResourceName(String resName, boolean isStrictS3) {
if (currChar != '.') {
isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4;
}
doCharacterChecks(currChar, prev, isStrictS3);
doCharacterChecks(currChar, prev, resType, isStrictS3);
prev = currChar;
}

if (isIPv4) {
throw new IllegalArgumentException(
"Bucket or Volume name cannot be an IPv4 address or all numeric");
throw new IllegalArgumentException(resType +
" name cannot be an IPv4 address or all numeric");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ public void createBucket(

private static void verifyVolumeName(String volumeName) throws OMException {
try {
HddsClientUtils.verifyResourceName(volumeName, false);
HddsClientUtils.verifyResourceName(volumeName, "volume", false);
} catch (IllegalArgumentException e) {
throw new OMException(e.getMessage(),
OMException.ResultCodes.INVALID_VOLUME_NAME);
Expand All @@ -718,7 +718,7 @@ private static void verifyVolumeName(String volumeName) throws OMException {

private static void verifyBucketName(String bucketName) throws OMException {
try {
HddsClientUtils.verifyResourceName(bucketName, false);
HddsClientUtils.verifyResourceName(bucketName, "bucket", false);
} catch (IllegalArgumentException e) {
throw new OMException(e.getMessage(),
OMException.ResultCodes.INVALID_BUCKET_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ public static RepeatedOmKeyInfo prepareKeyForDelete(OmKeyInfo keyInfo,
public static void validateVolumeName(String volumeName, boolean isStrictS3)
throws OMException {
try {
HddsClientUtils.verifyResourceName(volumeName, isStrictS3);
HddsClientUtils.verifyResourceName(volumeName, "volume", isStrictS3);
} catch (IllegalArgumentException e) {
throw new OMException("Invalid volume name: " + volumeName,
OMException.ResultCodes.INVALID_VOLUME_NAME);
Expand All @@ -535,7 +535,7 @@ public static void validateVolumeName(String volumeName, boolean isStrictS3)
public static void validateBucketName(String bucketName, boolean isStrictS3)
throws OMException {
try {
HddsClientUtils.verifyResourceName(bucketName, isStrictS3);
HddsClientUtils.verifyResourceName(bucketName, "bucket", isStrictS3);
} catch (IllegalArgumentException e) {
throw new OMException("Invalid bucket name: " + bucketName,
OMException.ResultCodes.INVALID_BUCKET_NAME);
Expand Down Expand Up @@ -571,9 +571,9 @@ public static void validateSnapshotName(String snapshotName)
return;
}
try {
HddsClientUtils.verifyResourceName(snapshotName);
HddsClientUtils.verifyResourceName(snapshotName, "snapshot");
} catch (IllegalArgumentException e) {
throw new OMException("Invalid snapshot name: " + snapshotName,
throw new OMException("Invalid snapshot name: " + snapshotName + "\n" + e.getMessage(),
OMException.ResultCodes.INVALID_SNAPSHOT_ERROR);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,7 @@ public void testInvalidBucketCreation() throws Exception {
OzoneVolume volume = store.getVolume(volumeName);
OMException omException = assertThrows(OMException.class,
() -> volume.createBucket(bucketName));
assertEquals("Bucket or Volume name has an unsupported character : #",
assertEquals("bucket name has an unsupported character : #",
omException.getMessage());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public void testPreExecuteFailure(String snapshotName) {
bucketName, snapshotName);
OMException omException =
assertThrows(OMException.class, () -> doPreExecute(omRequest));
assertEquals("Invalid snapshot name: " + snapshotName,
omException.getMessage());
assertTrue(omException.getMessage()
.contains("Invalid snapshot name: " + snapshotName));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ public void testPreExecuteFailure(String deleteSnapshotName) {
bucketName, deleteSnapshotName);
OMException omException =
assertThrows(OMException.class, () -> doPreExecute(omRequest));
assertEquals("Invalid snapshot name: " + deleteSnapshotName,
omException.getMessage());
assertTrue(omException.getMessage()
.contains("Invalid snapshot name: " + deleteSnapshotName));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -172,8 +173,7 @@ public void testPreExecuteFailure(String toSnapshotName) {
bucketName, currentSnapshotName, toSnapshotName);
OMException omException =
assertThrows(OMException.class, () -> doPreExecute(omRequest));
assertEquals("Invalid snapshot name: " + toSnapshotName,
omException.getMessage());
assertTrue(omException.getMessage().contains("Invalid snapshot name: " + toSnapshotName));
}

@Test
Expand Down