Class ValueMaskers
ValueMasker
.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ValueMasker>
TProvides information about theValueMasker
implementation.static ValueMasker.StringMasker
eachCharacterWith
(String value) Masks all characters of a target string value with a static string value.static ValueMasker.NumberMasker
eachDigitWith
(int digit) Masks all digits of a target numeric value with a static digit.static ValueMasker.StringMasker
Masks a target string value (containing an email) while keeping some number of the prefix or suffix characters and the ability to keep the domain unmasked.static ValueMasker.AnyValueMasker
noop()
Does not mask a target value (no-operation).static ValueMasker.AnyValueMasker
with
(boolean value) Masks a target value with a static boolean value.static ValueMasker.AnyValueMasker
with
(int value) Masks a target value with a static integer value.static ValueMasker.AnyValueMasker
Masks a target value with a static string value.static ValueMasker.AnyValueMasker
withNull()
Masks a target value withnull
.static ValueMasker.AnyValueMasker
withRawValueFunction
(Function<String, String> masker) Masks a target value with the providedFunction
.
-
Method Details
-
describe
Provides information about theValueMasker
implementation. Which is useful for debugging and testing purposes.- See Also:
-
DescriptiveValueMasker
-
with
Masks a target value with a static string value.For example, "maskMe": "secret" -> "maskMe": "***".
-
with
Masks a target value with a static integer value.For example, "maskMe": 12345 -> "maskMe": 0.
-
with
Masks a target value with a static boolean value.For example, "maskMe": true -> "maskMe": false.
-
withNull
Masks a target value withnull
. -
eachCharacterWith
Masks all characters of a target string value with a static string value.For example, "maskMe": "secret" -> "maskMe": "******".
Note: this implementation only replaces visible characters with a mask, meaning that JSON escape character ('\') will not count towards the length of the masked value and the unicode characters ('
\
u1000'), including 4-byte UTF-8 characters ('\
uD83D\
uDCA9'), will only count as a single character in the masked value. -
eachDigitWith
Masks all digits of a target numeric value with a static digit.For example, "maskMe": 12345 -> "maskMe": 88888.
-
noop
Does not mask a target value (no-operation). Can be used if certain JSON value types do not need to be masked, for example, not masking booleans or numbers. -
email
public static ValueMasker.StringMasker email(int keepPrefixLength, int keepSuffixLength, boolean keepDomain, String mask) Masks a target string value (containing an email) while keeping some number of the prefix or suffix characters and the ability to keep the domain unmasked.For example:
- "maskMe": "[email protected]" -> "maskMe": "***@gmail.com"
- "maskMe": "[email protected]" -> "maskMe": "ag***"
- "maskMe": "[email protected]" -> "maskMe": "ag***@gmail.com"
- "maskMe": "[email protected]" -> "maskMe": "ag***[email protected]"
- Parameters:
keepPrefixLength
- amount of prefix characters to keep unmaskedkeepDomain
- if true - the email domain will remain unmaskedmask
- the static mask applied to the rest of the value
-
withRawValueFunction
Masks a target value with the providedFunction
. The target value (as raw JSON literal) is passed into the function as a string regardless of the JSON type (string, numeric or a boolean). In case the target value is a JSON string the value the function will receive a JSON encoded value with the quotes as it appears in the JSON with line breaks encoded as \n, special characters like " or \ escaped with a backslash (\).Consequently, the return value of the provided function must be a valid JSON encoded literal (of any JSON type), otherwise the masking will result in an invalid JSON.
The table below contains a couple examples for the masking
Examples of using withRawValueFunction Input JSON Function Masked JSON { "maskMe": "a secret" }
value -> value.replaceAll("secret", "***")
{ "maskMe": "a ***" }
{ "maskMe": 12345 }
value -> value.startsWith(123) ? "0" : value
{ "maskMe": 0 }
{ "maskMe": "secret" }
value -> "***"
{ "maskMe": *** }
(invalid JSON){ "maskMe": "secret" }
value -> "\"***\""
{ "maskMe": "***" }
(valid JSON){ "maskMe": "secret value" }
value -> value.substring(0, 3) + "***"
{ "maskMe": "se*** }
(invalid JSON{ "maskMe": "secret value" }
value -> value.startsWith("\"") ? value.substring(0, 4) + "***\"" : value
{ "maskMe": "sec***" }
(valid JSON){ "maskMe": "Andrii \"Juice\" Pilshchykov" }
value -> value.replaceAll("\"", "(quote)")
{ "maskMe": "Andrii \(quote)Juice\(quote) Pilshchykov" }
(invalid JSON){ "maskMe": "Andrii \"Juice\" Pilshchykov" }
value -> value.replaceAll("\\\"", "(quote)")
{ "maskMe": "Andrii (quote)Juice(quote) Pilshchykov" }
(valid JSON)Note: usually the
ValueMasker
operates on a byte level without parsing JSON values into intermediate objects. This implementation, however, needs to allocate aString
before passing it into the function and then turn it back into a byte array for the replacement, which introduces some performance overhead.
-