-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #249 from xm-online/feature/log_masking_master
Add masking layout
- Loading branch information
Showing
8 changed files
with
233 additions
and
1 deletion.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...able/src/main/java/com/icthh/xm/commons/logging/configurable/LogMaskingConfiguration.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,46 @@ | ||
package com.icthh.xm.commons.logging.configurable; | ||
|
||
import ch.qos.logback.classic.LoggerContext; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.Layout; | ||
import ch.qos.logback.core.OutputStreamAppender; | ||
import ch.qos.logback.core.encoder.Encoder; | ||
import ch.qos.logback.core.encoder.LayoutWrappingEncoder; | ||
import com.icthh.xm.commons.logging.config.LoggingConfigService; | ||
import com.icthh.xm.commons.logging.util.MaskingLayout; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.core.annotation.Order; | ||
|
||
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE; | ||
|
||
@Slf4j | ||
@Configuration | ||
@Order(HIGHEST_PRECEDENCE) | ||
public class LogMaskingConfiguration { | ||
|
||
public LogMaskingConfiguration(LoggingConfigService loggingConfigService) { | ||
log.info("Init MaskingPatternLayout"); | ||
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); | ||
context.getLogger("ROOT").iteratorForAppenders().forEachRemaining(it -> { | ||
if (it instanceof OutputStreamAppender) { | ||
var appender = (OutputStreamAppender<ILoggingEvent>) it; | ||
Encoder<ILoggingEvent> encoder = appender.getEncoder(); | ||
if (encoder instanceof LayoutWrappingEncoder) { | ||
LayoutWrappingEncoder<ILoggingEvent> layoutEncoder = (LayoutWrappingEncoder<ILoggingEvent>) encoder; | ||
Layout<ILoggingEvent> layout = layoutEncoder.getLayout(); | ||
|
||
LayoutWrappingEncoder<ILoggingEvent> maskingEncoder = new LayoutWrappingEncoder<>(); | ||
maskingEncoder.setLayout(new MaskingLayout(layout, loggingConfigService)); | ||
maskingEncoder.setContext(context); | ||
maskingEncoder.setCharset(layoutEncoder.getCharset()); | ||
maskingEncoder.start(); | ||
|
||
appender.setEncoder(maskingEncoder); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
} |
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
68 changes: 68 additions & 0 deletions
68
...nfigurable/src/test/java/com/icthh/xm/commons/logging/configurable/LogMaskingIntTest.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,68 @@ | ||
package com.icthh.xm.commons.logging.configurable; | ||
|
||
import com.icthh.xm.commons.security.spring.config.XmAuthenticationContextConfiguration; | ||
import com.icthh.xm.commons.tenant.TenantContextHolder; | ||
import com.icthh.xm.commons.tenant.TenantContextUtils; | ||
import com.icthh.xm.commons.tenant.spring.config.TenantContextConfiguration; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.Test; | ||
import org.junit.runner.Description; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.model.Statement; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.system.OutputCaptureRule; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@Slf4j | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = { | ||
TenantContextConfiguration.class, | ||
LoggingRefreshableConfiguration.class, | ||
XmAuthenticationContextConfiguration.class, | ||
LogMaskingConfiguration.class | ||
}, properties = {"spring.application.name=testApp"}) | ||
public class LogMaskingIntTest { | ||
|
||
private static final String UPDATE_KEY = "/config/tenants/TESTWITHCONFIG/testApp/logging.yml"; | ||
|
||
@Autowired | ||
private TenantContextHolder tenantContextHolder; | ||
@Autowired | ||
private LoggingRefreshableConfiguration loggingRefreshableConfiguration; | ||
|
||
@Test | ||
public void testLogMaskingConfiguration() { | ||
String config = "masking:\n enabled: true\n maskPatterns:\n - \"token=([^,]+)\"\n - \"password=([^,]+)\"\n"; | ||
loggingRefreshableConfiguration.onRefresh(UPDATE_KEY, config); | ||
String originalLog = "Log with password=passwordvalue, token=tokenvalue, attribute=blabla"; | ||
String maskedLog = "Log with password=*************, token=**********, attribute=blabla"; | ||
|
||
writeLogExpectLog(originalLog, originalLog); | ||
|
||
TenantContextUtils.setTenant(tenantContextHolder, "TEST"); | ||
writeLogExpectLog(originalLog, originalLog); | ||
tenantContextHolder.getPrivilegedContext().destroyCurrentContext(); | ||
|
||
TenantContextUtils.setTenant(tenantContextHolder, "TESTWITHCONFIG"); | ||
writeLogExpectLog(originalLog, maskedLog); | ||
} | ||
|
||
@SneakyThrows | ||
private void writeLogExpectLog(String logToWrite, String expectLog) { | ||
String loggerName = LogMaskingIntTest.class.getSimpleName() + ": "; | ||
var outputCapture = new OutputCaptureRule(); | ||
outputCapture.apply(new Statement() { | ||
@Override | ||
public void evaluate() throws Throwable { | ||
log.info(logToWrite); | ||
String actual = outputCapture.toString(); | ||
actual = actual.substring(actual.indexOf(loggerName) + loggerName.length()).trim(); | ||
assertEquals(expectLog, actual); | ||
} | ||
}, Description.EMPTY).evaluate(); | ||
} | ||
} |
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
3 changes: 3 additions & 0 deletions
3
...mmons-logging/src/main/java/com/icthh/xm/commons/logging/config/LoggingConfigService.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
7 changes: 7 additions & 0 deletions
7
...s-logging/src/main/java/com/icthh/xm/commons/logging/config/LoggingConfigServiceStub.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
23 changes: 23 additions & 0 deletions
23
xm-commons-logging/src/main/java/com/icthh/xm/commons/logging/util/MaskingLayout.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,23 @@ | ||
package com.icthh.xm.commons.logging.util; | ||
|
||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
import ch.qos.logback.core.Layout; | ||
import ch.qos.logback.core.LayoutBase; | ||
import com.icthh.xm.commons.logging.config.LoggingConfigService; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
public class MaskingLayout extends LayoutBase<ILoggingEvent> { | ||
|
||
private final Layout<ILoggingEvent> layout; | ||
private final LoggingConfigService loggingConfigService; | ||
|
||
@Override | ||
public String doLayout(ILoggingEvent event) { | ||
return maskMessage(layout.doLayout(event)); | ||
} | ||
|
||
private String maskMessage(String message) { | ||
return loggingConfigService.getMaskingService().maskMessage(message); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
xm-commons-logging/src/main/java/com/icthh/xm/commons/logging/util/MaskingService.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,54 @@ | ||
package com.icthh.xm.commons.logging.util; | ||
|
||
import com.icthh.xm.commons.logging.config.LoggingConfig.MaskingLogConfiguration; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.IntStream; | ||
|
||
import static java.util.Collections.emptyList; | ||
|
||
public class MaskingService { | ||
private final Pattern multilinePattern; | ||
private final boolean enabled; | ||
|
||
public MaskingService(MaskingLogConfiguration maskingLogConfiguration, List<String> defaultMaskPatterns) { | ||
List<String> maskPatterns = getMaskPatterns(maskingLogConfiguration); | ||
maskPatterns.addAll(defaultMaskPatterns); | ||
this.enabled = isEnabled(maskingLogConfiguration); | ||
this.multilinePattern = Pattern.compile(String.join("|", maskPatterns), Pattern.MULTILINE); | ||
} | ||
|
||
private static List<String> getMaskPatterns(MaskingLogConfiguration maskingLogConfiguration) { | ||
List<String> patterns = Optional.ofNullable(maskingLogConfiguration) | ||
.map(MaskingLogConfiguration::getMaskPatterns) | ||
.orElse(emptyList()); | ||
return new ArrayList<>(patterns); | ||
} | ||
|
||
private static Boolean isEnabled(MaskingLogConfiguration maskingLogConfiguration) { | ||
return Optional.ofNullable(maskingLogConfiguration) | ||
.map(MaskingLogConfiguration::getEnabled) | ||
.orElse(false); | ||
} | ||
|
||
public String maskMessage(String message) { | ||
if (!enabled) { | ||
return message; | ||
} | ||
|
||
StringBuilder sb = new StringBuilder(message); | ||
Matcher matcher = multilinePattern.matcher(sb); | ||
while (matcher.find()) { | ||
IntStream.rangeClosed(1, matcher.groupCount()).forEach(group -> { | ||
if (matcher.group(group) != null) { | ||
IntStream.range(matcher.start(group), matcher.end(group)).forEach(i -> sb.setCharAt(i, '*')); | ||
} | ||
}); | ||
} | ||
return sb.toString(); | ||
} | ||
} |