Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsonValueUnmaskFromEndPatternBuilder #18

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ Alternatively you can pull it from the central Maven repositories:
<dependency>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-bom</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion ejmask-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ejmask-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion ejmask-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ejmask-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-extensions</artifactId>
Expand Down
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 {
prasanthkv marked this conversation as resolved.
Show resolved Hide resolved

//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;
}
}
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\\"
)
);
}
}
2 changes: 1 addition & 1 deletion ejmask-spring/ejmask-spring-autoconfig/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-spring</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-spring-autoconfig</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ejmask-spring/ejmask-spring-boot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-spring</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-spring-boot</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ejmask-spring/ejmask-spring-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-spring</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-spring-core</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion ejmask-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>
</parent>

<artifactId>ejmask-spring</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.ebay.ejmask</groupId>
<artifactId>ejmask-parent</artifactId>
<version>1.0.3</version>
<version>1.1.0-SNAPSHOT</version>

<packaging>pom</packaging>

Expand Down