jlib-framework / jlib-awslambda-logback

jlib AWS Lambda SLF4J/Logback Appender
Apache License 2.0
22 stars 0 forks source link

How to mask the PII/PCI data with this appender #10

Open DevProblems opened 8 months ago

DevProblems commented 8 months ago

I have added the layout in the appender like this

**

<appender name="awslambda" class="org.jlib.cloud.aws.lambda.logback.AwsLambdaAppender">
    <encoder type="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
        <layout class="com.example.app.MaskingPatternLayout">
            <maskPattern>(\w+@\w+\.\w+)</maskPattern>
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] &lt;%-36X{AWSRequestId}&gt; &lt;%-36X{messageUuid}&gt; %-5level
                %logger{10} - %msg%n
            </pattern>
        </layout>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="awslambda"/>
</root>

**

MaskingPatternLayout class:

package com.example.app;

import ch.qos.logback.classic.PatternLayout; import ch.qos.logback.classic.spi.ILoggingEvent;

import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.IntStream;

public class MaskingPatternLayout extends PatternLayout {

private Pattern pattern = null;

public void addMaskPattern(String maskPattern) {
    pattern = Pattern.compile(maskPattern, Pattern.MULTILINE);
}

@Override
public String doLayout(ILoggingEvent event) {
    String log = super.doLayout(event);
    StringBuilder logBuilder = new StringBuilder(log);
    Matcher matcher = pattern.matcher(logBuilder);
    while (matcher.find()) {
        int startIndex = matcher.start();
        int endIndex = matcher.end();
        IntStream.range(startIndex, endIndex).forEach(i -> logBuilder.setCharAt(i, '*'));
    }
    return logBuilder.toString();
}

}

Logs when invoking the Lambda Function:

|-ERROR in ch.qos.logback.core.joran.util.PropertySetter@77a567e1 - Could not invoke method setLayout in class ch.qos.logback.classic.encoder.PatternLayoutEncoder with parameter of type MaskingPatternLayout java.lang.reflect.InvocationTargetException at java.lang.reflect.InvocationTargetException at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at at java.lang.reflect.Method.invoke(Method.java:498) at at ch.qos.logback.core.joran.util.PropertySetter.invokeMethodWithSingleParameterOnThisObject(PropertySetter.java:250) at at ch.qos.logback.core.joran.util.PropertySetter.setComplexProperty(PropertySetter.java:300) at at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:173) at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:309) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:193) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:179) at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:165) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:152) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:110) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:53) at at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:82) at at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:157) at at ch.qos.logback.classic.spi.LogbackServiceProvider.initializeLoggerContext(LogbackServiceProvider.java:49) at at ch.qos.logback.classic.spi.LogbackServiceProvider.initialize(LogbackServiceProvider.java:40) at at org.slf4j.LoggerFactory.bind(LoggerFactory.java:153) at at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:141) at at org.slf4j.LoggerFactory.getProvider(LoggerFactory.java:419) at at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:405) at at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:354) at at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:380) at at com.example.app.handlers.LoggingHandler.(LoggingHandler.java:13) at at java.lang.Class.forName0(Native Method) at at java.lang.Class.forName(Class.java:348) at at lambdainternal.HandlerInfo.fromString(HandlerInfo.java:33) at at lambdainternal.AWSLambda.findUserMethods(AWSLambda.java:114) at at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:241) at at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:208) at at lambdainternal.AWSLambda.main(AWSLambda.java:198) Caused by: java.lang.UnsupportedOperationException: one cannot set the layout of ch.qos.logback.classic.encoder.PatternLayoutEncoder at at ch.qos.logback.core.pattern.PatternLayoutEncoderBase.setLayout(PatternLayoutEncoderBase.java:62) at ... 33 common frames omitted 21:17:15,410 |-ERROR in ch.qos.logback.classic.PatternLayout("null") - Empty or null pattern. 21:17:15,410 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO 21:17:15,410 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [awslambda] to Logger[ROOT] 21:17:15,412 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 21:17:15,413 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@736e9adb - Registering current configuration as safe fallback point START RequestId: 7f74005b-6462-4c3d-a042-4c35feb3e196 Version: $LATEST END RequestId: 7f74005b-6462-4c3d-a042-4c35feb3e196 REPORT RequestId: 7f74005b-6462-4c3d-a042-4c35feb3e196 Duration: 458.65 ms Billed Duration: 459 ms Memory Size: 512 MB Max Memory Used: 94 MB Init Duration: 1003.82 ms

igorakkerman commented 8 months ago

Hi @DevProblems, thanks for your report. I will look into it as soon as possible.