Search in sources :

Example 11 with UserException

use of org.elasticsearch.cli.UserException 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 12 with UserException

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

the class AddStringKeyStoreCommand method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
    KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
    if (keystore == null) {
        throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found. Use 'create' command to create one.");
    }
    keystore.decrypt(new char[0]);
    String setting = arguments.value(options);
    if (setting == null) {
        throw new UserException(ExitCodes.USAGE, "The setting name can not be null");
    }
    if (keystore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
        if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
            terminal.println("Exiting without modifying keystore.");
            return;
        }
    }
    final char[] value;
    if (options.has(stdinOption)) {
        BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
        value = stdinReader.readLine().toCharArray();
    } else {
        value = terminal.readSecret("Enter value for " + setting + ": ");
    }
    try {
        keystore.setString(setting, value);
    } catch (IllegalArgumentException e) {
        throw new UserException(ExitCodes.DATA_ERROR, "String value must contain only ASCII");
    }
    keystore.save(env.configFile());
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) UserException(org.elasticsearch.cli.UserException)

Example 13 with UserException

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

the class RemovePluginCommandTests method testMissingPluginName.

public void testMissingPluginName() throws Exception {
    UserException e = expectThrows(UserException.class, () -> removePlugin(null, home));
    assertEquals(ExitCodes.USAGE, e.exitCode);
    assertEquals("plugin name is required", e.getMessage());
}
Also used : UserException(org.elasticsearch.cli.UserException)

Example 14 with UserException

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

the class RemovePluginCommandTests method testBinNotDir.

public void testBinNotDir() throws Exception {
    Files.createDirectories(env.pluginsFile().resolve("elasticsearch"));
    UserException e = expectThrows(UserException.class, () -> removePlugin("elasticsearch", home));
    assertTrue(e.getMessage(), e.getMessage().contains("not a directory"));
    // did not remove
    assertTrue(Files.exists(env.pluginsFile().resolve("elasticsearch")));
    assertTrue(Files.exists(env.binFile().resolve("elasticsearch")));
    assertRemoveCleaned(env);
}
Also used : UserException(org.elasticsearch.cli.UserException)

Example 15 with UserException

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

the class AddStringKeyStoreCommandTests method testMissing.

public void testMissing() throws Exception {
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
    assertThat(e.getMessage(), containsString("keystore not found"));
}
Also used : UserException(org.elasticsearch.cli.UserException)

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