|
14 | 14 | */ |
15 | 15 | package org.owasp.esapi.logging.java; |
16 | 16 |
|
| 17 | +import static org.owasp.esapi.PropNames.APPLICATION_NAME; |
| 18 | +import static org.owasp.esapi.PropNames.LOG_APPLICATION_NAME; |
| 19 | +import static org.owasp.esapi.PropNames.LOG_CLIENT_INFO; |
| 20 | +import static org.owasp.esapi.PropNames.LOG_ENCODING_REQUIRED; |
| 21 | +import static org.owasp.esapi.PropNames.LOG_SERVER_IP; |
| 22 | +import static org.owasp.esapi.PropNames.LOG_USER_INFO; |
| 23 | + |
17 | 24 | import java.io.IOException; |
18 | 25 | import java.io.InputStream; |
19 | 26 | import java.util.ArrayList; |
20 | 27 | import java.util.HashMap; |
21 | 28 | import java.util.List; |
22 | 29 | import java.util.Map; |
23 | | -import java.util.logging.LogManager; |
24 | 30 |
|
25 | 31 | import org.owasp.esapi.ESAPI; |
26 | 32 | import org.owasp.esapi.LogFactory; |
27 | 33 | import org.owasp.esapi.Logger; |
28 | | -import org.owasp.esapi.PropNames; |
29 | 34 | import org.owasp.esapi.codecs.HTMLEntityCodec; |
30 | 35 | import org.owasp.esapi.errors.ConfigurationException; |
31 | 36 | import org.owasp.esapi.logging.appender.LogAppender; |
|
35 | 40 | import org.owasp.esapi.logging.cleaning.LogScrubber; |
36 | 41 | import org.owasp.esapi.logging.cleaning.NewlineLogScrubber; |
37 | 42 |
|
38 | | -import static org.owasp.esapi.PropNames.LOG_ENCODING_REQUIRED; |
39 | | -import static org.owasp.esapi.PropNames.LOG_USER_INFO; |
40 | | -import static org.owasp.esapi.PropNames.LOG_CLIENT_INFO; |
41 | | -import static org.owasp.esapi.PropNames.LOG_APPLICATION_NAME; |
42 | | -import static org.owasp.esapi.PropNames.APPLICATION_NAME; |
43 | | -import static org.owasp.esapi.PropNames.LOG_SERVER_IP; |
44 | | - |
45 | 43 | /** |
46 | 44 | * LogFactory implementation which creates JAVA supporting Loggers. |
47 | 45 | * <br><br> |
|
58 | 56 | * |
59 | 57 | */ |
60 | 58 | public class JavaLogFactory implements LogFactory { |
| 59 | + /**Consistent message offered as a part of the ConfigurationException which is thrown if esapi-java-logging.properties is found on the path. */ |
| 60 | + private static final String PROPERTY_CONFIG_MSG = "esapi-java-logging.properties is no longer supported. See https://github.com/ESAPI/esapi-java-legacy/wiki/Configuring-the-JavaLogFactory for information on corrective actions."; |
61 | 61 | /** Immune characters for the codec log scrubber for JAVA context.*/ |
62 | 62 | private static final char[] IMMUNE_JAVA_HTML = {',', '.', '-', '_', ' ' }; |
63 | 63 | /** Codec being used to clean messages for logging.*/ |
@@ -93,43 +93,24 @@ public class JavaLogFactory implements LogFactory { |
93 | 93 |
|
94 | 94 | LOG_BRIDGE = new JavaLogBridgeImpl(JAVA_LOG_APPENDER, JAVA_LOG_SCRUBBER, levelLookup); |
95 | 95 |
|
96 | | - readLoggerConfiguration(LogManager.getLogManager()); |
97 | | - } |
98 | | - |
99 | | - /** |
100 | | - * Attempts to load the expected property file path into the provided LogManager reference. |
101 | | - * @param logManager LogManager which is being configured. |
102 | | - */ |
103 | | - /*package*/ static void readLoggerConfiguration(LogManager logManager) { |
104 | | - if (System.getProperties().keySet().stream().anyMatch(propKey -> |
105 | | - "java.util.logging.config.class".equals(propKey) || "java.util.logging.config.file".equals(propKey))) { |
106 | | - // LogManager has external configuration. Do not load ESAPI defaults. |
107 | | - // See javadoc for the LogManager class for more information on properties. |
108 | | - boolean isStartupSysoutDisabled = Boolean.valueOf(System.getProperty(PropNames.DISCARD_LOGSPECIAL, Boolean.FALSE.toString())); |
109 | | - if (!isStartupSysoutDisabled) { |
110 | | - String logManagerPreferredMsg = String.format("[ESAPI-STARTUP] ESAPI JavaLogFactory Configuration will not be applied. " |
111 | | - + "java.util.LogManager configuration Detected. " |
112 | | - + "{\"java.util.logging.config.class\":\"%s\",\"java.util.logging.config.file\":\"%s\"}", |
113 | | - System.getProperty("java.util.logging.config.class"), System.getProperty("java.util.logging.config.file")); |
114 | | - |
115 | | - System.out.println(logManagerPreferredMsg); |
116 | | - // ::SAMPLE OUTPUT:: |
117 | | - //[ESAPI-STARTUP] ESAPI JavaLogFactory Configuration will not be applied. java.util.LogManager configuration Detected.{"java.util.logging.config.class":"some.defined.value","java.util.logging.config.file":"null"} |
118 | | - } |
119 | | - |
120 | | - return; |
121 | | - } |
122 | 96 | /* |
123 | | - * This will load the logging properties file to control the format of the output for Java logs. |
| 97 | + * esapi-java-logging.properties file may lead to confusing logging behavior |
| 98 | + * by overriding desired configurations provided through Java's LogManager class. |
| 99 | + * |
| 100 | + * Verify the file is not present and fail if found to enforce understanding of |
| 101 | + * the configuration method. |
124 | 102 | */ |
125 | 103 | try (InputStream stream = JavaLogFactory.class.getClassLoader(). |
126 | 104 | getResourceAsStream("esapi-java-logging.properties")) { |
127 | | - if (stream == null) { |
128 | | - throw new ConfigurationException("Unable to locate resource: esapi-java-logging.properties"); |
| 105 | + if (stream != null) { |
| 106 | + throw new ConfigurationException(PROPERTY_CONFIG_MSG); |
129 | 107 | } |
130 | | - logManager.readConfiguration(stream); |
| 108 | + |
131 | 109 | } catch (IOException ioe) { |
132 | | - throw new ConfigurationException("Failed to load esapi-java-logging.properties.", ioe); |
| 110 | + // This is a little strange, I know. |
| 111 | + // If the IOException is thrown, then the file actually exists but is malformatted or has some other issue. |
| 112 | + // The file should not exist at all, so use the same message as above but include the original exception in the log as well. |
| 113 | + throw new ConfigurationException(PROPERTY_CONFIG_MSG, ioe); |
133 | 114 | } |
134 | 115 | } |
135 | 116 |
|
|
0 commit comments