Skip to content

Commit a809fd7

Browse files
authored
Use Terminal.readSecret in add string keystore command (#126966) (#127070)
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
1 parent 0c48a96 commit a809fd7

File tree

1 file changed

+19
-36
lines changed

1 file changed

+19
-36
lines changed

distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/AddStringKeyStoreCommand.java

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@
1919
import org.elasticsearch.core.CheckedFunction;
2020
import org.elasticsearch.env.Environment;
2121

22-
import java.io.CharArrayWriter;
23-
import java.io.Closeable;
2422
import java.io.IOException;
25-
import java.io.Reader;
2623
import java.util.Arrays;
2724
import java.util.List;
2825

@@ -53,42 +50,28 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
5350

5451
final KeyStoreWrapper keyStore = getKeyStore();
5552

56-
final Closeable closeable;
57-
final CheckedFunction<String, char[], IOException> valueSupplier;
58-
if (options.has(stdinOption)) {
59-
final Reader stdinReader = terminal.getReader();
60-
valueSupplier = s -> {
61-
try (CharArrayWriter writer = new CharArrayWriter()) {
62-
int c;
63-
while ((c = stdinReader.read()) != -1) {
64-
if ((char) c == '\r' || (char) c == '\n') {
65-
break;
66-
}
67-
writer.write((char) c);
68-
}
69-
return writer.toCharArray();
70-
}
71-
};
72-
closeable = stdinReader;
73-
} else {
74-
valueSupplier = s -> terminal.readSecret("Enter value for " + s + ": ");
75-
closeable = () -> {};
76-
}
53+
final CheckedFunction<String, char[], IOException> valueSupplier = s -> {
54+
final String prompt;
55+
if (options.has(stdinOption)) {
56+
prompt = "";
57+
} else {
58+
prompt = "Enter value for " + s + ": ";
59+
}
60+
return terminal.readSecret(prompt);
61+
};
7762

78-
try (closeable) {
79-
for (final String setting : settings) {
80-
if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
81-
if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
82-
terminal.println("Exiting without modifying keystore.");
83-
return;
84-
}
63+
for (final String setting : settings) {
64+
if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
65+
if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
66+
terminal.println("Exiting without modifying keystore.");
67+
return;
8568
}
69+
}
8670

87-
try {
88-
keyStore.setString(setting, valueSupplier.apply(setting));
89-
} catch (final IllegalArgumentException e) {
90-
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
91-
}
71+
try {
72+
keyStore.setString(setting, valueSupplier.apply(setting));
73+
} catch (final IllegalArgumentException e) {
74+
throw new UserException(ExitCodes.DATA_ERROR, e.getMessage());
9275
}
9376
}
9477

0 commit comments

Comments
 (0)