Skip to content

Use terminal reader in keystore add command (#126729) #126963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
import org.elasticsearch.core.CheckedFunction;
import org.elasticsearch.env.Environment;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;

Expand All @@ -47,11 +44,6 @@ class AddStringKeyStoreCommand extends BaseKeyStoreCommand {
this.arguments = parser.nonOptions("setting names");
}

// pkg private so tests can manipulate
InputStream getStdin() {
return System.in;
}

@Override
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
final List<String> settings = arguments.values(options);
Expand All @@ -64,7 +56,7 @@ protected void executeCommand(Terminal terminal, OptionSet options, Environment
final Closeable closeable;
final CheckedFunction<String, char[], IOException> valueSupplier;
if (options.has(stdinOption)) {
final BufferedReader stdinReader = new BufferedReader(new InputStreamReader(getStdin(), StandardCharsets.UTF_8));
final Reader stdinReader = terminal.getReader();
valueSupplier = s -> {
try (CharArrayWriter writer = new CharArrayWriter()) {
int c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,20 @@
import org.elasticsearch.common.settings.KeyStoreWrapper;
import org.elasticsearch.env.Environment;

import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasToString;

public class AddStringKeyStoreCommandTests extends KeyStoreCommandTestCase {
InputStream input;

@Override
protected Command newCommand() {
return new AddStringKeyStoreCommand() {
@Override
protected Environment createEnv(OptionSet options, ProcessInfo processInfo) throws UserException {
return env;
}

@Override
InputStream getStdin() {
return input;
}
};
}

Expand Down Expand Up @@ -167,7 +157,7 @@ public void testStdinShort() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("secret value 1");
terminal.addSecretInput("secret value 1");
execute("-x", "foo");
assertSecureString("foo", "secret value 1", password);
}
Expand All @@ -176,7 +166,7 @@ public void testStdinLong() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("secret value 2");
terminal.addSecretInput("secret value 2");
execute("--stdin", "foo");
assertSecureString("foo", "secret value 2", password);
}
Expand All @@ -185,7 +175,7 @@ public void testStdinNoInput() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("");
terminal.addSecretInput("");
execute("-x", "foo");
assertSecureString("foo", "", password);
}
Expand All @@ -194,7 +184,7 @@ public void testStdinInputWithLineBreaks() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("Typedthisandhitenter\n");
terminal.addSecretInput("Typedthisandhitenter\n");
execute("-x", "foo");
assertSecureString("foo", "Typedthisandhitenter", password);
}
Expand All @@ -203,7 +193,7 @@ public void testStdinInputWithCarriageReturn() throws Exception {
String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("Typedthisandhitenter\r");
terminal.addSecretInput("Typedthisandhitenter\r");
execute("-x", "foo");
assertSecureString("foo", "Typedthisandhitenter", password);
}
Expand All @@ -212,7 +202,9 @@ public void testStdinWithMultipleValues() throws Exception {
final String password = "keystorepassword";
KeyStoreWrapper.create().save(env.configDir(), password.toCharArray());
terminal.addSecretInput(password);
setInput("bar1\nbar2\nbar3");
terminal.addSecretInput("bar1");
terminal.addSecretInput("bar2");
terminal.addSecretInput("bar3");
execute(randomFrom("-x", "--stdin"), "foo1", "foo2", "foo3");
assertSecureString("foo1", "bar1", password);
assertSecureString("foo2", "bar2", password);
Expand All @@ -228,7 +220,7 @@ public void testAddUtf8String() throws Exception {
for (int i = 0; i < stringSize; i++) {
secretChars.write((char) randomIntBetween(129, 2048));
}
setInput(secretChars.toString());
terminal.addSecretInput(secretChars.toString());
execute("-x", "foo");
assertSecureString("foo", secretChars.toString(), password);
}
Expand Down Expand Up @@ -265,8 +257,4 @@ public void testAddToUnprotectedKeystore() throws Exception {
execute("foo");
assertSecureString("foo", "bar", password);
}

void setInput(String inputStr) {
input = new ByteArrayInputStream(inputStr.getBytes(StandardCharsets.UTF_8));
}
}
6 changes: 6 additions & 0 deletions docs/changelog/126729.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 126729
summary: Use terminal reader in keystore add command
area: Infra/CLI
type: bug
issues:
- 98115