Skip to content

Commit 06b9444

Browse files
committed
Fix issue #71: redact sensitive connection properties in CassandraConnection.toString()
1 parent e84c147 commit 06b9444

File tree

5 files changed

+38
-2
lines changed

5 files changed

+38
-2
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to
55
[Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [4.13.1] - 2024-09-04
8+
### Fixed
9+
- Fix implementation of `CassandraConnection.toString()` to not leak connection password (see issue
10+
[#71](https://github.com/ing-bank/cassandra-jdbc-wrapper/issues/71)).
11+
712
## [4.13.0] - 2024-07-27
813
### Added
914
- Add support for switching execution profiles.
@@ -294,6 +299,7 @@ For this version, the changelog lists the main changes comparatively to the late
294299
- Fix logs in `CassandraConnection` constructor.
295300

296301
[original project]: https://github.com/adejanovski/cassandra-jdbc-wrapper/
302+
[4.13.1]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.13.0...v4.13.1
297303
[4.13.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.12.0...v4.13.0
298304
[4.12.0]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.11.1...v4.12.0
299305
[4.11.1]: https://github.com/ing-bank/cassandra-jdbc-wrapper/compare/v4.11.0...v4.11.1

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<groupId>com.ing.data</groupId>
77
<artifactId>cassandra-jdbc-wrapper</artifactId>
8-
<version>4.13.0</version>
8+
<version>4.13.1</version>
99
<packaging>jar</packaging>
1010

1111
<name>Cassandra JDBC Wrapper</name>

src/main/java/com/ing/data/cassandra/jdbc/CassandraConnection.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static com.ing.data.cassandra.jdbc.CassandraResultSet.DEFAULT_CONCURRENCY;
6868
import static com.ing.data.cassandra.jdbc.CassandraResultSet.DEFAULT_HOLDABILITY;
6969
import static com.ing.data.cassandra.jdbc.CassandraResultSet.DEFAULT_TYPE;
70+
import static com.ing.data.cassandra.jdbc.utils.DriverUtil.toStringWithoutSensitiveValues;
7071
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ALWAYS_AUTOCOMMIT;
7172
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_TIMEOUT;
7273
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.INVALID_FETCH_SIZE_PARAMETER;
@@ -603,7 +604,8 @@ protected boolean removeStatement(final Statement statement) {
603604

604605
@Override
605606
public String toString() {
606-
return "CassandraConnection [connectionProperties=" + this.connectionProperties + "]";
607+
return String.format("CassandraConnection [connectionProperties=%s]",
608+
toStringWithoutSensitiveValues(this.connectionProperties));
607609
}
608610

609611
/**

src/main/java/com/ing/data/cassandra/jdbc/utils/DriverUtil.java

+17
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Properties;
3131
import java.util.stream.Collectors;
3232

33+
import static com.ing.data.cassandra.jdbc.utils.JdbcUrlUtil.TAG_PASSWORD;
34+
3335
/**
3436
* A set of static utility methods and constants used by the JDBC driver.
3537
*/
@@ -186,4 +188,19 @@ public static DriverPropertyInfo buildPropertyInfo(final String propertyName, fi
186188
return propertyInfo;
187189
}
188190

191+
/**
192+
* Returns a string representation of the provided driver properties with redacted values for sensitive properties
193+
* such as passwords.
194+
*
195+
* @param properties The driver properties.
196+
* @return The string representation of the properties, without sensitive values.
197+
*/
198+
public static String toStringWithoutSensitiveValues(final Properties properties) {
199+
final Properties withRedactedSensitiveValues = (Properties) properties.clone();
200+
if (withRedactedSensitiveValues.containsKey(TAG_PASSWORD)) {
201+
withRedactedSensitiveValues.setProperty(TAG_PASSWORD, "***");
202+
}
203+
return withRedactedSensitiveValues.toString();
204+
}
205+
189206
}

src/test/java/com/ing/data/cassandra/jdbc/ConnectionUnitTest.java

+11
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.SSL_CONFIG_FAILED;
6868
import static org.hamcrest.MatcherAssert.assertThat;
6969
import static org.hamcrest.Matchers.instanceOf;
70+
import static org.hamcrest.Matchers.matchesPattern;
7071
import static org.junit.jupiter.api.Assertions.assertEquals;
7172
import static org.junit.jupiter.api.Assertions.assertFalse;
7273
import static org.junit.jupiter.api.Assertions.assertNotEquals;
@@ -627,4 +628,14 @@ void givenCassandraConnectionWithCustomExecProfile_whenExecuteStatement_useExpec
627628
assertEquals(customProfileName, executedStmt.getExecutionProfile().getName());
628629
}
629630

631+
@Test
632+
void givenCassandraConnection_whenToString_returnExpectedString() throws Exception {
633+
initConnection(KEYSPACE, "password=cassandra", "localdatacenter=datacenter1");
634+
assertNotNull(sqlConnection);
635+
assertThat(sqlConnection.toString(),
636+
matchesPattern("CassandraConnection \\[connectionProperties=\\{password=\\*\\*\\*, "
637+
+ "localDatacenter=datacenter1, databaseName=system, contactPoints=\\[localhost:\\d+]}]")
638+
);
639+
}
640+
630641
}

0 commit comments

Comments
 (0)