Selaa lähdekoodia

HDDS-289. Volume and Bucket name should not contain a delimiter ('/'). Contributed by chencan.

Hanisha Koneru 6 vuotta sitten
vanhempi
commit
72891fc9be

+ 41 - 14
hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java

@@ -17,6 +17,7 @@
  */
 package org.apache.hadoop.ozone.ozShell;
 
+import com.google.common.base.Strings;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -31,7 +32,7 @@ import java.util.List;
 import java.util.Random;
 import java.util.UUID;
 import java.util.stream.Collectors;
-
+import org.apache.commons.lang3.RandomStringUtils;
 import org.apache.hadoop.fs.FileUtil;
 import org.apache.hadoop.hdds.cli.MissingSubcommandException;
 import org.apache.hadoop.hdds.client.ReplicationFactor;
@@ -60,17 +61,9 @@ import org.apache.hadoop.ozone.web.response.KeyInfo;
 import org.apache.hadoop.ozone.web.response.VolumeInfo;
 import org.apache.hadoop.ozone.web.utils.JsonUtils;
 import org.apache.hadoop.test.GenericTestUtils;
-
-import com.google.common.base.Strings;
-import org.apache.commons.lang3.RandomStringUtils;
-import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION;
 import org.junit.After;
 import org.junit.AfterClass;
 import org.junit.Assert;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Rule;
@@ -87,6 +80,12 @@ import picocli.CommandLine.ParameterException;
 import picocli.CommandLine.ParseResult;
 import picocli.CommandLine.RunLast;
 
+import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_REPLICATION;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
 /**
  * This test class specified for testing Ozone shell command.
  */
@@ -211,8 +210,7 @@ public class TestOzoneShell {
     testCreateVolume("/////", "Volume name is required " +
         "to create a volume");
     testCreateVolume("/////vol/123",
-        "Bucket or Volume name has " +
-            "an unsupported character : /");
+        "Invalid volume name. Delimiters (/) not allowed in volume name");
   }
 
   private void testCreateVolume(String volumeName, String errorMsg)
