Skip to content

Commit

Permalink
DEV: Refactored and simplified few classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanthkv committed Dec 4, 2024
1 parent 8a7efa7 commit 3346aad
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 279 deletions.
28 changes: 15 additions & 13 deletions ejmask-api/src/main/java/com/ebay/ejmask/api/IPatternBuilder.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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
*
Expand All @@ -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<PatternEntity> 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<PatternEntity> buildPatternEntities(int visibleCharacters, String... fieldNames) {
return Collections.singletonList(new PatternEntity(this.buildPattern(visibleCharacters, fieldNames), this.buildReplacement(visibleCharacters, fieldNames)));
}
}
64 changes: 32 additions & 32 deletions ejmask-api/src/main/java/com/ebay/ejmask/api/MaskingPattern.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
public class PatternEntity {

private final String patternTemplate;

private final String replacementTemplate;

/**
Expand Down
15 changes: 11 additions & 4 deletions ejmask-core/src/main/java/com/ebay/ejmask/core/EJMask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,28 @@ public static synchronized void addFilters(Collection<IFilter> filters) {
return;
}
List<Filter> 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<PatternEntity> 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()) {
LoggerUtil.info("data-filter-initializer", "filter-pattern", filterPattern.toString());
}
}

/**
* Add grouped filter
*
* @param filter as Filter
*/
private static void addGroupedFilter(Filter filter) {
String[] fieldNames = toArray(filter.getFieldNames());
Collection<PatternEntity> 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
*
Expand All @@ -179,10 +182,10 @@ public static List<IContentProcessor> getContentPreProcessors() {
*/
private static void addNonGroupedFilters(Collection<IFilter> 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);
}
}
Expand Down Expand Up @@ -245,7 +248,7 @@ private static Map<Class<? extends IPatternBuilder>, Map<String, IFilter>> group
* @return list of Filter
*/
private static List<Filter> groupByOrderAndVisibleCharacters(Class<? extends IPatternBuilder> builderClass, Map<String, IFilter> fieldNameToFilterMapping) {
final IPatternBuilder builder = getBuilder(builderClass);
IPatternBuilder builder = getBuilder(builderClass);
if (!builder.isGroupable()) {
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -30,86 +25,39 @@
*/
public class JsonBooleanFieldPatternBuilder extends AbstractRegexPatternBuilder {

private static final List<PatternEntity> PATTERN_ENTITY_LIST = Arrays.asList(
/**
* Boolean field with value to be masked
* @see <a href="https://regex101.com/r/AEwc99/1">Regular Expresseion For Testing</a>
*/
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 <a href="https://regex101.com/r/AEwc99/1">Regular Expresseion For Testing</a>
*/
@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<PatternEntity> buildPatternEntities(int visibleCharacters, String... fieldNames) {
List<PatternEntity> 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;
}

}
Loading

0 comments on commit 3346aad

Please sign in to comment.