Search in sources :

Example 1 with PosixFileAttributeView

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

the class InstallPluginCommand method installConfig.

/**
     * Copies the files from {@code tmpConfigDir} into {@code destConfigDir}.
     * Any files existing in both the source and destination will be skipped.
     */
private void installConfig(PluginInfo info, Path tmpConfigDir, Path destConfigDir) throws Exception {
    if (Files.isDirectory(tmpConfigDir) == false) {
        throw new UserException(ExitCodes.IO_ERROR, "config in plugin " + info.getName() + " is not a directory");
    }
    Files.createDirectories(destConfigDir);
    setFileAttributes(destConfigDir, CONFIG_DIR_PERMS);
    final PosixFileAttributeView destConfigDirAttributesView = Files.getFileAttributeView(destConfigDir.getParent(), PosixFileAttributeView.class);
    final PosixFileAttributes destConfigDirAttributes = destConfigDirAttributesView != null ? destConfigDirAttributesView.readAttributes() : null;
    if (destConfigDirAttributes != null) {
        setOwnerGroup(destConfigDir, destConfigDirAttributes);
    }
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(tmpConfigDir)) {
        for (Path srcFile : stream) {
            if (Files.isDirectory(srcFile)) {
                throw new UserException(ExitCodes.DATA_ERROR, "Directories not allowed in config dir for plugin " + info.getName());
            }
            Path destFile = destConfigDir.resolve(tmpConfigDir.relativize(srcFile));
            if (Files.exists(destFile) == false) {
                Files.copy(srcFile, destFile);
                setFileAttributes(destFile, CONFIG_FILES_PERMS);
                if (destConfigDirAttributes != null) {
                    setOwnerGroup(destFile, destConfigDirAttributes);
                }
            }
        }
    }
    // clean up what we just copied
    IOUtils.rm(tmpConfigDir);
}
Also used : Path(java.nio.file.Path) UserException(org.elasticsearch.cli.UserException) PosixFileAttributes(java.nio.file.attribute.PosixFileAttributes) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 2 with PosixFileAttributeView

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

the class InstallPluginCommand method setOwnerGroup.

private static void setOwnerGroup(final Path path, final PosixFileAttributes attributes) throws IOException {
    Objects.requireNonNull(attributes);
    PosixFileAttributeView fileAttributeView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    assert fileAttributeView != null;
    fileAttributeView.setOwner(attributes.owner());
    fileAttributeView.setGroup(attributes.group());
}
Also used : PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 3 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView in project ignite by apache.

the class LocalFileSystemUtils method updateProperties.

/**
     * Update file properties.
     *
     * @param file File.
     * @param grp Group.
     * @param perm Permissions.
     */
public static void updateProperties(File file, String grp, String perm) {
    PosixFileAttributeView attrs = Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
    if (attrs == null)
        throw new UnsupportedOperationException("Posix file attributes not available");
    if (grp != null) {
        try {
            UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
            GroupPrincipal grp0 = lookupService.lookupPrincipalByGroupName(grp);
            attrs.setGroup(grp0);
        } catch (IOException e) {
            throw new IgfsException("Update the '" + IgfsUtils.PROP_GROUP_NAME + "' property is failed.", e);
        }
    }
    if (perm != null) {
        int perm0 = Integer.parseInt(perm, 8);
        Set<PosixFilePermission> permSet = new HashSet<>(9);
        for (int i = 0; i < LocalFileSystemUtils.POSIX_PERMISSIONS.length; ++i) {
            if ((perm0 & (1 << i)) != 0)
                permSet.add(LocalFileSystemUtils.POSIX_PERMISSIONS[i]);
        }
        try {
            attrs.setPermissions(permSet);
        } catch (IOException e) {
            throw new IgfsException("Update the '" + IgfsUtils.PROP_PERMISSION + "' property is failed.", e);
        }
    }
}
Also used : UserPrincipalLookupService(java.nio.file.attribute.UserPrincipalLookupService) IgfsException(org.apache.ignite.igfs.IgfsException) GroupPrincipal(java.nio.file.attribute.GroupPrincipal) IOException(java.io.IOException) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView) HashSet(java.util.HashSet)

Example 4 with PosixFileAttributeView

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

the class KeyStoreWrapper method save.

/** Write the keystore to the given config directory. */
void save(Path configDir) throws Exception {
    char[] password = this.keystorePassword.get().getPassword();
    SimpleFSDirectory directory = new SimpleFSDirectory(configDir);
    // write to tmp file first, then overwrite
    String tmpFile = KEYSTORE_FILENAME + ".tmp";
    try (IndexOutput output = directory.createOutput(tmpFile, IOContext.DEFAULT)) {
        CodecUtil.writeHeader(output, KEYSTORE_FILENAME, FORMAT_VERSION);
        output.writeByte(password.length == 0 ? (byte) 0 : (byte) 1);
        output.writeString(type);
        output.writeString(secretFactory.getAlgorithm());
        ByteArrayOutputStream keystoreBytesStream = new ByteArrayOutputStream();
        keystore.get().store(keystoreBytesStream, password);
        byte[] keystoreBytes = keystoreBytesStream.toByteArray();
        output.writeInt(keystoreBytes.length);
        output.writeBytes(keystoreBytes, keystoreBytes.length);
        CodecUtil.writeFooter(output);
    }
    Path keystoreFile = keystorePath(configDir);
    Files.move(configDir.resolve(tmpFile), keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
    PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreFile, PosixFileAttributeView.class);
    if (attrs != null) {
        // don't rely on umask: ensure the keystore has minimal permissions
        attrs.setPermissions(PosixFilePermissions.fromString("rw-------"));
    }
}
Also used : Path(java.nio.file.Path) IndexOutput(org.apache.lucene.store.IndexOutput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleFSDirectory(org.apache.lucene.store.SimpleFSDirectory) PosixFileAttributeView(java.nio.file.attribute.PosixFileAttributeView)

Example 5 with PosixFileAttributeView

use of java.nio.file.attribute.PosixFileAttributeView 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)

Aggregations

PosixFileAttributeView (java.nio.file.attribute.PosixFileAttributeView)12 Path (java.nio.file.Path)4 UserPrincipalLookupService (java.nio.file.attribute.UserPrincipalLookupService)4 GroupPrincipal (java.nio.file.attribute.GroupPrincipal)3 IOException (java.io.IOException)2 PosixFileAttributes (java.nio.file.attribute.PosixFileAttributes)2 PosixFilePermission (java.nio.file.attribute.PosixFilePermission)2 UserPrincipal (java.nio.file.attribute.UserPrincipal)2 MultiMap (com.intellij.util.containers.MultiMap)1 ChannelHandler (io.netty.channel.ChannelHandler)1 FileSystemException (io.vertx.core.file.FileSystemException)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 IgfsException (org.apache.ignite.igfs.IgfsException)1 IndexOutput (org.apache.lucene.store.IndexOutput)1 SimpleFSDirectory (org.apache.lucene.store.SimpleFSDirectory)1 UserException (org.elasticsearch.cli.UserException)1 NotNull (org.jetbrains.annotations.NotNull)1