Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,8 @@ static String maskPropertyValue(String key, Object value) {
this.reqGoogleDriveScope,
httpTransportFactory,
this.connectionClassName);
BigQueryJdbcDiagnosticTelemetry.initializeOrDisable(ds, this.credentials);
BigQueryJdbcDiagnosticTelemetry.initializeOrDisable(
ds, this.proxyProperties, this.credentials);
this.defaultDataset = BigQueryJdbcUrlUtility.parseDefaultDataset(ds.getDefaultDataset());
this.location = ds.getLocation();
this.enableHighThroughputAPI = ds.getEnableHighThroughputAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,46 @@

package com.google.cloud.bigquery.jdbc;

import com.google.api.client.http.HttpTransport;
import com.google.auth.Credentials;
import com.google.auth.oauth2.ComputeEngineCredentials;
import com.google.auth.oauth2.ImpersonatedCredentials;
import com.google.cloud.bigquery.jdbc.telemetry.v1.AuthenticationType;
import com.google.cloud.bigquery.jdbc.telemetry.v1.Status;
import com.google.cloud.bigquery.jdbc.telemetry.v1.TelemetryManager;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;

/**
* Coordinates Clearcut diagnostic telemetry eligibility and lifecycle transitions for BigQuery JDBC
* connections.
* Coordinates Clearcut diagnostic telemetry eligibility, transport-profile isolation, and lifecycle
* transitions for BigQuery JDBC connections.
*/
final class BigQueryJdbcDiagnosticTelemetry {

private static final String[] RAW_TRANSPORT_PROPERTY_NAMES = {
BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME,
BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME,
BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME,
BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME,
BigQueryJdbcUrlUtility.SSL_TRUST_STORE_PROPERTY_NAME
};

private BigQueryJdbcDiagnosticTelemetry() {}

/**
* Initializes Clearcut diagnostic telemetry for an eligible connection, or permanently disables
* it if the connection opts out or targets a non-default universe or endpoint override.
* Initializes Clearcut diagnostic telemetry with the connection's proxy and SSL TrustStore
* settings, or permanently disables it if the connection opts out, targets a non-default universe
* or endpoint override, or conflicts with an established transport profile.
*/
static void initializeOrDisable(DataSource ds, Credentials credentials) {
static void initializeOrDisable(
DataSource ds, Map<String, String> proxyProperties, Credentials credentials) {
if (TelemetryManager.isGloballyDisabled()) {
return;
}
Expand All @@ -46,13 +65,24 @@ static void initializeOrDisable(DataSource ds, Credentials credentials) {
TelemetryManager.disableGlobally("non-default universe or endpoint override");
return;
}
TelemetryManager.initializeForConnection(ds.getEnableDiagnosticTelemetry());
String profileKey =
computeTransportProfileKey(
proxyProperties,
ds.getSSLTrustStorePath(),
ds.getSSLTrustStorePassword(),
ds.getSSLTrustStoreType(),
ds.getSSLTrustStoreProvider());
TelemetryManager.initializeForConnection(
ds.getEnableDiagnosticTelemetry(),
profileKey,
createTransportSupplier(profileKey, proxyProperties, ds));
});
}

/**
* Enforces suppression and opt-out rules on failed {@link BigQueryDriver#connect} calls and
* records a failed connection attempt when diagnostic telemetry is eligible.
* records a failed connection attempt when diagnostic telemetry is eligible and transport
* configuration is valid.
*/
static void recordFailedConnection(
String connectionUri, DataSource ds, AuthenticationType authType, Throwable failure) {
Expand All @@ -63,24 +93,43 @@ static void recordFailedConnection(
() -> {
TelemetryManager manager =
ds != null
? initializeOrDisableForFailedDataSource(ds)
: initializeOrDisableFromRawUri(connectionUri);
? getOrDisableForFailedDataSource(ds)
: getOrDisableFromRawUri(connectionUri);
if (manager != null) {
TelemetryManager.recordConnectionAttempt(
Status.STATUS_ERROR, TelemetryManager.extractErrorCode(failure), authType);
}
});
}

