From 1cc2e57e5eccd898181f6aaf3f757eb77b335a65 Mon Sep 17 00:00:00 2001 From: prasanthkv Date: Tue, 3 Dec 2024 16:41:05 -0800 Subject: [PATCH 1/4] 1.2.0-SNAPSHOT --- ejmask-api/pom.xml | 5 +++-- ejmask-bom/pom.xml | 5 +++-- ejmask-core/pom.xml | 5 +++-- ejmask-extensions/pom.xml | 5 +++-- ejmask-spring/ejmask-spring-autoconfig/pom.xml | 5 +++-- ejmask-spring/ejmask-spring-boot/pom.xml | 5 +++-- ejmask-spring/ejmask-spring-core/pom.xml | 5 +++-- ejmask-spring/pom.xml | 5 +++-- pom.xml | 2 +- 9 files changed, 25 insertions(+), 17 deletions(-) diff --git a/ejmask-api/pom.xml b/ejmask-api/pom.xml index 36eb567..f7e2c95 100644 --- a/ejmask-api/pom.xml +++ b/ejmask-api/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT ejmask-api diff --git a/ejmask-bom/pom.xml b/ejmask-bom/pom.xml index b31374e..d0a94b5 100644 --- a/ejmask-bom/pom.xml +++ b/ejmask-bom/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT ../pom.xml diff --git a/ejmask-core/pom.xml b/ejmask-core/pom.xml index 97dd160..f8ac649 100644 --- a/ejmask-core/pom.xml +++ b/ejmask-core/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT ejmask-core diff --git a/ejmask-extensions/pom.xml b/ejmask-extensions/pom.xml index 88fcc4c..2fc7d3c 100644 --- a/ejmask-extensions/pom.xml +++ b/ejmask-extensions/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT ejmask-extensions diff --git a/ejmask-spring/ejmask-spring-autoconfig/pom.xml b/ejmask-spring/ejmask-spring-autoconfig/pom.xml index b48935d..04b16cb 100644 --- a/ejmask-spring/ejmask-spring-autoconfig/pom.xml +++ b/ejmask-spring/ejmask-spring-autoconfig/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-spring - 1.1.0 + 1.2.0-SNAPSHOT ejmask-spring-autoconfig diff --git a/ejmask-spring/ejmask-spring-boot/pom.xml b/ejmask-spring/ejmask-spring-boot/pom.xml index 7e2b529..194d8bf 100644 --- a/ejmask-spring/ejmask-spring-boot/pom.xml +++ b/ejmask-spring/ejmask-spring-boot/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-spring - 1.1.0 + 1.2.0-SNAPSHOT ejmask-spring-boot diff --git a/ejmask-spring/ejmask-spring-core/pom.xml b/ejmask-spring/ejmask-spring-core/pom.xml index d318a52..373c19e 100644 --- a/ejmask-spring/ejmask-spring-core/pom.xml +++ b/ejmask-spring/ejmask-spring-core/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-spring - 1.1.0 + 1.2.0-SNAPSHOT ejmask-spring-core diff --git a/ejmask-spring/pom.xml b/ejmask-spring/pom.xml index f56a12b..24894ed 100644 --- a/ejmask-spring/pom.xml +++ b/ejmask-spring/pom.xml @@ -1,9 +1,10 @@ - + + 4.0.0 com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT ejmask-spring diff --git a/pom.xml b/pom.xml index 39daea0..60f7d30 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ com.ebay.ejmask ejmask-parent - 1.1.0 + 1.2.0-SNAPSHOT pom From 8a7efa7a6990208c9b50880c8de9e150bbe23edd Mon Sep 17 00:00:00 2001 From: prasanthkv Date: Tue, 3 Dec 2024 16:43:39 -0800 Subject: [PATCH 2/4] DEV: Updated Dependency version --- pom.xml | 63 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/pom.xml b/pom.xml index 60f7d30..04a743b 100644 --- a/pom.xml +++ b/pom.xml @@ -1,4 +1,5 @@ - + + 4.0.0 com.ebay.ejmask @@ -49,6 +50,18 @@ + + + ossrh + Central Repository OSSRH + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + ossrh + https://oss.sonatype.org/content/repositories/snapshots + + + @@ -65,7 +78,7 @@ org.springframework.boot spring-boot-dependencies - 2.7.4 + 2.7.18 pom import @@ -73,25 +86,26 @@ org.junit junit-bom - 5.9.1 + 5.11.3 pom org.mockito mockito-bom - 4.8.1 + 5.14.2 pom + org.apache.commons commons-lang3 - 3.12.0 + 3.17.0 test commons-codec commons-codec - 1.15 + 1.17.1 test @@ -108,12 +122,12 @@ org.apache.maven.plugins maven-compiler-plugin - 3.10.1 + 3.13.0 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M8 + 3.5.2 target/jacoco.exec @@ -123,7 +137,7 @@ org.codehaus.mojo build-helper-maven-plugin - 3.3.0 + 3.6.0 add-integration-test-source-as-test-sources @@ -158,11 +172,10 @@ org.apache.maven.plugins maven-failsafe-plugin - 3.0.0-M8 + 3.5.2 2 false - ${defaultArg} **/*IT @@ -186,7 +199,7 @@ org.jacoco jacoco-maven-plugin - 0.8.8 + 0.8.12 @@ -227,14 +240,14 @@ com.github.spotbugs spotbugs-maven-plugin - 4.7.3.0 + 4.8.6.6 spotbugs-exclusions.xml com.h3xstream.findsecbugs findsecbugs-plugin - 1.12.0 + 1.13.0 false @@ -245,7 +258,7 @@ com.github.spotbugs spotbugs - 4.7.3 + 4.8.6 @@ -261,7 +274,7 @@ org.apache.maven.plugins maven-javadoc-plugin - 3.4.1 + 3.11.1 true @@ -280,7 +293,7 @@ org.sonatype.plugins nexus-staging-maven-plugin - 1.6.7 + 1.7.0 true ossrh @@ -306,27 +319,17 @@ - - - ossrh - https://oss.sonatype.org/content/repositories/snapshots - - - ossrh - https://oss.sonatype.org/service/local/staging/deploy/maven2/ - - release - + org.apache.maven.plugins maven-gpg-plugin - 3.0.1 + 3.2.7 sign-artifacts @@ -341,7 +344,7 @@ org.apache.maven.plugins maven-source-plugin - 3.2.1 + 3.3.1 attach-sources From 3346aad4a9c44780b07df82f5747f79f33c53b3d Mon Sep 17 00:00:00 2001 From: prasanthkv Date: Tue, 3 Dec 2024 16:44:16 -0800 Subject: [PATCH 3/4] DEV: Refactored and simplified few classes. --- .../com/ebay/ejmask/api/IPatternBuilder.java | 28 +++---- .../com/ebay/ejmask/api/MaskingPattern.java | 64 +++++++-------- .../com/ebay/ejmask/api/PatternEntity.java | 1 - .../java/com/ebay/ejmask/core/EJMask.java | 15 +++- .../ebay/ejmask/core/EJMaskInitializer.java | 35 +++++---- .../json/JsonBooleanFieldPatternBuilder.java | 76 +++--------------- .../builder/json/JsonFieldPatternBuilder.java | 77 +++---------------- .../json/JsonNumericFieldPatternBuilder.java | 76 +++--------------- .../json/JsonRelativeFieldPatternBuilder.java | 2 +- .../JsonBooleanFieldPatternBuilderTest.java | 18 ++--- .../json/JsonFieldPatternBuilderTest.java | 4 +- .../JsonNumericFieldPatternBuilderTest.java | 17 ++-- 12 files changed, 134 insertions(+), 279 deletions(-) diff --git a/ejmask-api/src/main/java/com/ebay/ejmask/api/IPatternBuilder.java b/ejmask-api/src/main/java/com/ebay/ejmask/api/IPatternBuilder.java index 4628150..5d5d578 100644 --- a/ejmask-api/src/main/java/com/ebay/ejmask/api/IPatternBuilder.java +++ b/ejmask-api/src/main/java/com/ebay/ejmask/api/IPatternBuilder.java @@ -1,7 +1,7 @@ package com.ebay.ejmask.api; -import java.util.Arrays; -import java.util.List; +import java.util.Collection; +import java.util.Collections; /** * Copyright (c) 2023 eBay Inc. @@ -21,6 +21,15 @@ public interface IPatternBuilder { + /** + * Set true if the build can be groupable. + * + * @return true if groupable + */ + default boolean isGroupable() { + return true; + } + /** * Build pattern to match * @@ -41,19 +50,12 @@ public interface IPatternBuilder { /** * Build pattern to match + * * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names + * @param fieldNames as list of field names * @return list of pattern entities */ - default List buildPatternEntities(int visibleCharacters, String... fieldNames) { - return Arrays.asList(new PatternEntity(buildPattern(visibleCharacters, fieldNames), buildReplacement(visibleCharacters, fieldNames))); - } - /** - * Set true if the build can be groupable. - * - * @return true if groupable - */ - default boolean isGroupable() { - return true; + default Collection buildPatternEntities(int visibleCharacters, String... fieldNames) { + return Collections.singletonList(new PatternEntity(this.buildPattern(visibleCharacters, fieldNames), this.buildReplacement(visibleCharacters, fieldNames))); } } diff --git a/ejmask-api/src/main/java/com/ebay/ejmask/api/MaskingPattern.java b/ejmask-api/src/main/java/com/ebay/ejmask/api/MaskingPattern.java index b536ba7..b23aa07 100644 --- a/ejmask-api/src/main/java/com/ebay/ejmask/api/MaskingPattern.java +++ b/ejmask-api/src/main/java/com/ebay/ejmask/api/MaskingPattern.java @@ -43,6 +43,27 @@ public MaskingPattern(int order, String regex, String replacement) { this.replacement = replacement; } + /** + * Get the value of order + * + * @return the value of order + */ + public int getOrder() { return order; } + + /** + * Get the value of pattern + * + * @return the value of pattern + */ + public Pattern getPattern() { return pattern; } + + /** + * Get the value of replacement + * + * @return the value of replacement + */ + public String getReplacement() { return replacement; } + /** * Replace sensitive data with mask * @@ -71,6 +92,17 @@ public int compareTo(MaskingPattern that) { return that.order > this.order ? -1 : 1; } + /** + * Returns the hash code of the given instance + * + * @return the hash code of this object. + * @see Object#hashCode + */ + @Override + public int hashCode() { + return Objects.hashCode(this); + } + /** * Indicates whether some other object is "equal to" this one. * @@ -89,17 +121,6 @@ public boolean equals(Object obj) { && Objects.equals(this.pattern, that.pattern); } - /** - * Returns the hash code of the given instance - * - * @return the hash code of this object. - * @see Object#hashCode - */ - @Override - public int hashCode() { - return Objects.hashCode(this); - } - /** * Returns the string representation of this pattern. * @@ -109,25 +130,4 @@ public int hashCode() { public String toString() { return "order=" + this.order + ";pattern=" + this.pattern.pattern() + ";replacement=" + this.replacement; } - - /** - * Get the value of order - * - * @return the value of order - */ - public int getOrder() { return order; } - - /** - * Get the value of pattern - * - * @return the value of pattern - */ - public Pattern getPattern() { return pattern; } - - /** - * Get the value of replacement - * - * @return the value of replacement - */ - public String getReplacement() { return replacement; } } diff --git a/ejmask-api/src/main/java/com/ebay/ejmask/api/PatternEntity.java b/ejmask-api/src/main/java/com/ebay/ejmask/api/PatternEntity.java index dd84541..d7fd0b3 100644 --- a/ejmask-api/src/main/java/com/ebay/ejmask/api/PatternEntity.java +++ b/ejmask-api/src/main/java/com/ebay/ejmask/api/PatternEntity.java @@ -20,7 +20,6 @@ public class PatternEntity { private final String patternTemplate; - private final String replacementTemplate; /** diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java index 2cd0100..cf7e92c 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java @@ -96,7 +96,7 @@ static void addFilter(int order, String regex, String substitute) { * @param content as sting which need to be masked * @return cleaned up string */ - public static String mask(final String content) { + public static String mask(String content) { return mask(content, true, true); } @@ -130,7 +130,7 @@ public static String mask(String content, boolean preProcessingRequired, boolean * @param content as sting which need to be masked * @return cleaned up string */ - public static String maskWithOutProcessor(final String content) { + public static String maskWithOutProcessor(String content) { return mask(content, false, false); } @@ -142,7 +142,7 @@ public static String maskWithOutProcessor(final String content) { * @param postProcessingRequired as boolean whether post-processing step required * @return cleaned up string */ - public static String mask(final String content, boolean preProcessingRequired, boolean postProcessingRequired) { + public static String mask(String content, boolean preProcessingRequired, boolean postProcessingRequired) { try { //filterPattern on original content if (CommonUtils.isBlank(content)) { @@ -171,7 +171,7 @@ public static String mask(final String content, boolean preProcessingRequired, b */ private static String process(String content, Operation operation) { for (IContentProcessor processor : PROCESSORS) { - final ProcessorResult result = operation.process(processor, content); + ProcessorResult result = operation.process(processor, content); if (result != null) { if (CommonUtils.isNotBlank(result.getContent())) { content = result.getContent(); @@ -203,6 +203,13 @@ private static String maskSensitiveContent(String content) { */ @FunctionalInterface interface Operation { + /** + * Process the given content. + * + * @param processor the processor + * @param content the content + * @return the result + */ ProcessorResult process(IContentProcessor processor, String content); } } diff --git a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMaskInitializer.java b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMaskInitializer.java index 50de4f3..22c55e8 100644 --- a/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMaskInitializer.java +++ b/ejmask-core/src/main/java/com/ebay/ejmask/core/EJMaskInitializer.java @@ -135,18 +135,8 @@ public static synchronized void addFilters(Collection filters) { return; } List filterGroups = removeDuplicatesAndBuildFilterGroups(filters); - //time to update filter pattern - for (Filter filter : filterGroups) { - //avoid empty due to duplicate - if (CommonUtils.isNotEmpty(filter.getFieldNames())) { - final String[] fieldNames = toArray(filter.getFieldNames()); - List patternEntityList = filter.getBuilder().buildPatternEntities(filter.getVisibleCharacters(), fieldNames); - emptyIfNull(patternEntityList).forEach(patternEntity -> { - //add masking pattern to data masking utility - addMaskingPattern(filter.getOrder(), patternEntity.getPatternTemplate(), patternEntity.getReplacementTemplate()); - }); - } - } + filterGroups.stream() + .filter(filter -> CommonUtils.isNotEmpty(filter.getFieldNames())).forEach(EJMaskInitializer::addGroupedFilter); addNonGroupedFilters(filters); //sort for order for (MaskingPattern filterPattern : EJMask.getMaskingPatterns()) { @@ -154,6 +144,19 @@ public static synchronized void addFilters(Collection filters) { } } + /** + * Add grouped filter + * + * @param filter as Filter + */ + private static void addGroupedFilter(Filter filter) { + String[] fieldNames = toArray(filter.getFieldNames()); + Collection patternEntityList = filter.getBuilder().buildPatternEntities(filter.getVisibleCharacters(), fieldNames); + //add masking pattern to data masking utility + emptyIfNull(patternEntityList) + .forEach(patternEntity -> addMaskingPattern(filter.getOrder(), patternEntity.getPatternTemplate(), patternEntity.getReplacementTemplate())); + } + /** * Get list of active MaskingPattern * @@ -179,10 +182,10 @@ public static List getContentPreProcessors() { */ private static void addNonGroupedFilters(Collection filters) { for (IFilter ifilter : filters) { - final IPatternBuilder builder = getBuilder(ifilter.getPatternBuilder()); + IPatternBuilder builder = getBuilder(ifilter.getPatternBuilder()); if (!builder.isGroupable()) { - final String pattern = builder.buildPattern(ifilter.getVisibleCharacters(), ifilter.getFieldNames()); - final String replacement = builder.buildReplacement(ifilter.getVisibleCharacters(), ifilter.getFieldNames()); + String pattern = builder.buildPattern(ifilter.getVisibleCharacters(), ifilter.getFieldNames()); + String replacement = builder.buildReplacement(ifilter.getVisibleCharacters(), ifilter.getFieldNames()); EJMask.addFilter(ifilter.getOrder(), pattern, replacement); } } @@ -245,7 +248,7 @@ private static Map, Map> group * @return list of Filter */ private static List groupByOrderAndVisibleCharacters(Class builderClass, Map fieldNameToFilterMapping) { - final IPatternBuilder builder = getBuilder(builderClass); + IPatternBuilder builder = getBuilder(builderClass); if (!builder.isGroupable()) { return Collections.emptyList(); } diff --git a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilder.java b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilder.java index 429d9f5..8a2b5d9 100644 --- a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilder.java +++ b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilder.java @@ -15,13 +15,8 @@ * limitations under the License. */ -import com.ebay.ejmask.api.PatternEntity; import com.ebay.ejmask.extenstion.builder.AbstractRegexPatternBuilder; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - /** * An implementation of IPatternBuilder to support sensitive JSON field, whose value need to be partially masked. * This builder is for masking the field value with a Boolean type. @@ -30,86 +25,39 @@ */ public class JsonBooleanFieldPatternBuilder extends AbstractRegexPatternBuilder { - private static final List PATTERN_ENTITY_LIST = Arrays.asList( - /** - * Boolean field with value to be masked - * @see Regular Expresseion For Testing - */ - new PatternEntity("\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*)(\\b(true|TRUE|True|false|FALSE|False)\\b)([^\\\"]{1,3})[^\\\"]*(\\\\?\\\"|)", "\"$1$2\"xxxx\"$5$6") - ); - /** - * Build pattern to match + * Boolean field with value to be masked * - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern + * @see Regular Expresseion For Testing */ - @Override - public String buildPattern(int visibleCharacters, String... fieldNames) { - return this.buildPattern(null, visibleCharacters, fieldNames); - } - - /** - * Build pattern to match - * - * @param patternEntity as instance of PatternEntity - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return - */ - private String buildPattern(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (visibleCharacters < 1) { - throw new IllegalArgumentException("visibleCharacters must be a possessive value instead of " + visibleCharacters); - } - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); - } - return String.format(patternEntity.getPatternTemplate(), super.buildFieldNamesForRegexOr(fieldNames), visibleCharacters); - } + private static final String PATTERN_TEMPLATE = "\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*)(\\b(true|TRUE|True|false|FALSE|False)\\b)([^\\\"]{1,3})[^\\\"]*(\\\\?\\\"|)"; + private static final String REPLACEMENT_TEMPLATE = "\"$1$2\"xxxx\"$5$6"; /** - * Build pattern to replace. + * Build pattern to match * * @param visibleCharacters as no of characters to be visible. * @param fieldNames as list of field names * @return match pattern */ @Override - public String buildReplacement(int visibleCharacters, String... fieldNames) { - return this.buildReplacement(null, visibleCharacters, fieldNames); + public String buildPattern(int visibleCharacters, String... fieldNames) { + if (visibleCharacters != 0) { + throw new IllegalArgumentException("visibleCharacters must be zero instead of " + visibleCharacters); + } + return String.format(PATTERN_TEMPLATE, super.buildFieldNamesForRegexOr(fieldNames)); } /** * Build pattern to replace. * - * @param patternEntity as instance of PatternEntity * @param visibleCharacters as no of characters to be visible. * @param fieldNames as list of field names * @return match pattern */ - private String buildReplacement(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); - } - return patternEntity.getReplacementTemplate(); - } - - /** - * Build list of PatternEntity - * - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern list - */ @Override - public List buildPatternEntities(int visibleCharacters, String... fieldNames) { - List result = new LinkedList<>(); - PATTERN_ENTITY_LIST.forEach(patternEntity -> { - result.add(new PatternEntity(this.buildPattern(patternEntity, visibleCharacters, fieldNames), this.buildReplacement(patternEntity, visibleCharacters, fieldNames))); - }); - return result; + public String buildReplacement(int visibleCharacters, String... fieldNames) { + return REPLACEMENT_TEMPLATE; } - } diff --git a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilder.java b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilder.java index 82228bf..d089b43 100644 --- a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilder.java +++ b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilder.java @@ -15,13 +15,8 @@ * limitations under the License. */ -import com.ebay.ejmask.api.PatternEntity; import com.ebay.ejmask.extenstion.builder.AbstractRegexPatternBuilder; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - /** * An implementation of IPatternBuilder to support sensitive JSON field, whose value need to be partially masked. * This builder is for masking the field value with a String type. @@ -29,14 +24,14 @@ * @author prakv */ public class JsonFieldPatternBuilder extends AbstractRegexPatternBuilder { + /** + * String field with value to be masked + * + * @see Regular Expresseion For Testing + */ + private static final String PATTERN_TEMPLATE = "\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*\\\")([^\\\"]{1,%d})[^\\\"]*(\\\\?\\\"|)"; - private static final List PATTERN_ENTITY_LIST = Arrays.asList( - /** - * String field with value to be masked - * @see Regular Expresseion For Testing - */ - new PatternEntity("\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*\\\")([^\\\"]{1,%d})[^\\\"]*(\\\\?\\\"|)", "\"$1$2$3-xxxx$4") - ); + private static final String REPLACEMENT_TEMPLATE = "\"$1$2$3-xxxx$4"; /** * Build pattern to match @@ -47,28 +42,12 @@ public class JsonFieldPatternBuilder extends AbstractRegexPatternBuilder { */ @Override public String buildPattern(int visibleCharacters, String... fieldNames) { - return this.buildPattern(null, visibleCharacters, fieldNames); - } - - /** - * Build pattern to match - * - * @param patternEntity as instance of PatternEntity - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return - */ - private String buildPattern(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (visibleCharacters < 1) { - throw new IllegalArgumentException("visibleCharacters must be a possessive value instead of " + visibleCharacters); - } - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); + if (visibleCharacters <= 0) { + throw new IllegalArgumentException("visibleCharacters must be a value greater than zero instead of " + visibleCharacters); } - return String.format(patternEntity.getPatternTemplate(), super.buildFieldNamesForRegexOr(fieldNames), visibleCharacters); + return String.format(PATTERN_TEMPLATE, super.buildFieldNamesForRegexOr(fieldNames), visibleCharacters); } - /** * Build pattern to replace. * @@ -78,38 +57,6 @@ private String buildPattern(PatternEntity patternEntity, int visibleCharacters, */ @Override public String buildReplacement(int visibleCharacters, String... fieldNames) { - return this.buildReplacement(null, visibleCharacters, fieldNames); + return REPLACEMENT_TEMPLATE; } - - /** - * Build pattern to replace. - * - * @param patternEntity as instance of PatternEntity - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern - */ - private String buildReplacement(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); - } - return patternEntity.getReplacementTemplate(); - } - - /** - * Build list of PatternEntity - * - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern list - */ - @Override - public List buildPatternEntities(int visibleCharacters, String... fieldNames) { - List result = new LinkedList<>(); - PATTERN_ENTITY_LIST.forEach(patternEntity -> { - result.add(new PatternEntity(this.buildPattern(patternEntity, visibleCharacters, fieldNames), this.buildReplacement(patternEntity, visibleCharacters, fieldNames))); - }); - return result; - } - -} +} \ No newline at end of file diff --git a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilder.java b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilder.java index eb09cd9..b551e0c 100644 --- a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilder.java +++ b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilder.java @@ -15,13 +15,8 @@ * limitations under the License. */ -import com.ebay.ejmask.api.PatternEntity; import com.ebay.ejmask.extenstion.builder.AbstractRegexPatternBuilder; -import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - /** * An implementation of IPatternBuilder to support sensitive JSON field, whose value need to be partially masked. * This builder is for masking the field value with a Numeric type. @@ -30,86 +25,39 @@ */ public class JsonNumericFieldPatternBuilder extends AbstractRegexPatternBuilder { - private static final List PATTERN_ENTITY_LIST = Arrays.asList( - /** - * Numeric field with value to be masked - * @see Regular Expresseion For Testing - */ - new PatternEntity("\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*)(-?\\b\\d+(\\.\\d+)?(e-?\\d+)?\\b)([^\\\"]{1,2})", "\"$1$2\"xxxx\"$6") - ); - /** - * Build pattern to match + * Numeric field with value to be masked * - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern + * @see Regular Expresseion For Testing */ - @Override - public String buildPattern(int visibleCharacters, String... fieldNames) { - return this.buildPattern(null, visibleCharacters, fieldNames); - } - - /** - * Build pattern to match - * - * @param patternEntity as instance of PatternEntity - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return - */ - private String buildPattern(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (visibleCharacters < 1) { - throw new IllegalArgumentException("visibleCharacters must be a possessive value instead of " + visibleCharacters); - } - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); - } - return String.format(patternEntity.getPatternTemplate(), super.buildFieldNamesForRegexOr(fieldNames), visibleCharacters); - } + private static final String PATTERN_TEMPLATE = "\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*)(-?\\b\\d+(\\.\\d+)?(e-?\\d+)?\\b)([^\\\"]{1,2})"; + private static final String REPLACEMENT_TEMPLATE = "\"$1$2\"xxxx\"$6"; /** - * Build pattern to replace. + * Build pattern to match * * @param visibleCharacters as no of characters to be visible. * @param fieldNames as list of field names * @return match pattern */ @Override - public String buildReplacement(int visibleCharacters, String... fieldNames) { - return this.buildReplacement(null, visibleCharacters, fieldNames); + public String buildPattern(int visibleCharacters, String... fieldNames) { + if (visibleCharacters != 0) { + throw new IllegalArgumentException("visibleCharacters must be a value greater than zero instead of " + visibleCharacters); + } + return String.format(PATTERN_TEMPLATE, super.buildFieldNamesForRegexOr(fieldNames)); } /** * Build pattern to replace. * - * @param patternEntity as instance of PatternEntity * @param visibleCharacters as no of characters to be visible. * @param fieldNames as list of field names * @return match pattern */ - private String buildReplacement(PatternEntity patternEntity, int visibleCharacters, String... fieldNames) { - if (patternEntity == null) { - patternEntity = PATTERN_ENTITY_LIST.get(0); - } - return patternEntity.getReplacementTemplate(); - } - - /** - * Build list of PatternEntity - * - * @param visibleCharacters as no of characters to be visible. - * @param fieldNames as list of field names - * @return match pattern list - */ @Override - public List buildPatternEntities(int visibleCharacters, String... fieldNames) { - List result = new LinkedList<>(); - PATTERN_ENTITY_LIST.forEach(patternEntity -> { - result.add(new PatternEntity(this.buildPattern(patternEntity, visibleCharacters, fieldNames), this.buildReplacement(patternEntity, visibleCharacters, fieldNames))); - }); - return result; + public String buildReplacement(int visibleCharacters, String... fieldNames) { + return REPLACEMENT_TEMPLATE; } - } diff --git a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonRelativeFieldPatternBuilder.java b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonRelativeFieldPatternBuilder.java index f936635..86514a3 100644 --- a/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonRelativeFieldPatternBuilder.java +++ b/ejmask-extensions/src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonRelativeFieldPatternBuilder.java @@ -53,7 +53,7 @@ public class JsonRelativeFieldPatternBuilder implements IPatternBuilder { @Override public String buildPattern(int visibleCharacters, String... fieldNames) { if (visibleCharacters < 1) { - throw new IllegalArgumentException("visibleCharacters must be a possessive value instead of " + visibleCharacters); + throw new IllegalArgumentException("visibleCharacters must be a value greater than zero instead of " + visibleCharacters); } return this.buildRegex(visibleCharacters, fieldNames); } diff --git a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilderTest.java b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilderTest.java index c5827e5..8beaf87 100644 --- a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilderTest.java +++ b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilderTest.java @@ -6,8 +6,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; -import java.util.List; +import java.util.Collection; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -25,17 +26,16 @@ public class JsonBooleanFieldPatternBuilderTest { */ @Test public void testBuildPattern() { - int visibleCharacters = 12; - String result = instance.buildPattern(visibleCharacters, fieldNames); + String result = instance.buildPattern(0, fieldNames); Assertions.assertEquals("\\\"(firstName|lastName|number|boolValue)(\\\\*\\\"\\s*:\\s*\\\\*)(\\b(true|TRUE|True|false|FALSE|False)\\b)([^\\\"]{1,3})[^\\\"]*(\\\\?\\\"|)", result); } /** * Test of buildPattern method, of class JsonFieldPatternBuilder. */ - @Test - public void testBuildPattern_without_visible_characters() { - int visibleCharacters = 0; + @ParameterizedTest + @ValueSource(ints = {-1, -10}) + public void testBuildPattern_without_visible_characters(int visibleCharacters) { Assertions.assertThrows(IllegalArgumentException.class, () -> instance.buildPattern(visibleCharacters, fieldNames)); } @@ -52,8 +52,8 @@ public void testBuildReplacement() { @ParameterizedTest @MethodSource("dataForTestMatch") public void testMatch(String name, String data, String expected) { - String regex = instance.buildPattern(2, fieldNames); - String replacement = instance.buildReplacement(2, fieldNames); + String regex = instance.buildPattern(0, fieldNames); + String replacement = instance.buildReplacement(0, fieldNames); Pattern pattern = Pattern.compile(regex); String result = pattern.matcher(data).replaceAll(replacement); Assertions.assertEquals(expected, result); @@ -62,7 +62,7 @@ public void testMatch(String name, String data, String expected) { @ParameterizedTest @MethodSource("dataForTestMatch") public void testMatchForPatternList(String name, String data, String expected) { - List patternEntityList = instance.buildPatternEntities(2, fieldNames); + Collection patternEntityList = instance.buildPatternEntities(0, fieldNames); String result = data; for (PatternEntity patternEntity : patternEntityList) { Pattern pattern = Pattern.compile(patternEntity.getPatternTemplate()); diff --git a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilderTest.java b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilderTest.java index f6d278b..f8bcc5b 100644 --- a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilderTest.java +++ b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonFieldPatternBuilderTest.java @@ -7,7 +7,7 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -import java.util.List; +import java.util.Collection; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -63,7 +63,7 @@ public void testMatch(String name, String data, String expected) { @ParameterizedTest @MethodSource("dataForTestMatch") public void testMatchForPatternList(String name, String data, String expected) { - List patternEntityList = instance.buildPatternEntities(2, fieldNames); + Collection patternEntityList = instance.buildPatternEntities(2, fieldNames); String result = data; for (PatternEntity patternEntity : patternEntityList) { Pattern pattern = Pattern.compile(patternEntity.getPatternTemplate()); diff --git a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilderTest.java b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilderTest.java index ab287a1..227f41e 100644 --- a/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilderTest.java +++ b/ejmask-extensions/src/test/java/com/ebay/ejmask/extenstion/builder/json/JsonNumericFieldPatternBuilderTest.java @@ -6,8 +6,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; -import java.util.List; +import java.util.Collection; import java.util.regex.Pattern; import java.util.stream.Stream; @@ -25,7 +26,7 @@ public class JsonNumericFieldPatternBuilderTest { */ @Test public void testBuildPattern() { - int visibleCharacters = 12; + int visibleCharacters = 0; String result = instance.buildPattern(visibleCharacters, fieldNames); Assertions.assertEquals("\\\"(firstName|lastName|number|boolValue)(\\\\*\\\"\\s*:\\s*\\\\*)(-?\\b\\d+(\\.\\d+)?(e-?\\d+)?\\b)([^\\\"]{1,2})", result); } @@ -33,9 +34,9 @@ public void testBuildPattern() { /** * Test of buildPattern method, of class JsonFieldPatternBuilder. */ - @Test - public void testBuildPattern_without_visible_characters() { - int visibleCharacters = 0; + @ParameterizedTest + @ValueSource(ints = {-1, -10}) + public void testBuildPattern_without_visible_characters(int visibleCharacters) { Assertions.assertThrows(IllegalArgumentException.class, () -> instance.buildPattern(visibleCharacters, fieldNames)); } @@ -52,8 +53,8 @@ public void testBuildReplacement() { @ParameterizedTest @MethodSource("dataForTestMatch") public void testMatch(String name, String data, String expected) { - String regex = instance.buildPattern(2, fieldNames); - String replacement = instance.buildReplacement(2, fieldNames); + String regex = instance.buildPattern(0, fieldNames); + String replacement = instance.buildReplacement(0, fieldNames); Pattern pattern = Pattern.compile(regex); String result = pattern.matcher(data).replaceAll(replacement); Assertions.assertEquals(expected, result); @@ -62,7 +63,7 @@ public void testMatch(String name, String data, String expected) { @ParameterizedTest @MethodSource("dataForTestMatch") public void testMatchForPatternList(String name, String data, String expected) { - List patternEntityList = instance.buildPatternEntities(2, fieldNames); + Collection patternEntityList = instance.buildPatternEntities(0, fieldNames); String result = data; for (PatternEntity patternEntity : patternEntityList) { Pattern pattern = Pattern.compile(patternEntity.getPatternTemplate()); From 2e125e8ff4eb72c11f0d42d37b8397c5c9af2596 Mon Sep 17 00:00:00 2001 From: Prasanth Kaimattil Venu Date: Tue, 3 Dec 2024 17:41:42 -0800 Subject: [PATCH 4/4] DEV: Moved Contributing.md to .github folder --- .github/CONTRIBUTING.md | 56 ++++++++++++++++++++++++++++++++++ CONTRIBUTING.adoc | 66 ----------------------------------------- 2 files changed, 56 insertions(+), 66 deletions(-) create mode 100644 .github/CONTRIBUTING.md delete mode 100644 CONTRIBUTING.adoc diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md new file mode 100644 index 0000000..240ded4 --- /dev/null +++ b/.github/CONTRIBUTING.md @@ -0,0 +1,56 @@ +# eJMask Contribution Guidelines + +Thank you so much for wanting to contribute to eJMask! Here are a few important things you should know about contributing: + +1. API changes require discussion, use cases, etc. Code comes later. +2. Pull requests are great for small fixes for bugs, documentation, etc. +3. Code contributions require updating relevant documentation. + +This project takes all contributions through [pull requests](https://help.github.com/articles/using-pull-requests). Code should *not* be pushed directly to `master`. + +The following guidelines apply to all contributors. + +## Types of contributions + +All types of contributions, from minor documentation changes to new capabilities, performance improvements, extending tests, etc., are all welcome. + +## Making Changes + +* Fork the `eJMask` repository. +* Make your changes and push them to a topic branch in your fork. +* See our commit message guidelines further down in this document. +* Submit a pull request to the repository. +* Update the `eJMask` GitHub issue with the generated pull request link. + +## General Guidelines + +* Only one logical change per commit. +* Do not mix whitespace changes with functional code changes. +* Do not mix unrelated functional changes. +* When writing a commit message: + * Describe *why* a change is being made. + * Do not assume the reviewer understands what the original problem was. + * Do not assume the code is self-evident/self-documenting. + * Describe any limitations of the current code. +* Any significant changes should be accompanied by tests. +* The project already has good test coverage, so look at some of the existing tests if you're unsure how to go about it. +* Please squash all commits for a change into a single commit (this can be done using `git rebase -i`). + +## Commit Message Guidelines + +* Provide a brief description of the change in the first line. +* Insert a single blank line after the first line. +* Provide a detailed description of the change in the following lines, breaking paragraphs where needed. +* The first line should be limited to 50 characters and should not end in a period. +* Subsequent lines should be wrapped at 72 characters. + +## Java Guidelines + +- Please make sure: + * Source code is always formatted before commit. + * You remove all unused imports. + * Usage of `@Override`. +- Please avoid: + * Trailing whitespace on all lines. + * All unused imports. + * Empty blocks. diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc deleted file mode 100644 index c2b9c63..0000000 --- a/CONTRIBUTING.adoc +++ /dev/null @@ -1,66 +0,0 @@ -= eJMask Contribution Guidelines - -ifdef::env-github[] -:outfilesuffix: .adoc -:note-caption: :bulb: -endif::[] -:toc: -:toclevels: 4 - -Thank you so much for wanting to contribute to eJMask ! -Here are a few important things you should know about contributing: - -1. API changes require discussion, use cases, etc. -Code comes later. -2. Pull requests are great for small fixes for bugs, documentation, etc. -3. Code contributions require updating relevant documentation. - -This project takes all contributions through https://help.github.com/articles/using-pull-requests[pull requests]. -Code should *not* be pushed directly to `master`. - -The following guidelines apply to all contributors. - -== Types of contributions - -All types of contributions from minor documentation changes to new capabilities, performance improvement, extending test etc., are all welcome. - -== Making Changes - -* Fork the `eJMask` repository -* Make your changes and push them to a topic branch in your fork -* See our commit message guidelines further down in this document -* Submit a pull request to the repository -* Update `eJMask` GITHUB issue with the generated pull request link - -== General Guidelines - -* Only one logical change per commit -* Do not mix whitespace changes with functional code changes. -* Do not mix unrelated functional changes. -* When writing a commit message: -** Describe _why_ a change is being made -** Do not assume the reviewer understands what the original problem was -** Do not assume the code is self-evident/self-documenting -** Describe any limitations of the current code -* Any significant changes should be accompanied by tests. -* The project already has good test coverage, so look at someofthe existing tests if you're unsure how to go about it. -* Please squash all commits for a change into a single commit (this can be done using `git rebase -i`). - -== Commit Message Guidelines - -* Provide a brief description of the change in the first line. -* Insert a single blank line after the first line -* Provide a detailed description of the change in the following lines, breaking paragraphs where needed. -* The first line should be limited to 50 characters and should not end in a period. -* Subsequent lines should be wrapped at 72 characters. - -== Java Guidelines - -- Please make sure -* source code is always formatted before commit. -* you remove all unused imports. -* usage of `@Override` -- Please avoid -* trailing whitespace on all lines. -* all unused imports. -* empty blocks