From cd4a97bf80f1170175447b95b95ce78d717d86ff Mon Sep 17 00:00:00 2001 From: Inigo Goiri Date: Tue, 27 Feb 2018 08:53:00 -0800 Subject: [PATCH] HDFS-13192. Change the code order in getFileEncryptionInfo to avoid unnecessary call of assignment. Contributed by LiXin Ge. --- .../namenode/FSDirEncryptionZoneOp.java | 8 ++--- .../hadoop/hdfs/TestEncryptionZones.java | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java index 9fbdaeb16de..bf5652d6fe0 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirEncryptionZoneOp.java @@ -456,17 +456,17 @@ final class FSDirEncryptionZoneOp { } } - final CryptoProtocolVersion version = encryptionZone.getVersion(); - final CipherSuite suite = encryptionZone.getSuite(); - final String keyName = encryptionZone.getKeyName(); XAttr fileXAttr = FSDirXAttrOp.unprotectedGetXAttrByPrefixedName( iip, CRYPTO_XATTR_FILE_ENCRYPTION_INFO); - if (fileXAttr == null) { NameNode.LOG.warn("Could not find encryption XAttr for file " + iip.getPath() + " in encryption zone " + encryptionZone.getPath()); return null; } + + final CryptoProtocolVersion version = encryptionZone.getVersion(); + final CipherSuite suite = encryptionZone.getSuite(); + final String keyName = encryptionZone.getKeyName(); try { HdfsProtos.PerFileEncryptionInfoProto fileProto = HdfsProtos.PerFileEncryptionInfoProto.parseFrom( diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java index cf13057b500..e2d307a4e1b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestEncryptionZones.java @@ -1418,6 +1418,39 @@ public class TestEncryptionZones { dfsAdmin.getEncryptionZoneForPath(snap3Zone).getPath().toString()); } + /** + * Test correctness of encryption zones on a existing snapshot path. + * Specifically, test the file in encryption zones with no encryption info + */ + @Test + public void testSnapshotWithFile() throws Exception { + final int len = 8196; + final Path zoneParent = new Path("/zones"); + final Path zone = new Path(zoneParent, "zone"); + final Path zoneFile = new Path(zone, "zoneFile"); + fsWrapper.mkdir(zone, FsPermission.getDirDefault(), true); + DFSTestUtil.createFile(fs, zoneFile, len, (short) 1, 0xFEED); + String contents = DFSTestUtil.readFile(fs, zoneFile); + + // Create the snapshot which contains the file + dfsAdmin.allowSnapshot(zoneParent); + final Path snap1 = fs.createSnapshot(zoneParent, "snap1"); + + // Now delete the file and create encryption zone + fsWrapper.delete(zoneFile, false); + dfsAdmin.createEncryptionZone(zone, TEST_KEY, NO_TRASH); + assertEquals("Got unexpected ez path", zone.toString(), + dfsAdmin.getEncryptionZoneForPath(zone).getPath()); + + // The file in snapshot shouldn't have any encryption info + final Path snapshottedZoneFile = new Path( + snap1 + "/" + zone.getName() + "/" + zoneFile.getName()); + FileEncryptionInfo feInfo = getFileEncryptionInfo(snapshottedZoneFile); + assertNull("Expected null ez info", feInfo); + assertEquals("Contents of snapshotted file have changed unexpectedly", + contents, DFSTestUtil.readFile(fs, snapshottedZoneFile)); + } + /** * Verify symlinks can be created in encryption zones and that * they function properly when the target is in the same