private static TelemetryManager initializeOrDisableForFailedDataSource(DataSource ds) {
private static TelemetryManager getOrDisableForFailedDataSource(DataSource ds) {
if (isSuppressed(ds, null)) {
TelemetryManager.disableGlobally("non-default universe or endpoint override");
return null;
}
return TelemetryManager.initializeForConnection(ds.getEnableDiagnosticTelemetry());
Boolean explicitEnabled = ds.getEnableDiagnosticTelemetry();
if (!explicitEnabled) {
TelemetryManager.disableGlobally("opt-out configuration");
return null;
}
Optional<Map<String, String>> parsedProxy =
BigQueryJdbcProxyUtility.tryParseProxyProperties(ds);
if (!parsedProxy.isPresent()) {
return null;
}
Map<String, String> proxyProperties = parsedProxy.get();
String profileKey =
computeTransportProfileKey(
proxyProperties,
ds.getSSLTrustStorePath(),
ds.getSSLTrustStorePassword(),
ds.getSSLTrustStoreType(),
ds.getSSLTrustStoreProvider());
return TelemetryManager.getForFailedConnection(
explicitEnabled, profileKey, createTransportSupplier(profileKey, proxyProperties, ds));
}

private static TelemetryManager initializeOrDisableFromRawUri(String connectionUri) {
private static TelemetryManager getOrDisableFromRawUri(String connectionUri) {
if (isSuppressedFromRawUri(connectionUri)) {
TelemetryManager.disableGlobally("non-default universe or endpoint override");
return null;
Expand All @@ -101,8 +150,44 @@ private static TelemetryManager initializeOrDisableFromRawUri(String connectionU
TelemetryManager.disableGlobally("invalid EnableDiagnosticTelemetry value");
return null;
}
if (!explicitEnabled) {
TelemetryManager.disableGlobally("opt-out configuration");
return null;
}
}
if (hasRawProxyOrTrustStore(connectionUri)) {
return null;
}
return TelemetryManager.getForFailedConnection(explicitEnabled, null, null);
}

private static Supplier<HttpTransport> createTransportSupplier(
String profileKey, Map<String, String> proxyProperties, DataSource ds) {
if (profileKey == null) {
return null;
}
return () ->
BigQueryJdbcProxyUtility.createCustomHttpTransport(
proxyProperties,
ds.getSSLTrustStorePath(),
ds.getSSLTrustStorePassword(),
ds.getSSLTrustStoreType(),
ds.getSSLTrustStoreProvider(),
BigQueryJdbcDiagnosticTelemetry.class.getName());
}

private static boolean hasRawProxyOrTrustStore(String connectionUri) {
try {
for (String property : RAW_TRANSPORT_PROPERTY_NAMES) {
if (BigQueryJdbcUrlUtility.parseLastUriPropertyWithoutValidation(connectionUri, property)
!= null) {
return true;
}
}
return false;
} catch (RuntimeException e) {
return true;
}
return TelemetryManager.initializeForConnection(explicitEnabled);
}

private static Boolean parseStrictBoolean(String rawValue) {
Expand Down Expand Up @@ -184,4 +269,68 @@ private static boolean isNonDefaultUniverse(String universeDomain) {
private static boolean isNonBlank(String value) {
return value != null && !value.trim().isEmpty();
}

private static String normalizeBlankToNull(String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}

static String computeTransportProfileKey(
Map<String, String> proxyProperties,
String sslTrustStorePath,
String sslTrustStorePassword,
String sslTrustStoreType,
String sslTrustStoreProvider) {
boolean hasProxy = proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
boolean hasSsl = sslTrustStorePath != null;
if (!hasProxy && !hasSsl) {
return null;
}
String normalizedHost =
hasProxy
? proxyProperties
.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME)
.trim()
.toLowerCase(Locale.ROOT)
: null;
String normalizedPort =
hasProxy
? proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME).trim()
: null;
Comment thread
keshavdandeva marked this conversation as resolved.
Resolved
String normalizedTrustStoreType = null;
String normalizedTrustStoreProvider = null;
if (hasSsl) {
String explicitType = normalizeBlankToNull(sslTrustStoreType);
normalizedTrustStoreType = explicitType != null ? explicitType : KeyStore.getDefaultType();
normalizedTrustStoreProvider = normalizeBlankToNull(sslTrustStoreProvider);
}

Hasher hasher = Hashing.sha256().newHasher();
putLengthPrefixed(hasher, normalizedHost);
putLengthPrefixed(hasher, normalizedPort);
putLengthPrefixed(
hasher,
hasProxy ? proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME) : null);
putLengthPrefixed(
hasher,
hasProxy ? proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME) : null);
putLengthPrefixed(hasher, sslTrustStorePath);
putLengthPrefixed(hasher, hasSsl ? sslTrustStorePassword : null);
putLengthPrefixed(hasher, normalizedTrustStoreType);
putLengthPrefixed(hasher, normalizedTrustStoreProvider);
return hasher.hash().toString();
}

private static void putLengthPrefixed(Hasher hasher, String value) {
if (value == null) {
hasher.putInt(-1);
return;
}
byte[] valueBytes = value.getBytes(StandardCharsets.UTF_8);
hasher.putInt(valueBytes.length);
hasher.putBytes(valueBytes);
}
}
Loading
Loading