Skip to content

Commit c241252

Browse files
committed
Settings: Add keystore creation to add commands (#26126)
This commits changes the keystore cli add commands to prompt for creating the keystore if it does not exist. This will make it easier on users starting out, not having to run a separate command for creation.
1 parent 850606b commit c241252

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

server/src/main/java/org/elasticsearch/common/settings/AddFileKeyStoreCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,18 @@ class AddFileKeyStoreCommand extends EnvironmentAwareCommand {
6161
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
6262
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
6363
if (keystore == null) {
64-
throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found. Use 'create' command to create one.");
64+
if (options.has(forceOption) == false &&
65+
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
66+
terminal.println("Exiting without creating keystore.");
67+
return;
68+
}
69+
keystore = KeyStoreWrapper.create(new char[0] /* always use empty passphrase for auto created keystore */);
70+
keystore.save(env.configFile());
71+
terminal.println("Created elasticsearch keystore in " + env.configFile());
72+
} else {
73+
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
6574
}
6675

67-
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
68-
6976
List<String> argumentValues = arguments.values(options);
7077
if (argumentValues.size() == 0) {
7178
throw new UserException(ExitCodes.USAGE, "Missing setting name");

server/src/main/java/org/elasticsearch/common/settings/AddStringKeyStoreCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,18 @@ InputStream getStdin() {
5858
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
5959
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
6060
if (keystore == null) {
61-
throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found. Use 'create' command to create one.");
61+
if (options.has(forceOption) == false &&
62+
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
63+
terminal.println("Exiting without creating keystore.");
64+
return;
65+
}
66+
keystore = KeyStoreWrapper.create(new char[0] /* always use empty passphrase for auto created keystore */);
67+
keystore.save(env.configFile());
68+
terminal.println("Created elasticsearch keystore in " + env.configFile());
69+
} else {
70+
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
6271
}
6372

64-
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
65-
6673
String setting = arguments.value(options);
6774
if (setting == null) {
6875
throw new UserException(ExitCodes.USAGE, "The setting name can not be null");

server/src/test/java/org/elasticsearch/common/settings/AddFileKeyStoreCommandTests.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,24 @@ private void addFile(KeyStoreWrapper keystore, String setting, Path file) throws
5959
keystore.save(env.configFile());
6060
}
6161

62-
public void testMissing() throws Exception {
63-
UserException e = expectThrows(UserException.class, this::execute);
64-
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
65-
assertThat(e.getMessage(), containsString("keystore not found"));
62+
public void testMissingPromptCreate() throws Exception {
63+
Path file1 = createRandomFile();
64+
terminal.addTextInput("y");
65+
execute("foo", file1.toString());
66+
assertSecureFile("foo", file1);
67+
}
68+
69+
public void testMissingForceCreate() throws Exception {
70+
Path file1 = createRandomFile();
71+
terminal.addSecretInput("bar");
72+
execute("-f", "foo", file1.toString());
73+
assertSecureFile("foo", file1);
74+
}
75+
76+
public void testMissingNoCreate() throws Exception {
77+
terminal.addTextInput("n"); // explicit no
78+
execute("foo");
79+
assertNull(KeyStoreWrapper.load(env.configFile()));
6680
}
6781

6882
public void testOverwritePromptDefault() throws Exception {

server/src/test/java/org/elasticsearch/common/settings/AddStringKeyStoreCommandTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,23 @@ InputStream getStdin() {
4949
};
5050
}
5151

52-
public void testMissing() throws Exception {
53-
UserException e = expectThrows(UserException.class, this::execute);
54-
assertEquals(ExitCodes.DATA_ERROR, e.exitCode);
55-
assertThat(e.getMessage(), containsString("keystore not found"));
52+
public void testMissingPromptCreate() throws Exception {
53+
terminal.addTextInput("y");
54+
terminal.addSecretInput("bar");
55+
execute("foo");
56+
assertSecureString("foo", "bar");
57+
}
58+
59+
public void testMissingForceCreate() throws Exception {
60+
terminal.addSecretInput("bar");
61+
execute("-f", "foo");
62+
assertSecureString("foo", "bar");
63+
}
64+
65+
public void testMissingNoCreate() throws Exception {
66+
terminal.addTextInput("n"); // explicit no
67+
execute("foo");
68+
assertNull(KeyStoreWrapper.load(env.configFile()));
5669
}
5770

5871
public void testOverwritePromptDefault() throws Exception {

0 commit comments

Comments
 (0)