Skip to content

Commit d33c3a6

Browse files
committed
Use Terminal.readSecret in add string keystore command
As a followon to elastic#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 elastic#126882
1 parent 22adfeb commit d33c3a6

File tree

1 file changed

+18
-34
lines changed

1 file changed

+18
-34
lines changed

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

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -53,42 +53,26 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
5353

5454
final KeyStoreWrapper keyStore = getKeyStore();
5555

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-
}
77-
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-
}
56+
final CheckedFunction<String, char[], IOException> valueSupplier = s -> {
57+
String prompt = "";
58+
if (options.has(stdinOption) == false) {
59+
prompt = "Enter value for " + s + ": ";
60+
}
61+
return terminal.readSecret(prompt);
62+
};
63+
64+
for (final String setting : settings) {
65+
if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
66+
if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
67+
terminal.println("Exiting without modifying keystore.");
68+
return;
8569
}
70+
}
8671

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

0 commit comments

Comments
 (0)