nl.hsac.fitnesse.fixture.util.SecretMasker Maven / Gradle / Ivy
package nl.hsac.fitnesse.fixture.util;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Helper to allow some map's values to be hidden in test reports.
*/
public class SecretMasker {
public Map replaceSecrets(Collection keysToMask, Map originalMap) {
boolean replaced = false;
Map newMap = null;
try {
newMap = new LinkedHashMap<>(originalMap);
replaced = replaceDirectSecrets(keysToMask, newMap);
if (replaceNestedSecrets(keysToMask, newMap)) {
replaced = true;
}
} catch (RuntimeException e) {
System.err.println("Unable to remove secrets from: " + originalMap);
}
return replaced ? newMap : (Map) originalMap;
}
protected boolean replaceDirectSecrets(Iterable keysToMask, Map newMap) {
AtomicBoolean replaced = new AtomicBoolean(false);
keysToMask.forEach(s -> {
if (newMap.containsKey(s)) {
newMap.replace(s, "*****");
replaced.set(true);
}
});
return replaced.get();
}
protected boolean replaceNestedSecrets(Collection keysToMask, Map newMap) {
AtomicBoolean replaced = new AtomicBoolean(false);
newMap.entrySet().forEach(e -> {
Object currentValue = e.getValue();
if (currentValue instanceof Map) {
Map safeValue = replaceSecrets(keysToMask, (Map) currentValue);
if (currentValue != safeValue) {
newMap.replace(e.getKey(), safeValue);
replaced.set(true);
}
}});
return replaced.get();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy