diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java index db06bbe78a31d8..083d4d6ff37ca8 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSUtilClient.java @@ -1118,7 +1118,12 @@ public static String getHomeDirectory(Configuration conf, HdfsClientConfigKeys.DFS_USER_HOME_DIR_PREFIX_KEY, HdfsClientConfigKeys.DFS_USER_HOME_DIR_PREFIX_DEFAULT); } - return userHomePrefix + Path.SEPARATOR + ugi.getShortUserName(); + String homeDirectory = + userHomePrefix + Path.SEPARATOR + ugi.getShortUserName(); + Preconditions.checkArgument(new Path(homeDirectory).isAbsolute(), + "Invalid value of %s: %s; must be an absolute path", + HdfsClientConfigKeys.DFS_USER_HOME_DIR_PREFIX_KEY, userHomePrefix); + return homeDirectory; } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java index 8dc895dc9b8e73..9b01f4aba23e54 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSUtil.java @@ -65,6 +65,7 @@ import org.apache.hadoop.fs.BlockLocation; import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.fs.CommonConfigurationKeysPublic; +import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys; import org.apache.hadoop.hdfs.protocol.DatanodeInfo; @@ -1117,6 +1118,34 @@ public void testFileIdPath() throws Throwable { } } + @Test + public void testUserHomeDirectoryPrefix() throws Exception { + UserGroupInformation ugi = + UserGroupInformation.createRemoteUser("test-user"); + Configuration conf = new Configuration(false); + + assertEquals("/user/test-user", + DFSUtilClient.getHomeDirectory(conf, ugi)); + + conf.set(HdfsClientConfigKeys.DFS_USER_HOME_DIR_PREFIX_KEY, "/home"); + assertEquals("/home/test-user", + DFSUtilClient.getHomeDirectory(conf, ugi)); + + conf.set(HdfsClientConfigKeys.DFS_USER_HOME_DIR_PREFIX_KEY, "@name@"); + IllegalArgumentException exception = assertThrows( + IllegalArgumentException.class, + () -> DFSUtilClient.getHomeDirectory(conf, ugi)); + assertEquals("Invalid value of dfs.user.home.dir.prefix: @name@; " + + "must be an absolute path", exception.getMessage()); + + conf.setClass("fs.hdfs.impl", DistributedFileSystem.class, + FileSystem.class); + exception = assertThrows(IllegalArgumentException.class, + () -> FileSystem.newInstance(new URI("hdfs://127.0.0.1:9000"), conf)); + assertEquals("Invalid value of dfs.user.home.dir.prefix: @name@; " + + "must be an absolute path", exception.getMessage()); + } + @Test public void testErrorMessageForInvalidNameservice() throws Exception { Configuration conf = new HdfsConfiguration();