|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.common.settings; |
| 21 | + |
| 22 | +import joptsimple.OptionSet; |
| 23 | +import org.elasticsearch.cli.EnvironmentAwareCommand; |
| 24 | +import org.elasticsearch.cli.ExitCodes; |
| 25 | +import org.elasticsearch.cli.Terminal; |
| 26 | +import org.elasticsearch.cli.UserException; |
| 27 | +import org.elasticsearch.env.Environment; |
| 28 | + |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.util.Arrays; |
| 31 | + |
| 32 | +public abstract class BaseKeyStoreCommand extends EnvironmentAwareCommand { |
| 33 | + |
| 34 | + private KeyStoreWrapper keyStore; |
| 35 | + private SecureString keyStorePassword; |
| 36 | + private final boolean keyStoreMustExist; |
| 37 | + |
| 38 | + public BaseKeyStoreCommand(String description, boolean keyStoreMustExist) { |
| 39 | + super(description); |
| 40 | + this.keyStoreMustExist = keyStoreMustExist; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected final void execute(Terminal terminal, OptionSet options, Environment env) throws Exception { |
| 45 | + try { |
| 46 | + final Path configFile = env.configFile(); |
| 47 | + keyStore = KeyStoreWrapper.load(configFile); |
| 48 | + if (keyStore == null) { |
| 49 | + if (keyStoreMustExist) { |
| 50 | + throw new UserException(ExitCodes.DATA_ERROR, "Elasticsearch keystore not found at [" + |
| 51 | + KeyStoreWrapper.keystorePath(env.configFile()) + "]. Use 'create' command to create one."); |
| 52 | + } else { |
| 53 | + if (terminal.promptYesNo("The elasticsearch keystore does not exist. Do you want to create it?", false) == false) { |
| 54 | + terminal.println("Exiting without creating keystore."); |
| 55 | + return; |
| 56 | + } |
| 57 | + } |
| 58 | + keyStorePassword = new SecureString(new char[0]); |
| 59 | + keyStore = KeyStoreWrapper.create(); |
| 60 | + keyStore.save(configFile, keyStorePassword.getChars()); |
| 61 | + } else { |
| 62 | + keyStorePassword = keyStore.hasPassword() ? readPassword(terminal, false) : new SecureString(new char[0]); |
| 63 | + keyStore.decrypt(keyStorePassword.getChars()); |
| 64 | + } |
| 65 | + executeCommand(terminal, options, env); |
| 66 | + } catch (SecurityException e) { |
| 67 | + throw new UserException(ExitCodes.DATA_ERROR, e.getMessage()); |
| 68 | + } finally { |
| 69 | + if (keyStorePassword != null) { |
| 70 | + keyStorePassword.close(); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected KeyStoreWrapper getKeyStore() { |
| 76 | + return keyStore; |
| 77 | + } |
| 78 | + |
| 79 | + protected SecureString getKeyStorePassword() { |
| 80 | + return keyStorePassword; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Reads the keystore password from the {@link Terminal}, prompting for verification where applicable and returns it as a |
| 85 | + * {@link SecureString}. |
| 86 | + * |
| 87 | + * @param terminal the terminal to use for user inputs |
| 88 | + * @param withVerification whether the user should be prompted for password verification |
| 89 | + * @return a SecureString with the password the user entered |
| 90 | + * @throws UserException If the user is prompted for verification and enters a different password |
| 91 | + */ |
| 92 | + static SecureString readPassword(Terminal terminal, boolean withVerification) throws UserException { |
| 93 | + final char[] passwordArray; |
| 94 | + if (withVerification) { |
| 95 | + passwordArray = terminal.readSecret("Enter new password for the elasticsearch keystore (empty for no password): "); |
| 96 | + char[] passwordVerification = terminal.readSecret("Enter same password again: "); |
| 97 | + if (Arrays.equals(passwordArray, passwordVerification) == false) { |
| 98 | + throw new UserException(ExitCodes.DATA_ERROR, "Passwords are not equal, exiting."); |
| 99 | + } |
| 100 | + Arrays.fill(passwordVerification, '\u0000'); |
| 101 | + } else { |
| 102 | + passwordArray = terminal.readSecret("Enter password for the elasticsearch keystore : "); |
| 103 | + } |
| 104 | + final SecureString password = new SecureString(passwordArray); |
| 105 | + return password; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * This is called after the keystore password has been read from the stdin and the keystore is decrypted and |
| 110 | + * loaded. The keystore and keystore passwords are available to classes extending {@link BaseKeyStoreCommand} |
| 111 | + * using {@link BaseKeyStoreCommand#getKeyStore()} and {@link BaseKeyStoreCommand#getKeyStorePassword()} |
| 112 | + * respectively. |
| 113 | + */ |
| 114 | + protected abstract void executeCommand(Terminal terminal, OptionSet options, Environment env) throws Exception; |
| 115 | +} |
0 commit comments