-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make connection errors during notifications silent (logged only) (#52)
Also scrub apikey from any future log statements (not aware that it was logged prior, but with this new change it would of been without the scrub) + Test cases
- Loading branch information
Showing
9 changed files
with
160 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/botdarr/utilities/Log4jSensitiveDataPattern.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.botdarr.utilities; | ||
|
||
import org.apache.logging.log4j.core.LogEvent; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.pattern.ConverterKeys; | ||
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
@Plugin(name="LogMaskingConverter", category = "Converter") | ||
@ConverterKeys({"scrubbedMsg"}) | ||
public class Log4jSensitiveDataPattern extends LogEventPatternConverter { | ||
private static final Pattern API_KEY_PATTERN = Pattern.compile("apikey=([0-9a-zA-z]+)"); | ||
|
||
protected Log4jSensitiveDataPattern(String name, String style) { | ||
super(name, style); | ||
} | ||
public static Log4jSensitiveDataPattern newInstance(String[] options) { | ||
return new Log4jSensitiveDataPattern("scrubbedMsg", Thread.currentThread().getName()); | ||
} | ||
|
||
@Override | ||
public void format(LogEvent event, StringBuilder toAppendTo) { | ||
String message = event.getMessage().getFormattedMessage(); | ||
String maskedMessage = message; | ||
try { | ||
StringBuffer modifiedBuffer = new StringBuffer(); | ||
Matcher matcher = API_KEY_PATTERN.matcher(message); | ||
while(matcher.find()) { | ||
matcher.appendReplacement(modifiedBuffer, "apikey=****"); | ||
} | ||
if (modifiedBuffer.length() > 0) { | ||
maskedMessage = modifiedBuffer.toString(); | ||
} | ||
} catch (Exception e) { | ||
System.out.println("Error trying to mask message, message = " + e.getMessage()); | ||
maskedMessage = message; | ||
} | ||
toAppendTo.append(maskedMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5.1.14 | ||
5.1.15 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/test/java/com/botdarr/utilities/Log4jSensitiveDataPatternTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.botdarr.utilities; | ||
|
||
import mockit.Expectations; | ||
import mockit.Mocked; | ||
import org.apache.logging.log4j.core.impl.Log4jLogEvent; | ||
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class Log4jSensitiveDataPatternTests { | ||
@Test | ||
public void format_noApiKeyInString_noMaskedText() { | ||
String input = "http://localhost"; | ||
LogEventPatternConverter patternConverter = new Log4jSensitiveDataPattern("test", "style"); | ||
StringBuilder output = new StringBuilder(); | ||
new Expectations() {{ | ||
mockedEvent.getMessage().getFormattedMessage(); result = input; | ||
}}; | ||
patternConverter.format(mockedEvent, output); | ||
//input should remain unchanged | ||
Assert.assertEquals(input, output.toString()); | ||
} | ||
|
||
@Test | ||
public void format_apiKeyInString_noMaskedText() { | ||
String input = "http://localhost?apikey=fdskjkjfd"; | ||
LogEventPatternConverter patternConverter = new Log4jSensitiveDataPattern("test", "style"); | ||
StringBuilder output = new StringBuilder(); | ||
new Expectations() {{ | ||
mockedEvent.getMessage().getFormattedMessage(); result = input; | ||
}}; | ||
patternConverter.format(mockedEvent, output); | ||
//input should remain unchanged | ||
Assert.assertEquals("http://localhost?apikey=****", output.toString()); | ||
} | ||
|
||
@Mocked | ||
private Log4jLogEvent mockedEvent; | ||
} |