Repository navigation
Conversation
…on logging Resolves b/584840083 (PR 2 of 3). Redacts sensitive properties (including BYOID_CredentialSource) via SAFE_TO_LOG_PROPERTIES in BigQueryJdbcUrlUtility.appendPropertiesToURL, isolates per-connection LogLevel and LogPath inside a single persistent PerConnectionFileHandler without stripping OpenTelemetryJulHandler or external JUL handlers on LogLevel=0, makes BigQueryJdbcMdc.registerInstance reentrant, and adds -Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath for multi-tenant deployments.
There was a problem hiding this comment.
Code Review
This pull request introduces multi-tenant logging isolation and secure log path configuration for the BigQuery JDBC driver, enabling connection-specific logging levels and paths while masking sensitive properties. Key feedback includes addressing potential issues where external log level configurations are ignored, avoiding disabling parent handler propagation, simplifying a redundant ternary operator in PerConnectionFileHandler, and wrapping system property retrieval in a try-catch block to prevent SecurityException in restricted environments.
| if (hasExternalHandlerAttached()) { | ||
| if (current != lastDriverAppliedLevel) { | ||
| externalFloorLevel = current; | ||
| } | ||
| } else { | ||
| for (Handler h : logger.getHandlers()) { | ||
| h.close(); | ||
| logger.removeHandler(h); | ||
| externalFloorLevel = null; | ||
| } |
There was a problem hiding this comment.
By restricting the detection of external log level changes to only when hasExternalHandlerAttached() is true, any external log level configuration set on the com.google.cloud.bigquery logger (e.g., via logging.properties or Logger.setLevel(...) without directly attaching a custom handler to this specific logger) will be ignored and overwritten by the driver's dynamic level adjustment. In standard JUL usage, handlers are typically attached to the root logger, not package-specific loggers. Consider removing the hasExternalHandlerAttached() guard or finding a way to preserve externally configured levels even when no direct handlers are attached.
| } | ||
| if (!attached) { | ||
| logger.addHandler(persistentFileHandler); | ||
| logger.setUseParentHandlers(false); |
There was a problem hiding this comment.
Setting logger.setUseParentHandlers(false) on the com.google.cloud.bigquery logger will prevent log messages from propagating to parent loggers. In global static utilities shared across multiple concurrent sessions, avoid state changes (such as closing or removing shared handlers) triggered by a single session's configuration if those changes would negatively affect other active sessions.
References
- In global static utilities shared across multiple concurrent sessions, avoid state changes (such as closing or removing shared handlers) triggered by a single session's configuration if those changes would negatively affect other active sessions.
| Exception ex = (e instanceof Exception) ? (Exception) e : new IOException(e); | ||
| reportError("Failed to initialize env default log file", ex, ErrorManager.OPEN_FAILURE); |
There was a problem hiding this comment.
Since e is caught as RuntimeException | IOException, both of which are subclasses of Exception, e is guaranteed to be an instance of Exception. The ternary operator (e instanceof Exception) ? (Exception) e : new IOException(e) is redundant and can be simplified by passing e directly to reportError.
| Exception ex = (e instanceof Exception) ? (Exception) e : new IOException(e); | |
| reportError("Failed to initialize env default log file", ex, ErrorManager.OPEN_FAILURE); | |
| reportError("Failed to initialize env default log file", e, ErrorManager.OPEN_FAILURE); |
| static boolean isUrlLogPathAllowed() { | ||
| String prop = System.getProperty(ALLOW_URL_LOG_PATH_SYSTEM_PROPERTY); | ||
| return prop == null || !"false".equalsIgnoreCase(prop.trim()); | ||
| } |
There was a problem hiding this comment.
Calling System.getProperty can throw a SecurityException if run in a restricted environment (such as an application server with a security manager enabled). Consider wrapping this call in a try-catch block to gracefully handle SecurityException and fall back to a default value (e.g., true).
static boolean isUrlLogPathAllowed() {
try {
String prop = System.getProperty(ALLOW_URL_LOG_PATH_SYSTEM_PROPERTY);
return prop == null || !"false".equalsIgnoreCase(prop.trim());
} catch (SecurityException e) {
return true;
}
}
What & Why
PR 2 of 3 in the diagnostic logging hardening stack (stacked on
fix/bq-jdbc-log-file-hardening).Previously, connecting via
BigQueryDriver.connectorDataSource.getConnectionhad two multi-tenant isolation and redaction issues:BigQueryJdbcUrlUtility.appendPropertiesToURLloggedAppending %s with value %s to URLatLevel.FINESTwithout masking sensitive properties (OAuthAccessToken,OAuthRefreshToken,OAuthPvtKey,OAuthClientSecret,ProxyPwd,gcpTelemetryCredentials, etc.) and before any connection ID was bound inBigQueryJdbcMdc. In addition,BYOID_CredentialSource(which can carry JSON HTTP authentication headers in Workload Identity Federation) was listed inBigQueryConnection.SAFE_TO_LOG_PROPERTIES.BigQueryDriver.connectcall mutated the JVM-widecom.google.cloud.bigquerylogger level and replaced the singletonPerConnectionFileHandler, redirecting concurrent connections' SQL and property logs into the latest caller'sLogPath— while any connection with defaultLogLevel=0closed and removed all JUL handlers oncom.google.cloud.bigquery(includingOpenTelemetryJulHandlerand activePerConnectionFileHandlerinstances) and set the global logger level toLevel.OFF.Changes in this PR
BYOID_CredentialSourcefromSAFE_TO_LOG_PROPERTIES, introducesBigQueryConnection.maskPropertyValue(key, value), and applies it insideBigQueryJdbcUrlUtility.appendPropertiesToURL(with null-safe / non-String-safePropertieshandling and lazySupplier<String>evaluation atFINEST).(LogLevel, LogPath)isolation & handler preservation:connectionIdand binds reentrantBigQueryJdbcMdcat the entry ofBigQueryDriver.connect, passingconnectionIdintoBigQueryConnectionand cleaning up registration on connection setup failure.PerConnectionFileHandlerstoring per-connectionId(Path logPath, Level level)slots (BQ-JDBC-GLOBAL.logis opened only when JVM-levelBIGQUERY_JDBC_LOG_PATHandBIGQUERY_JDBC_LOG_LEVELenvironment variables are configured).OpenTelemetryJulHandler,ConsoleHandler, or external JUL handlers when a connection specifiesLogLevel=0, and dynamically computes the root logger threshold as the minimum (most verbose) active level across open connections (plus externally attached JUL handlers).LogPathguardrail: Adds-Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath(defaulttrue; whenfalse, ignores URL/Properties-suppliedLogPathand skips the CWD default fallback so only admin-configuredBIGQUERY_JDBC_LOG_PATHwrites local files).How It Was Tested
JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make lint(fmt-maven-plugin:format).JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 make unittest(Checkstyle, Java 8animal-sniffer, and the full JUnit 5 unit test suite) and integration test compilation.BigQueryJdbcUrlUtilityTest,BigQueryConnectionTest,BigQueryDriverTest,BigQueryJdbcMdcTest, andPerConnectionFileHandlerTestcovering parameterized secret masking, concurrent multi-tenantLogPathandLogLevelisolation,OpenTelemetryJulHandlerpreservation, external floor restoration, and-Dcom.google.cloud.bigquery.jdbc.allowUrlLogPath=falseenforcement.IT*) were not executed against production BigQuery in this step.