-
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.
Enable Non String values in JsonFieldPatternBuilder
- Loading branch information
1 parent
afcd8bc
commit 17c55fe
Showing
9 changed files
with
805 additions
and
18 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
53 changes: 53 additions & 0 deletions
53
ejmask-api/src/main/java/com/ebay/ejmask/api/PatternEntity.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,53 @@ | ||
package com.ebay.ejmask.api; | ||
|
||
import javax.annotation.Nonnull; | ||
|
||
/** | ||
* Copyright (c) 2023 eBay Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
public class PatternEntity { | ||
|
||
private final String patternTemplate; | ||
|
||
private final String replacementTemplate; | ||
|
||
/** | ||
* Constructor | ||
* @param patternTemplate as instance of String | ||
* @param replacementTemplate as instance of String | ||
*/ | ||
public PatternEntity(@Nonnull String patternTemplate, @Nonnull String replacementTemplate) { | ||
this.patternTemplate = patternTemplate; | ||
this.replacementTemplate = replacementTemplate; | ||
} | ||
|
||
/** | ||
* Get pattern template | ||
* | ||
* @return match pattern | ||
*/ | ||
public String getPatternTemplate() { | ||
return patternTemplate; | ||
} | ||
|
||
/** | ||
* Get replacement template | ||
* | ||
* @return replacement pattern | ||
*/ | ||
public String getReplacementTemplate() { | ||
return replacementTemplate; | ||
} | ||
} |
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
115 changes: 115 additions & 0 deletions
115
...src/main/java/com/ebay/ejmask/extenstion/builder/json/JsonBooleanFieldPatternBuilder.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,115 @@ | ||
package com.ebay.ejmask.extenstion.builder.json; | ||
/** | ||
* Copyright (c) 2024 eBay Inc. | ||
* <p> | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* 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. | ||
* | ||
* @author fsun1 | ||
*/ | ||
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 | ||
* | ||
* @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) { | ||
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); | ||
} | ||
|
||
|
||
/** | ||
* 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 this.buildReplacement(null, visibleCharacters, 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; | ||
} | ||
|
||
} |
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
Oops, something went wrong.