Search in sources :

Example 6 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project java-chassis by ServiceComb.

the class FortifyUtils method getDefaultFileAttributes.

public static FileAttribute<?> getDefaultFileAttributes(String filePath) {
    Path file = new File(filePath).toPath();
    if (isPosix()) {
        return PosixFilePermissions.asFileAttribute(FilePerm.getDefaultPosixPerm());
    } else {
        // for not posix must support ACL, or failed.
        String userName = System.getProperty("user.name");
        UserPrincipal user = null;
        try {
            user = file.getFileSystem().getUserPrincipalLookupService().lookupPrincipalByName(userName);
        } catch (IOException e) {
            throw new RuntimeException("Unknown user error.");
        }
        final AclEntry entry = AclEntry.newBuilder().setType(AclEntryType.ALLOW).setPrincipal(user).setPermissions(FilePerm.getDefaultAclPerm()).setFlags(new AclEntryFlag[] { AclEntryFlag.FILE_INHERIT, AclEntryFlag.DIRECTORY_INHERIT }).build();
        return new FileAttribute<List<AclEntry>>() {

            public String name() {
                return "acl:acl";
            }

            /* Windows ACL */
            //public Object value() { ArrayList l = new ArrayList(); l.add(entry); return l; }
            public List<AclEntry> value() {
                ArrayList<AclEntry> l = new ArrayList<AclEntry>();
                l.add(entry);
                return l;
            }
        };
    }
}
Also used : Path(java.nio.file.Path) AclEntryFlag(java.nio.file.attribute.AclEntryFlag) AclEntry(java.nio.file.attribute.AclEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) File(java.io.File) UserPrincipal(java.nio.file.attribute.UserPrincipal) FileAttribute(java.nio.file.attribute.FileAttribute)

Example 7 with UserPrincipal

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

the class WindowsSshScript method protectPrivateKeyFile.

@Override
protected void protectPrivateKeyFile(File sshKey) throws ServerException {
    try {
        AclFileAttributeView attributes = Files.getFileAttributeView(sshKey.toPath(), AclFileAttributeView.class);
        AclEntry.Builder builder = AclEntry.newBuilder();
        builder.setType(ALLOW);
        String ownerName = System.getProperty(OWNER_NAME_PROPERTY);
        UserPrincipal userPrincipal = FileSystems.getDefault().getUserPrincipalLookupService().lookupPrincipalByName(ownerName);
        builder.setPrincipal(userPrincipal);
        builder.setPermissions(READ_DATA, APPEND_DATA, READ_NAMED_ATTRS, READ_ATTRIBUTES, DELETE, READ_ACL, SYNCHRONIZE);
        AclEntry entry = builder.build();
        List<AclEntry> aclEntryList = new ArrayList<>();
        aclEntryList.add(entry);
        attributes.setAcl(aclEntryList);
    } catch (IOException e) {
        throw new ServerException("Failed to set file permissions");
    }
}
Also used : ServerException(org.eclipse.che.api.core.ServerException) AclFileAttributeView(java.nio.file.attribute.AclFileAttributeView) AclEntry(java.nio.file.attribute.AclEntry) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal)

Example 8 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project vert.x by eclipse.

the class FileSystemImpl method chownInternal.

