diff options
author | BENJAMIN, MAX <max.benjamin@att.com> | 2021-03-16 11:20:24 -0400 |
---|---|---|
committer | AT&T Open Source <g22940@att.com> | 2021-03-16 11:20:25 -0400 |
commit | 55da0634facacf1e1c77e7b1d89a42176552b227 (patch) | |
tree | 714bc5b035238737cb207a21a155a9241274e4e2 /common/src/main | |
parent | 1390709516d9d88cb498dd9d02ca39515eb59c24 (diff) |
wrap logger patterns in masking class
wrap logger patterns in masking class
Issue-ID: SO-3589
Signed-off-by: AT&T Open Source <g22940@att.com>
Change-Id: Ic7893001b77a9791cd43c958b9bad7065d7a4e2a
Diffstat (limited to 'common/src/main')
-rw-r--r-- | common/src/main/java/org/onap/so/logger/MaskLogStatements.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/common/src/main/java/org/onap/so/logger/MaskLogStatements.java b/common/src/main/java/org/onap/so/logger/MaskLogStatements.java new file mode 100644 index 0000000000..cadadcf9f3 --- /dev/null +++ b/common/src/main/java/org/onap/so/logger/MaskLogStatements.java @@ -0,0 +1,71 @@ +package org.onap.so.logger; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import ch.qos.logback.classic.PatternLayout; +import ch.qos.logback.classic.spi.ILoggingEvent; + +public class MaskLogStatements extends PatternLayout { + + private String patternsProperty; + private String maskChar = "*"; + private Optional<Pattern> pattern = Optional.empty(); + private static final Pattern authPattern = + Pattern.compile("Authorization(?:\\:|=)\\s?(?:\"|\\[)(?:Basic|Bearer) (.*?)(?:\"|\\])"); + private static final Pattern openstackPattern = Pattern.compile("\"password\"\\s?:\\s?\"(.*?)\""); + + public String getPatternsProperty() { + return patternsProperty; + } + + public void setPatternsProperty(String patternsProperty) { + this.patternsProperty = patternsProperty; + if (this.patternsProperty != null) { + this.pattern = Optional.of(Pattern.compile(patternsProperty, Pattern.MULTILINE)); + } + } + + public String getMaskChar() { + return maskChar; + } + + public void setMaskChar(String maskChar) { + this.maskChar = maskChar; + } + + + protected Collection<Pattern> getPatterns() { + return Arrays.asList(authPattern, openstackPattern); + } + + @Override + public String doLayout(ILoggingEvent event) { + + final StringBuilder message = new StringBuilder(super.doLayout(event)); + List<Pattern> patterns = new ArrayList<>(getPatterns()); + if (pattern.isPresent()) { + patterns.add(pattern.get()); + } + patterns.forEach(p -> { + Matcher matcher = p.matcher(message); + while (matcher.find()) { + int group = 1; + while (group <= matcher.groupCount()) { + if (matcher.group(group) != null) { + for (int i = matcher.start(group); i < matcher.end(group); i++) { + message.setCharAt(i, maskChar.charAt(0)); + } + } + group++; + } + } + }); + return message.toString(); + } + +} |