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.
This commit is contained in:
Ryan Ernst 2017-08-15 10:15:55 -07:00 committed by GitHub
parent a51faea79f
commit 7ed501b230
4 changed files with 55 additions and 14 deletions

View File

@ -61,11 +61,18 @@ class AddFileKeyStoreCommand extends EnvironmentAwareCommand {
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception { protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile()); KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
if (keystore == null) { if (keystore == null) {
throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found. Use 'create' command to create one."); if (options.has(forceOption) == false &&
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
terminal.println("Exiting without creating keystore.");
return;
}
keystore = KeyStoreWrapper.create(new char[0] /* always use empty passphrase for auto created keystore */);
keystore.save(env.configFile());
terminal.println("Created elasticsearch keystore in " + env.configFile());
} else {
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
} }
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
List<String> argumentValues = arguments.values(options); List<String> argumentValues = arguments.values(options);
if (argumentValues.size() == 0) { if (argumentValues.size() == 0) {
throw new UserException(ExitCodes.USAGE, "Missing setting name"); throw new UserException(ExitCodes.USAGE, "Missing setting name");

View File

@ -58,11 +58,18 @@ class AddStringKeyStoreCommand extends EnvironmentAwareCommand {
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception { protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile()); KeyStoreWrapper keystore = KeyStoreWrapper.load(env.configFile());
if (keystore == null) { if (keystore == null) {
throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found. Use 'create' command to create one."); if (options.has(forceOption) == false &&
terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) {
terminal.println("Exiting without creating keystore.");
return;
}
keystore = KeyStoreWrapper.create(new char[0] /* always use empty passphrase for auto created keystore */);
keystore.save(env.configFile());
terminal.println("Created elasticsearch keystore in " + env.configFile());
} else {
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
} }
keystore.decrypt(new char[0] /* TODO: prompt for password when they are supported */);
String setting = arguments.value(options); String setting = arguments.value(options);
if (setting == null) { if (setting == null) {
throw new UserException(ExitCodes.USAGE, "The setting name can not be null"); throw new UserException(ExitCodes.USAGE, "The setting name can not be null");

View File

@ -59,10 +59,24 @@ public class AddFileKeyStoreCommandTests extends KeyStoreCommandTestCase {
keystore.save(env.configFile()); keystore.save(env.configFile());
} }
public void testMissing() throws Exception { public void testMissingPromptCreate() throws Exception {
UserException e = expectThrows(UserException.class, this::execute); Path file1 = createRandomFile();
assertEquals(ExitCodes.DATA_ERROR, e.exitCode); terminal.addTextInput("y");
assertThat(e.getMessage(), containsString("keystore not found")); execute("foo", file1.toString());
assertSecureFile("foo", file1);
}
public void testMissingForceCreate() throws Exception {
Path file1 = createRandomFile();
terminal.addSecretInput("bar");
execute("-f", "foo", file1.toString());
assertSecureFile("foo", file1);
}
public void testMissingNoCreate() throws Exception {
terminal.addTextInput("n"); // explicit no
execute("foo");
assertNull(KeyStoreWrapper.load(env.configFile()));
} }
public void testOverwritePromptDefault() throws Exception { public void testOverwritePromptDefault() throws Exception {

View File

@ -49,10 +49,23 @@ public class AddStringKeyStoreCommandTests extends KeyStoreCommandTestCase {
}; };
} }
public void testMissing() throws Exception { public void testMissingPromptCreate() throws Exception {
UserException e = expectThrows(UserException.class, this::execute); terminal.addTextInput("y");
assertEquals(ExitCodes.DATA_ERROR, e.exitCode); terminal.addSecretInput("bar");
assertThat(e.getMessage(), containsString("keystore not found")); execute("foo");
assertSecureString("foo", "bar");
}
public void testMissingForceCreate() throws Exception {
terminal.addSecretInput("bar");
execute("-f", "foo");
assertSecureString("foo", "bar");
}
public void testMissingNoCreate() throws Exception {
terminal.addTextInput("n"); // explicit no
execute("foo");
assertNull(KeyStoreWrapper.load(env.configFile()));
} }
public void testOverwritePromptDefault() throws Exception { public void testOverwritePromptDefault() throws Exception {