in mr/src/main/java/org/elasticsearch/hadoop/cli/Keytool.java [175:265]
public int run(String arg, boolean stdin, boolean force) throws IOException {
OutputStream outputStream = null;
InputStream inputStream = null;
KeystoreWrapper keystoreWrapper;
try {
switch (command) {
case CREATE:
if (ksExists()) {
boolean proceed = promptYesNo("An es-hadoop keystore already exists. Overwrite? [y/N]");
if (proceed == false) {
prompt.println("Exiting without creating keystore");
return 0;
}
}
keystoreWrapper = KeystoreWrapper.newStore().build();
outputStream = openWrite();
keystoreWrapper.saveKeystore(outputStream);
return 0;
case LIST:
if (!ksExists()) {
prompt.printf("ERROR: ES-Hadoop keystore not found. Use '%s' command to create one.%n", Command.CREATE.getText());
return 5;
}
inputStream = openRead();
keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build();
for (String entry : keystoreWrapper.listEntries()) {
prompt.println(entry);
}
return 0;
case ADD:
if (!ksExists()) {
prompt.printf("ERROR: ES-Hadoop keystore not found. Use '%s' command to create one.%n", Command.CREATE.getText());
return 5;
}
inputStream = openRead();
keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build();
if (keystoreWrapper.containsEntry(arg) && force == false) {
boolean proceed = promptYesNo("Setting %s already exists. Overwrite? [y/N]", arg);
if (proceed == false) {
prompt.println("Exiting without modifying keystore");
return 0;
}
}
if (stdin) {
String data = prompt.readLine();
keystoreWrapper.setSecureSetting(arg, data);
} else {
char[] data = prompt.readPassword("Enter value for %s:", arg);
keystoreWrapper.setSecureSetting(arg, new String(data));
Arrays.fill(data, (char) 0);
}
outputStream = openWrite();
keystoreWrapper.saveKeystore(outputStream);
return 0;
case REMOVE:
if (!ksExists()) {
prompt.printf("ERROR: ES-Hadoop keystore not found. Use '%s' command to create one.%n", Command.CREATE.getText());
return 5;
}
inputStream = openRead();
keystoreWrapper = KeystoreWrapper.loadStore(inputStream).build();
if (keystoreWrapper.containsEntry(arg) == false) {
prompt.printf("ERROR: Setting [%s] does not exist in the keystore.%n", arg);
return 6;
}
keystoreWrapper.removeSecureSetting(arg);
outputStream = openWrite();
keystoreWrapper.saveKeystore(outputStream);
return 0;
default:
prompt.println("ERROR: Unsupported command " + command.getText());
return 7;
}
} catch (EsHadoopSecurityException ehse) {
prompt.println("ERRORCould not load keystore file: " + ehse.getMessage());
return 8;
} catch (FileNotFoundException fnfe) {
prompt.println("ERROR: Could not load keystore file: " + fnfe.getMessage());
return 9;
} catch (IOException ioe) {
prompt.println("ERROR: " + ioe.getMessage());
return 10;
} finally {
if (outputStream != null) {
outputStream.close();
}
if (inputStream != null) {
inputStream.close();
}
}
}