Search in sources :

Example 21 with UserException

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

the class CreateKeyStoreCommandTests method testNotMatchingPasswords.

public void testNotMatchingPasswords() throws Exception {
    String password = randomFrom("", "keystorepassword");
    terminal.addSecretInput(password);
    terminal.addSecretInput("notthekeystorepasswordyouarelookingfor");
    UserException e = expectThrows(UserException.class, () -> execute(randomFrom("-p", "--password")));
    assertEquals(e.getMessage(), ExitCodes.DATA_ERROR, e.exitCode);
    assertThat(e.getMessage(), containsString("Passwords are not equal, exiting"));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) UserException(org.opensearch.cli.UserException)

Example 22 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 23 with UserException

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

the class BaseKeyStoreCommand method execute.

@Override
protected final void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
    try {
        final Path configFile = env.configDir();
        keyStore = KeyStoreWrapper.load(configFile);
        if (keyStore == null) {
            if (keyStoreMustExist) {
                throw new UserException(ExitCodes.DATA_ERROR, "OpenSearch keystore not found at [" + KeyStoreWrapper.keystorePath(env.configDir()) + "]. Use 'create' command to create one.");
            } else if (options.has(forceOption) == false) {
                if (terminal.promptYesNo("The opensearch keystore does not exist. Do you want to create it?", false) == false) {
                    terminal.println("Exiting without creating keystore.");
                    return;
                }
            }
            keyStorePassword = new SecureString(new char[0]);
            keyStore = KeyStoreWrapper.create();
            keyStore.save(configFile, keyStorePassword.getChars());
        } else {
            keyStorePassword = keyStore.hasPassword() ? readPassword(terminal, false) : new SecureString(new char[0]);
            keyStore.decrypt(keyStorePassword.getChars());
        }
        executeCommand(terminal, options, env);
    } catch (SecurityException e) {
        throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
    } finally {
        if (keyStorePassword != null) {
            keyStorePassword.close();
        }
    }
}
Also used : Path(java.nio.file.Path) UserException(org.opensearch.cli.UserException)

Example 24 with UserException

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

the class ChangeKeyStorePasswordCommand method executeCommand.

@Override
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
    try (SecureString newPassword = readPassword(terminal, true)) {
        final KeyStoreWrapper keyStore = getKeyStore();
        keyStore.save(env.configDir(), newPassword.getChars());
        terminal.println("OpenSearch keystore password changed successfully.");
    } catch (SecurityException e) {
        throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
    }
}
Also used : UserException(org.opensearch.cli.UserException)

Example 25 with UserException

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

the class RemoveCustomsCommand method processNodePaths.

@Override
protected void processNodePaths(Terminal terminal, Path[] dataPaths, int nodeLockId, OptionSet options, Environment env) throws IOException, UserException {
    final List<String> customsToRemove = arguments.values(options);
    if (customsToRemove.isEmpty()) {
        throw new UserException(ExitCodes.USAGE, "Must supply at least one custom metadata name to remove");
    }
    final PersistedClusterStateService persistedClusterStateService = createPersistedClusterStateService(env.settings(), dataPaths);
    terminal.println(Terminal.Verbosity.VERBOSE, "Loading cluster state");
    final Tuple<Long, ClusterState> termAndClusterState = loadTermAndClusterState(persistedClusterStateService, env);
    final ClusterState oldClusterState = termAndClusterState.v2();
    terminal.println(Terminal.Verbosity.VERBOSE, "custom metadata names: " + oldClusterState.metadata().customs().keys());
    final Metadata.Builder metadataBuilder = Metadata.builder(oldClusterState.metadata());
    for (String customToRemove : customsToRemove) {
        boolean matched = false;
        for (ObjectCursor<String> customKeyCur : oldClusterState.metadata().customs().keys()) {
            final String customKey = customKeyCur.value;
            if (Regex.simpleMatch(customToRemove, customKey)) {
                metadataBuilder.removeCustom(customKey);
                if (matched == false) {
                    terminal.println("The following customs will be removed:");
                }
                matched = true;
                terminal.println(customKey);
            }
        }
        if (matched == false) {
            throw new UserException(ExitCodes.USAGE, "No custom metadata matching [" + customToRemove + "] were found on this node");
        }
    }
    final ClusterState newClusterState = ClusterState.builder(oldClusterState).metadata(metadataBuilder.build()).build();
    terminal.println(Terminal.Verbosity.VERBOSE, "[old cluster state = " + oldClusterState + ", new cluster state = " + newClusterState + "]");
    confirm(terminal, CONFIRMATION_MSG);
    try (PersistedClusterStateService.Writer writer = persistedClusterStateService.createWriter()) {
        writer.writeFullStateAndCommit(termAndClusterState.v1(), newClusterState);
    }
    terminal.println(CUSTOMS_REMOVED_MSG);
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) Metadata(org.opensearch.cluster.metadata.Metadata) UserException(org.opensearch.cli.UserException) PersistedClusterStateService(org.opensearch.gateway.PersistedClusterStateService)

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