Java Code Examples for org.elasticsearch.common.io.FileSystemUtils#isHidden()

The following examples show how to use org.elasticsearch.common.io.FileSystemUtils#isHidden() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetaDataCreateIndexService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(mappingsDir)) {
        for (Path mappingFile : stream) {
            final String fileName = mappingFile.getFileName().toString();
            if (FileSystemUtils.isHidden(mappingFile)) {
                continue;
            }
            int lastDotIndex = fileName.lastIndexOf('.');
            String mappingType = lastDotIndex != -1 ? mappingFile.getFileName().toString().substring(0, lastDotIndex) : mappingFile.getFileName().toString();
            try (BufferedReader reader = Files.newBufferedReader(mappingFile, Charsets.UTF_8)) {
                String mappingSource = Streams.copyToString(reader);
                if (mappings.containsKey(mappingType)) {
                    XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
                } else {
                    mappings.put(mappingType, parseMapping(mappingSource));
                }
            } catch (Exception e) {
                logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
            }
        }
    }
}
 
Example 2
Source File: PluginsService.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts all installed plugin directories from the provided {@code rootPath}.
 *
 * @param rootPath the path where the plugins are installed
 * @return a list of all plugin paths installed in the {@code rootPath}
 * @throws IOException if an I/O exception occurred reading the directories
 */
public static List<Path> findPluginDirs(final Path rootPath) throws IOException {
    final List<Path> plugins = new ArrayList<>();
    final Set<String> seen = new HashSet<>();
    if (Files.exists(rootPath)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(rootPath)) {
            for (Path plugin : stream) {
                if (FileSystemUtils.isHidden(plugin)) {
                    continue;
                }
                if (FileSystemUtils.isDesktopServicesStore(plugin) ||
                    plugin.getFileName().toString().startsWith(".removing-")) {
                    continue;
                }
                if (seen.add(plugin.getFileName().toString()) == false) {
                    throw new IllegalStateException("duplicate plugin: " + plugin);
                } else if (Files.exists(plugin.resolve(ES_PLUGIN_PROPERTIES))) {
                    plugins.add(plugin);
                }
            }
        }
    }
    return plugins;
}
 
Example 3
Source File: PluginsService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
static List<Bundle> getModuleBundles(Path modulesDirectory) throws IOException {
    // damn leniency
    if (Files.notExists(modulesDirectory)) {
        return Collections.emptyList();
    }
    List<Bundle> bundles = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(modulesDirectory)) {
        for (Path module : stream) {
            if (FileSystemUtils.isHidden(module)) {
                continue; // skip over .DS_Store etc
            }
            PluginInfo info = PluginInfo.readFromProperties(module);
            if (!info.isJvm()) {
                throw new IllegalStateException("modules must be jvm plugins: " + info);
            }
            if (!info.isIsolated()) {
                throw new IllegalStateException("modules must be isolated: " + info);
            }
            Bundle bundle = new Bundle();
            bundle.plugins.add(info);
            // gather urls for jar files
            try (DirectoryStream<Path> jarStream = Files.newDirectoryStream(module, "*.jar")) {
                for (Path jar : jarStream) {
                    // normalize with toRealPath to get symlinks out of our hair
                    bundle.urls.add(jar.toRealPath().toUri().toURL());
                }
            }
            bundles.add(bundle);
        }
    }
    return bundles;
}
 
Example 4
Source File: HttpServer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
void handlePluginSite(HttpRequest request, HttpChannel channel) throws IOException {
    if (disableSites) {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN));
        return;
    }
    if (request.method() == RestRequest.Method.OPTIONS) {
        // when we have OPTIONS request, simply send OK by default (with the Access Control Origin header which gets automatically added)
        channel.sendResponse(new BytesRestResponse(OK));
        return;
    }
    if (request.method() != RestRequest.Method.GET) {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN));
        return;
    }
    // TODO for a "/_plugin" endpoint, we should have a page that lists all the plugins?

    String path = request.rawPath().substring("/_plugin/".length());
    int i1 = path.indexOf('/');
    String pluginName;
    String sitePath;
    if (i1 == -1) {
        pluginName = path;
        sitePath = null;
        // If a trailing / is missing, we redirect to the right page #2654
        String redirectUrl = request.rawPath() + "/";
        BytesRestResponse restResponse = new BytesRestResponse(RestStatus.MOVED_PERMANENTLY, "text/html", "<head><meta http-equiv=\"refresh\" content=\"0; URL=" + redirectUrl + "\"></head>");
        restResponse.addHeader("Location", redirectUrl);
        channel.sendResponse(restResponse);
        return;
    } else {
        pluginName = path.substring(0, i1);
        sitePath = path.substring(i1 + 1);
    }

    // we default to index.html, or what the plugin provides (as a unix-style path)
    // this is a relative path under _site configured by the plugin.
    if (sitePath.length() == 0) {
        sitePath = "index.html";
    } else {
        // remove extraneous leading slashes, its not an absolute path.
        while (sitePath.length() > 0 && sitePath.charAt(0) == '/') {
            sitePath = sitePath.substring(1);
        }
    }
    final Path siteFile = environment.pluginsFile().resolve(pluginName).resolve("_site");

    final String separator = siteFile.getFileSystem().getSeparator();
    // Convert file separators.
    sitePath = sitePath.replace("/", separator);
    
    Path file = siteFile.resolve(sitePath);

    // return not found instead of forbidden to prevent malicious requests to find out if files exist or dont exist
    if (!Files.exists(file) || FileSystemUtils.isHidden(file) || !file.toAbsolutePath().normalize().startsWith(siteFile.toAbsolutePath().normalize())) {
        channel.sendResponse(new BytesRestResponse(NOT_FOUND));
        return;
    }

    BasicFileAttributes attributes = Files.readAttributes(file, BasicFileAttributes.class);
    if (!attributes.isRegularFile()) {
        // If it's not a dir, we send a 403
        if (!attributes.isDirectory()) {
            channel.sendResponse(new BytesRestResponse(FORBIDDEN));
            return;
        }
        // We don't serve dir but if index.html exists in dir we should serve it
        file = file.resolve("index.html");
        if (!Files.exists(file) || FileSystemUtils.isHidden(file) || !Files.isRegularFile(file)) {
            channel.sendResponse(new BytesRestResponse(FORBIDDEN));
            return;
        }
    }

    try {
        byte[] data = Files.readAllBytes(file);
        channel.sendResponse(new BytesRestResponse(OK, guessMimeType(sitePath), data));
    } catch (IOException e) {
        channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR));
    }
}
 
