Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
this.connectionId = UUID.randomUUID().toString();
try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(this.connectionId)) {
this.connectionUrl = url;
if (LOG.isLoggable(java.util.logging.Level.CONFIG)) {
Properties connectionProps = ds.createProperties();
Properties maskedProps = new Properties();
for (String name : connectionProps.stringPropertyNames()) {
Comment thread
Neenu1995 marked this conversation as resolved.
String value = connectionProps.getProperty(name);
String lowerName = name.toLowerCase();
if ((lowerName.contains("key")
|| lowerName.contains("token")
|| lowerName.contains("password")
|| lowerName.contains("pwd")
|| lowerName.contains("secret"))
&& !lowerName.equals("partnertoken")) {
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
value = "*****";
}
maskedProps.setProperty(name, value);
}
LOG.config("Connection properties: %s", maskedProps.toString());
}
this.openStatements = ConcurrentHashMap.newKeySet();
this.autoCommit = true;
this.sqlWarnings = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ public Connection getConnection() throws SQLException {
return DriverManager.getConnection(getURL(), createProperties());
}

private Properties createProperties() {
Properties createProperties() {
Properties connectionProperties = new Properties();
if (this.projectId != null) {
connectionProperties.setProperty(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,4 +456,55 @@ public void testIsReadOnlyTokenProvided(String readonlyProp, boolean expectedIsR
assertEquals(expectedIsReadOnly, connection.isReadOnlyTokenUsed());
}
}

@Test
public void testConnectionPropertiesLoggingAndMasking() throws IOException, SQLException {
java.util.logging.Logger rootLogger = BigQueryJdbcRootLogger.getRootLogger();
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
java.util.logging.Level originalLevel = rootLogger.getLevel();
rootLogger.setLevel(java.util.logging.Level.INFO);
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated

java.util.List<java.util.logging.LogRecord> records = new java.util.ArrayList<>();
java.util.logging.Handler handler =
new java.util.logging.Handler() {
@Override
public void publish(java.util.logging.LogRecord record) {
records.add(record);
}

@Override
public void flush() {}

@Override
public void close() throws SecurityException {}
};
rootLogger.addHandler(handler);

try {
String url =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyTestProjectId;"
+ "OAuthAccessToken=secretAccessToken;Location=US;"
+ "PartnerToken=GPN:secretPartnerToken;";
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
try (BigQueryConnection connection = new BigQueryConnection(url)) {
// Just trigger the constructor
}

boolean foundLog = false;
for (java.util.logging.LogRecord record : records) {
if (record.getMessage().contains("Connection properties:")) {
foundLog = true;
String logMessage = record.getMessage();
assertTrue(logMessage.contains("ProjectId=MyTestProjectId"));
assertTrue(logMessage.contains("Location=US"));
assertTrue(logMessage.contains("OAuthAccessToken=*****"));
assertTrue(logMessage.contains("PartnerToken= (GPN:secretPartnerToken)"));
Comment thread
Neenu1995 marked this conversation as resolved.
Outdated
assertFalse(logMessage.contains("secretAccessToken"));
}
}
assertTrue(foundLog, "Log message about Connection properties was not found");
} finally {
rootLogger.removeHandler(handler);
rootLogger.setLevel(originalLevel);
}
}
}
Loading