|
@@ -19,11 +19,6 @@
|
|
|
|
|
|
package org.elasticsearch.common.settings;
|
|
|
|
|
|
-import java.nio.file.Files;
|
|
|
-import java.nio.file.Path;
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
-
|
|
|
import joptsimple.OptionSet;
|
|
|
import joptsimple.OptionSpec;
|
|
|
import org.elasticsearch.cli.ExitCodes;
|
|
@@ -33,6 +28,11 @@ import org.elasticsearch.common.SuppressForbidden;
|
|
|
import org.elasticsearch.common.io.PathUtils;
|
|
|
import org.elasticsearch.env.Environment;
|
|
|
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.util.Arrays;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
/**
|
|
|
* A subcommand for the keystore cli which adds a file setting.
|
|
|
*/
|
|
@@ -49,38 +49,39 @@ class AddFileKeyStoreCommand extends BaseKeyStoreCommand {
|
|
|
// jopt simple has issue with multiple non options, so we just get one set of them here
|
|
|
// and convert to File when necessary
|
|
|
// see https://github.com/jopt-simple/jopt-simple/issues/103
|
|
|
- this.arguments = parser.nonOptions("setting [filepath]");
|
|
|
+ this.arguments = parser.nonOptions("(setting path)+");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
protected void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception {
|
|
|
- List<String> argumentValues = arguments.values(options);
|
|
|
+ final List<String> argumentValues = arguments.values(options);
|
|
|
if (argumentValues.size() == 0) {
|
|
|
throw new UserException(ExitCodes.USAGE, "Missing setting name");
|
|
|
}
|
|
|
- String setting = argumentValues.get(0);
|
|
|
+ if (argumentValues.size() % 2 != 0) {
|
|
|
+ throw new UserException(ExitCodes.USAGE, "settings and filenames must come in pairs");
|
|
|
+ }
|
|
|
+
|
|
|
final KeyStoreWrapper keyStore = getKeyStore();
|
|
|
- if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
|
|
|
- if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
|
|
|
- terminal.println("Exiting without modifying keystore.");
|
|
|
- return;
|
|
|
+
|
|
|
+ for (int i = 0; i < argumentValues.size(); i += 2) {
|
|
|
+ final String setting = argumentValues.get(i);
|
|
|
+
|
|
|
+ if (keyStore.getSettingNames().contains(setting) && options.has(forceOption) == false) {
|
|
|
+ if (terminal.promptYesNo("Setting " + setting + " already exists. Overwrite?", false) == false) {
|
|
|
+ terminal.println("Exiting without modifying keystore.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if (argumentValues.size() == 1) {
|
|
|
- throw new UserException(ExitCodes.USAGE, "Missing file name");
|
|
|
- }
|
|
|
- Path file = getPath(argumentValues.get(1));
|
|
|
- if (Files.exists(file) == false) {
|
|
|
- throw new UserException(ExitCodes.IO_ERROR, "File [" + file.toString() + "] does not exist");
|
|
|
- }
|
|
|
- if (argumentValues.size() > 2) {
|
|
|
- throw new UserException(
|
|
|
- ExitCodes.USAGE,
|
|
|
- "Unrecognized extra arguments [" + String.join(", ", argumentValues.subList(2, argumentValues.size())) + "] after filepath"
|
|
|
- );
|
|
|
+ final Path file = getPath(argumentValues.get(i + 1));
|
|
|
+ if (Files.exists(file) == false) {
|
|
|
+ throw new UserException(ExitCodes.IO_ERROR, "File [" + file.toString() + "] does not exist");
|
|
|
+ }
|
|
|
+
|
|
|
+ keyStore.setFile(setting, Files.readAllBytes(file));
|
|
|
}
|
|
|
- keyStore.setFile(setting, Files.readAllBytes(file));
|
|
|
+
|
|
|
keyStore.save(env.configFile(), getKeyStorePassword().getChars());
|
|
|
}
|
|
|
|
|
@@ -88,4 +89,5 @@ class AddFileKeyStoreCommand extends BaseKeyStoreCommand {
|
|
|
private Path getPath(String file) {
|
|
|
return PathUtils.get(file);
|
|
|
}
|
|
|
+
|
|
|
}
|