Example 5
Source File: PluginsService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
static List<Bundle> getPluginBundles(Path pluginsDirectory) throws IOException {
    ESLogger logger = Loggers.getLogger(PluginsService.class);

    // TODO: remove this leniency, but tests bogusly rely on it
    if (!isAccessibleDirectory(pluginsDirectory, logger)) {
        return Collections.emptyList();
    }

    List<Bundle> bundles = new ArrayList<>();
    // a special purgatory for plugins that directly depend on each other
    bundles.add(new Bundle());

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(pluginsDirectory)) {
        for (Path plugin : stream) {
            if (FileSystemUtils.isHidden(plugin)) {
                logger.trace("--- skip hidden plugin file[{}]", plugin.toAbsolutePath());
                continue;
            }
            if (!FileSystemUtils.isAccessibleDirectory(plugin, logger)) {
                continue;
            }
            PluginInfo info;
            try {
                info = PluginInfo.readFromProperties(plugin);
            } catch (NoSuchFileException e) {
                // es plugin descriptor file not found, ignore, could be a Crate plugin
                logger.trace("--- plugin descriptor file not found, ignoring plugin [{}]", plugin.toAbsolutePath());
                continue;
            }
            logger.trace("--- adding plugin [{}]", plugin.toAbsolutePath());
            List<URL> urls = new ArrayList<>();
            if (info.isJvm()) {
                // a jvm plugin: gather urls for jar files
                try (DirectoryStream<Path> jarStream = Files.newDirectoryStream(plugin, "*.jar")) {
                    for (Path jar : jarStream) {
                        // normalize with toRealPath to get symlinks out of our hair
                        urls.add(jar.toRealPath().toUri().toURL());
                    }
                }
            }
            final Bundle bundle;
            if (info.isJvm() && info.isIsolated() == false) {
                bundle = bundles.get(0); // purgatory
            } else {
                bundle = new Bundle();
                bundles.add(bundle);
            }
            bundle.plugins.add(info);
            bundle.urls.addAll(urls);
        }
    }

    return bundles;
}
 
Example 6
Source File: StaticSite.java    From crate with Apache License 2.0 4 votes vote down vote up
public static FullHttpResponse serveSite(Path siteDirectory,
                                         FullHttpRequest request,
                                         ByteBufAllocator alloc) throws IOException {
    if (request.method() != HttpMethod.GET) {
        return new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.FORBIDDEN);
    }
    String sitePath = request.uri();
    while (sitePath.length() > 0 && sitePath.charAt(0) == '/') {
        sitePath = sitePath.substring(1);
    }

    // we default to index.html, or what the plugin provides (as a unix-style path)
    // this is a relative path under _site configured by the plugin.
    if (sitePath.length() == 0) {
        sitePath = "index.html";
    }

    final String separator = siteDirectory.getFileSystem().getSeparator();
    // Convert file separators.
    sitePath = sitePath.replace("/", separator);
    Path file = siteDirectory.resolve(sitePath);

    // return not found instead of forbidden to prevent malicious requests to find out if files exist or don't exist
    if (!Files.exists(file) || FileSystemUtils.isHidden(file) ||
        !file.toAbsolutePath().normalize().startsWith(siteDirectory.toAbsolutePath().normalize())) {

        return Responses.contentResponse(
            HttpResponseStatus.NOT_FOUND, alloc, "Requested file [" + file + "] was not found");
    }

    BasicFileAttributes attributes = readAttributes(file, BasicFileAttributes.class);
    if (!attributes.isRegularFile()) {
        // If it's not a regular file, we send a 403
        final String msg = "Requested file [" + file + "] is not a valid file.";
        return Responses.contentResponse(HttpResponseStatus.NOT_FOUND, alloc, msg);
    }
    try {
        byte[] data = Files.readAllBytes(file);
        var resp = Responses.contentResponse(HttpResponseStatus.OK, alloc, data);
        resp.headers().set(HttpHeaderNames.CONTENT_TYPE, guessMimeType(file.toAbsolutePath().toString()));
        return resp;
    } catch (IOException e) {
        return Responses.contentResponse(HttpResponseStatus.INTERNAL_SERVER_ERROR, alloc, e.getMessage());
    }
}