protected BlockingAction<Void> chownInternal(String path, String user, String group, Handler<AsyncResult<Void>> handler) {
    Objects.requireNonNull(path);
    return new BlockingAction<Void>(handler) {

        public Void perform() {
            try {
                Path target = vertx.resolveFile(path).toPath();
                UserPrincipalLookupService service = target.getFileSystem().getUserPrincipalLookupService();
                UserPrincipal userPrincipal = user == null ? null : service.lookupPrincipalByName(user);
                GroupPrincipal groupPrincipal = group == null ? null : service.lookupPrincipalByGroupName(group);
                if (groupPrincipal != null) {
                    PosixFileAttributeView view = Files.getFileAttributeView(target, PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
                    if (view == null) {
                        throw new FileSystemException("Change group of file not supported");
                    }
                    view.setGroup(groupPrincipal);
                }
                if (userPrincipal != null) {
                    Files.setOwner(target, userPrincipal);
                }
            } catch (SecurityException e) {
                throw new FileSystemException("Accessed denied for chown on " + path);
            } catch (IOException e) {
                throw new FileSystemException(e);
            }
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) FileSystemException(io.vertx.core.file.FileSystemException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 9 with UserPrincipal

use of java.nio.file.attribute.UserPrincipal in project vert.x by eclipse.

the class FileSystemTest method testChownToOwnUser.

@Test
public void testChownToOwnUser() throws Exception {
    String file1 = "some-file.dat";
    createFileWithJunk(file1, 100);
    String fullPath = testDir + pathSep + file1;
    Path path = Paths.get(fullPath);
    UserPrincipal owner = Files.getOwner(path);
    String user = owner.getName();
    vertx.fileSystem().chown(fullPath, user, null, ar -> {
        deleteFile(file1);
        assertTrue(ar.succeeded());
        testComplete();
    });
    await();
}
Also used : Path(java.nio.file.Path) UserPrincipal(java.nio.file.attribute.UserPrincipal) Test(org.junit.Test)

Example 10 with UserPrincipal

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

the class InstallPluginCommandTests method assertPlugin.

void assertPlugin(String name, Path original, Environment env) throws IOException {
    Path got = env.pluginsFile().resolve(name);
    assertTrue("dir " + name + " exists", Files.exists(got));
    if (isPosix) {
        Set<PosixFilePermission> perms = Files.getPosixFilePermissions(got);
        assertThat(perms, containsInAnyOrder(PosixFilePermission.OWNER_READ, PosixFilePermission.OWNER_WRITE, PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_READ, PosixFilePermission.GROUP_EXECUTE, PosixFilePermission.OTHERS_READ, PosixFilePermission.OTHERS_EXECUTE));
    }
    assertTrue("jar was copied", Files.exists(got.resolve("plugin.jar")));
    assertFalse("bin was not copied", Files.exists(got.resolve("bin")));
    assertFalse("config was not copied", Files.exists(got.resolve("config")));
    if (Files.exists(original.resolve("bin"))) {
        Path binDir = env.binFile().resolve(name);
        assertTrue("bin dir exists", Files.exists(binDir));
        assertTrue("bin is a dir", Files.isDirectory(binDir));
        PosixFileAttributes binAttributes = null;
        if (isPosix) {
            binAttributes = Files.readAttributes(env.binFile(), PosixFileAttributes.class);
        }
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(binDir)) {
            for (Path file : stream) {
                assertFalse("not a dir", Files.isDirectory(file));
                if (isPosix) {
                    PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
                    assertEquals(InstallPluginCommand.BIN_FILES_PERMS, attributes.permissions());
                }
            }
        }
    }
    if (Files.exists(original.resolve("config"))) {
        Path configDir = env.configFile().resolve(name);
        assertTrue("config dir exists", Files.exists(configDir));
        assertTrue("config is a dir", Files.isDirectory(configDir));
        UserPrincipal user = null;
        GroupPrincipal group = null;
        if (isPosix) {
            PosixFileAttributes configAttributes = Files.getFileAttributeView(env.configFile(), PosixFileAttributeView.class).readAttributes();
            user = configAttributes.owner();
            group = configAttributes.group();
            PosixFileAttributes attributes = Files.getFileAttributeView(configDir, PosixFileAttributeView.class).readAttributes();
            assertThat(attributes.owner(), equalTo(user));
            assertThat(attributes.group(), equalTo(group));
        }
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(configDir)) {
            for (Path file : stream) {
                assertFalse("not a dir", Files.isDirectory(file));
                if (isPosix) {
                    PosixFileAttributes attributes = Files.readAttributes(file, PosixFileAttributes.class);
                    if (user != null) {
                        assertThat(attributes.owner(), equalTo(user));
                    }
                    if (group != null) {
                        assertThat(attributes.group(), equalTo(group));
                    }
                }
            }
        }
    }
    assertInstallCleaned(env);
}
Also used : Path(java.nio.file.Path) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) UserPrincipal(java.nio.file.attribute.UserPrincipal) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Aggregations

UserPrincipal (java.nio.file.attribute.UserPrincipal)19 Path (java.nio.file.Path)13 Test (org.junit.Test)7 File (java.io.File)6 IOException (java.io.IOException)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)4 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 ArrayList (java.util.ArrayList)4 FileOwnerAttributeView (java.nio.file.attribute.FileOwnerAttributeView)3 PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)3 UserLookupService.createUserPrincipal (com.google.common.jimfs.UserLookupService.createUserPrincipal)2 FileSystem (java.nio.file.FileSystem)2 AclEntry (java.nio.file.attribute.AclEntry)2 BasicFileAttributeView (java.nio.file.attribute.BasicFileAttributeView)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 HashMap (java.util.HashMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 UserLookupService.createGroupPrincipal (com.google.common.jimfs.UserLookupService.createGroupPrincipal)1 FileSystemException (io.vertx.core.file.FileSystemException)1 AclEntryFlag (java.nio.file.attribute.AclEntryFlag)1