begin_unit|revision:0.9.5;language:Java;cregit-version:0.0.1
begin_comment
comment|/**  * Licensed to the Apache Software Foundation (ASF) under one  * or more contributor license agreements.  See the NOTICE file  * distributed with this work for additional information  * regarding copyright ownership.  The ASF licenses this file  * to you under the Apache License, Version 2.0 (the  * "License"); you may not use this file except in compliance  * with the License.  You may obtain a copy of the License at  *  *     http://www.apache.org/licenses/LICENSE-2.0  *  * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */
end_comment

begin_package
DECL|package|org.apache.hadoop.hdfs
package|package
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|hdfs
package|;
end_package

begin_import
import|import static
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|hdfs
operator|.
name|DFSConfigKeys
operator|.
name|*
import|;
end_import

begin_import
import|import static
name|org
operator|.
name|junit
operator|.
name|Assert
operator|.
name|*
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|conf
operator|.
name|Configuration
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|crypto
operator|.
name|key
operator|.
name|JavaKeyStoreProvider
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|CommonConfigurationKeysPublic
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FileSystem
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FileSystemTestHelper
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FileSystemTestWrapper
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|FsShell
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|Path
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|permission
operator|.
name|FsAction
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|fs
operator|.
name|permission
operator|.
name|FsPermission
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|hdfs
operator|.
name|client
operator|.
name|CreateEncryptionZoneFlag
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|hdfs
operator|.
name|client
operator|.
name|HdfsAdmin
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|hdfs
operator|.
name|server
operator|.
name|namenode
operator|.
name|EncryptionZoneManager
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|security
operator|.
name|UserGroupInformation
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|hadoop
operator|.
name|util
operator|.
name|ToolRunner
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|log4j
operator|.
name|Level
import|;
end_import

begin_import
import|import
name|org
operator|.
name|junit
operator|.
name|After
import|;
end_import

begin_import
import|import
name|org
operator|.
name|junit
operator|.
name|Before
import|;
end_import

begin_import
import|import
name|org
operator|.
name|junit
operator|.
name|Test
import|;
end_import

begin_import
import|import
name|java
operator|.
name|io
operator|.
name|File
import|;
end_import

begin_import
import|import
name|java
operator|.
name|security
operator|.
name|PrivilegedExceptionAction
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|EnumSet
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|concurrent
operator|.
name|atomic
operator|.
name|AtomicInteger
import|;
end_import

begin_comment
comment|/**  * This class tests Trash functionality in Encryption Zones.  */
end_comment

