diff --git a/src/main/java/com/uid2/shared/util/MaskingLokiJsonEncoder.java b/src/main/java/com/uid2/shared/util/MaskingLokiJsonEncoder.java index ee199121..44b73150 100644 --- a/src/main/java/com/uid2/shared/util/MaskingLokiJsonEncoder.java +++ b/src/main/java/com/uid2/shared/util/MaskingLokiJsonEncoder.java @@ -12,7 +12,7 @@ public void start() { super.start(); maskingPatternLayout = new MaskingPatternLayout(); - maskingPatternLayout.setContext(this.context); + maskingPatternLayout.setContext(context); maskingPatternLayout.setPattern(message.pattern); maskingPatternLayout.start(); } @@ -34,10 +34,7 @@ public void setMessage(MessageCfg message) { } public static final class MessageCfg { - String pattern = "l=%level c=%logger{20} t=%thread | %msg %ex"; - - public MessageCfg() { - } + private String pattern = "l=%level c=%logger{20} t=%thread | %msg %ex"; public void setPattern(String pattern) { this.pattern = pattern; diff --git a/src/test/java/com/uid2/shared/util/MaskingLokiJsonEncoderTest.java b/src/test/java/com/uid2/shared/util/MaskingLokiJsonEncoderTest.java new file mode 100644 index 00000000..05c90a48 --- /dev/null +++ b/src/test/java/com/uid2/shared/util/MaskingLokiJsonEncoderTest.java @@ -0,0 +1,96 @@ +package com.uid2.shared.util; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.classic.spi.LoggingEvent; +import ch.qos.logback.core.pattern.FormattingConverter; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.junit.jupiter.MockitoExtension; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.*; + +@ExtendWith(MockitoExtension.class) +public class MaskingLokiJsonEncoderTest { + private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(MaskingLokiJsonEncoderTest.class); + private static final MaskingLokiJsonEncoder MASKING_LOKI_JSON_ENCODER = new MaskingLokiJsonEncoder(); + private static final String URL_WITHOUT_PROTOCOL = "myservice.s3.amazonaws.com/some/path?param1=value1&X-Amz-Security-Token=mysecurityToken¶m3=value3"; + private static final Map MASKED_MESSAGES = Map.of( + "Error: " + URL_WITHOUT_PROTOCOL + " and something else", "Error: REDACTED - S3 and something else", + "https://" + URL_WITHOUT_PROTOCOL, "REDACTED - S3", + "http://" + URL_WITHOUT_PROTOCOL, "REDACTED - S3", + URL_WITHOUT_PROTOCOL, "REDACTED - S3", + "Should not be redacted", "Should not be redacted" + ); + + @BeforeAll + public static void setupAll() { + MaskingLokiJsonEncoder.MessageCfg message = new MaskingLokiJsonEncoder.MessageCfg(); + message.setPattern("%msg %ex"); + MASKING_LOKI_JSON_ENCODER.setMessage(message); + MASKING_LOKI_JSON_ENCODER.setContext(LOGGER.getLoggerContext()); + MASKING_LOKI_JSON_ENCODER.start(); + } + + @AfterAll + public static void teardownAll() { + MASKING_LOKI_JSON_ENCODER.stop(); + } + + @ParameterizedTest + @MethodSource("maskedMessagesWithS3") + public void testMaskedMessagesWithS3(String message, String maskedMessage) { + String log = MASKING_LOKI_JSON_ENCODER.eventToMessage(getLoggingEvent(message)); + + assertEquals(maskedMessage, log.trim()); + } + + private static Set maskedMessagesWithS3() { + return MASKED_MESSAGES.entrySet().stream() + .map(entry -> Arguments.of( + entry.getKey(), + entry.getValue() + )) + .collect(Collectors.toSet()); + } + + @ParameterizedTest + @MethodSource("maskedExceptionsWithS3") + public void testMaskedExceptionsWithS3(Exception ex, String maskedMessage, String stackTrace) { + String log = MASKING_LOKI_JSON_ENCODER.eventToMessage(getLoggingEvent(ex.getMessage(), ex)); + + assertAll( + "testMaskedExceptionsWithS3", + () -> assertEquals(log.split("\n")[0].trim(), maskedMessage), + () -> assertTrue(log.split("\n")[1].trim().startsWith(stackTrace)) + ); + } + + private static Set maskedExceptionsWithS3() { + return MASKED_MESSAGES.entrySet().stream() + .map(entry -> Arguments.of( + new Exception(entry.getKey()), + entry.getValue() + " java.lang.Exception: " + entry.getValue(), + "at com.uid2.shared.util.MaskingPatternLayoutTest.lambda$maskedExceptionsWithS3" + )) + .collect(Collectors.toSet()); + } + + private static ILoggingEvent getLoggingEvent(String message, Exception ex) { + return new LoggingEvent(FormattingConverter.class.getName(), LOGGER, Level.ERROR, message, ex, null); + } + + private static ILoggingEvent getLoggingEvent(String message) { + return getLoggingEvent(message, null); + } +} diff --git a/src/test/java/com/uid2/shared/util/MaskingPatternLayoutTest.java b/src/test/java/com/uid2/shared/util/MaskingPatternLayoutTest.java index ea4dc00b..f54711b4 100644 --- a/src/test/java/com/uid2/shared/util/MaskingPatternLayoutTest.java +++ b/src/test/java/com/uid2/shared/util/MaskingPatternLayoutTest.java @@ -5,6 +5,7 @@ import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.classic.spi.LoggingEvent; import ch.qos.logback.core.pattern.FormattingConverter; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.params.ParameterizedTest; @@ -39,6 +40,11 @@ public static void setupAll() { MASKING_PATTERN_LAYOUT.start(); } + @AfterAll + public static void teardownAll() { + MASKING_PATTERN_LAYOUT.stop(); + } + @ParameterizedTest @MethodSource("maskedMessagesWithS3") public void testMaskedMessagesWithS3(String message, String maskedMessage) {