Example usage for java.nio.file.attribute PosixFilePermissions toString

List of usage examples for java.nio.file.attribute PosixFilePermissions toString

Introduction

In this page you can find the example usage for java.nio.file.attribute PosixFilePermissions toString.

Prototype

public static String toString(Set<PosixFilePermission> perms) 

Source Link

Document

Returns the String representation of a set of permissions.

Usage

From source file:io.hops.hopsworks.common.security.CertificateMaterializer.java

@PostConstruct
public void init() {
    File tmpDir = new File(settings.getHopsworksTmpCertDir());
    if (!tmpDir.exists()) {
        throw new IllegalStateException(
                "Transient certificates directory <" + tmpDir.getAbsolutePath() + "> does NOT exist!");
    }//from  w w  w .j a  va 2 s  .c o m

    try {
        PosixFileAttributeView fileView = Files.getFileAttributeView(tmpDir.toPath(),
                PosixFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        Set<PosixFilePermission> permissions = fileView.readAttributes().permissions();
        boolean ownerRead = permissions.contains(PosixFilePermission.OWNER_READ);
        boolean ownerWrite = permissions.contains(PosixFilePermission.OWNER_WRITE);
        boolean ownerExecute = permissions.contains(PosixFilePermission.OWNER_EXECUTE);

        boolean groupRead = permissions.contains(PosixFilePermission.GROUP_READ);
        boolean groupWrite = permissions.contains(PosixFilePermission.GROUP_WRITE);
        boolean groupExecute = permissions.contains(PosixFilePermission.GROUP_EXECUTE);

        boolean othersRead = permissions.contains(PosixFilePermission.OTHERS_READ);
        boolean othersWrite = permissions.contains(PosixFilePermission.OTHERS_WRITE);
        boolean othersExecute = permissions.contains(PosixFilePermission.OTHERS_EXECUTE);

        // Permissions should be 750
        if ((ownerRead && ownerWrite && ownerExecute) && (groupRead && !groupWrite && groupExecute)
                && (!othersRead && !othersWrite & !othersExecute)) {
            String owner = fileView.readAttributes().owner().getName();
            String group = fileView.readAttributes().group().getName();
            String permStr = PosixFilePermissions.toString(permissions);
            LOG.log(Level.INFO, "Passed permissions check for " + tmpDir.getAbsolutePath() + ". Owner: " + owner
                    + " Group: " + group + " permissions: " + permStr);
        } else {
            throw new IllegalStateException(
                    "Wrong permissions for " + tmpDir.getAbsolutePath() + ", it should be 0750");
        }
    } catch (UnsupportedOperationException ex) {
        LOG.log(Level.WARNING,
                "Associated filesystem is not POSIX compliant. "
                        + "Continue without checking the permissions of " + tmpDir.getAbsolutePath()
                        + " This might be a security problem.");
    } catch (IOException ex) {
        throw new IllegalStateException(
                "Error while getting filesystem " + "permissions of " + tmpDir.getAbsolutePath(), ex);
    }

    try {
        FileUtils.cleanDirectory(tmpDir);
    } catch (IOException ex) {
        LOG.log(Level.WARNING, "Could not clean directory " + tmpDir.getAbsolutePath()
                + " during startup, there might be stale " + "certificates", ex);
    }
    transientDir = tmpDir.getAbsolutePath();
    String delayRaw = settings.getCertificateMaterializerDelay();
    DELAY_VALUE = settings.getConfTimeValue(delayRaw);
    DELAY_TIMEUNIT = settings.getConfTimeTimeUnit(delayRaw);

    try {
        String hostAddress = InetAddress.getLocalHost().getHostAddress();
        long threadId = Thread.currentThread().getId();
        String lock_identifier = hostAddress + "_" + threadId;
        lock_id = lock_identifier.length() <= 30 ? lock_identifier : lock_identifier.substring(0, 30);
    } catch (UnknownHostException ex) {
        throw new IllegalStateException(ex);
    }
}

From source file:com.cloudbees.clickstack.util.Files2.java

private static void dump(@Nonnull Path path, int depth) throws RuntimeIOException {
    try {/*from w ww  . j  a va 2  s .co  m*/
        depth++;
        String icon = Files.isDirectory(path) ? " + " : " |- ";
        System.out.println(Strings.repeat(" ", depth) + icon + path.getFileName() + "\t"
                + PosixFilePermissions.toString(Files.getPosixFilePermissions(path)));

        if (Files.isDirectory(path)) {
            DirectoryStream<Path> children = Files.newDirectoryStream(path);
            for (Path child : children) {
                dump(child, depth);
            }
        }
    } catch (IOException e) {
        throw new RuntimeIOException("Exception dumping " + path, e);
    }
}