From 6356d7b9ee7cd9a84515a441724a2659397a0ffa Mon Sep 17 00:00:00 2001 From: Andrew Psaltis Date: Sun, 20 May 2018 15:10:39 +0800 Subject: [PATCH] NIFI-4988 This closes #2725. Changed the exception handling so that an invliad ZIP is handled. When an invalid zip is processed, the exception is an IllegalArgumentException which was not being handled and thus the session was being rollbacked. Signed-off-by: joewitt --- .../nifi-standard-processors/pom.xml | 1 + .../processors/standard/UnpackContent.java | 3 +- .../standard/TestUnpackContent.java | 33 ++++++++++++++++++ .../TestUnpackContent/invalid_data.zip | Bin 0 -> 1019 bytes 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestUnpackContent/invalid_data.zip diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml index 1e7ffb49fc..6052dafd67 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml @@ -448,6 +448,7 @@ src/test/resources/TestIdentifyMimeType/flowfilev1.tar src/test/resources/TestUnpackContent/data.tar src/test/resources/TestUnpackContent/data.zip + src/test/resources/TestUnpackContent/invalid_data.zip src/test/resources/TestEncryptContent/plain.txt src/test/resources/TestEncryptContent/salted_raw.enc src/test/resources/TestEncryptContent/salted_128_raw.enc diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UnpackContent.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UnpackContent.java index 1e7f05b4c1..26308da264 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UnpackContent.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UnpackContent.java @@ -20,7 +20,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; @@ -268,7 +267,7 @@ public class UnpackContent extends AbstractProcessor { session.transfer(flowFile, REL_ORIGINAL); session.getProvenanceReporter().fork(flowFile, unpacked); logger.info("Unpacked {} into {} and transferred to success", new Object[]{flowFile, unpacked}); - } catch (final ProcessException | InvalidPathException e) { + } catch (final Exception e) { logger.error("Unable to unpack {} due to {}; routing to failure", new Object[]{flowFile, e}); session.transfer(flowFile, REL_FAILURE); session.remove(unpacked); diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUnpackContent.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUnpackContent.java index 587d050ee6..980d293ab2 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUnpackContent.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestUnpackContent.java @@ -168,6 +168,39 @@ public class TestUnpackContent { flowFile.assertContentEquals(path.toFile()); } } + @Test + public void testInvalidZip() throws IOException { + final TestRunner unpackRunner = TestRunners.newTestRunner(new UnpackContent()); + final TestRunner autoUnpackRunner = TestRunners.newTestRunner(new UnpackContent()); + unpackRunner.setProperty(UnpackContent.PACKAGING_FORMAT, UnpackContent.PackageFormat.ZIP_FORMAT.toString()); + autoUnpackRunner.setProperty(UnpackContent.PACKAGING_FORMAT, UnpackContent.PackageFormat.AUTO_DETECT_FORMAT.toString()); + unpackRunner.enqueue(dataPath.resolve("invalid_data.zip")); + unpackRunner.enqueue(dataPath.resolve("invalid_data.zip")); + Map attributes = new HashMap<>(1); + attributes.put("mime.type", "application/zip"); + autoUnpackRunner.enqueue(dataPath.resolve("invalid_data.zip"), attributes); + autoUnpackRunner.enqueue(dataPath.resolve("invalid_data.zip"), attributes); + unpackRunner.run(2); + autoUnpackRunner.run(2); + + unpackRunner.assertTransferCount(UnpackContent.REL_FAILURE, 2); + unpackRunner.assertTransferCount(UnpackContent.REL_ORIGINAL, 0); + unpackRunner.assertTransferCount(UnpackContent.REL_SUCCESS, 0); + + autoUnpackRunner.assertTransferCount(UnpackContent.REL_FAILURE, 2); + autoUnpackRunner.assertTransferCount(UnpackContent.REL_ORIGINAL, 0); + autoUnpackRunner.assertTransferCount(UnpackContent.REL_SUCCESS, 0); + + final List unpacked = unpackRunner.getFlowFilesForRelationship(UnpackContent.REL_FAILURE); + for (final MockFlowFile flowFile : unpacked) { + final String filename = flowFile.getAttribute(CoreAttributes.FILENAME.key()); + // final String folder = flowFile.getAttribute(CoreAttributes.PATH.key()); + final Path path = dataPath.resolve(filename); + assertTrue(Files.exists(path)); + + flowFile.assertContentEquals(path.toFile()); + } + } @Test public void testZipWithFilter() throws IOException { diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestUnpackContent/invalid_data.zip b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestUnpackContent/invalid_data.zip new file mode 100644 index 0000000000000000000000000000000000000000..73d3fe6afe9f4ea4b05f8977acb2e3fb4abc8cdc GIT binary patch literal 1019 zcmWIWW@Zs#;Nak3&}iQ1!+-9 zx72q;@7#aum*h2#xmk>5dCvRu_LLQNT-NSfUc~Ba?3RCj`xKQAS&Qm2Q%<^UT_$l# z@5QF9^60%=141Y6md@A6dpn8G@o-cxzfh^Y&eN%nZd`GQ-}A=#-MyM;J)1e2Zp~^u zYvs6Q)1Q`Vk4wehBi;9x*>&@6@Ah6B{3LrTo41Oq?bg_Ji}|jw-_xtOnAUW2Ci{nz zRos$%XOHhUDN|oJCw<@IKTgLL-};D$+4cml)JShpKJwoD%$r9?(t{+UW&~YJUu&l2 zw&eZX_j3fGf4OwIDrRub6^lhwe z9w>^JmS|o6!lORrNb!^;o9%l-CV8!^iT+cW;M2BsJ4a^Dj}NnU$}lr5?)dy&!d3c` zapL?%C%MbFd)3YeuXy^cd52))gzY;2+(eW0A6I10?{YEHnLp)0*Szy7U-HzXI$f{X z?3Ziqa_pbb7PMy8b@R1z_!54+JudTA!<1v=pOfd7D!x!|OT8{4RXb^|^32;0!~PWZ z-utt0)f|P7=Q9|0xinWr?_-W*O_o}tw({AJIbY6H%RCCaTbDYqTix-(#AKuATmC$9 zY}7kEsm7W$F~~&l>%P_)_vGZr%LnTN7U`PRiqF{hJ%iy&jk23KuVm|jzfC851CIV{ zIX2}^+QTW+&PErdM1FDn-0P92^?CK%xjYG~p&x##Mz7%cC~;@c#-L~>j%(XruVp#f zV*QvSGSG1L|Bsp;6ADkI!%Q zx%lUAzjGw*w9v(c{pU~b-n+J&^MK5`_>EiHYSeak@%S$Ddd{2Bf7x61Yt$DP|3e&( z1zwv@?N)iInYQItdfsQAbwRem6O7)7|A}O~^e8L$!RBo3UWErGA6}`uqzLn`Tojy= z&Hw#Rw)!;1l7m6>UthXd|N7rw_5g2oj&8Hy