Skip to content

Commit

Permalink
IMPL: Added new Pattern Builder and Unit Test (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
parasdharwal33 authored Oct 3, 2024
1 parent 1cbc0c8 commit afcd8bc
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,14 @@ 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>
```

### Using in your Gradle Project.

```groovy
compile group: 'com.ebay.ejmask', name: 'ejmask-bom', version: '1.0.3'
compile group: 'com.ebay.ejmask', name: 'ejmask-bom', version: '1.1.0-SNAPSHOT'
```

## Roadmap
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 {

//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

0 comments on commit afcd8bc

Please sign in to comment.