DBZ-1748 Mask JDBC passwords from log messages

This commit is contained in:
Jiri Pechanec 2020-01-31 10:31:46 +01:00 committed by Gunnar Morling
parent 1508252b29
commit 6f7b8d1707

View File

@ -121,7 +121,9 @@ public static interface Operations {
*/
public static ConnectionFactory patternBasedFactory(String urlPattern, Field... variables) {
return (config) -> {
LOGGER.trace("Config: {}", config.asProperties());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Config: {}", propsWithMaskedPassword(config.asProperties()));
}
Properties props = config.asProperties();
Field[] varsWithDefaults = combineVariables(variables,
JdbcConfiguration.HOSTNAME,
@ -130,10 +132,14 @@ public static ConnectionFactory patternBasedFactory(String urlPattern, Field...
JdbcConfiguration.PASSWORD,
JdbcConfiguration.DATABASE);
String url = findAndReplace(urlPattern, props, varsWithDefaults);
LOGGER.trace("Props: {}", props);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Props: {}", propsWithMaskedPassword(props));
}
LOGGER.trace("URL: {}", url);
Connection conn = DriverManager.getConnection(url, props);
LOGGER.debug("Connected to {} with {}", url, props);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Connected to {} with {}", url, propsWithMaskedPassword(props));
}
return conn;
};
}
@ -159,7 +165,9 @@ public static ConnectionFactory patternBasedFactory(String urlPattern, Field...
public static ConnectionFactory patternBasedFactory(String urlPattern, String driverClassName,
ClassLoader classloader, Field... variables) {
return (config) -> {
LOGGER.trace("Config: {}", config.asProperties());
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Config: {}", propsWithMaskedPassword(config.asProperties()));
}
Properties props = config.asProperties();
Field[] varsWithDefaults = combineVariables(variables,
JdbcConfiguration.HOSTNAME,
@ -168,7 +176,9 @@ public static ConnectionFactory patternBasedFactory(String urlPattern, String dr
JdbcConfiguration.PASSWORD,
JdbcConfiguration.DATABASE);
String url = findAndReplace(urlPattern, props, varsWithDefaults);
LOGGER.trace("Props: {}", props);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Props: {}", propsWithMaskedPassword(props));
}
LOGGER.trace("URL: {}", url);
Connection conn = null;
try {
@ -183,11 +193,22 @@ public static ConnectionFactory patternBasedFactory(String urlPattern, String dr
catch (ClassNotFoundException | IllegalAccessException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
throw new SQLException(e);
}
LOGGER.debug("Connected to {} with {}", url, props);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Connected to {} with {}", url, propsWithMaskedPassword(props));
}
return conn;
};
}
private static Properties propsWithMaskedPassword(Properties props) {
final Properties filtered = new Properties();
filtered.putAll(props);
if (props.containsKey(JdbcConfiguration.PASSWORD.name())) {
filtered.put(JdbcConfiguration.PASSWORD.name(), "***");
}
return filtered;
}
private static Field[] combineVariables(Field[] overriddenVariables,
Field... defaultVariables) {
Map<String, Field> fields = new HashMap<>();