From e528ee9c0c58746cb45054f46b42cce6cfe169cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Wed, 20 Dec 2023 22:15:51 +0100 Subject: [PATCH 01/11] Add helper to AWS checkbox --- .../help-enabledDefaultRegexpAWS.html | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpAWS.html diff --git a/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpAWS.html b/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpAWS.html new file mode 100644 index 0000000..19fc5a9 --- /dev/null +++ b/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/help-enabledDefaultRegexpAWS.html @@ -0,0 +1,31 @@ + +
+ + + +

These are the default regular expressions and their respective replacements. These expressions are fixed and come with the plugin.

+ + + + + + + + + + + +
DescriptionRegexpReplacementSample
Masks AWS credential and token(AWS_[a-zA-Z_]+=)(\S+)$1******** +
    +
  • AWS_ACCESS_KEY_ID=A7OTR4HWEGX2SKQMRAXI -> AWS_ACCESS_KEY_ID=********
  • +
  • AWS_SECRET_ACCESS_KEY=agfkjgsakfjhsgad -> AWS_SECRET_ACCESS_KEY=********
  • +
  • AWS_SESSION_TOKEN=ThGHJJHF7jdfnjsdzf -> AWS_SESSION_TOKEN=********
  • +
+
+
\ No newline at end of file From daf944e898f0170245253ff93d07737c0bfef02f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 08:39:14 +0100 Subject: [PATCH 02/11] Add testcase and AWS pattern --- .../com/tsystems/sbs/DefaultRegexpPairs.java | 10 ++++ .../com/tsystems/sbs/LogFileFilterConfig.java | 11 ++++ .../sbs/LogFileFilterOutputStream.java | 36 +++++++---- .../java/com/tsystems/sbs/RegexpPair.java | 6 ++ .../sbs/LogFileFilterConfig/config.jelly | 4 ++ .../sbs/DefaultRegexpPairsAWSTest.java | 59 +++++++++++++++++++ .../tsystems/sbs/DefaultRegexpPairsTest.java | 1 - 7 files changed, 113 insertions(+), 14 deletions(-) create mode 100644 src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java diff --git a/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java b/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java index 7dd1cf0..d1954c3 100644 --- a/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java +++ b/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java @@ -19,7 +19,17 @@ public final class DefaultRegexpPairs { new RegexpPair("password=\\S*", "password=********") //PASSWORD MASKING )); + private final static List DEFAULT_REGEXES_AWS + = Collections.unmodifiableList( + Arrays.asList( + new RegexpPair("(AWS_[a-zA-Z_]+=)(\\S+)", "$1********") // AWS RegExp MASKING + )); + public static List getDefaultRegexes() { return DEFAULT_REGEXES; } + + public static List getDefaultRegexesAWS() { + return DEFAULT_REGEXES_AWS; + } } diff --git a/src/main/java/com/tsystems/sbs/LogFileFilterConfig.java b/src/main/java/com/tsystems/sbs/LogFileFilterConfig.java index f65ac4a..4e46323 100644 --- a/src/main/java/com/tsystems/sbs/LogFileFilterConfig.java +++ b/src/main/java/com/tsystems/sbs/LogFileFilterConfig.java @@ -45,6 +45,7 @@ public static LogFileFilterConfig get() { * Determines whether the regexp replacements which come fixed with the plugin are enabled. */ private boolean enabledDefaultRegexp; + private boolean enabledDefaultRegexpAWS; /** * Represents the custom regexp pairs specified by the user in the global settings. @@ -70,12 +71,22 @@ public boolean isEnabledDefaultRegexp() { return enabledDefaultRegexp; } + public boolean isEnabledDefaultRegexpAWS() { + return enabledDefaultRegexpAWS; + } + @DataBoundSetter public void setEnabledDefaultRegexp(boolean enabledDefaultRegexp) { this.enabledDefaultRegexp = enabledDefaultRegexp; save(); } + @DataBoundSetter + public void setEnabledDefaultRegexpAWS(boolean enabledDefaultRegexpAWS) { + this.enabledDefaultRegexpAWS = enabledDefaultRegexpAWS; + save(); + } + public List getRegexpPairs() { return regexpPairs; } diff --git a/src/main/java/com/tsystems/sbs/LogFileFilterOutputStream.java b/src/main/java/com/tsystems/sbs/LogFileFilterOutputStream.java index 23e9cac..8143a33 100644 --- a/src/main/java/com/tsystems/sbs/LogFileFilterOutputStream.java +++ b/src/main/java/com/tsystems/sbs/LogFileFilterOutputStream.java @@ -6,6 +6,7 @@ import java.io.IOException; import java.io.OutputStream; import java.nio.charset.Charset; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -29,6 +30,7 @@ public class LogFileFilterOutputStream extends LineTransformationOutputStream { //Global settings private final boolean isEnabledGlobally; private final boolean isEnabledDefaultRegexp; + private final boolean isEnabledDefaultRegexpAWS; private final List defaultRegexpPairs; private final List customRegexpPairs; private final String jobName; @@ -47,19 +49,27 @@ public LogFileFilterOutputStream(OutputStream out, Charset charset, String jobNa } isEnabledGlobally = config.isEnabledGlobally(); - isEnabledDefaultRegexp = config.isEnabledDefaultRegexp(); - if (isEnabledGlobally) { - //Load regexes - customRegexpPairs = config.getRegexpPairs(); - if (isEnabledDefaultRegexp) { - defaultRegexpPairs = DefaultRegexpPairs.getDefaultRegexes(); - } else { - defaultRegexpPairs = Collections.emptyList(); - } - } else { - customRegexpPairs = Collections.emptyList(); - defaultRegexpPairs = Collections.emptyList(); - } + isEnabledDefaultRegexp = config.isEnabledDefaultRegexp(); + isEnabledDefaultRegexpAWS = config.isEnabledDefaultRegexpAWS(); + + if (isEnabledGlobally) { + // Load regexes + customRegexpPairs = config.getRegexpPairs(); + defaultRegexpPairs = new ArrayList<>(); + if (isEnabledDefaultRegexp) { + defaultRegexpPairs.addAll(DefaultRegexpPairs.getDefaultRegexes()); + } + if (isEnabledDefaultRegexpAWS) { + defaultRegexpPairs.addAll(DefaultRegexpPairs.getDefaultRegexesAWS()); + } + // Log defaultRegexpPairs + for (RegexpPair pair : defaultRegexpPairs) { + LOGGER.log(Level.INFO, pair.toString()); + } + } else { + customRegexpPairs = Collections.emptyList(); + defaultRegexpPairs = Collections.emptyList(); + } } @Override diff --git a/src/main/java/com/tsystems/sbs/RegexpPair.java b/src/main/java/com/tsystems/sbs/RegexpPair.java index 9844f7a..1cfbf40 100644 --- a/src/main/java/com/tsystems/sbs/RegexpPair.java +++ b/src/main/java/com/tsystems/sbs/RegexpPair.java @@ -1,5 +1,6 @@ package com.tsystems.sbs; +import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; import hudson.model.AbstractDescribableImpl; import hudson.model.Descriptor; @@ -23,6 +24,11 @@ public class RegexpPair extends AbstractDescribableImpl implements S private String replacement; private transient Pattern compiledRegexp; + @Override + public String toString() { + return "RegexpPair{pattern=" + regexp + ", replacement=" + replacement + "}"; + } + @DataBoundConstructor public RegexpPair(String regexp, String replacement) { this.regexp = regexp; diff --git a/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/config.jelly b/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/config.jelly index 79b8f69..ae228e9 100644 --- a/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/config.jelly +++ b/src/main/resources/com/tsystems/sbs/LogFileFilterConfig/config.jelly @@ -24,6 +24,10 @@ + + + + diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java new file mode 100644 index 0000000..1863480 --- /dev/null +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java @@ -0,0 +1,59 @@ +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.hamcrest.Matchers.is; +import static org.junit.Assert.assertEquals; + + +public class DefaultRegexpPairsAWSTest { + private List getDefaultRegexpPairs() { + return DefaultRegexpPairs.getDefaultRegexesAWS(); + } + @Test + public void testDefaultPairsList() { + List defaultRegexpPairs = getDefaultRegexpPairs(); + assertThat(defaultRegexpPairs.size(), greaterThan(0)); + + } + + @Test + public void testDefaultPairs() { + List defaultRegexpPairs = getDefaultRegexpPairs(); + + // Define the input string + String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; + String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; + + + // Test the regular expression on each RegexpPair + // ToDo: Need to move this to a separate method and build a final result with all the replacements and regexes + for (RegexpPair pair : defaultRegexpPairs) { + String pattern = pair.getRegexp(); + String replacement = pair.getReplacement(); + + // Create a Pattern object + Pattern regexPattern = Pattern.compile(pattern); + + // Match the pattern against the input string + Matcher matcher = regexPattern.matcher(input); + + // Replace the matched pattern with the replacement + String replacedInput = matcher.replaceAll(replacement); + + System.out.println("Pattern: " + replacedInput); + + // Test the behavior + assertThat(replacedInput.contains(pattern), is(false)); + assertEquals(expected, replacedInput); + + } + } +} + diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java index 5bfb51a..3895e58 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java @@ -8,7 +8,6 @@ import static org.hamcrest.Matchers.is; public class DefaultRegexpPairsTest { - /** Sorry, this is really just a dummy testcase. */ @Test public void testDefaultPairs() { List defaultRegexpPairs = DefaultRegexpPairs.getDefaultRegexes(); From 03acffef7add74109575a29d6cd41ce345381c85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 08:44:32 +0100 Subject: [PATCH 03/11] Update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index aa0f125..74cbf5e 100644 --- a/.gitignore +++ b/.gitignore @@ -80,3 +80,4 @@ fabric.properties # Android studio 3.1+ serialized cache file .idea/caches/build_file_checksums.ser +/.idea/ From 2361997f3153c0f5d1ace0928a0ed5d81487a5f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:06:06 +0100 Subject: [PATCH 04/11] Delete src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java --- .../sbs/DefaultRegexpPairsAWSTest.java | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java deleted file mode 100644 index 1863480..0000000 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ /dev/null @@ -1,59 +0,0 @@ -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.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; - - -public class DefaultRegexpPairsAWSTest { - private List getDefaultRegexpPairs() { - return DefaultRegexpPairs.getDefaultRegexesAWS(); - } - @Test - public void testDefaultPairsList() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - assertThat(defaultRegexpPairs.size(), greaterThan(0)); - - } - - @Test - public void testDefaultPairs() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - - // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; - String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; - - - // Test the regular expression on each RegexpPair - // ToDo: Need to move this to a separate method and build a final result with all the replacements and regexes - for (RegexpPair pair : defaultRegexpPairs) { - String pattern = pair.getRegexp(); - String replacement = pair.getReplacement(); - - // Create a Pattern object - Pattern regexPattern = Pattern.compile(pattern); - - // Match the pattern against the input string - Matcher matcher = regexPattern.matcher(input); - - // Replace the matched pattern with the replacement - String replacedInput = matcher.replaceAll(replacement); - - System.out.println("Pattern: " + replacedInput); - - // Test the behavior - assertThat(replacedInput.contains(pattern), is(false)); - assertEquals(expected, replacedInput); - - } - } -} - From b8e77767c07527024f2d07a11964823a82fc3c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:14:18 +0100 Subject: [PATCH 05/11] New testcase for AWS --- src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java index 1863480..d904313 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java @@ -28,7 +28,7 @@ public void testDefaultPairs() { List defaultRegexpPairs = getDefaultRegexpPairs(); // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; + String input = "AWS_ACCESS_KEY_ID=R2RHTXG7QKSRWMOHEIAMH4A AWS_SECRET_ACCESS_KEY=/lkasjdflkGH6özsldf AWS_SESSION_TOKEN=Z1XKqTnKIHd7eLJhBZb9QWVcG0Rj3f8z1uYgO4Xm6vNiD5F7cM9pAa/2SsPqRrTtEoUyHwC+DxGJlqWZ7b3JYm0QaPn6gTtKsVhC+DxGJlWbVfNkOYK6hI3eX1L0j2+//////////"; String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; From d0c24ff89238bfa899ca7f96ca445d46f96aaac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:17:43 +0100 Subject: [PATCH 06/11] Delete src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java --- .../sbs/DefaultRegexpPairsAWSTest.java | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java deleted file mode 100644 index 1863480..0000000 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ /dev/null @@ -1,59 +0,0 @@ -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.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; - - -public class DefaultRegexpPairsAWSTest { - private List getDefaultRegexpPairs() { - return DefaultRegexpPairs.getDefaultRegexesAWS(); - } - @Test - public void testDefaultPairsList() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - assertThat(defaultRegexpPairs.size(), greaterThan(0)); - - } - - @Test - public void testDefaultPairs() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - - // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; - String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; - - - // Test the regular expression on each RegexpPair - // ToDo: Need to move this to a separate method and build a final result with all the replacements and regexes - for (RegexpPair pair : defaultRegexpPairs) { - String pattern = pair.getRegexp(); - String replacement = pair.getReplacement(); - - // Create a Pattern object - Pattern regexPattern = Pattern.compile(pattern); - - // Match the pattern against the input string - Matcher matcher = regexPattern.matcher(input); - - // Replace the matched pattern with the replacement - String replacedInput = matcher.replaceAll(replacement); - - System.out.println("Pattern: " + replacedInput); - - // Test the behavior - assertThat(replacedInput.contains(pattern), is(false)); - assertEquals(expected, replacedInput); - - } - } -} - From ed90d7753007935b2367c865b3ac373cd69521b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:19:06 +0100 Subject: [PATCH 07/11] New testcase for AWS --- src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java index 1863480..69cc9f0 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java @@ -28,7 +28,7 @@ public void testDefaultPairs() { List defaultRegexpPairs = getDefaultRegexpPairs(); // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; + String input = "AWS_ACCESS_KEY_ID=R2RHTXG7QKSRWMOHEIAMH4A AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=Z1XKqTnKIHd7eLJhBZb9QWVcG0Rj3f8z1uYgO4Xm6vNiD5F7cM9pAa/2SsPqRrTtEoUyHwC+DxGJlWbVfNkOYK6hI3eX1L0j2+//////////fj5M1Hn9yqRZvVUcL4i8qWZ7b3JYm0QaPn6gTtKsVhC+DxGJlWbVfNkOYK6hI3eX1L0j2+//////////Z1XKqTnKIHd7eLJhBZb9QWVcG0Rj3f8z1uYgO4Xm6vNiD5F7cM9pAa/2SsPqRrTtEoUyHwC+DxGJlWbVfNkOYK6hI3eX1L0j2+//////////"; String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; From 7fbadd42d444f1904ba6eeb427358e6cda858c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:22:18 +0100 Subject: [PATCH 08/11] Delete src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java --- .../sbs/DefaultRegexpPairsAWSTest.java | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java deleted file mode 100644 index 1863480..0000000 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ /dev/null @@ -1,59 +0,0 @@ -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.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; - - -public class DefaultRegexpPairsAWSTest { - private List getDefaultRegexpPairs() { - return DefaultRegexpPairs.getDefaultRegexesAWS(); - } - @Test - public void testDefaultPairsList() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - assertThat(defaultRegexpPairs.size(), greaterThan(0)); - - } - - @Test - public void testDefaultPairs() { - List defaultRegexpPairs = getDefaultRegexpPairs(); - - // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; - String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; - - - // Test the regular expression on each RegexpPair - // ToDo: Need to move this to a separate method and build a final result with all the replacements and regexes - for (RegexpPair pair : defaultRegexpPairs) { - String pattern = pair.getRegexp(); - String replacement = pair.getReplacement(); - - // Create a Pattern object - Pattern regexPattern = Pattern.compile(pattern); - - // Match the pattern against the input string - Matcher matcher = regexPattern.matcher(input); - - // Replace the matched pattern with the replacement - String replacedInput = matcher.replaceAll(replacement); - - System.out.println("Pattern: " + replacedInput); - - // Test the behavior - assertThat(replacedInput.contains(pattern), is(false)); - assertEquals(expected, replacedInput); - - } - } -} - From 5152327135b4bc9c2eaa2ac278f3010792ac7235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 09:23:15 +0100 Subject: [PATCH 09/11] New testcase for AWS --- src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java index 1863480..ce8dedd 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java @@ -28,7 +28,7 @@ public void testDefaultPairs() { List defaultRegexpPairs = getDefaultRegexpPairs(); // Define the input string - String input = "AWS_ACCESS_KEY_ID=ASIAQ42TKRXRW7GOEHMH AWS_SECRET_ACCESS_KEY=/lD8T9bXuZUW/F/8MutOB1vDXK2uG/gNHUe/d8bG AWS_SESSION_TOKEN=IQoJb3JpZ2luX2VjEF0aCWV1LXdlc3QtMSJHMEUCIQCML/uc20DoPT4BuxzRcspTZRWCLZeKxdhOCzt9GfVehQIgaSHU0fWmraj7iXv1UqNfagTaoPZZ189TyiIfabqKZNEqpAII1v//////////ARADGgwwNjE5MTQ5MTAxNzkiDDFOYsVLshKybCBrHSr4AYngLlJWHrHKeVcD9hfn+3Um37+ITcrq5mFbfC7uPuzV2JiGn3Sk1H8WcBLQTI87fdbqGNhKP9yC6ExDq3grTFkvHhUo4ctS/nQDrGoK8lRJIhymZO4DPB9BTLndUKaMHSIN"; + 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=********"; From b95ccb688f552e549cc9a29fb61cf5668c7cd28a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 10:59:08 +0100 Subject: [PATCH 10/11] Update test case --- .../com/tsystems/sbs/DefaultRegexpPairs.java | 3 +- .../sbs/DefaultRegexpPairsAWSTest.java | 33 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java b/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java index d1954c3..7d84672 100644 --- a/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java +++ b/src/main/java/com/tsystems/sbs/DefaultRegexpPairs.java @@ -22,7 +22,8 @@ public final class DefaultRegexpPairs { private final static List DEFAULT_REGEXES_AWS = Collections.unmodifiableList( Arrays.asList( - new RegexpPair("(AWS_[a-zA-Z_]+=)(\\S+)", "$1********") // AWS RegExp MASKING + new RegexpPair("(AWS_[a-zA-Z_]+=)(\\S+)", "$1********"), // AWS RegExp MASKING + new RegexpPair("(aws_[a-zA-Z_]+=)(\\S+)", "$1********") )); public static List getDefaultRegexes() { diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java index ce8dedd..7fd51d1 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsAWSTest.java @@ -1,11 +1,13 @@ 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; @@ -32,28 +34,35 @@ public void testDefaultPairs() { String expected = "AWS_ACCESS_KEY_ID=******** AWS_SECRET_ACCESS_KEY=******** AWS_SESSION_TOKEN=********"; - // Test the regular expression on each RegexpPair - // ToDo: Need to move this to a separate method and build a final result with all the replacements and regexes + StringBuilder replacedInput = new StringBuilder(input); + for (RegexpPair pair : defaultRegexpPairs) { String pattern = pair.getRegexp(); String replacement = pair.getReplacement(); - // Create a Pattern object Pattern regexPattern = Pattern.compile(pattern); + Matcher matcher = regexPattern.matcher(replacedInput); - // Match the pattern against the input string - Matcher matcher = regexPattern.matcher(input); + while (matcher.find()) { + String matchedPattern = matcher.group(); + String replacedString = replacement; - // Replace the matched pattern with the replacement - String replacedInput = matcher.replaceAll(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); + } - System.out.println("Pattern: " + replacedInput); + replacedInput.replace(matcher.start(), matcher.end(), replacedString); + matcher.region(matcher.start() + replacedString.length(), replacedInput.length()); + } + } - // Test the behavior - assertThat(replacedInput.contains(pattern), is(false)); - assertEquals(expected, replacedInput); + String replacedInputString = replacedInput.toString(); + System.out.println("Replaced input result: " + replacedInputString); - } + // Test the behavior + assertEquals(expected, replacedInputString); } } From 932c7ed76f3d8261db48a39866ffff83c5a8a3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Lys=C3=A9n?= Date: Thu, 21 Dec 2023 11:00:36 +0100 Subject: [PATCH 11/11] Update test case --- src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java index 3895e58..5bfb51a 100644 --- a/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java +++ b/src/test/java/com/tsystems/sbs/DefaultRegexpPairsTest.java @@ -8,6 +8,7 @@ import static org.hamcrest.Matchers.is; public class DefaultRegexpPairsTest { + /** Sorry, this is really just a dummy testcase. */ @Test public void testDefaultPairs() { List defaultRegexpPairs = DefaultRegexpPairs.getDefaultRegexes();