NIFI-4570: Skip permission setting if Posix is not supported

Signed-off-by: Pierre Villard <pierre.villard.fr@gmail.com>

This closes #2252.
This commit is contained in:
Koji Kawamura 2017-11-06 14:42:44 +09:00 committed by Pierre Villard
parent 5cd8a3e729
commit 296928c513
1 changed files with 17 additions and 9 deletions

View File

@ -34,6 +34,7 @@ import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
@ -1185,17 +1186,24 @@ public class RunNiFi {
private Path createSensitiveKeyFile(File confDir) { private Path createSensitiveKeyFile(File confDir) {
Path sensitiveKeyFile = Paths.get(confDir+"/sensitive.key"); Path sensitiveKeyFile = Paths.get(confDir+"/sensitive.key");
final boolean isPosixSupported = FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
try { try {
// Initially create file with the empty permission set (so nobody can get a file descriptor on it): if (isPosixSupported) {
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>(); // Initially create file with the empty permission set (so nobody can get a file descriptor on it):
FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms); Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
sensitiveKeyFile = Files.createFile(sensitiveKeyFile, attr); FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
sensitiveKeyFile = Files.createFile(sensitiveKeyFile, attr);
// Then, once created, add owner-only rights: // Then, once created, add owner-only rights:
perms.add(PosixFilePermission.OWNER_WRITE); perms.add(PosixFilePermission.OWNER_WRITE);
perms.add(PosixFilePermission.OWNER_READ); perms.add(PosixFilePermission.OWNER_READ);
attr = PosixFilePermissions.asFileAttribute(perms); attr = PosixFilePermissions.asFileAttribute(perms);
Files.setPosixFilePermissions(sensitiveKeyFile, perms); Files.setPosixFilePermissions(sensitiveKeyFile, perms);
} else {
// If Posix is not supported (e.g. Windows) then create the key file without permission settings.
cmdLogger.info("Current file system does not support Posix, using default permission settings.");
sensitiveKeyFile = Files.createFile(sensitiveKeyFile);
}
} catch (final FileAlreadyExistsException faee) { } catch (final FileAlreadyExistsException faee) {
cmdLogger.error("The sensitive.key file {} already exists. That shouldn't have been. Aborting.", sensitiveKeyFile); cmdLogger.error("The sensitive.key file {} already exists. That shouldn't have been. Aborting.", sensitiveKeyFile);