NIFI-2919 improved GetFile to fail if target directory is inaccessible

This commit is contained in:
Oleg Zhurakousky 2016-10-19 09:30:12 -04:00
parent 596b98865b
commit e755cbddad
2 changed files with 71 additions and 4 deletions

View File

@ -295,14 +295,14 @@ public class GetFile extends AbstractProcessor {
}
private Set<File> performListing(final File directory, final FileFilter filter, final boolean recurseSubdirectories) {
Path p = directory.toPath();
if (!Files.isWritable(p) || !Files.isReadable(p)) {
throw new IllegalStateException("Directory '" + directory + "' does not have sufficient permissions (i.e., not writable and redable)");
}
final Set<File> queue = new HashSet<>();
if (!directory.exists()) {
return queue;
}
// this check doesn't work on Windows
if (!directory.canRead()) {
getLogger().warn("No read permission on directory {}", new Object[]{directory.toString()});
}
final File[] children = directory.listFiles();
if (children == null) {

View File

@ -32,6 +32,7 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
@ -44,6 +45,72 @@ import org.junit.Test;
public class TestGetFile {
@Test
public void testWithInaccessibleDir() throws IOException {
File inaccessibleDir = new File("target/inaccessible");
inaccessibleDir.deleteOnExit();
inaccessibleDir.mkdir();
Set<PosixFilePermission> posixFilePermissions = new HashSet<>();
Files.setPosixFilePermissions(inaccessibleDir.toPath(), posixFilePermissions);
final TestRunner runner = TestRunners.newTestRunner(new GetFile());
runner.setProperty(GetFile.DIRECTORY, inaccessibleDir.getAbsolutePath());
try {
runner.run();
fail();
} catch (AssertionError e) {
assertTrue(e.getCause().getMessage()
.endsWith("does not have sufficient permissions (i.e., not writable and redable)"));
}
}
@Test
public void testWithUnreadableDir() throws IOException {
File unreadableDir = new File("target/unreadable");
unreadableDir.deleteOnExit();
unreadableDir.mkdir();
Set<PosixFilePermission> posixFilePermissions = new HashSet<>();
posixFilePermissions.add(PosixFilePermission.GROUP_EXECUTE);
posixFilePermissions.add(PosixFilePermission.GROUP_WRITE);
posixFilePermissions.add(PosixFilePermission.OTHERS_EXECUTE);
posixFilePermissions.add(PosixFilePermission.OTHERS_WRITE);
posixFilePermissions.add(PosixFilePermission.OWNER_EXECUTE);
posixFilePermissions.add(PosixFilePermission.OWNER_WRITE);
Files.setPosixFilePermissions(unreadableDir.toPath(), posixFilePermissions);
final TestRunner runner = TestRunners.newTestRunner(new GetFile());
runner.setProperty(GetFile.DIRECTORY, unreadableDir.getAbsolutePath());
try {
runner.run();
fail();
} catch (AssertionError e) {
assertTrue(e.getCause().getMessage()
.endsWith("does not have sufficient permissions (i.e., not writable and redable)"));
}
}
@Test
public void testWithUnwritableDir() throws IOException {
File unreadableDir = new File("target/unreadable");
unreadableDir.deleteOnExit();
unreadableDir.mkdir();
Set<PosixFilePermission> posixFilePermissions = new HashSet<>();
posixFilePermissions.add(PosixFilePermission.GROUP_EXECUTE);
posixFilePermissions.add(PosixFilePermission.GROUP_READ);
posixFilePermissions.add(PosixFilePermission.OTHERS_EXECUTE);
posixFilePermissions.add(PosixFilePermission.OTHERS_READ);
posixFilePermissions.add(PosixFilePermission.OWNER_EXECUTE);
posixFilePermissions.add(PosixFilePermission.OWNER_READ);
Files.setPosixFilePermissions(unreadableDir.toPath(), posixFilePermissions);
final TestRunner runner = TestRunners.newTestRunner(new GetFile());
runner.setProperty(GetFile.DIRECTORY, unreadableDir.getAbsolutePath());
try {
runner.run();
fail();
} catch (AssertionError e) {
assertTrue(e.getCause().getMessage()
.endsWith("does not have sufficient permissions (i.e., not writable and redable)"));
}
}
@Test
public void testFilePickedUp() throws IOException {
final File directory = new File("target/test/data/in");