HDFS-12525. Ozone: OzoneClient: Verify bucket/volume name in create calls. Contributed by Nandakumar.

This commit is contained in:
Yiqun Lin 2017-09-27 11:28:45 +08:00 committed by Owen O'Malley
parent 5e4a6b686c
commit 6b10723ec3
5 changed files with 109 additions and 69 deletions

View File

@ -50,6 +50,7 @@ public ObjectStore(ClientProtocol proxy) {
*/
public void createVolume(String volumeName) throws IOException {
Preconditions.checkNotNull(volumeName);
OzoneClientUtils.verifyResourceName(volumeName);
proxy.createVolume(volumeName);
}
@ -63,6 +64,7 @@ public void createVolume(String volumeName, VolumeArgs volumeArgs)
throws IOException {
Preconditions.checkNotNull(volumeName);
Preconditions.checkNotNull(volumeArgs);
OzoneClientUtils.verifyResourceName(volumeName);
proxy.createVolume(volumeName, volumeArgs);
}
@ -74,6 +76,7 @@ public void createVolume(String volumeName, VolumeArgs volumeArgs)
*/
public OzoneVolume getVolume(String volumeName) throws IOException {
Preconditions.checkNotNull(volumeName);
OzoneClientUtils.verifyResourceName(volumeName);
OzoneVolume volume = proxy.getVolumeDetails(volumeName);
volume.setClientProxy(proxy);
return volume;
@ -86,6 +89,7 @@ public OzoneVolume getVolume(String volumeName) throws IOException {
*/
public void deleteVolume(String volumeName) throws IOException {
Preconditions.checkNotNull(volumeName);
OzoneClientUtils.verifyResourceName(volumeName);
proxy.deleteVolume(volumeName);
}
}

View File

@ -28,6 +28,7 @@
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.ozone.OzoneConfigKeys;
import org.apache.hadoop.ozone.OzoneConfiguration;
import org.apache.hadoop.ozone.OzoneConsts;
import org.apache.hadoop.scm.ScmConfigKeys;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
@ -731,4 +732,83 @@ public static CloseableHttpClient newHttpClient(Configuration conf) {
.build();
return client;
}
/**
* verifies that bucket name / volume name is a valid DNS name.
*
* @param resName Bucket or volume Name to be validated
*
* @throws IllegalArgumentException
*/
public static void verifyResourceName(String resName)
throws IllegalArgumentException {
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");
}
}
}

View File

@ -148,6 +148,7 @@ public void createBucket(String bucketName)
throws IOException {
Preconditions.checkNotNull(proxy, "Client proxy is not set.");
Preconditions.checkNotNull(bucketName);
OzoneClientUtils.verifyResourceName(bucketName);
proxy.createBucket(name, bucketName);
}
@ -162,6 +163,7 @@ public void createBucket(String bucketName, BucketArgs bucketArgs)
Preconditions.checkNotNull(proxy, "Client proxy is not set.");
Preconditions.checkNotNull(bucketName);
Preconditions.checkNotNull(bucketArgs);
OzoneClientUtils.verifyResourceName(bucketName);
proxy.createBucket(name, bucketName, bucketArgs);
}
@ -174,6 +176,7 @@ public void createBucket(String bucketName, BucketArgs bucketArgs)
public OzoneBucket getBucket(String bucketName) throws IOException {
Preconditions.checkNotNull(proxy, "Client proxy is not set.");
Preconditions.checkNotNull(bucketName);
OzoneClientUtils.verifyResourceName(bucketName);
OzoneBucket bucket = proxy.getBucketDetails(name, bucketName);
bucket.setClientProxy(proxy);
return bucket;
@ -187,6 +190,7 @@ public OzoneBucket getBucket(String bucketName) throws IOException {
public void deleteBucket(String bucketName) throws IOException {
Preconditions.checkNotNull(proxy, "Client proxy is not set.");
Preconditions.checkNotNull(bucketName);
OzoneClientUtils.verifyResourceName(bucketName);
proxy.deleteBucket(name, bucketName);
}
}

View File

@ -24,6 +24,7 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.ozone.OzoneConfigKeys;
import org.apache.hadoop.ozone.OzoneConsts;
import org.apache.hadoop.ozone.client.OzoneClientUtils;
import org.apache.hadoop.ozone.client.io.LengthInputStream;
import org.apache.hadoop.ozone.web.exceptions.ErrorTable;
import org.apache.hadoop.ozone.web.exceptions.OzoneException;
@ -86,75 +87,7 @@ protected SimpleDateFormat initialValue() {
*/
public static void verifyResourceName(String resName)
throws IllegalArgumentException {
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");
}
OzoneClientUtils.verifyResourceName(resName);
}
/**

View File

@ -116,6 +116,14 @@ public void testCreateVolumeWithQuota()
Assert.assertEquals(1000000000L, volume.getQuota());
}
@Test
public void testInvalidVolumeCreation() throws IOException {
thrown.expectMessage("Bucket or Volume name has an unsupported" +
" character : #");
String volumeName = "invalid#name";
store.createVolume(volumeName);
}
@Test
public void testVolumeAlreadyExist()
throws IOException, OzoneException {
@ -247,6 +255,17 @@ public void testCreateBucketWithAllArgument()
Assert.assertTrue(bucket.getAcls().contains(userAcl));
}
@Test
public void testInvalidBucketCreation() throws IOException {
thrown.expectMessage("Bucket or Volume name has an unsupported" +
" character : #");
String volumeName = UUID.randomUUID().toString();
String bucketName = "invalid#bucket";
store.createVolume(volumeName);
OzoneVolume volume = store.getVolume(volumeName);
volume.createBucket(bucketName);
}
@Test
public void testAddBucketAcl()
throws IOException, OzoneException {