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.
New feature that enhances the replacement functionality in the DefaultRegexpPairs. It allows for more flexible replacements by supporting dynamic replacement of matched groups from the regular expression pattern. - checkbox for explicit AWS usage - Refactored LogFileFilterOutputStream to handle multiple DefaultRegexpPairs --------- Authored-by: Fredrik Lysén <[email protected]>
- Loading branch information
Showing
8 changed files
with
155 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,3 +80,4 @@ fabric.properties | |
|
||
# Android studio 3.1+ serialized cache file | ||
.idea/caches/build_file_checksums.ser | ||
/.idea/ |
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
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
31 changes: 31 additions & 0 deletions
31
src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpAWS.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,31 @@ | ||
<!--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 AWS credential and token</td> | ||
<td>(AWS_[a-zA-Z_]+=)(\S+)</td> | ||
<td>$1********</td> | ||
<td> | ||
<ul> | ||
<li>AWS_ACCESS_KEY_ID=A7OTR4HWEGX2SKQMRAXI -> AWS_ACCESS_KEY_ID=********</li> | ||
<li>AWS_SECRET_ACCESS_KEY=agfkjgsakfjhsgad -> AWS_SECRET_ACCESS_KEY=********</li> | ||
<li>AWS_SESSION_TOKEN=ThGHJJHF7jdfnjsdzf -> AWS_SESSION_TOKEN=********</li> | ||
</ul> | ||
</td> | ||
</tr> | ||
</table> | ||
</div> |
68 changes: 68 additions & 0 deletions
68
src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.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.tsystems.sbs; | ||
|
||
import hudson.console.LineTransformationOutputStream; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static org.codehaus.groovy.runtime.ResourceGroovyMethods.filterLine; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
|
||
public class DefaultRegexpPairsAWSTest { | ||
private List<RegexpPair> getDefaultRegexpPairs() { | ||
return DefaultRegexpPairs.getDefaultRegexesAWS(); | ||
} | ||
@Test | ||
public void testDefaultPairsList() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
assertThat(defaultRegexpPairs.size(), greaterThan(0)); | ||
|
||
} | ||
|
||
@Test | ||
public void testDefaultPairs() { | ||
List<RegexpPair> defaultRegexpPairs = getDefaultRegexpPairs(); | ||
|
||
// Define the input string | ||
String input = "AWS_ACCESS_KEY_ID=R2RHTXG7QKSRWMOHEIAMH4A AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=Z1XKqTnKIHd7eLJhBZb9QWVcG0Rj3f8z1uYgO4Xm6vNiD5F7cM9pAa/2SsPqRrTtEoUyHwC+DxGJlWbVfNkOYK6hI3eX1L0j2+//////////"; | ||
String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_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); | ||
} | ||
} | ||
|