Search in sources :

Example 1 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class ChangeKeyStorePasswordCommandTests method testChangeKeyStorePasswordWrongExistingPassword.

public void testChangeKeyStorePasswordWrongExistingPassword() throws Exception {
    createKeystore("theoldpassword");
    loadKeystore("theoldpassword");
    terminal.addSecretInput("theoldmisspelledpassword");
    // We'll only be prompted once (for the old password)
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals(e.getMessage(), ExitCodes.DATA_ERROR, e.exitCode);
    if (inFipsJvm()) {
        assertThat(e.getMessage(), anyOf(containsString("Provided keystore password was incorrect"), containsString("Keystore has been corrupted or tampered with")));
    } else {
        assertThat(e.getMessage(), containsString("Provided keystore password was incorrect"));
    }
}
Also used : UserException(org.opensearch.cli.UserException)

Example 2 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class HasPasswordKeyStoreCommandTests method testFailsWhenKeystoreLacksPassword.

public void testFailsWhenKeystoreLacksPassword() throws Exception {
    createKeystore("");
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals("Unexpected exit code", HasPasswordKeyStoreCommand.NO_PASSWORD_EXIT_CODE, e.exitCode);
    assertThat("Exception should have null message", e.getMessage(), is(nullValue()));
}
Also used : UserException(org.opensearch.cli.UserException)

Example 3 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class CreateKeyStoreCommand method execute.

@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
    try (SecureString password = options.has(passwordOption) ? readPassword(terminal, true) : new SecureString(new char[0])) {
        Path keystoreFile = KeyStoreWrapper.keystorePath(env.configFile());
        if (Files.exists(keystoreFile)) {
            if (terminal.promptYesNo("An opensearch keystore already exists. Overwrite?", false) == false) {
                terminal.println("Exiting without creating keystore.");
                return;
            }
        }
        KeyStoreWrapper keystore = KeyStoreWrapper.create();
        keystore.save(env.configFile(), password.getChars());
        terminal.println("Created opensearch keystore in " + KeyStoreWrapper.keystorePath(env.configFile()));
    } catch (SecurityException e) {
        throw new UserException(ExitCodes.IO_ERROR, "Error creating the opensearch keystore.");
    }
}
Also used : Path(java.nio.file.Path) UserException(org.opensearch.cli.UserException)

Example 4 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddStringKeyStoreCommandTests method testMissingSettingName.

public void testMissingSettingName() throws Exception {
    String password = "keystorepassword";
    createKeystore(password);
    terminal.addSecretInput(password);
    terminal.addSecretInput(password);
    terminal.addTextInput("");
    UserException e = expectThrows(UserException.class, this::execute);
    assertEquals(ExitCodes.USAGE, e.exitCode);
    assertThat(e.getMessage(), containsString("the setting names can not be empty"));
}
Also used : Matchers.hasToString(org.hamcrest.Matchers.hasToString) Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 5 with UserException

use of org.opensearch.cli.UserException in project OpenSearch by opensearch-project.

the class AddFileKeyStoreCommand method executeCommand.

@Override
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
    final List<String> argumentValues = arguments.values(options);
    if (argumentValues.size() == 0) {
        throw new UserException(ExitCodes.USAGE, "Missing setting name");
    }
    if (argumentValues.size() % 2 != 0) {
        throw new UserException(ExitCodes.USAGE, "settings and filenames must come in pairs");
    }
    final KeyStoreWrapper keyStore = getKeyStore();
    for (int i = 0; i < argumentValues.size(); i += 2) {
        final String setting = argumentValues.get(i);
        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 Path file = getPath(argumentValues.get(i + 1));
        if (Files.exists(file) == false) {
            throw new UserException(ExitCodes.IO_ERROR, "File [" + file.toString() + "] does not exist");
        }
        keyStore.setFile(setting, Files.readAllBytes(file));
    }
    keyStore.save(env.configFile(), getKeyStorePassword().getChars());
}
Also used : Path(java.nio.file.Path) UserException(org.opensearch.cli.UserException)

Aggregations

UserException (org.opensearch.cli.UserException)76 Path (java.nio.file.Path)44 Matchers.containsString (org.hamcrest.Matchers.containsString)38 Environment (org.opensearch.env.Environment)29 Matchers.hasToString (org.hamcrest.Matchers.hasToString)25 TestEnvironment (org.opensearch.env.TestEnvironment)25 IOException (java.io.IOException)16 Settings (org.opensearch.common.settings.Settings)16 ArrayList (java.util.ArrayList)12 BufferedReader (java.io.BufferedReader)11 Files (java.nio.file.Files)11 MockTerminal (org.opensearch.cli.MockTerminal)11 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)11 InputStream (java.io.InputStream)10 List (java.util.List)10 Collectors (java.util.stream.Collectors)10 Terminal (org.opensearch.cli.Terminal)10 Tuple (org.opensearch.common.collect.Tuple)10 StringReader (java.io.StringReader)9 URL (java.net.URL)9