Search in sources :

Example 1 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project elasticsearch by elastic.

the class InstallPluginCommandTests method testPlatformBinPermissions.

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));
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Example 2 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project elasticsearch by elastic.

the class InstallPluginCommandTests method testBinPermissions.

public void testBinPermissions() throws Exception {
    assumeTrue("posix filesystem", isPosix);
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Path binDir = pluginDir.resolve("bin");
    Files.createDirectory(binDir);
    Files.createFile(binDir.resolve("somescript"));
    String pluginZip = createPlugin("fake", pluginDir);
    try (PosixPermissionsResetter binAttrs = new PosixPermissionsResetter(env.v2().binFile())) {
        Set<PosixFilePermission> perms = binAttrs.getCopyPermissions();
        // make sure at least one execute perm is missing, so we know we forced it during installation
        perms.remove(PosixFilePermission.GROUP_EXECUTE);
        binAttrs.setPermissions(perms);
        installPlugin(pluginZip, env.v1());
        assertPlugin("fake", pluginDir, env.v2());
    }
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixPermissionsResetter(org.elasticsearch.test.PosixPermissionsResetter)

Example 3 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project elasticsearch by elastic.

the class SpawnerNoBootstrapTests method createControllerProgram.

private void createControllerProgram(Path outputFile) throws IOException {
    Path outputDir = outputFile.getParent();
    Files.createDirectories(outputDir);
    Files.write(outputFile, CONTROLLER_SOURCE.getBytes(StandardCharsets.UTF_8));
    Set<PosixFilePermission> perms = new HashSet<>();
    perms.add(PosixFilePermission.OWNER_READ);
    perms.add(PosixFilePermission.OWNER_WRITE);
    perms.add(PosixFilePermission.OWNER_EXECUTE);
    perms.add(PosixFilePermission.GROUP_READ);
    perms.add(PosixFilePermission.GROUP_EXECUTE);
    perms.add(PosixFilePermission.OTHERS_READ);
    perms.add(PosixFilePermission.OTHERS_EXECUTE);
    Files.setPosixFilePermissions(outputFile, perms);
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) HashSet(java.util.HashSet)

Example 4 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project buck by facebook.

the class ProjectFilesystemTest method testCreateReadOnlyFileSetsPermissions.

@Test
public void testCreateReadOnlyFileSetsPermissions() throws IOException {
    Assume.assumeTrue(FileSystems.getDefault().supportedFileAttributeViews().contains("posix"));
    Path path = Paths.get("hello.txt");
    ImmutableSet<PosixFilePermission> permissions = ImmutableSet.of(PosixFilePermission.OWNER_READ, PosixFilePermission.GROUP_READ, PosixFilePermission.OTHERS_READ);
    filesystem.writeContentsToPath("hello world", path, PosixFilePermissions.asFileAttribute(permissions));
    // The umask may restrict the actual permissions on the filesystem:
    // https://fburl.com/26569549
    // So the best we can do is to check that the actual permissions are a
    // strict subset of the expected permissions.
    PosixFileAttributes attrs = filesystem.readAttributes(path, PosixFileAttributes.class);
    assertTrue(permissions.containsAll(attrs.permissions()));
}
Also used : Path(java.nio.file.Path) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) Test(org.junit.Test)

Example 5 with PosixFilePermission

use of java.nio.file.attribute.PosixFilePermission in project che by eclipse.

the class UnixSshScript method protectPrivateKeyFile.

@Override
protected void protectPrivateKeyFile(File sshKey) throws ServerException {
    try {
        //set permission to -rw-------
        Set<PosixFilePermission> permissions = EnumSet.of(OWNER_READ, OWNER_WRITE);
        Files.setPosixFilePermissions(sshKey.toPath(), permissions);
    } catch (IOException e) {
        throw new ServerException("Failed to set file permissions");
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission)

Aggregations

PosixFilePermission (java.nio.file.attribute.PosixFilePermission)139 Path (java.nio.file.Path)69 IOException (java.io.IOException)41 File (java.io.File)40 HashSet (java.util.HashSet)40 Test (org.junit.Test)35 Set (java.util.Set)22 FileOutputStream (java.io.FileOutputStream)17 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)14 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)11 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)7 RandomAccessFile (java.io.RandomAccessFile)6 UserPrincipal (java.nio.file.attribute.UserPrincipal)6 OutputStream (java.io.OutputStream)5 SimpleDateFormat (java.text.SimpleDateFormat)5 BufferedWriter (java.io.BufferedWriter)4 InputStream (java.io.InputStream)4 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)4 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)4