begin_class
DECL|class|TestTrashWithEncryptionZones
specifier|public
class|class
name|TestTrashWithEncryptionZones
block|{
DECL|field|conf
specifier|private
name|Configuration
name|conf
decl_stmt|;
DECL|field|fsHelper
specifier|private
name|FileSystemTestHelper
name|fsHelper
decl_stmt|;
DECL|field|cluster
specifier|private
name|MiniDFSCluster
name|cluster
decl_stmt|;
DECL|field|dfsAdmin
specifier|private
name|HdfsAdmin
name|dfsAdmin
decl_stmt|;
DECL|field|fs
specifier|private
name|DistributedFileSystem
name|fs
decl_stmt|;
DECL|field|testRootDir
specifier|private
name|File
name|testRootDir
decl_stmt|;
DECL|field|TEST_KEY
specifier|private
specifier|static
specifier|final
name|String
name|TEST_KEY
init|=
literal|"test_key"
decl_stmt|;
DECL|field|fsWrapper
specifier|private
name|FileSystemTestWrapper
name|fsWrapper
decl_stmt|;
DECL|field|clientConf
specifier|private
specifier|static
name|Configuration
name|clientConf
decl_stmt|;
DECL|field|shell
specifier|private
specifier|static
name|FsShell
name|shell
decl_stmt|;
DECL|field|zoneCounter
specifier|private
specifier|static
name|AtomicInteger
name|zoneCounter
init|=
operator|new
name|AtomicInteger
argument_list|(
literal|1
argument_list|)
decl_stmt|;
DECL|field|fileCounter
specifier|private
specifier|static
name|AtomicInteger
name|fileCounter
init|=
operator|new
name|AtomicInteger
argument_list|(
literal|1
argument_list|)
decl_stmt|;
DECL|field|LEN
specifier|private
specifier|static
specifier|final
name|int
name|LEN
init|=
literal|8192
decl_stmt|;
DECL|field|NO_TRASH
specifier|private
specifier|static
specifier|final
name|EnumSet
argument_list|<
name|CreateEncryptionZoneFlag
argument_list|>
name|NO_TRASH
init|=
name|EnumSet
operator|.
name|of
argument_list|(
name|CreateEncryptionZoneFlag
operator|.
name|NO_TRASH
argument_list|)
decl_stmt|;
DECL|field|PROVISION_TRASH
specifier|private
specifier|static
specifier|final
name|EnumSet
argument_list|<
name|CreateEncryptionZoneFlag
argument_list|>
name|PROVISION_TRASH
init|=
name|EnumSet
operator|.
name|of
argument_list|(
name|CreateEncryptionZoneFlag
operator|.
name|PROVISION_TRASH
argument_list|)
decl_stmt|;
DECL|method|getKeyProviderURI ()
specifier|private
name|String
name|getKeyProviderURI
parameter_list|()
block|{
return|return
name|JavaKeyStoreProvider
operator|.
name|SCHEME_NAME
operator|+
literal|"://file"
operator|+
operator|new
name|Path
argument_list|(
name|testRootDir
operator|.
name|toString
argument_list|()
argument_list|,
literal|"test.jks"
argument_list|)
operator|.
name|toUri
argument_list|()
return|;
block|}
annotation|@
name|Before
DECL|method|setup ()
specifier|public
name|void
name|setup
parameter_list|()
throws|throws
name|Exception
block|{
name|conf
operator|=
operator|new
name|HdfsConfiguration
argument_list|()
expr_stmt|;
name|fsHelper
operator|=
operator|new
name|FileSystemTestHelper
argument_list|()
expr_stmt|;
comment|// Set up java key store
name|String
name|testRoot
init|=
name|fsHelper
operator|.
name|getTestRootDir
argument_list|()
decl_stmt|;
name|testRootDir
operator|=
operator|new
name|File
argument_list|(
name|testRoot
argument_list|)
operator|.
name|getAbsoluteFile
argument_list|()
expr_stmt|;
name|conf
operator|.
name|set
argument_list|(
name|CommonConfigurationKeysPublic
operator|.
name|HADOOP_SECURITY_KEY_PROVIDER_PATH
argument_list|,
name|getKeyProviderURI
argument_list|()
argument_list|)
expr_stmt|;
name|conf
operator|.
name|setBoolean
argument_list|(
name|DFSConfigKeys
operator|.
name|DFS_NAMENODE_DELEGATION_TOKEN_ALWAYS_USE_KEY
argument_list|,
literal|true
argument_list|)
expr_stmt|;
comment|// Lower the batch size for testing
name|conf
operator|.
name|setInt
argument_list|(
name|DFSConfigKeys
operator|.
name|DFS_NAMENODE_LIST_ENCRYPTION_ZONES_NUM_RESPONSES
argument_list|,
literal|2
argument_list|)
expr_stmt|;
name|cluster
operator|=
operator|new
name|MiniDFSCluster
operator|.
name|Builder
argument_list|(
name|conf
argument_list|)
operator|.
name|numDataNodes
argument_list|(
literal|1
argument_list|)
operator|.
name|build
argument_list|()
expr_stmt|;
name|org
operator|.
name|apache
operator|.
name|log4j
operator|.
name|Logger
operator|.
name|getLogger
argument_list|(
name|EncryptionZoneManager
operator|.
name|class
argument_list|)
operator|.
name|setLevel
argument_list|(
name|Level
operator|.
name|TRACE
argument_list|)
expr_stmt|;
name|fs
operator|=
name|cluster
operator|.
name|getFileSystem
argument_list|()
expr_stmt|;
name|fsWrapper
operator|=
operator|new
name|FileSystemTestWrapper
argument_list|(
name|fs
argument_list|)
expr_stmt|;
name|dfsAdmin
operator|=
operator|new
name|HdfsAdmin
argument_list|(
name|cluster
operator|.
name|getURI
argument_list|()
argument_list|,
name|conf
argument_list|)
expr_stmt|;
name|setProvider
argument_list|()
expr_stmt|;
comment|// Create a test key
name|DFSTestUtil
operator|.
name|createKey
argument_list|(
name|TEST_KEY
argument_list|,
name|cluster
argument_list|,
name|conf
argument_list|)
expr_stmt|;
name|clientConf
operator|=
operator|new
name|Configuration
argument_list|(
name|conf
argument_list|)
expr_stmt|;
name|clientConf
operator|.
name|setLong
argument_list|(
name|FS_TRASH_INTERVAL_KEY
argument_list|,
literal|1
argument_list|)
expr_stmt|;
name|shell
operator|=
operator|new
name|FsShell
argument_list|(
name|clientConf
argument_list|)
expr_stmt|;
block|}
DECL|method|setProvider ()
specifier|protected
name|void
name|setProvider
parameter_list|()
block|{
comment|// Need to set the client's KeyProvider to the NN's for JKS,
comment|// else the updates do not get flushed properly
name|fs
operator|.
name|getClient
argument_list|()
operator|.
name|setKeyProvider
argument_list|(
name|cluster
operator|.
name|getNameNode
argument_list|()
operator|.
name|getNamesystem
argument_list|()
operator|.
name|getProvider
argument_list|()
argument_list|)
expr_stmt|;
block|}
annotation|@
name|After
DECL|method|teardown ()
specifier|public
name|void
name|teardown
parameter_list|()
block|{
if|if
condition|(
name|cluster
operator|!=
literal|null
condition|)
block|{
name|cluster
operator|.
name|shutdown
argument_list|()
expr_stmt|;
name|cluster
operator|=
literal|null
expr_stmt|;
block|}
block|}
annotation|@
name|Test
DECL|method|testDeleteWithinEncryptionZone ()
specifier|public
name|void
name|testDeleteWithinEncryptionZone
parameter_list|()
throws|throws
name|Exception
block|{
specifier|final
name|Path
name|zone
init|=
operator|new
name|Path
argument_list|(
literal|"/zones"
argument_list|)
decl_stmt|;
name|fs
operator|.
name|mkdirs
argument_list|(
name|zone
argument_list|)
expr_stmt|;
specifier|final
name|Path
name|zone1
init|=
operator|new
name|Path
argument_list|(
literal|"/zones/zone"
operator|+
name|zoneCounter
operator|.
name|getAndIncrement
argument_list|()
argument_list|)
decl_stmt|;
name|fs
operator|.
name|mkdirs
argument_list|(
name|zone1
argument_list|)
expr_stmt|;
name|dfsAdmin
operator|.
name|createEncryptionZone
argument_list|(
name|zone1
argument_list|,
name|TEST_KEY
argument_list|,
name|PROVISION_TRASH
argument_list|)
expr_stmt|;
specifier|final
name|Path
name|encFile1
init|=
operator|new
name|Path
argument_list|(
name|zone1
argument_list|,
literal|"encFile"
operator|+
name|fileCounter
operator|.
name|getAndIncrement
argument_list|()
argument_list|)
decl_stmt|;
name|DFSTestUtil
operator|.
name|createFile
argument_list|(
name|fs
argument_list|,
name|encFile1
argument_list|,
name|LEN
argument_list|,
operator|(
name|short
operator|)
literal|1
argument_list|,
literal|0xFEED
argument_list|)
expr_stmt|;
comment|//Verify file deletion
name|DFSTestUtil
operator|.
name|verifyDelete
argument_list|(
name|shell
argument_list|,
name|fs
argument_list|,
name|encFile1
argument_list|,
literal|true
argument_list|)
expr_stmt|;
comment|//Verify directory deletion
name|DFSTestUtil
operator|.
name|verifyDelete
argument_list|(
name|shell
argument_list|,
name|fs
argument_list|,
name|zone1
argument_list|,
literal|true
argument_list|)
expr_stmt|;
block|}
annotation|@
name|Test
DECL|method|testDeleteEZWithMultipleUsers ()
specifier|public
name|void
name|testDeleteEZWithMultipleUsers
parameter_list|()
throws|throws
name|Exception
block|{
specifier|final
name|Path
name|zone
init|=
operator|new
name|Path
argument_list|(
literal|"/zones"
argument_list|)
decl_stmt|;
name|fs
operator|.
name|mkdirs
argument_list|(
name|zone
argument_list|)
expr_stmt|;
specifier|final
name|Path
name|zone1
init|=
operator|new
name|Path
argument_list|(
literal|"/zones/zone"
operator|+
name|zoneCounter
operator|.
name|getAndIncrement
argument_list|()
argument_list|)
decl_stmt|;
name|fs
operator|.
name|mkdirs
argument_list|(
name|zone1
argument_list|)
expr_stmt|;
name|dfsAdmin
operator|.
name|createEncryptionZone
argument_list|(
name|zone1
argument_list|,
name|TEST_KEY
argument_list|,
name|NO_TRASH
argument_list|)
expr_stmt|;
name|fsWrapper
operator|.
name|setPermission
argument_list|(
name|zone1
argument_list|,
operator|new
name|FsPermission
argument_list|(
name|FsAction
operator|.
name|ALL
argument_list|,
name|FsAction
operator|.
name|ALL
argument_list|,
name|FsAction
operator|.
name|ALL
argument_list|)
argument_list|)
expr_stmt|;
specifier|final
name|Path
name|encFile1
init|=
operator|new
name|Path
argument_list|(
name|zone1
argument_list|,
literal|"encFile"
operator|+
name|fileCounter
operator|.
name|getAndIncrement
argument_list|()
argument_list|)
decl_stmt|;
name|DFSTestUtil
operator|.
name|createFile
argument_list|(
name|fs
argument_list|,
name|encFile1
argument_list|,
name|LEN
argument_list|,
operator|(
name|short
operator|)
literal|1
argument_list|,
literal|0xFEED
argument_list|)
expr_stmt|;
comment|// create a non-privileged user
specifier|final
name|UserGroupInformation
name|user
init|=
name|UserGroupInformation
operator|.
name|createUserForTesting
argument_list|(
literal|"user"
argument_list|,
operator|new
name|String
index|[]
block|{
literal|"mygroup"
block|}
argument_list|)
decl_stmt|;
specifier|final
name|Path
name|encFile2
init|=
operator|new
name|Path
argument_list|(
name|zone1
argument_list|,
literal|"encFile"
operator|+
name|fileCounter
operator|.
name|getAndIncrement
argument_list|()
argument_list|)
decl_stmt|;
name|user
operator|.
name|doAs
argument_list|(
operator|new
name|PrivilegedExceptionAction
argument_list|<
name|Object
argument_list|>
argument_list|()
block|{
annotation|@
name|Override
specifier|public
name|Object
name|run
parameter_list|()
throws|throws
name|Exception
block|{
comment|// create a file /zones/zone1/encFile2 in EZ
comment|// this file is owned by user:mygroup
name|FileSystem
name|fs2
init|=
name|FileSystem
operator|.
name|get
argument_list|(
name|cluster
operator|.
name|getConfiguration
argument_list|(
literal|0
argument_list|)
argument_list|)
decl_stmt|;
name|DFSTestUtil
operator|.
name|createFile
argument_list|(
name|fs2
argument_list|,
name|encFile2
argument_list|,
name|LEN
argument_list|,
operator|(
name|short
operator|)
literal|1
argument_list|,
literal|0xFEED
argument_list|)
expr_stmt|;
comment|// Delete /zones/zone1/encFile2, which moves the file to
comment|// /zones/zone1/.Trash/user/Current/zones/zone1/encFile2
name|DFSTestUtil
operator|.
name|verifyDelete
argument_list|(
name|shell
argument_list|,
name|fs
argument_list|,
name|encFile2
argument_list|,
literal|true
argument_list|)
expr_stmt|;
comment|// Delete /zones/zone1 should not succeed as current user is not admin
name|String
index|[]
name|argv
init|=
operator|new
name|String
index|[]
block|{
literal|"-rm"
block|,
literal|"-r"
block|,
name|zone1
operator|.
name|toString
argument_list|()
block|}
decl_stmt|;
name|int
name|res
init|=
name|ToolRunner
operator|.
name|run
argument_list|(
name|shell
argument_list|,
name|argv
argument_list|)
decl_stmt|;
name|assertEquals
argument_list|(
literal|"Non-admin could delete an encryption zone with multiple"
operator|+
literal|" users : "
operator|+
name|zone1
argument_list|,
literal|1
argument_list|,
name|res
argument_list|)
expr_stmt|;
return|return
literal|null
return|;
block|}
block|}
argument_list|)
expr_stmt|;
name|shell
operator|=
operator|new
name|FsShell
argument_list|(
name|clientConf
argument_list|)
expr_stmt|;
name|DFSTestUtil
operator|.
name|verifyDelete
argument_list|(
name|shell
argument_list|,
name|fs
argument_list|,
name|zone1
argument_list|,
literal|true
argument_list|)
expr_stmt|;
block|}
block|}
end_class

end_unit

