Skip to content

Commit 073193e

Browse files
Issue ESAPI#560 JUL fixes (ESAPI#562)
* JUL Property Resource Logic fix Adjusting the loading behavior of the esapi-java-logging.properties file to account for a possible null stream resolution. Updating the error handling from system error output to throwing ConfigurationExceptions. Tests updated accordingly * JUL Default Logging configuration Adding a default version of esapi-java-logging.properties to the configuration directory. * JUL Documentation updates Noting the property file requirement and resource location in the class java doc. * Property Comment Updates Supplying a more accurate description of the effect of Logger.ClientInfo * Property Comment Updates Supplying a more accurate description of the effect of Logger.ClientInfo
1 parent 92ae8e1 commit 073193e

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

configuration/esapi/ESAPI.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ Logger.LogFileName=ESAPI_logging_file
394394
Logger.MaxLogFileSize=10000000
395395
# Determines whether ESAPI should log the user info.
396396
Logger.UserInfo=true
397-
# Determines whether ESAPI should log the app info.
397+
# Determines whether ESAPI should log the session id and client IP.
398398
Logger.ClientInfo=true
399399

400400
#===========================================================================
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
handlers= java.util.logging.ConsoleHandler
2+
.level= INFO
3+
java.util.logging.ConsoleHandler.level = INFO
4+
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
5+
java.util.logging.SimpleFormatter.format=[%1$tF %1$tT] [%3$-7s] %5$s %n
6+
#https://www.logicbig.com/tutorials/core-java-tutorial/logging/customizing-default-format.html

src/main/java/org/owasp/esapi/logging/java/JavaLogFactory.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.owasp.esapi.LogFactory;
2727
import org.owasp.esapi.Logger;
2828
import org.owasp.esapi.codecs.HTMLEntityCodec;
29+
import org.owasp.esapi.errors.ConfigurationException;
2930
import org.owasp.esapi.logging.appender.LogAppender;
3031
import org.owasp.esapi.logging.appender.LogPrefixAppender;
3132
import org.owasp.esapi.logging.cleaning.CodecLogScrubber;
@@ -35,6 +36,10 @@
3536
import org.owasp.esapi.reference.DefaultSecurityConfiguration;
3637
/**
3738
* LogFactory implementation which creates JAVA supporting Loggers.
39+
*
40+
* This implementation requires that a file named 'esapi-java-logging.properties' exists on the classpath.
41+
* <br>
42+
* A default file implementation is available in the configuration jar on GitHub under the 'Releases'
3843
*
3944
*/
4045
public class JavaLogFactory implements LogFactory {
@@ -86,9 +91,12 @@ public class JavaLogFactory implements LogFactory {
8691
*/
8792
try (InputStream stream = JavaLogFactory.class.getClassLoader().
8893
getResourceAsStream("esapi-java-logging.properties")) {
94+
if (stream == null) {
95+
throw new ConfigurationException("Unable to locate resource: esapi-java-logging.properties");
96+
}
8997
logManager.readConfiguration(stream);
9098
} catch (IOException ioe) {
91-
System.err.print(new IOException("Failed to load esapi-java-logging.properties.", ioe));
99+
throw new ConfigurationException("Failed to load esapi-java-logging.properties.", ioe);
92100
}
93101
}
94102

src/test/java/org/owasp/esapi/logging/java/JavaLogFactoryTest.java

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@
1414
*/
1515
package org.owasp.esapi.logging.java;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
19-
2017
import java.io.IOException;
2118
import java.io.InputStream;
22-
import java.io.OutputStream;
23-
import java.io.PrintStream;
2419
import java.util.List;
2520
import java.util.logging.LogManager;
2621

22+
import org.hamcrest.CustomMatcher;
2723
import org.junit.Assert;
2824
import org.junit.Rule;
2925
import org.junit.Test;
26+
import org.junit.rules.ExpectedException;
3027
import org.junit.rules.TestName;
3128
import org.junit.runner.RunWith;
3229
import org.mockito.ArgumentCaptor;
33-
import org.mockito.Mockito;
3430
import org.owasp.esapi.Logger;
31+
import org.owasp.esapi.errors.ConfigurationException;
3532
import org.owasp.esapi.logging.appender.LogAppender;
3633
import org.owasp.esapi.logging.appender.LogPrefixAppender;
3734
import org.owasp.esapi.logging.cleaning.CodecLogScrubber;
@@ -47,9 +44,12 @@
4744
public class JavaLogFactoryTest {
4845
@Rule
4946
public TestName testName = new TestName();
47+
48+
@Rule
49+
public ExpectedException exEx = ExpectedException.none();
5050

5151
@Test
52-
public void testIOExceptionOnMissingConfiguration() throws Exception {
52+
public void testConfigurationExceptionOnMissingConfiguration() throws Exception {
5353
final IOException originException = new IOException(testName.getMethodName());
5454

5555
LogManager testLogManager = new LogManager() {
@@ -59,31 +59,17 @@ public void readConfiguration(InputStream ins) throws IOException, SecurityExcep
5959
}
6060
};
6161

62-
OutputStream nullOutputStream = new OutputStream() {
62+
exEx.expectMessage("Failed to load esapi-java-logging.properties");
63+
exEx.expect(ConfigurationException.class);
64+
65+
exEx.expectCause(new CustomMatcher<Throwable>("Check for IOException") {
6366
@Override
64-
public void write(int b) throws IOException {
65-
//No Op
67+
public boolean matches(Object item) {
68+
return item instanceof IOException;
6669
}
67-
};
68-
69-
ArgumentCaptor<Object> stdErrOut = ArgumentCaptor.forClass(Object.class);
70-
PrintStream orig = System.err;
71-
try (PrintStream errPrinter = new PrintStream(nullOutputStream)) {
72-
PrintStream spyPrinter = PowerMockito.spy(errPrinter);
73-
Mockito.doCallRealMethod().when(spyPrinter).print(stdErrOut.capture());
74-
System.setErr(spyPrinter);
75-
76-
JavaLogFactory.readLoggerConfiguration(testLogManager);
77-
78-
Object writeData = stdErrOut.getValue();
79-
assertTrue(writeData instanceof IOException);
80-
IOException actual = (IOException) writeData;
81-
assertEquals(originException, actual.getCause());
82-
assertEquals("Failed to load esapi-java-logging.properties.", actual.getMessage());
83-
} finally {
84-
System.setErr(orig);
85-
}
70+
});
8671

72+
JavaLogFactory.readLoggerConfiguration(testLogManager);
8773
}
8874

8975
@Test

src/test/resources/esapi/ESAPI.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ Logger.LogFileName=ESAPI_logging_file
426426
Logger.MaxLogFileSize=10000000
427427
# Determines whether ESAPI should log the user info.
428428
Logger.UserInfo=true
429-
# Determines whether ESAPI should log the app info.
429+
# Determines whether ESAPI should log the session id and client IP.
430430
Logger.ClientInfo=true
431431

432432
#===========================================================================

0 commit comments

Comments
 (0)