From 0762e52b8608577e4542db0f282f14752b948fa5 Mon Sep 17 00:00:00 2001 From: Ryan Ernst Date: Fri, 18 Apr 2025 10:50:15 -0700 Subject: [PATCH] Use Terminal.readSecret in add string keystore command (#126966) As a followon to #126729, the add string keystore command doesn't need to use a reader at all (and it was incorrect for it to close the reader from the terminal). Instead, the Terminal abstraction already handles how to get at line by line secrets. This commit removes that usage of reader and uses readSecret calls instead. closes #126882 --- .../keystore/AddStringKeyStoreCommand.java | 55 +++++++------------ 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/AddStringKeyStoreCommand.java b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/AddStringKeyStoreCommand.java index 7d89879958dba..f8f5f9d3865c0 100644 --- a/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/AddStringKeyStoreCommand.java +++ b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/AddStringKeyStoreCommand.java @@ -19,10 +19,7 @@ import org.elasticsearch.core.CheckedFunction; import org.elasticsearch.env.Environment; -import java.io.CharArrayWriter; -import java.io.Closeable; import java.io.IOException; -import java.io.Reader; import java.util.Arrays; import java.util.List; @@ -53,42 +50,28 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment final KeyStoreWrapper keyStore = getKeyStore(); - final Closeable closeable; - final CheckedFunction valueSupplier; - if (options.has(stdinOption)) { - final Reader stdinReader = terminal.getReader(); - valueSupplier = s -> { - try (CharArrayWriter writer = new CharArrayWriter()) { - int c; - while ((c = stdinReader.read()) != -1) { - if ((char) c == '\r' || (char) c == '\n') { - break; - } - writer.write((char) c); - } - return writer.toCharArray(); - } - }; - closeable = stdinReader; - } else { - valueSupplier = s -> terminal.readSecret("Enter value for " + s + ": "); - closeable = () -> {}; - } + final CheckedFunction valueSupplier = s -> { + final String prompt; + if (options.has(stdinOption)) { + prompt = ""; + } else { + prompt = "Enter value for " + s + ": "; + } + return terminal.readSecret(prompt); + }; - try (closeable) { - for (final String setting : settings) { - 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; - } + for (final String setting : settings) { + 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; } + } - try { - keyStore.setString(setting, valueSupplier.apply(setting)); - } catch (final IllegalArgumentException e) { - throw new UserException(ExitCodes.DATA_ERROR, e.getMessage()); - } + try { + keyStore.setString(setting, valueSupplier.apply(setting)); + } catch (final IllegalArgumentException e) { + throw new UserException(ExitCodes.DATA_ERROR, e.getMessage()); } }