-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new Pattern Builder and Unit Test
- Loading branch information
1 parent
1cbc0c8
commit 231b5bf
Showing
12 changed files
with
163 additions
and
11 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
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
47 changes: 47 additions & 0 deletions
47
...in/java/com/ebay/ejmask/extenstion/builder/json/JsonValueUnmaskFromEndPatternBuilder.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,47 @@ | ||
package com.ebay.ejmask.extenstion.builder.json; | ||
|
||
import com.ebay.ejmask.extenstion.builder.AbstractRegexPatternBuilder; | ||
|
||
/** | ||
* An implementation of IPatternBuilder to support high sensitive JSON field, whose value | ||
* need to be partially masked with relative field from end of the field. | ||
* | ||
* @author parasdharwal33 | ||
*/ | ||
public class JsonValueUnmaskFromEndPatternBuilder extends AbstractRegexPatternBuilder { | ||
|
||
//https://regex101.com/r/LL3EY7/1/ | ||
private static final String PATTERN_TEMPLATE = "\\\"(%s)(\\\\*\\\"\\s*:\\s*\\\\*\\\")[^\\\"]*([^\\\\\"]{%d})(\\\\?\\\"|)"; | ||
//group $1 = field name | ||
//group $2 = ":" (with json serialization support) | ||
//group $3 = sensitive information -> unmasked | ||
//group $4 = ending qts | ||
private static final String REPLACEMENT_TEMPLATE = "\"$1$2xxxx-$3$4"; | ||
|
||
/** | ||
* 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 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), visibleCharacters); | ||
} | ||
|
||
/** | ||
* Build pattern to replace. | ||
* | ||
* @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 REPLACEMENT_TEMPLATE; | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...ava/com/ebay/ejmask/extenstion/builder/json/JsonValueUnmaskFromEndPatternBuilderTest.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,105 @@ | ||
package com.ebay.ejmask.extenstion.builder.json; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.regex.Pattern; | ||
import java.util.stream.Stream; | ||
|
||
public class JsonValueUnmaskFromEndPatternBuilderTest { | ||
|
||
public static final JsonValueUnmaskFromEndPatternBuilder instance = new JsonValueUnmaskFromEndPatternBuilder(); | ||
public static final String[] fieldNames = new String[]{"ccNumber", "ssn"}; | ||
|
||
@Test | ||
public void testBuildPattern() { | ||
int visibleCharacters = 4; | ||
String result = instance.buildPattern(visibleCharacters, fieldNames); | ||
Assertions.assertEquals("\\\"(ccNumber|ssn)(\\\\*\\\"\\s*:\\s*\\\\*\\\")[^\\\"]*([^\\\\\"]{4})(\\\\?\\\"|)", result); | ||
} | ||
|
||
@Test | ||
public void testBuildPattern_withNegativeVisibleCharacters() { | ||
int visibleCharacters = -1; | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> instance.buildPattern(visibleCharacters, fieldNames)); | ||
} | ||
|
||
@Test | ||
public void testBuildReplacement() { | ||
int visibleCharacters = 4; | ||
String result = instance.buildReplacement(visibleCharacters, fieldNames); | ||
Assertions.assertEquals("\"$1$2xxxx-$3$4", result); | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("dataForTestMatch") | ||
public void testMatch(String name, String data, String expected) { | ||
String regex = instance.buildPattern(4, fieldNames); | ||
String replacement = instance.buildReplacement(4, fieldNames); | ||
Pattern pattern = Pattern.compile(regex); | ||
String result = pattern.matcher(data).replaceAll(replacement); | ||
Assertions.assertFalse(result.contains("1234567890")); | ||
Assertions.assertEquals(expected, result); | ||
} | ||
|
||
static Stream<Arguments> dataForTestMatch() { | ||
return Stream.of( | ||
Arguments.arguments( | ||
"test with normal json", | ||
"{\"ccNumber\":\"1234567890123456\",\"ssn\":\"123456789\",\"nonSensitiveData\":\"data\"}", | ||
"{\"ccNumber\":\"xxxx-3456\",\"ssn\":\"xxxx-6789\",\"nonSensitiveData\":\"data\"}" | ||
), | ||
Arguments.arguments( | ||
"test with empty values", | ||
"{\"ccNumber\":\"\",\"ssn\":null,\"nonSensitiveData\":\"data\"}", | ||
"{\"ccNumber\":\"\",\"ssn\":null,\"nonSensitiveData\":\"data\"}" | ||
), | ||
Arguments.arguments( | ||
"test with space", | ||
"{\"ccNumber\" : \"1234567890123456\", \"ssn\" :\"123456789\",\"nonSensitiveData\":\"data\"}", | ||
"{\"ccNumber\" : \"xxxx-3456\", \"ssn\" :\"xxxx-6789\",\"nonSensitiveData\":\"data\"}" | ||
), | ||
Arguments.arguments( | ||
"test with line break", | ||
"{\n" | ||
+ " \"ccNumber\": \"1234567890123456\",\n" | ||
+ " \"ssn\": \"123456789\",\n" | ||
+ " \"nonSensitiveData\": \"data\"\n" | ||
+ "}", | ||
"{\n" | ||
+ " \"ccNumber\": \"xxxx-3456\",\n" | ||
+ " \"ssn\": \"xxxx-6789\",\n" | ||
+ " \"nonSensitiveData\": \"data\"\n" | ||
+ "}" | ||
), | ||
Arguments.arguments( | ||
"test with broken json", | ||
"{\"ccNumber\":\"1234567890123456\",\"ssn\":\"123456789", | ||
"{\"ccNumber\":\"xxxx-3456\",\"ssn\":\"xxxx-6789" | ||
), | ||
Arguments.arguments( | ||
"test with json encoded json", | ||
"{\\\"ccNumber\\\":\\\"1234567890123456\\\",\\\"ssn\\\":\\\"123456789\\\",\\\"nonSensitiveData\\\":\\\"data\\\"}", | ||
"{\\\"ccNumber\\\":\\\"xxxx-3456\\\",\\\"ssn\\\":\\\"xxxx-6789\\\",\\\"nonSensitiveData\\\":\\\"data\\\"}" | ||
), | ||
Arguments.arguments( | ||
"test with double json encoded json", | ||
"{\\\\\\\"ccNumber\\\\\\\":\\\\\\\"1234567890123456\\\\\\\",\\\\\\\"ssn\\\\\\\":\\\\\\\"123456789\\\\\\\",\\\\\\\"nonSensitiveData\\\\\\\":\\\\\\\"data\\\\\\\"}", | ||
"{\\\\\\\"ccNumber\\\\\\\":\\\\\\\"xxxx-3456\\\\\\\",\\\\\\\"ssn\\\\\\\":\\\\\\\"xxxx-6789\\\\\\\",\\\\\\\"nonSensitiveData\\\\\\\":\\\\\\\"data\\\\\\\"}" | ||
), | ||
Arguments.arguments( | ||
"test with encoded broken json", | ||
"{\\\"ccNumber\\\":\\\"1234567890123456\\\",\\\"ssn\\\":\\\"123456789", | ||
"{\\\"ccNumber\\\":\\\"xxxx-3456\\\",\\\"ssn\\\":\\\"xxxx-6789" | ||
), | ||
Arguments.arguments( | ||
"test with encoded broken json 2", | ||
"{\\\"ccNumber\\\":\\\"1234567890123456\\\",\\\"ssn\\\":\\\"123456789\\", | ||
"{\\\"ccNumber\\\":\\\"xxxx-3456\\\",\\\"ssn\\\":\\\"xxxx-6789\\" | ||
) | ||
); | ||
} | ||
} |
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