From 781437c219dc3422797a32dc7ba72cd4f5ee38e2 Mon Sep 17 00:00:00 2001 From: Kitti Nanasi Date: Wed, 29 Aug 2018 22:06:36 -0700 Subject: [PATCH] HADOOP-15698. KMS log4j is not initialized properly at startup. Contributed by Kitti Nanasi. --- .../key/kms/server/KMSConfiguration.java | 31 +++++++++++++++ .../crypto/key/kms/server/KMSWebApp.java | 38 +------------------ .../crypto/key/kms/server/KMSWebServer.java | 1 + 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSConfiguration.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSConfiguration.java index 18eec19a72..35ffb42981 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSConfiguration.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSConfiguration.java @@ -20,6 +20,7 @@ import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; +import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,6 +104,8 @@ public class KMSConfiguration { public static final boolean KEY_AUTHORIZATION_ENABLE_DEFAULT = true; + private static final String LOG4J_PROPERTIES = "kms-log4j.properties"; + static { Configuration.addDefaultResource(KMS_DEFAULT_XML); Configuration.addDefaultResource(KMS_SITE_XML); @@ -159,4 +162,32 @@ public static boolean isACLsFileNewer(long time) { } return newer; } + + public static void initLogging() { + String confDir = System.getProperty(KMS_CONFIG_DIR); + if (confDir == null) { + throw new RuntimeException("System property '" + + KMSConfiguration.KMS_CONFIG_DIR + "' not defined"); + } + if (System.getProperty("log4j.configuration") == null) { + System.setProperty("log4j.defaultInitOverride", "true"); + boolean fromClasspath = true; + File log4jConf = new File(confDir, LOG4J_PROPERTIES).getAbsoluteFile(); + if (log4jConf.exists()) { + PropertyConfigurator.configureAndWatch(log4jConf.getPath(), 1000); + fromClasspath = false; + } else { + ClassLoader cl = Thread.currentThread().getContextClassLoader(); + URL log4jUrl = cl.getResource(LOG4J_PROPERTIES); + if (log4jUrl != null) { + PropertyConfigurator.configure(log4jUrl); + } + } + LOG.debug("KMS log starting"); + if (fromClasspath) { + LOG.warn("Log4j configuration file '{}' not found", LOG4J_PROPERTIES); + LOG.warn("Logging with INFO level to standard output"); + } + } + } } diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebApp.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebApp.java index cb4bf7e15a..0640e25b76 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebApp.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebApp.java @@ -17,10 +17,8 @@ */ package org.apache.hadoop.crypto.key.kms.server; -import java.io.File; import java.io.IOException; import java.net.URI; -import java.net.URL; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -37,14 +35,13 @@ import org.apache.hadoop.crypto.key.KeyProviderFactory; import org.apache.hadoop.security.UserGroupInformation; import org.apache.hadoop.util.VersionInfo; -import org.apache.log4j.PropertyConfigurator; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @InterfaceAudience.Private public class KMSWebApp implements ServletContextListener { - private static final String LOG4J_PROPERTIES = "kms-log4j.properties"; + private static final Logger LOG = LoggerFactory.getLogger(KMSWebApp.class); private static final String METRICS_PREFIX = "hadoop.kms."; private static final String ADMIN_CALLS_METER = METRICS_PREFIX + @@ -66,7 +63,6 @@ public class KMSWebApp implements ServletContextListener { private static final String REENCRYPT_EEK_BATCH_METER = METRICS_PREFIX + "reencrypt_eek_batch.calls.meter"; - private static Logger LOG; private static MetricRegistry metricRegistry; private JmxReporter jmxReporter; @@ -84,42 +80,10 @@ public class KMSWebApp implements ServletContextListener { private static KMSAudit kmsAudit; private static KeyProviderCryptoExtension keyProviderCryptoExtension; - private void initLogging(String confDir) { - if (System.getProperty("log4j.configuration") == null) { - System.setProperty("log4j.defaultInitOverride", "true"); - boolean fromClasspath = true; - File log4jConf = new File(confDir, LOG4J_PROPERTIES).getAbsoluteFile(); - if (log4jConf.exists()) { - PropertyConfigurator.configureAndWatch(log4jConf.getPath(), 1000); - fromClasspath = false; - } else { - ClassLoader cl = Thread.currentThread().getContextClassLoader(); - URL log4jUrl = cl.getResource(LOG4J_PROPERTIES); - if (log4jUrl != null) { - PropertyConfigurator.configure(log4jUrl); - } - } - LOG = LoggerFactory.getLogger(KMSWebApp.class); - LOG.debug("KMS log starting"); - if (fromClasspath) { - LOG.warn("Log4j configuration file '{}' not found", LOG4J_PROPERTIES); - LOG.warn("Logging with INFO level to standard output"); - } - } else { - LOG = LoggerFactory.getLogger(KMSWebApp.class); - } - } - @Override public void contextInitialized(ServletContextEvent sce) { try { - String confDir = System.getProperty(KMSConfiguration.KMS_CONFIG_DIR); - if (confDir == null) { - throw new RuntimeException("System property '" + - KMSConfiguration.KMS_CONFIG_DIR + "' not defined"); - } kmsConf = KMSConfiguration.getKMSConf(); - initLogging(confDir); UserGroupInformation.setConfiguration(kmsConf); LOG.info("-------------------------------------------------------------"); LOG.info(" Java runtime version : {}", System.getProperty( diff --git a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebServer.java b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebServer.java index 19f7227127..036231de70 100644 --- a/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebServer.java +++ b/hadoop-common-project/hadoop-kms/src/main/java/org/apache/hadoop/crypto/key/kms/server/KMSWebServer.java @@ -166,6 +166,7 @@ public URL getKMSUrl() { } public static void main(String[] args) throws Exception { + KMSConfiguration.initLogging(); StringUtils.startupShutdownMessage(KMSWebServer.class, args, LOG); Configuration conf = new ConfigurationWithLogging( KMSConfiguration.getKMSConf());