|
| 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.KeyStoreAwareCommand; |
| 24 | +import org.elasticsearch.cli.Terminal; |
| 25 | +import org.elasticsearch.cli.UserException; |
| 26 | +import org.elasticsearch.env.Environment; |
| 27 | + |
| 28 | +import java.nio.file.Path; |
| 29 | + |
| 30 | +public class HasPasswordKeyStoreCommand extends KeyStoreAwareCommand { |
| 31 | + |
| 32 | + static final int NO_PASSWORD_EXIT_CODE = 1; |
| 33 | + |
| 34 | + HasPasswordKeyStoreCommand() { |
| 35 | + super("Succeeds if the keystore exists and is password-protected, " + |
| 36 | + "fails with exit code " + NO_PASSWORD_EXIT_CODE + " otherwise."); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception { |
| 41 | + final Path configFile = env.configFile(); |
| 42 | + final KeyStoreWrapper keyStore = KeyStoreWrapper.load(configFile); |
| 43 | + |
| 44 | + // We handle error printing here so we can respect the "--silent" flag |
| 45 | + // We have to throw an exception to get a nonzero exit code |
| 46 | + if (keyStore == null) { |
| 47 | + terminal.errorPrintln(Terminal.Verbosity.NORMAL, "ERROR: Elasticsearch keystore not found"); |
| 48 | + throw new UserException(NO_PASSWORD_EXIT_CODE, null); |
| 49 | + } |
| 50 | + if (keyStore.hasPassword() == false) { |
| 51 | + terminal.errorPrintln(Terminal.Verbosity.NORMAL, "ERROR: Keystore is not password-protected"); |
| 52 | + throw new UserException(NO_PASSWORD_EXIT_CODE, null); |
| 53 | + } |
| 54 | + |
| 55 | + terminal.println(Terminal.Verbosity.NORMAL, "Keystore is password-protected"); |
| 56 | + } |
| 57 | +} |
0 commit comments