Set execute permissions for native plugin programs (#21657)

This commit is contained in:
David Roberts 2016-11-21 09:20:09 +00:00 committed by GitHub
parent 9594b6f50f
commit 6daeb56969
2 changed files with 44 additions and 4 deletions

View File

@ -47,9 +47,12 @@ import java.net.URLConnection;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryStream;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
@ -493,15 +496,24 @@ class InstallPluginCommand extends SettingCommand {
}
Files.move(tmpRoot, destination, StandardCopyOption.ATOMIC_MOVE);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(destination)) {
for (Path pluginFile : stream) {
Files.walkFileTree(destination, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path pluginFile, BasicFileAttributes attrs) throws IOException {
if (Files.isDirectory(pluginFile)) {
setFileAttributes(pluginFile, PLUGIN_DIR_PERMS);
} else {
// There can also be "bin" directories under the plugin directory, storing native code executables
Path parentDir = pluginFile.getParent().getFileName();
if ("bin".equals(parentDir.toString())) {
setFileAttributes(pluginFile, BIN_FILES_PERMS);
} else {
setFileAttributes(pluginFile, PLUGIN_FILES_PERMS);
}
}
return FileVisitResult.CONTINUE;
}
});
terminal.println("-> Installed " + info.getName());
} catch (Exception installProblem) {

View File

@ -477,6 +477,34 @@ public class InstallPluginCommandTests extends ESTestCase {
}
}
public void testPlatformBinPermissions() throws Exception {
assumeTrue("posix filesystem", isPosix);
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);
Path platformDir = pluginDir.resolve("platform");
Path platformNameDir = platformDir.resolve("linux-x86_64");
Path platformBinDir = platformNameDir.resolve("bin");
Files.createDirectories(platformBinDir);
Path programFile = Files.createFile(platformBinDir.resolve("someprogram"));
// a file created with Files.createFile() should not have execute permissions
Set<PosixFilePermission> sourcePerms = Files.getPosixFilePermissions(programFile);
assertFalse(sourcePerms.contains(PosixFilePermission.OWNER_EXECUTE));
assertFalse(sourcePerms.contains(PosixFilePermission.GROUP_EXECUTE));
assertFalse(sourcePerms.contains(PosixFilePermission.OTHERS_EXECUTE));
String pluginZip = createPlugin("fake", pluginDir);
installPlugin(pluginZip, env.v1());
assertPlugin("fake", pluginDir, env.v2());
// check that the installed program has execute permissions, even though the one added to the plugin didn't
Path installedPlatformBinDir = env.v2().pluginsFile().resolve("fake").resolve("platform").resolve("linux-x86_64").resolve("bin");
assertTrue(Files.isDirectory(installedPlatformBinDir));
Path installedProgramFile = installedPlatformBinDir.resolve("someprogram");
assertTrue(Files.isRegularFile(installedProgramFile));
Set<PosixFilePermission> installedPerms = Files.getPosixFilePermissions(installedProgramFile);
assertTrue(installedPerms.contains(PosixFilePermission.OWNER_EXECUTE));
assertTrue(installedPerms.contains(PosixFilePermission.GROUP_EXECUTE));
assertTrue(installedPerms.contains(PosixFilePermission.OTHERS_EXECUTE));
}
public void testConfig() throws Exception {
Tuple<Path, Environment> env = createEnv(fs, temp);
Path pluginDir = createPluginDir(temp);