Skip to content

Commit 80577ed

Browse files
authored
fix: using masked properties for logging (#723)
1 parent e64bdb9 commit 80577ed

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

wrapper/src/main/java/software/amazon/jdbc/DriverConnectionProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public Connection connect(
119119
final ConnectInfo connectInfo = this.targetDriverDialect.prepareConnectInfo(protocol, hostSpec, copy);
120120

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

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

wrapper/src/main/java/software/amazon/jdbc/targetdriverdialect/GenericTargetDriverDialect.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public void prepareDataSource(
7878
props.setProperty("url", finalUrl);
7979

8080
PropertyDefinition.removeAllExceptCredentials(props);
81-
82-
LOGGER.finest(() -> PropertyUtils.logProperties(props, "Connecting with properties: \n"));
81+
LOGGER.finest(() -> PropertyUtils.logProperties(PropertyUtils.maskProperties(props),
82+
"Connecting with properties: \n"));
8383

8484
if (!props.isEmpty()) {
8585
PropertyUtils.applyProperties(dataSource, props);

wrapper/src/main/java/software/amazon/jdbc/util/PropertyUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@
1919
import java.lang.reflect.InvocationTargetException;
2020
import java.lang.reflect.Method;
2121
import java.util.Arrays;
22+
import java.util.Collections;
2223
import java.util.Enumeration;
24+
import java.util.HashSet;
2325
import java.util.List;
2426
import java.util.Map;
2527
import java.util.Properties;
28+
import java.util.Set;
2629
import java.util.logging.Logger;
2730
import org.checkerframework.checker.nullness.qual.NonNull;
2831
import software.amazon.jdbc.PropertyDefinition;
2932

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

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

101108
} catch (final InvocationTargetException ex) {
102109
LOGGER.warning(
@@ -127,6 +134,10 @@ public static void setPropertyOnTarget(
127134
return copy;
128135
}
129136

137+
private static boolean isSecretProperty(final Object propertyKey) {
138+
return SECRET_PROPERTIES.contains(propertyKey);
139+
}
140+
130141
public static @NonNull Properties maskProperties(final Properties props) {
131142
final Properties maskedProperties = copyProperties(props);
132143
if (maskedProperties.containsKey(PropertyDefinition.PASSWORD.name)) {

0 commit comments

Comments
 (0)