Handle empty input in AddStringKeyStoreCommand (#39490)
This change ensures that we do not make assumptions about the length of the input that we can read from the stdin. It still consumes only one line, as the previous implementation
This commit is contained in:
parent
e050d50759
commit
35aaf04c8c
|
@ -20,6 +20,7 @@
|
|||
package org.elasticsearch.common.settings;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.CharArrayWriter;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
@ -83,8 +84,17 @@ class AddStringKeyStoreCommand extends EnvironmentAwareCommand {
|
|||
|
||||
final char[] value;
|
||||
if (options.has(stdinOption)) {
|
||||
BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
|
||||
value = stdinReader.readLine().toCharArray();
|
||||
try (BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
|
||||
CharArrayWriter writer = new CharArrayWriter()) {
|
||||
int charInt;
|
||||
while ((charInt = stdinReader.read()) != -1) {
|
||||
if ((char) charInt == '\r' || (char) charInt == '\n') {
|
||||
break;
|
||||
}
|
||||
writer.write((char) charInt);
|
||||
}
|
||||
value = writer.toCharArray();
|
||||
}
|
||||
} else {
|
||||
value = terminal.readSecret("Enter value for " + setting + ": ");
|
||||
}
|
||||
|
|
|
@ -134,6 +134,27 @@ public class AddStringKeyStoreCommandTests extends KeyStoreCommandTestCase {
|
|||
assertSecureString("foo", "secret value 2");
|
||||
}
|
||||
|
||||
public void testStdinNoInput() throws Exception {
|
||||
KeyStoreWrapper.create().save(env.configFile(), new char[0]);
|
||||
setInput("");
|
||||
execute("-x", "foo");
|
||||
assertSecureString("foo", "");
|
||||
}
|
||||
|
||||
public void testStdinInputWithLineBreaks() throws Exception {
|
||||
KeyStoreWrapper.create().save(env.configFile(), new char[0]);
|
||||
setInput("Typedthisandhitenter\n");
|
||||
execute("-x", "foo");
|
||||
assertSecureString("foo", "Typedthisandhitenter");
|
||||
}
|
||||
|
||||
public void testStdinInputWithCarriageReturn() throws Exception {
|
||||
KeyStoreWrapper.create().save(env.configFile(), new char[0]);
|
||||
setInput("Typedthisandhitenter\r");
|
||||
execute("-x", "foo");
|
||||
assertSecureString("foo", "Typedthisandhitenter");
|
||||
}
|
||||
|
||||
public void testAddUtf8String() throws Exception {
|
||||
KeyStoreWrapper.create().save(env.configFile(), new char[0]);
|
||||
final int stringSize = randomIntBetween(8, 16);
|
||||
|
|
Loading…
Reference in New Issue