Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add log masking class for sensitive logs (#2003)
* Add logging mask class Signed-off-by: Carson Cook <carson.cook@ibm.com> * Clean up masking layout Signed-off-by: Carson Cook <carson.cook@ibm.com> * Add json value helpers to masker Signed-off-by: Carson Cook <carson.cook@ibm.com> * Add log mask to all logback configurations Signed-off-by: Carson Cook <carson.cook@ibm.com> * Fix typo in json patterns Signed-off-by: Carson Cook <carson.cook@ibm.com> * Add unit tests Signed-off-by: Carson Cook <carson.cook@ibm.com> * Add license header Signed-off-by: Carson Cook <carson.cook@ibm.com>
- Loading branch information
1 parent
56a4c2b
commit 994b483
Showing
5 changed files
with
162 additions
and
12 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
71 changes: 71 additions & 0 deletions
71
apiml-utility/src/main/java/org/zowe/apiml/product/logging/MaskingLogPatternLayout.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,71 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
package org.zowe.apiml.product.logging; | ||
|
||
import ch.qos.logback.classic.PatternLayout; | ||
import ch.qos.logback.classic.spi.ILoggingEvent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.IntStream; | ||
|
||
public class MaskingLogPatternLayout extends PatternLayout { | ||
private static final String MASK_VALUE = "***"; | ||
private static final Pattern maskPatterns = new MaskPatternBuilder() | ||
.addJsonValue("password") | ||
.addJsonValue("newPassword") | ||
.build(); | ||
|
||
@Override | ||
public String doLayout(ILoggingEvent event) { | ||
return maskMessage(super.doLayout(event)); | ||
} | ||
|
||
protected String maskMessage(String message) { | ||
StringBuilder sb = new StringBuilder(message); | ||
Matcher matcher = maskPatterns.matcher(sb); | ||
while (matcher.find()) { | ||
IntStream.rangeClosed(1, matcher.groupCount()).forEach(group -> { | ||
if (matcher.group(group) != null) { | ||
sb.replace(matcher.start(group), matcher.end(group), MASK_VALUE); | ||
} | ||
}); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public static class MaskPatternBuilder { | ||
private final List<String> maskPatterns = new ArrayList<>(); | ||
|
||
public MaskPatternBuilder add(String prefix, String capture) { | ||
return add(prefix, capture, ""); | ||
} | ||
|
||
public MaskPatternBuilder add(String prefix, String capture, String postfix) { | ||
maskPatterns.add(prefix + "(" + capture + ")" + postfix); | ||
return this; | ||
} | ||
|
||
public MaskPatternBuilder addJsonValue(String jsonKey, String... keys) { | ||
// pattern to get \"KEY\":\"VALUE\" with optional white space separating them | ||
add("\\\"" + jsonKey + "\\\"\\s*:\\s*", "\\\".*?\\\""); | ||
for (String k : keys) { | ||
add("\\\"" + k + "\\\"\\s*:\\s*", "\\\".*?\\\""); | ||
} | ||
return this; | ||
} | ||
|
||
public Pattern build() { | ||
return Pattern.compile(String.join("|", maskPatterns), Pattern.MULTILINE); | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
apiml-utility/src/test/java/org/zowe/apiml/product/logging/MaskingLogPatternLayoutTest.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,67 @@ | ||
/* | ||
* This program and the accompanying materials are made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Copyright Contributors to the Zowe Project. | ||
*/ | ||
package org.zowe.apiml.product.logging; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class MaskingLogPatternLayoutTest { | ||
@Nested | ||
class WhenBuildMask { | ||
private MaskingLogPatternLayout.MaskPatternBuilder underTest; | ||
|
||
@BeforeEach | ||
void setup() { | ||
underTest = new MaskingLogPatternLayout.MaskPatternBuilder(); | ||
} | ||
|
||
@Test | ||
void givenMultiplePatterns_thenReturnMaskRegexForBoth() { | ||
String expectedRegex = "one(cap1)|two(cap2)post"; | ||
underTest.add("one", "cap1").add("two", "cap2", "post"); | ||
|
||
assertEquals(expectedRegex, underTest.build().pattern()); | ||
} | ||
|
||
@Test | ||
void givenJsons_thenMaskRegexWithJsonFormat() { | ||
String expectedRegex = "\\\"key1\\\"\\s*:\\s*(\\\".*?\\\")|\\\"key2\\\"\\s*:\\s*(\\\".*?\\\")"; | ||
underTest.addJsonValue("key1", "key2"); | ||
|
||
assertEquals(expectedRegex, underTest.build().pattern()); | ||
} | ||
} | ||
|
||
@Nested | ||
class WhenMaskMessage { | ||
private MaskingLogPatternLayout underTest; | ||
|
||
@BeforeEach | ||
void setup() { | ||
underTest = new MaskingLogPatternLayout(); | ||
} | ||
|
||
@Test | ||
void givenNothingToMask_thenNoMask() { | ||
String message = "expected log"; | ||
String actual = underTest.maskMessage(message); | ||
assertEquals(message, actual); | ||
} | ||
|
||
@Test | ||
void givenSensitiveData_thenMaskData() { | ||
String actual = underTest.maskMessage("\"password\":\"mypassword\""); | ||
assertEquals("\"password\":***", actual); | ||
} | ||
} | ||
} |
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