Skip to content

Commit

Permalink
fix: using masked properties for logging (#723)
Browse files Browse the repository at this point in the history
  • Loading branch information
brunos-bq authored Nov 6, 2023
1 parent e64bdb9 commit 80577ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public Connection connect(
final ConnectInfo connectInfo = this.targetDriverDialect.prepareConnectInfo(protocol, hostSpec, copy);

LOGGER.finest(() -> "Connecting to " + connectInfo.url
+ PropertyUtils.logProperties(connectInfo.props, "\nwith properties: \n"));
+ PropertyUtils.logProperties(PropertyUtils.maskProperties(connectInfo.props), "\nwith properties: \n"));

Connection conn = this.driver.connect(connectInfo.url, connectInfo.props);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public void prepareDataSource(
props.setProperty("url", finalUrl);

PropertyDefinition.removeAllExceptCredentials(props);

LOGGER.finest(() -> PropertyUtils.logProperties(props, "Connecting with properties: \n"));
LOGGER.finest(() -> PropertyUtils.logProperties(PropertyUtils.maskProperties(props),
"Connecting with properties: \n"));

if (!props.isEmpty()) {
PropertyUtils.applyProperties(dataSource, props);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Logger;
import org.checkerframework.checker.nullness.qual.NonNull;
import software.amazon.jdbc.PropertyDefinition;

public class PropertyUtils {
private static final Logger LOGGER = Logger.getLogger(PropertyUtils.class.getName());
private static final Set<Object> SECRET_PROPERTIES = Collections.unmodifiableSet(
new HashSet<>(Collections.singletonList(PropertyDefinition.PASSWORD.name))
);

public static void applyProperties(final Object target, final Properties properties) {
if (target == null || properties == null) {
Expand Down Expand Up @@ -96,7 +102,8 @@ public static void setPropertyOnTarget(
} else {
writeMethod.invoke(target, propValue);
}
LOGGER.finest(() -> String.format("Set property '%s' with value: %s", propName, propValue));
Object cleanPropValue = isSecretProperty(propName) ? "***" : propValue;
LOGGER.finest(() -> String.format("Set property '%s' with value: %s", propName, cleanPropValue));

} catch (final InvocationTargetException ex) {
LOGGER.warning(
Expand Down Expand Up @@ -127,6 +134,10 @@ public static void setPropertyOnTarget(
return copy;
}

private static boolean isSecretProperty(final Object propertyKey) {
return SECRET_PROPERTIES.contains(propertyKey);
}

public static @NonNull Properties maskProperties(final Properties props) {
final Properties maskedProperties = copyProperties(props);
if (maskedProperties.containsKey(PropertyDefinition.PASSWORD.name)) {
Expand Down

0 comments on commit 80577ed

Please sign in to comment.