HDDS-2137. HddsClientUtils and OzoneUtils have duplicate verifyResourceName()

Closes #1455
This commit is contained in:
Viraj Jasani 2019-09-18 16:43:24 +02:00 committed by Márton Elek
parent 087ed86bcc
commit 285ed0a849
No known key found for this signature in database
GPG Key ID: D51EA8F00EE79B28
2 changed files with 17 additions and 96 deletions

View File

@ -126,8 +126,6 @@ public static long formatDateTime(String date) throws ParseException {
.toInstant().toEpochMilli(); .toInstant().toEpochMilli();
} }
/** /**
* verifies that bucket name / volume name is a valid DNS name. * verifies that bucket name / volume name is a valid DNS name.
* *
@ -135,29 +133,25 @@ public static long formatDateTime(String date) throws ParseException {
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
public static void verifyResourceName(String resName) public static void verifyResourceName(String resName) throws IllegalArgumentException {
throws IllegalArgumentException {
if (resName == null) { if (resName == null) {
throw new IllegalArgumentException("Bucket or Volume name is null"); throw new IllegalArgumentException("Bucket or Volume name is null");
} }
if ((resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH) || if (resName.length() < OzoneConsts.OZONE_MIN_BUCKET_NAME_LENGTH ||
(resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH)) { resName.length() > OzoneConsts.OZONE_MAX_BUCKET_NAME_LENGTH) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Bucket or Volume length is illegal, " + "Bucket or Volume length is illegal, valid length is 3-63 characters");
"valid length is 3-63 characters");
} }
if ((resName.charAt(0) == '.') || (resName.charAt(0) == '-')) { if (resName.charAt(0) == '.' || resName.charAt(0) == '-') {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Bucket or Volume name cannot start with a period or dash"); "Bucket or Volume name cannot start with a period or dash");
} }
if ((resName.charAt(resName.length() - 1) == '.') || if (resName.charAt(resName.length() - 1) == '.' ||
(resName.charAt(resName.length() - 1) == '-')) { resName.charAt(resName.length() - 1) == '-') {
throw new IllegalArgumentException( throw new IllegalArgumentException("Bucket or Volume name cannot end with a period or dash");
"Bucket or Volume name cannot end with a period or dash");
} }
boolean isIPv4 = true; boolean isIPv4 = true;
@ -165,36 +159,30 @@ public static void verifyResourceName(String resName)
for (int index = 0; index < resName.length(); index++) { for (int index = 0; index < resName.length(); index++) {
char currChar = resName.charAt(index); char currChar = resName.charAt(index);
if (currChar != '.') { if (currChar != '.') {
isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4; isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4;
} }
if (currChar > 'A' && currChar < 'Z') { if (currChar > 'A' && currChar < 'Z') {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Bucket or Volume name does not support uppercase characters"); "Bucket or Volume name does not support uppercase characters");
} }
if (currChar != '.' && currChar != '-') {
if ((currChar != '.') && (currChar != '-')) { if (currChar < '0' || (currChar > '9' && currChar < 'a') ||
if ((currChar < '0') || (currChar > '9' && currChar < 'a') || currChar > 'z') {
(currChar > 'z')) {
throw new IllegalArgumentException("Bucket or Volume name has an " + throw new IllegalArgumentException("Bucket or Volume name has an " +
"unsupported character : " + "unsupported character : " +
currChar); currChar);
} }
} }
if (prev == '.' && currChar == '.') {
if ((prev == '.') && (currChar == '.')) {
throw new IllegalArgumentException("Bucket or Volume name should not " + throw new IllegalArgumentException("Bucket or Volume name should not " +
"have two contiguous periods"); "have two contiguous periods");
} }
if (prev == '-' && currChar == '.') {
if ((prev == '-') && (currChar == '.')) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Bucket or Volume name should not have period after dash"); "Bucket or Volume name should not have period after dash");
} }
if (prev == '.' && currChar == '-') {
if ((prev == '.') && (currChar == '-')) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"Bucket or Volume name should not have dash after period"); "Bucket or Volume name should not have dash after period");
} }

View File

@ -31,6 +31,7 @@
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.HddsUtils; import org.apache.hadoop.hdds.HddsUtils;
import org.apache.hadoop.hdds.scm.client.HddsClientUtils;
import org.apache.hadoop.ozone.OzoneConsts; import org.apache.hadoop.ozone.OzoneConsts;
import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
@ -144,76 +145,8 @@ public static boolean isOzoneEnabled(Configuration conf) {
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
*/ */
public static void verifyResourceName(String resName) public static void verifyResourceName(String resName) throws IllegalArgumentException {
throws IllegalArgumentException { HddsClientUtils.verifyResourceName(resName);
if (resName == null) {
throw new IllegalArgumentException("Bucket or Volume 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");
}
if ((resName.charAt(0) == '.') || (resName.charAt(0) == '-')) {
throw new IllegalArgumentException(
"Bucket or Volume 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");
}
boolean isIPv4 = true;
char prev = (char) 0;
for (int index = 0; index < resName.length(); index++) {
char currChar = resName.charAt(index);
if (currChar != '.') {
isIPv4 = ((currChar >= '0') && (currChar <= '9')) && isIPv4;
}
if (currChar > 'A' && currChar < 'Z') {
throw new IllegalArgumentException(
"Bucket or Volume name does not support uppercase characters");
}
if ((currChar != '.') && (currChar != '-')) {
if ((currChar < '0') || (currChar > '9' && currChar < 'a') ||
(currChar > 'z')) {
throw new IllegalArgumentException("Bucket or Volume name has an " +
"unsupported character : " +
currChar);
}
}
if ((prev == '.') && (currChar == '.')) {
throw new IllegalArgumentException("Bucket or Volume name should not " +
"have two contiguous periods");
}
if ((prev == '-') && (currChar == '.')) {
throw new IllegalArgumentException(
"Bucket or Volume name should not have period after dash");
}
if ((prev == '.') && (currChar == '-')) {
throw new IllegalArgumentException(
"Bucket or Volume name should not have dash after period");
}
prev = currChar;
}
if (isIPv4) {
throw new IllegalArgumentException(
"Bucket or Volume name cannot be an IPv4 address or all numeric");
}
} }
/** /**