Refactor CLI commands as logging-aware commands

We have to ensure logging is configured for any CLI command that depends
on core Elasticsearch (since it might directly or indirectly touch
logging). This commit does this for all commands in X-Pack.

Relates elastic/x-pack-elasticsearch#3112

Original commit: elastic/x-pack-elasticsearch@f77f9b5052
This commit is contained in:
Jason Tedor 2017-11-25 11:40:29 -05:00 committed by GitHub
parent eb4186dd5c
commit c777c1d36f
9 changed files with 18 additions and 9 deletions

View File

@ -9,6 +9,7 @@ import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.SuppressForbidden;
@ -23,7 +24,7 @@ import java.security.SecureRandom;
import static org.elasticsearch.license.CryptUtils.writeEncryptedPrivateKey;
import static org.elasticsearch.license.CryptUtils.writeEncryptedPublicKey;
public class KeyPairGeneratorTool extends Command {
public class KeyPairGeneratorTool extends LoggingAwareCommand {
private final OptionSpec<String> publicKeyPathOption;
private final OptionSpec<String> privateKeyPathOption;

View File

@ -9,6 +9,7 @@ import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.SuppressForbidden;
@ -25,7 +26,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class LicenseGeneratorTool extends Command {
public class LicenseGeneratorTool extends LoggingAwareCommand {
private final OptionSpec<String> publicKeyPathOption;
private final OptionSpec<String> privateKeyPathOption;

View File

@ -9,6 +9,7 @@ import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.SuppressForbidden;
@ -25,7 +26,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class LicenseVerificationTool extends Command {
public class LicenseVerificationTool extends LoggingAwareCommand {
private final OptionSpec<String> publicKeyPathOption;
private final OptionSpec<String> licenseOption;

View File

@ -5,13 +5,14 @@
*/
package org.elasticsearch.xpack.extensions;
import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.Terminal;
/**
* A cli tool for adding, removing and listing extensions for x-pack.
*/
public class XPackExtensionCli extends MultiCommand {
public class XPackExtensionCli extends LoggingAwareMultiCommand {
private XPackExtensionCli() {
super("A tool for managing installed x-pack extensions");

View File

@ -19,6 +19,7 @@ import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.EnvironmentAwareCommand;
import org.elasticsearch.cli.Terminal;
@ -73,7 +74,7 @@ import static org.elasticsearch.xpack.security.Security.setting;
* import. It reads from the files and tries its best to add the users, showing an error if it was incapable of importing them. Any existing
* users or roles are skipped.
*/
public class ESNativeRealmMigrateTool extends MultiCommand {
public class ESNativeRealmMigrateTool extends LoggingAwareMultiCommand {
public static void main(String[] args) throws Exception {
exit(new ESNativeRealmMigrateTool().main(args, Terminal.DEFAULT));

View File

@ -24,6 +24,7 @@ import org.bouncycastle.util.io.Streams;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.cli.EnvironmentAwareCommand;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.Terminal.Verbosity;
@ -51,7 +52,7 @@ import org.elasticsearch.xpack.security.user.LogstashSystemUser;
* if successful. After the elastic user password is set you have to use the
* `security` API to manipulate passwords.
*/
public class SetupPasswordTool extends MultiCommand {
public class SetupPasswordTool extends LoggingAwareMultiCommand {
private static final char[] CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%^&*-_=+?").toCharArray();
public static final List<String> USERS = Arrays.asList(ElasticUser.NAME, KibanaUser.NAME, LogstashSystemUser.NAME);

View File

@ -8,6 +8,7 @@ package org.elasticsearch.xpack.security.authc.file.tool;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.EnvironmentAwareCommand;
import org.elasticsearch.cli.Terminal;
@ -39,7 +40,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class UsersTool extends MultiCommand {
public class UsersTool extends LoggingAwareMultiCommand {
public static void main(String[] args) throws Exception {
exit(new UsersTool().main(args, Terminal.DEFAULT));

View File

@ -55,6 +55,7 @@ import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.elasticsearch.ExceptionsHelper;
import org.elasticsearch.cli.EnvironmentAwareCommand;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareMultiCommand;
import org.elasticsearch.cli.MultiCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.Terminal.Verbosity;
@ -77,7 +78,7 @@ import org.elasticsearch.env.Environment;
/**
* CLI tool to make generation of certificates or certificate requests easier for users
*/
public class CertificateTool extends MultiCommand {
public class CertificateTool extends LoggingAwareMultiCommand {
private static final String AUTO_GEN_CA_DN = "CN=Elastic Certificate Tool Autogenerated CA";
private static final String DESCRIPTION = "Simplifies certificate creation for use with the Elastic Stack";

View File

@ -12,6 +12,7 @@ import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.elasticsearch.cli.Command;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.LoggingAwareCommand;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.xpack.watcher.trigger.schedule.Cron;
@ -20,7 +21,7 @@ import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
public class CronEvalTool extends Command {
public class CronEvalTool extends LoggingAwareCommand {
public static void main(String[] args) throws Exception {
exit(new CronEvalTool().main(args, Terminal.DEFAULT));