Skip to content

Commit ef44598

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

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

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

Lines changed: 30 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,32 @@ 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+
* @return string representation
59+
*/
60+
private static String mask(Map<?, ?> map, Set<String> sensitiveKeys) {
61+
if (map == null || map.isEmpty()) {
62+
return String.valueOf(map);
63+
}
64+
65+
return map.entrySet().stream()
66+
.collect(
67+
Collectors.toMap(
68+
entry -> String.valueOf(entry.getKey()),
69+
entry -> {
70+
final var key = String.valueOf(entry.getKey());
71+
final var value = String.valueOf(entry.getValue());
72+
if (entry.getKey() == null || entry.getValue() == null) {
73+
return value;
74+
}
75+
return sensitiveKeys.contains(key) ? MaskUtil.mask(value) : value;
76+
},
77+
(v1, v2) -> v1,
78+
LinkedHashMap::new))
79+
.toString();
80+
}
5181
}

0 commit comments

Comments
 (0)