forked from SoftwareBuildService/log-file-filter-plugin
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional regex pattern for Datadog API/APP tokens.
Authored-by: Fredrik Lysén <[email protected]>
- Loading branch information
Showing
6 changed files
with
217 additions
and
1 deletion.
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
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
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
40 changes: 40 additions & 0 deletions
40
src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpDD.html
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,40 @@ | ||
<!--THIS FILE IS AUTOGENERATED FROM THE global.properties FILE--> | ||
<div> | ||
|
||
<style> | ||
.log-file-filter-plugin th, .log-file-filter-plugin td{ | ||
border: solid 1px black; | ||
border-collapse: collapse; | ||
padding: 10px | ||
} | ||
</style> | ||
|
||
<p>These are the default regular expressions and their respective replacements. These expressions are fixed and come with the plugin.</p> | ||
<table class="log-file-filter-plugin"> | ||
<!-- Table header --> | ||
<tr><th>Description</th><th>Regexp</th><th>Replacement</th><th>Sample</th></tr> | ||
|
||
<!-- Table rows (regexes) --> | ||
<tr> | ||
<td>Masks Datadog APP secrets</td> | ||
<td>((?i)(\bdatadog|dd|dogapi\b).*)(\b([a-zA-Z-0-9]{40})\b)</td> | ||
<td>$1********</td> | ||
<td> | ||
<ul> | ||
<li>"datadog key = 3c0c3965368a6b10f7640dbda46abfd2 secret= 3c0c3965368a6b10f7640dbda46abfdca981c2d3" -> <b>datadog key = ******** secret= ********</b></li> | ||
</ul> | ||
</td> | ||
</tr> | ||
<tr> | ||
<td>Masks Datadog API secrets</td> | ||
<td>((?i)(\bdatadog|dd|dogapi\b).*)(\b([a-zA-Z-0-9]{32})\b)</td> | ||
<td>$1********</td> | ||
<td> | ||
<ul> | ||
<li>dAtAdOg token = "3c0c3965368a6b10f7640dbda46abfdc"; -> <b>dAtAdOg token = "********";</b></li> | ||
<li>curl -X GET "https://api.datadoghq.eu/api/v1/validate" -H "Accept: application/json" -H "DD-API-KEY: characteristicallycharacteristic" -> <b>curl -X GET "https://api.datadoghq.eu/api/v1/validate" -H "Accept: application/json" -H "DD-API-KEY: ********"</b></li> | ||
</ul> | ||
</td> | ||
</tr> | ||
</table> | ||
</div> |
144 changes: 144 additions & 0 deletions
144
src/test/java/com/tsystems/sbs/DefaultRegexpPairsDDTest.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,144 @@ | ||
package com.tsystems.sbs; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
public class DefaultRegexpPairsDDTest { | ||
private List<RegexpPair> getDefaultRegexpPairs() { | ||
return DefaultRegexpPairs.getDefaultRegexesDD(); | ||
} | ||
@Test | ||
public void testDefaultPairsList() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
assertThat(defaultRegexpPairs.size(), greaterThan(0)); | ||
|
||
} | ||
|
||
@Test | ||
public void testDefaultPairsApi() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
|
||
// Define the input string 32 characters | ||
String input = "curl -X GET \"https://api.datadoghq.eu/api/v1/validate\" -H \"Accept: application/json\" -H \"DD-API-KEY: characteristicallycharacteristic\""; | ||
String expected = "curl -X GET \"https://api.datadoghq.eu/api/v1/validate\" -H \"Accept: application/json\" -H \"DD-API-KEY: ********\""; | ||
|
||
|
||
StringBuilder replacedInput = new StringBuilder(input); | ||
|
||
for (RegexpPair pair : defaultRegexpPairs) { | ||
String pattern = pair.getRegexp(); | ||
String replacement = pair.getReplacement(); | ||
|
||
Pattern regexPattern = Pattern.compile(pattern); | ||
Matcher matcher = regexPattern.matcher(replacedInput); | ||
|
||
while (matcher.find()) { | ||
String matchedPattern = matcher.group(); | ||
String replacedString = replacement; | ||
|
||
// Replace all occurrences of $n with the matched groups | ||
for (int i = 1; i <= matcher.groupCount(); i++) { | ||
String group = matcher.group(i); | ||
replacedString = replacedString.replace("$" + i, group); | ||
} | ||
|
||
replacedInput.replace(matcher.start(), matcher.end(), replacedString); | ||
matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); | ||
} | ||
} | ||
|
||
String replacedInputString = replacedInput.toString(); | ||
System.out.println("Replaced input result: " + replacedInputString); | ||
|
||
// Test the behavior | ||
assertEquals(expected, replacedInputString); | ||
} | ||
|
||
@Test | ||
public void testDefaultPairsKey() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
|
||
// Define the input string 32 characters | ||
String input = "datadog key = 3c0c3965368a6b10f7640dbda46abfd2 secret= 3c0c3965368a6b10f7640dbda46abfdca981c2d3"; | ||
String expected = "datadog key = ******** secret= ********"; | ||
|
||
|
||
StringBuilder replacedInput = new StringBuilder(input); | ||
|
||
for (RegexpPair pair : defaultRegexpPairs) { | ||
String pattern = pair.getRegexp(); | ||
String replacement = pair.getReplacement(); | ||
|
||
Pattern regexPattern = Pattern.compile(pattern); | ||
Matcher matcher = regexPattern.matcher(replacedInput); | ||
|
||
while (matcher.find()) { | ||
String matchedPattern = matcher.group(); | ||
String replacedString = replacement; | ||
|
||
// Replace all occurrences of $n with the matched groups | ||
for (int i = 1; i <= matcher.groupCount(); i++) { | ||
String group = matcher.group(i); | ||
replacedString = replacedString.replace("$" + i, group); | ||
} | ||
|
||
replacedInput.replace(matcher.start(), matcher.end(), replacedString); | ||
matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); | ||
} | ||
} | ||
|
||
String replacedInputString = replacedInput.toString(); | ||
System.out.println("Replaced input result: " + replacedInputString); | ||
|
||
// Test the behavior | ||
assertEquals(expected, replacedInputString); | ||
} | ||
@Test | ||
public void testDefaultPairsToken() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
|
||
// Define the input string 32 characters | ||
String input = "dAtAdOg token = \"3c0c3965368a6b10f7640dbda46abfdc\";"; | ||
String expected = "dAtAdOg token = \"********\";"; | ||
|
||
|
||
StringBuilder replacedInput = new StringBuilder(input); | ||
|
||
for (RegexpPair pair : defaultRegexpPairs) { | ||
String pattern = pair.getRegexp(); | ||
String replacement = pair.getReplacement(); | ||
|
||
Pattern regexPattern = Pattern.compile(pattern); | ||
Matcher matcher = regexPattern.matcher(replacedInput); | ||
|
||
while (matcher.find()) { | ||
String matchedPattern = matcher.group(); | ||
String replacedString = replacement; | ||
|
||
// Replace all occurrences of $n with the matched groups | ||
for (int i = 1; i <= matcher.groupCount(); i++) { | ||
String group = matcher.group(i); | ||
replacedString = replacedString.replace("$" + i, group); | ||
} | ||
|
||
replacedInput.replace(matcher.start(), matcher.end(), replacedString); | ||
matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); | ||
} | ||
} | ||
|
||
String replacedInputString = replacedInput.toString(); | ||
System.out.println("Replaced input result: " + replacedInputString); | ||
|
||
// Test the behavior | ||
assertEquals(expected, replacedInputString); | ||
} | ||
} | ||
|