Search in sources :

Example 6 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testMissingDirectory.

public void testMissingDirectory() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    Files.createFile(pluginDir.resolve(PluginInfo.ES_PLUGIN_PROPERTIES));
    String pluginZip = writeZip(pluginDir, null);
    UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("`elasticsearch` directory is missing in the plugin zip"));
    assertInstallCleaned(env.v2());
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException)

Example 7 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testMissingPluginId.

public void testMissingPluginId() throws IOException {
    final Tuple<Path, Environment> env = createEnv(fs, temp);
    final UserException e = expectThrows(UserException.class, () -> installPlugin(null, env.v1()));
    assertTrue(e.getMessage(), e.getMessage().contains("plugin id is required"));
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) UserException(org.elasticsearch.cli.UserException)

Example 8 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testInstallMisspelledOfficialPlugins.

public void testInstallMisspelledOfficialPlugins() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    UserException e = expectThrows(UserException.class, () -> installPlugin("xpack", env.v1()));
    assertThat(e.getMessage(), containsString("Unknown plugin xpack, did you mean [x-pack]?"));
    e = expectThrows(UserException.class, () -> installPlugin("analysis-smartnc", env.v1()));
    assertThat(e.getMessage(), containsString("Unknown plugin analysis-smartnc, did you mean [analysis-smartcn]?"));
    e = expectThrows(UserException.class, () -> installPlugin("repository", env.v1()));
    assertThat(e.getMessage(), containsString("Unknown plugin repository, did you mean any of [repository-s3, repository-gcs]?"));
    e = expectThrows(UserException.class, () -> installPlugin("unknown_plugin", env.v1()));
    assertThat(e.getMessage(), containsString("Unknown plugin unknown_plugin"));
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) UserException(org.elasticsearch.cli.UserException)

Example 9 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommandTests method testPluginAlreadyInstalled.

public void testPluginAlreadyInstalled() throws Exception {
    Tuple<Path, Environment> env = createEnv(fs, temp);
    Path pluginDir = createPluginDir(temp);
    String pluginZip = createPlugin("fake", pluginDir);
    installPlugin(pluginZip, env.v1());
    final UserException e = expectThrows(UserException.class, () -> installPlugin(pluginZip, env.v1(), randomBoolean()));
    assertThat(e.getMessage(), equalTo("plugin directory [" + env.v2().pluginsFile().resolve("fake") + "] already exists; " + "if you need to update the plugin, uninstall it first using command 'remove fake'"));
}
Also used : Path(java.nio.file.Path) Environment(org.elasticsearch.env.Environment) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.elasticsearch.cli.UserException)

Example 10 with UserException

use of org.elasticsearch.cli.UserException in project elasticsearch by elastic.

the class InstallPluginCommand method downloadZipAndChecksum.

/** Downloads a zip from the url, as well as a SHA1 checksum, and checks the checksum. */
@SuppressForbidden(reason = "We use openStream to download plugins")
private Path downloadZipAndChecksum(Terminal terminal, String urlString, Path tmpDir) throws Exception {
    Path zip = downloadZip(terminal, urlString, tmpDir);
    pathsToDeleteOnShutdown.add(zip);
    URL checksumUrl = new URL(urlString + ".sha1");
    final String expectedChecksum;
    try (InputStream in = checksumUrl.openStream()) {
        BufferedReader checksumReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        expectedChecksum = checksumReader.readLine();
        if (checksumReader.readLine() != null) {
            throw new UserException(ExitCodes.IO_ERROR, "Invalid checksum file at " + checksumUrl);
        }
    }
    byte[] zipbytes = Files.readAllBytes(zip);
    String gotChecksum = MessageDigests.toHexString(MessageDigests.sha1().digest(zipbytes));
    if (expectedChecksum.equals(gotChecksum) == false) {
        throw new UserException(ExitCodes.IO_ERROR, "SHA1 mismatch, expected " + expectedChecksum + " but got " + gotChecksum);
    }
    return zip;
}
Also used : Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) UserException(org.elasticsearch.cli.UserException) URL(java.net.URL) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Aggregations

UserException (org.elasticsearch.cli.UserException)39 Path (java.nio.file.Path)24 Environment (org.elasticsearch.env.Environment)14 Matchers.containsString (org.hamcrest.Matchers.containsString)9 IOException (java.io.IOException)5 Settings (org.elasticsearch.common.settings.Settings)4 BufferedReader (java.io.BufferedReader)3 ArrayList (java.util.ArrayList)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 FileVisitOption (java.nio.file.FileVisitOption)2 FileVisitResult (java.nio.file.FileVisitResult)2 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)2 ZipInputStream (java.util.zip.ZipInputStream)2 Logger (org.apache.logging.log4j.Logger)2 LoggerContext (org.apache.logging.log4j.core.LoggerContext)2 AbstractConfiguration (org.apache.logging.log4j.core.config.AbstractConfiguration)2 CompositeConfiguration (org.apache.logging.log4j.core.config.composite.CompositeConfiguration)2 PropertiesConfiguration (org.apache.logging.log4j.core.config.properties.PropertiesConfiguration)2 PropertiesConfigurationFactory (org.apache.logging.log4j.core.config.properties.PropertiesConfigurationFactory)2