From c777c1d36f4363e6d96486230ccd99457c0037dd Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Sat, 25 Nov 2017 11:40:29 -0500 Subject: [PATCH] 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@f77f9b5052e3973625cb98dfa8e84596c94e4b23 --- .../license/licensor/tools/KeyPairGeneratorTool.java | 3 ++- .../license/licensor/tools/LicenseGeneratorTool.java | 3 ++- .../license/licensor/tools/LicenseVerificationTool.java | 3 ++- .../org/elasticsearch/xpack/extensions/XPackExtensionCli.java | 3 ++- .../security/authc/esnative/ESNativeRealmMigrateTool.java | 3 ++- .../xpack/security/authc/esnative/tool/SetupPasswordTool.java | 3 ++- .../xpack/security/authc/file/tool/UsersTool.java | 3 ++- .../main/java/org/elasticsearch/xpack/ssl/CertificateTool.java | 3 ++- .../xpack/watcher/trigger/schedule/tool/CronEvalTool.java | 3 ++- 9 files changed, 18 insertions(+), 9 deletions(-) diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java index 80cf2d350f0..5a8ea91d362 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/KeyPairGeneratorTool.java @@ -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 publicKeyPathOption; private final OptionSpec privateKeyPathOption; diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java index f3ceb1810bb..8319b4fdec4 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java @@ -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 publicKeyPathOption; private final OptionSpec privateKeyPathOption; diff --git a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java index 1936a37844c..08fe49475fe 100644 --- a/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java +++ b/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseVerificationTool.java @@ -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 publicKeyPathOption; private final OptionSpec licenseOption; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java b/plugin/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java index 2bfc84c344d..b67b2a31962 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/extensions/XPackExtensionCli.java @@ -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"); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ESNativeRealmMigrateTool.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ESNativeRealmMigrateTool.java index d1437b50197..a03beafb08d 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ESNativeRealmMigrateTool.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/ESNativeRealmMigrateTool.java @@ -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)); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java index 2cea16fee78..69f18e9e865 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java @@ -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 USERS = Arrays.asList(ElasticUser.NAME, KibanaUser.NAME, LogstashSystemUser.NAME); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java index 5fed73005e3..d2b2bb5f385 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java @@ -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)); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/ssl/CertificateTool.java b/plugin/src/main/java/org/elasticsearch/xpack/ssl/CertificateTool.java index cfd9f6b4d88..fba0006799a 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/ssl/CertificateTool.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/ssl/CertificateTool.java @@ -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"; diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java index 2af25c830d1..3bcd817aad0 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java @@ -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));