-
Notifications
You must be signed in to change notification settings - Fork 1.1k
chore(bqjdbc): log connection properties set by the user #13244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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()) { | ||
| 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 on lines
+161
to
+166
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better readability and maintainability, consider extracting the sensitive keywords into a For example, you could define a static set of keywords at the class level: private static final java.util.Set<String> SENSITIVE_KEYWORDS = new java.util.HashSet<>(java.util.Arrays.asList("key", "token", "password", "pwd", "secret"));And then use it in the condition like this: if (SENSITIVE_KEYWORDS.stream().anyMatch(lowerName::contains)
&& !lowerName.equals("partnertoken")) { |
||
| value = "*****"; | ||
| } | ||
| maskedProps.setProperty(name, value); | ||
| } | ||
| LOG.config("Connection properties: %s", maskedProps.toString()); | ||
| } | ||
| this.openStatements = ConcurrentHashMap.newKeySet(); | ||
| this.autoCommit = true; | ||
| this.sqlWarnings = new ArrayList<>(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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(); | ||||||
| java.util.logging.Level originalLevel = rootLogger.getLevel(); | ||||||
| rootLogger.setLevel(java.util.logging.Level.INFO); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code in
Suggested change
|
||||||
|
|
||||||
| 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;"; | ||||||
| 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)")); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assertion appears to be incorrect. Given the URL
Suggested change
|
||||||
| assertFalse(logMessage.contains("secretAccessToken")); | ||||||
| } | ||||||
| } | ||||||
| assertTrue(foundLog, "Log message about Connection properties was not found"); | ||||||
| } finally { | ||||||
| rootLogger.removeHandler(handler); | ||||||
| rootLogger.setLevel(originalLevel); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest using reverse approach. Instead of "opt-out" have an "opt-in".
We can create a list of properties that are "safe" to log.