Skip to content

Commit 6140547

Browse files
Add new mask util method (by set of keys) (#939)
1 parent cec86ea commit 6140547

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

services-api/src/main/java/io/scalecube/services/MaskUtil.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package io.scalecube.services;
22

3+
import java.util.LinkedHashMap;
34
import java.util.Map;
45
import java.util.Map.Entry;
6+
import java.util.Set;
57
import java.util.UUID;
68
import java.util.stream.Collectors;
79

@@ -48,4 +50,33 @@ public static String mask(Map<String, String> map) {
4850
.collect(Collectors.toMap(Entry::getKey, entry -> mask(entry.getValue())))
4951
.toString();
5052
}
53+
54+
/**
55+
* Mask sensitive data by replacing part of string with an asterisk symbol.
56+
*
57+
* @param map map sensitive data to be masked
58+
* @param sensitiveKeys keys whose corresponding values should be masked
59+
* @return string representation
60+
*/
61+
public static String mask(Map<?, ?> map, Set<String> sensitiveKeys) {
62+
if (map == null || map.isEmpty()) {
63+
return String.valueOf(map);
64+
}
65+
66+
return map.entrySet().stream()
67+
.collect(
68+
Collectors.toMap(
69+
entry -> String.valueOf(entry.getKey()),
70+
entry -> {
71+
final var key = String.valueOf(entry.getKey());
72+
final var value = String.valueOf(entry.getValue());
73+
if (entry.getKey() == null || entry.getValue() == null) {
74+
return value;
75+
}
76+
return sensitiveKeys.contains(key) ? MaskUtil.mask(value) : value;
77+
},
78+
(v1, v2) -> v1,
79+
LinkedHashMap::new))
80+
.toString();
81+
}
5182
}

0 commit comments

Comments
 (0)