-
Notifications
You must be signed in to change notification settings - Fork 370
Added property to omit event type information in logs #813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
eeda7ff
fe4841c
49e2e8e
29db8c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,11 @@ | |
|
||
package org.owasp.esapi.logging.appender; | ||
|
||
import org.owasp.esapi.ESAPI; | ||
import org.owasp.esapi.Logger.EventType; | ||
import org.owasp.esapi.errors.ConfigurationException; | ||
|
||
import static org.owasp.esapi.PropNames.OMIT_EVENT_TYPE_IN_LOGS; | ||
|
||
/** | ||
* LogAppender Implementation which can prefix the common logger information for | ||
|
@@ -35,6 +39,18 @@ public class LogPrefixAppender implements LogAppender { | |
private final boolean logApplicationName; | ||
/** Application Name to record. */ | ||
private final String appName; | ||
/** Whether to omit event type in logs or not. */ | ||
private static boolean omitEventTypeInLogs; | ||
|
||
static { | ||
|
||
try { | ||
omitEventTypeInLogs = | ||
ESAPI.securityConfiguration().getBooleanProp(OMIT_EVENT_TYPE_IN_LOGS); | ||
} catch (ConfigurationException ex) { | ||
omitEventTypeInLogs = false; | ||
} | ||
jeremiahjstacey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Ctr. | ||
|
@@ -67,7 +83,7 @@ public String appendTo(String logName, EventType eventType, String message) { | |
serverInfoSupplier.setLogServerIp(logServerIp); | ||
serverInfoSupplier.setLogApplicationName(logApplicationName, appName); | ||
|
||
String eventTypeMsg = eventTypeSupplier.get().trim(); | ||
String eventTypeMsg = omitEventTypeInLogs ? "" : eventTypeSupplier.get().trim(); | ||
String userInfoMsg = userInfoSupplier.get().trim(); | ||
String clientInfoMsg = clientInfoSupplier.get().trim(); | ||
String serverInfoMsg = serverInfoSupplier.get().trim(); | ||
|
@@ -81,7 +97,7 @@ public String appendTo(String logName, EventType eventType, String message) { | |
String[] optionalPrefixContent = new String[] {userInfoMsg + clientInfoMsg, serverInfoMsg}; | ||
|
||
StringBuilder logPrefix = new StringBuilder(); | ||
//EventType is always appended | ||
|
||
logPrefix.append(eventTypeMsg); | ||
|
||
for (String element : optionalPrefixContent) { | ||
|
@@ -91,6 +107,11 @@ public String appendTo(String logName, EventType eventType, String message) { | |
} | ||
} | ||
|
||
return String.format(RESULT_FORMAT, logPrefix.toString(), message); | ||
if (logPrefix.toString().trim().isEmpty()) { | ||
// if there isn't any log prefix we just send back the message without touching it | ||
return message; | ||
} | ||
|
||
return String.format(RESULT_FORMAT, logPrefix.toString().trim(), message); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
import org.powermock.core.classloader.annotations.PrepareForTest; | ||
import org.powermock.modules.junit4.PowerMockRunner; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
@RunWith(PowerMockRunner.class) | ||
@PrepareForTest(LogPrefixAppender.class) | ||
public class LogPrefixAppenderTest { | ||
|
@@ -109,44 +111,69 @@ public void testDelegateCtrArgs() throws Exception { | |
assertEquals(testLoggerName, logNameCapture.getValue()); | ||
} | ||
|
||
@Test | ||
jeremiahjstacey marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public void testLongContentWithOmitEventTypeInLogs() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, EMPTY_RESULT, true, ""); | ||
} | ||
|
||
@Test | ||
public void testLongContentWithOmitEventTypeInLogsAndUserInfo() throws Exception { | ||
runTest(ETL_RESULT, UIS_RESULT, EMPTY_RESULT, EMPTY_RESULT, true, "[USER_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLongContentWithOmitEventTypeInLogsAndClientInfo() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, CIS_RESULT, EMPTY_RESULT, true, "[CLIENT_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLongContentWithOmitEventTypeInLogsAndServerInfo() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, SIS_RESULT, true, "[-> SERVER_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLongContentWithoutOmitEventTypeInLogs() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, EMPTY_RESULT, false, "[EVENT_TYPE]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenClientInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, UIS_RESULT, EMPTY_RESULT,SIS_RESULT, "[EVENT_TYPE USER_INFO -> SERVER_INFO]"); | ||
runTest(ETL_RESULT, UIS_RESULT, EMPTY_RESULT,SIS_RESULT, false, "[EVENT_TYPE USER_INFO -> SERVER_INFO]"); | ||
} | ||
|
||
|
||
@Test | ||
public void testLogContentWhenUserInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, CIS_RESULT,SIS_RESULT, "[EVENT_TYPE CLIENT_INFO -> SERVER_INFO]"); | ||
runTest(ETL_RESULT, EMPTY_RESULT, CIS_RESULT,SIS_RESULT, false, "[EVENT_TYPE CLIENT_INFO -> SERVER_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenClientInfoEmptyAndServerInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, UIS_RESULT, EMPTY_RESULT,EMPTY_RESULT, "[EVENT_TYPE USER_INFO]"); | ||
runTest(ETL_RESULT, UIS_RESULT, EMPTY_RESULT,EMPTY_RESULT, false, "[EVENT_TYPE USER_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenUserInfoEmptyAndServerInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, CIS_RESULT,EMPTY_RESULT, "[EVENT_TYPE CLIENT_INFO]"); | ||
runTest(ETL_RESULT, EMPTY_RESULT, CIS_RESULT,EMPTY_RESULT, false, "[EVENT_TYPE CLIENT_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenUserInfoAndClientInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, SIS_RESULT, "[EVENT_TYPE -> SERVER_INFO]"); | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, SIS_RESULT, false, "[EVENT_TYPE -> SERVER_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenServerInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, UIS_RESULT, CIS_RESULT, EMPTY_RESULT, "[EVENT_TYPE USER_INFO:CLIENT_INFO]"); | ||
runTest(ETL_RESULT, UIS_RESULT, CIS_RESULT, EMPTY_RESULT, false, "[EVENT_TYPE USER_INFO:CLIENT_INFO]"); | ||
} | ||
|
||
@Test | ||
public void testLogContentWhenUserInfoEmptyAndClientInfoEmptyAndServerInfoEmpty() throws Exception { | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, EMPTY_RESULT, "[EVENT_TYPE]"); | ||
runTest(ETL_RESULT, EMPTY_RESULT, EMPTY_RESULT, EMPTY_RESULT, false, "[EVENT_TYPE]"); | ||
} | ||
|
||
|
||
private void runTest(String typeResult, String userResult, String clientResult, String serverResult, String exResult) throws Exception{ | ||
private void runTest(String typeResult, String userResult, String clientResult, String serverResult, boolean omitEventTypeInLogs, String exResult) throws Exception{ | ||
when(etlsSpy.get()).thenReturn(typeResult); | ||
when(uisSpy.get()).thenReturn(userResult); | ||
when(cisSpy.get()).thenReturn(clientResult); | ||
|
@@ -159,8 +186,26 @@ private void runTest(String typeResult, String userResult, String clientResult, | |
|
||
//Since everything is mocked these booleans don't much matter aside from the later verifies | ||
LogPrefixAppender lpa = new LogPrefixAppender(false, false, false, false, null); | ||
String result = lpa.appendTo(testLoggerName, testEventType, testLogMessage); | ||
|
||
assertEquals(exResult + " " + testName.getMethodName() + "-MESSAGE", result); | ||
// Using reflection API to set omitEventTypeInLogs field in LogPrefixAppender. | ||
setOmitEventTypeInLogsFieldUsingReflection(lpa, omitEventTypeInLogs); | ||
|
||
|
||
String actualResult = lpa.appendTo(testLoggerName, testEventType, testLogMessage); | ||
|
||
StringBuilder expectedResult = new StringBuilder(); | ||
if (!exResult.isEmpty()) { | ||
expectedResult.append(exResult); | ||
expectedResult.append(" "); | ||
} | ||
expectedResult.append(testName.getMethodName()); | ||
expectedResult.append("-MESSAGE"); | ||
|
||
assertEquals(expectedResult.toString() , actualResult); | ||
} | ||
|
||
private static void setOmitEventTypeInLogsFieldUsingReflection(LogPrefixAppender lpa, boolean omitEventTypeInLogs) throws NoSuchFieldException, IllegalAccessException { | ||
Field omitEventTypeInLogsField = lpa.getClass().getDeclaredField("omitEventTypeInLogs"); | ||
omitEventTypeInLogsField.setAccessible(true); | ||
omitEventTypeInLogsField.setBoolean(lpa,omitEventTypeInLogs); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there are also ESAPI.properties files in the test scope that should also be updated
https://github.com/ESAPI/esapi-java-legacy/blob/develop/src/test/resources/esapi/ESAPI.properties
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Absolutely correct. I thought at the time that I originally reviewed this, he had updated the src/test/resources/esapi/ESAPI.properties file to add that new property. Maybe he undid it in the last commit or maybe I just was imagining it, but yeah, it needs to be there too.