@@ -305,6 +303,12 @@ public class TestOzoneShell {
     assertTrue(output.contains("createdOn")
         && output.contains(OzoneConsts.OZONE_TIME_ZONE));
 
+    // test infoVolume with invalid volume name
+    args = new String[] {"volume", "info",
+        url + "/" + volumeName + "/invalid-name"};
+    executeWithError(shell, args, "Invalid volume name. " +
+        "Delimiters (/) not allowed in volume name");
+
     // get info for non-exist volume
     args = new String[] {"volume", "info", url + "/invalid-volume"};
     executeWithError(shell, args, "VOLUME_NOT_FOUND");
@@ -563,8 +567,13 @@ public class TestOzoneShell {
     // test create a bucket in a non-exist volume
     args = new String[] {"bucket", "create",
         url + "/invalid-volume/" + bucketName};
-
     executeWithError(shell, args, "Info Volume failed, error:VOLUME_NOT_FOUND");
+
+    // test createBucket with invalid bucket name
+    args = new String[] {"bucket", "create",
+        url + "/" + vol.getName() + "/" + bucketName + "/invalid-name"};
+    executeWithError(shell, args,
+        "Invalid bucket name. Delimiters (/) not allowed in bucket name");
   }
 
   @Test
@@ -618,6 +627,12 @@ public class TestOzoneShell {
     assertTrue(output.contains("createdOn")
         && output.contains(OzoneConsts.OZONE_TIME_ZONE));
 
+    // test infoBucket with invalid bucket name
+    args = new String[] {"bucket", "info",
+        url + "/" + vol.getName() + "/" + bucketName + "/invalid-name"};
+    executeWithError(shell, args,
+        "Invalid bucket name. Delimiters (/) not allowed in bucket name");
+
     // test get info from a non-exist bucket
     args = new String[] {"bucket", "info",
         url + "/" + vol.getName() + "/invalid-bucket" + bucketName};
@@ -688,8 +703,14 @@ public class TestOzoneShell {
       assertNotNull(bucket);
     }
 
-    // test -length option
+    // test listBucket with invalid volume name
     String[] args = new String[] {"bucket", "list",
+        url + "/" + vol.getName() + "/invalid-name"};
+    executeWithError(shell, args, "Invalid volume name. " +
+        "Delimiters (/) not allowed in volume name");
+
+    // test -length option
+    args = new String[] {"bucket", "list",
         url + "/" + vol.getName(), "--length", "100"};
     execute(shell, args);
     commandOutput = out.toString();
@@ -954,8 +975,14 @@ public class TestOzoneShell {
       keyOutputStream.close();
     }
 
-    // test -length option
+    // test listKey with invalid bucket name
     String[] args = new String[] {"key", "list",
+        url + "/" + volumeName + "/" + bucketName + "/invalid-name"};
+    executeWithError(shell, args, "Invalid bucket name. " +
+        "Delimiters (/) not allowed in bucket name");
+
+    // test -length option
+    args = new String[] {"key", "list",
         url + "/" + volumeName + "/" + bucketName, "--length", "100"};
     execute(shell, args);
     commandOutput = out.toString();

+ 10 - 3
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/CreateBucketHandler.java

@@ -50,9 +50,16 @@ public class CreateBucketHandler extends Handler {
 
     URI ozoneURI = verifyURI(uri);
     Path path = Paths.get(ozoneURI.getPath());
-    if (path.getNameCount() < 2) {
-      throw new OzoneClientException(
-          "volume and bucket name required in createBucket");
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 2) {
+      String errorMessage;
+      if (pathNameCount < 2) {
+        errorMessage = "volume and bucket name required in createBucket";
+      } else {
+        errorMessage = "Invalid bucket name. Delimiters (/) not allowed in " +
+            "bucket name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
     String volumeName = path.getName(0).toString();

+ 10 - 4
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/InfoBucketHandler.java

@@ -50,10 +50,16 @@ public class InfoBucketHandler extends Handler {
     String volumeName, bucketName;
     URI ozoneURI = verifyURI(uri);
     Path path = Paths.get(ozoneURI.getPath());
-
-    if (path.getNameCount() < 2) {
-      throw new OzoneClientException(
-          "volume and bucket name required in info Bucket");
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 2) {
+      String errorMessage;
+      if (pathNameCount < 2) {
+        errorMessage = "volume and bucket name required in infoBucket";
+      } else {
+        errorMessage = "Invalid bucket name. Delimiters (/) not allowed in " +
+            "bucket name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
     volumeName = path.getName(0).toString();

+ 10 - 2
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/bucket/ListBucketHandler.java

@@ -71,8 +71,16 @@ public class ListBucketHandler extends Handler {
 
     URI ozoneURI = verifyURI(uri);
     Path path = Paths.get(ozoneURI.getPath());
-    if (path.getNameCount() < 1) {
-      throw new OzoneClientException("volume is required in listBucket");
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 1) {
+      String errorMessage;
+      if (pathNameCount < 1) {
+        errorMessage = "volume is required in listBucket";
+      } else {
+        errorMessage = "Invalid volume name. Delimiters (/) not allowed in " +
+            "volume name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
     if (maxBuckets < 1) {

+ 10 - 3
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/keys/ListKeyHandler.java

@@ -71,9 +71,16 @@ public class ListKeyHandler extends Handler {
 
     URI ozoneURI = verifyURI(uri);
     Path path = Paths.get(ozoneURI.getPath());
-    if (path.getNameCount() < 2) {
-      throw new OzoneClientException(
-          "volume/bucket is required in listKey");
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 2) {
+      String errorMessage;
+      if (pathNameCount < 2) {
+        errorMessage = "volume/bucket is required in listKey";
+      } else {
+        errorMessage = "Invalid bucket name. Delimiters (/) not allowed in " +
+            "bucket name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
     if (maxKeys < 1) {

+ 14 - 7
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/CreateVolumeHandler.java

@@ -19,6 +19,8 @@
 package org.apache.hadoop.ozone.web.ozShell.volume;
 
 import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import org.apache.hadoop.ozone.client.OzoneClientException;
 import org.apache.hadoop.ozone.client.OzoneClientUtils;
@@ -64,15 +66,20 @@ public class CreateVolumeHandler extends Handler {
   public Void call() throws Exception {
 
     URI ozoneURI = verifyURI(uri);
-
-    // we need to skip the slash in the URI path
-    // getPath returns /volumeName needs to remove the initial slash.
-    String volumeName = ozoneURI.getPath().replaceAll("^/+", "");
-    if (volumeName.isEmpty()) {
-      throw new OzoneClientException(
-          "Volume name is required to create a volume");
+    Path path = Paths.get(ozoneURI.getPath());
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 1) {
+      String errorMessage;
+      if (pathNameCount < 1) {
+        errorMessage = "Volume name is required to create a volume";
+      } else {
+        errorMessage = "Invalid volume name. Delimiters (/) not allowed in " +
+            "volume name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
+    String volumeName = ozoneURI.getPath().replaceAll("^/+", "");
     if (isVerbose()) {
       System.out.printf("Volume name : %s%n", volumeName);
     }

+ 13 - 3
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/volume/InfoVolumeHandler.java

@@ -19,6 +19,8 @@
 package org.apache.hadoop.ozone.web.ozShell.volume;
 
 import java.net.URI;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 
 import org.apache.hadoop.ozone.client.OzoneClientException;
 import org.apache.hadoop.ozone.client.OzoneClientUtils;
@@ -47,9 +49,17 @@ public class InfoVolumeHandler extends Handler{
   public Void call() throws Exception {
 
     URI ozoneURI = verifyURI(uri);
-    if (ozoneURI.getPath().isEmpty()) {
-      throw new OzoneClientException(
-          "Volume name is required to get info of a volume");
+    Path path = Paths.get(ozoneURI.getPath());
+    int pathNameCount = path.getNameCount();
+    if (pathNameCount != 1) {
+      String errorMessage;
+      if (pathNameCount < 1) {
+        errorMessage = "Volume name is required to get info of a volume";
+      } else {
+        errorMessage = "Invalid volume name. Delimiters (/) not allowed in " +
+            "volume name";
+      }
+      throw new OzoneClientException(errorMessage);
     }
 
     // we need to skip the slash in the URI path