Repository navigation
test(bigquery-jdbc): align ITOpenTelemetryTest with per-connection proxy, TrustStore, and GAPIC Logging #15348
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
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 |
|---|---|---|
|
|
@@ -22,46 +22,68 @@ | |
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import com.google.api.client.http.HttpTransport; | ||
| import com.google.api.client.http.apache.v5.Apache5HttpTransport; | ||
| import com.google.api.gax.core.FixedCredentialsProvider; | ||
| import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; | ||
| import com.google.api.gax.paging.Page; | ||
| import com.google.api.gax.rpc.TransportChannelProvider; | ||
| import com.google.auth.http.HttpTransportFactory; | ||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.cloud.ServiceOptions; | ||
| import com.google.cloud.bigquery.jdbc.BigQueryConnection; | ||
| import com.google.cloud.bigquery.jdbc.DataSource; | ||
| import com.google.cloud.logging.LogEntry; | ||
| import com.google.cloud.logging.Logging; | ||
| import com.google.cloud.logging.LoggingOptions; | ||
| import com.google.cloud.logging.v2.LoggingClient; | ||
| import com.google.cloud.logging.v2.LoggingSettings; | ||
| import com.google.cloud.trace.v1.TraceServiceClient; | ||
| import com.google.cloud.trace.v1.TraceServiceSettings; | ||
| import com.google.devtools.cloudtrace.v1.Trace; | ||
| import com.google.devtools.cloudtrace.v1.TraceSpan; | ||
| import com.google.gson.JsonObject; | ||
| import com.google.logging.v2.ListLogEntriesRequest; | ||
| import com.google.logging.v2.LogEntry; | ||
| import io.grpc.HttpConnectProxiedSocketAddress; | ||
| import io.grpc.ProxiedSocketAddress; | ||
| import io.grpc.ProxyDetector; | ||
| import io.grpc.netty.shaded.io.grpc.netty.GrpcSslContexts; | ||
| import io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder; | ||
| import io.grpc.netty.shaded.io.netty.handler.ssl.SslContext; | ||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanContext; | ||
| import io.opentelemetry.api.trace.TraceFlags; | ||
| import io.opentelemetry.api.trace.TraceState; | ||
| import io.opentelemetry.context.Scope; | ||
| import io.opentelemetry.sdk.trace.IdGenerator; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.File; | ||
| import java.io.FileInputStream; | ||
| import java.io.IOException; | ||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.security.GeneralSecurityException; | ||
| import java.security.KeyStore; | ||
| import java.sql.Connection; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.TrustManagerFactory; | ||
| import org.apache.hc.client5.http.auth.AuthScope; | ||
| import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; | ||
| import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; | ||
| import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
| import org.apache.hc.client5.http.impl.classic.HttpClients; | ||
| import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; | ||
| import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner; | ||
| import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; | ||
| import org.apache.hc.core5.http.HttpHost; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
|
|
@@ -70,35 +92,8 @@ public class ITOpenTelemetryTest extends ITBase { | |
|
|
||
| private static final String PROJECT_ID = ServiceOptions.getDefaultProjectId(); | ||
| private static final String CONNECTION_URL = connectionUrl; | ||
| private static final String[] PROXY_SYS_PROPS = { | ||
| "https.proxyHost", "https.proxyPort", "http.proxyHost", "http.proxyPort" | ||
| }; | ||
| private static final Map<String, String> originalSystemProperties = new HashMap<>(); | ||
|
|
||
| @BeforeAll | ||
| public static void setUpProxyProperties() { | ||
| DataSource ds = DataSource.fromUrl(CONNECTION_URL); | ||
| if (ds.getProxyHost() != null && ds.getProxyPort() != null) { | ||
| for (String prop : PROXY_SYS_PROPS) { | ||
| originalSystemProperties.put(prop, System.getProperty(prop)); | ||
| } | ||
| System.setProperty("https.proxyHost", ds.getProxyHost()); | ||
| System.setProperty("https.proxyPort", ds.getProxyPort()); | ||
| System.setProperty("http.proxyHost", ds.getProxyHost()); | ||
| System.setProperty("http.proxyPort", ds.getProxyPort()); | ||
| } | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void tearDownProxyProperties() { | ||
| for (Map.Entry<String, String> entry : originalSystemProperties.entrySet()) { | ||
| if (entry.getValue() != null) { | ||
| System.setProperty(entry.getKey(), entry.getValue()); | ||
| } else { | ||
| System.clearProperty(entry.getKey()); | ||
| } | ||
| } | ||
| } | ||
| private static final String CLOUD_PLATFORM_SCOPE = | ||
| "https://www.googleapis.com/auth/cloud-platform"; | ||
|
|
||
| @Test | ||
| public void testExecute_withOpenTelemetryGcpExporter() throws Exception { | ||
|
|
@@ -329,26 +324,116 @@ private void verifyTraceDelivery(DataSource ds) throws Exception { | |
| assertNotNull(trace, "Trace must be found"); | ||
| } | ||
|
|
||
| private GoogleCredentials getVerificationCredentials(DataSource ds) throws Exception { | ||
| JsonObject authJson = getAuthJson(); | ||
| ByteArrayInputStream stream = | ||
| new ByteArrayInputStream(authJson.toString().getBytes(StandardCharsets.UTF_8)); | ||
| HttpTransportFactory transportFactory = createVerificationHttpTransportFactory(ds); | ||
| GoogleCredentials credentials = | ||
| transportFactory != null | ||
| ? GoogleCredentials.fromStream(stream, transportFactory) | ||
| : GoogleCredentials.fromStream(stream); | ||
| if (credentials.createScopedRequired()) { | ||
| return credentials.createScoped(Collections.singletonList(CLOUD_PLATFORM_SCOPE)); | ||
| } | ||
| return credentials; | ||
| } | ||
|
|
||
| private HttpTransportFactory createVerificationHttpTransportFactory(DataSource ds) | ||
| throws IOException, GeneralSecurityException { | ||
| String proxyHost = ds.getProxyHost(); | ||
| String proxyPortStr = ds.getProxyPort(); | ||
| boolean hasProxy = proxyHost != null && proxyPortStr != null; | ||
| boolean hasTrustStore = | ||
| ds.getSSLTrustStorePath() != null && !ds.getSSLTrustStorePath().trim().isEmpty(); | ||
| if (!hasProxy && !hasTrustStore) { | ||
| return null; | ||
| } | ||
|
|
||
| HttpClientBuilder httpClientBuilder = HttpClients.custom(); | ||
| if (hasProxy) { | ||
| int port = Integer.parseInt(proxyPortStr); | ||
| httpClientBuilder.setRoutePlanner( | ||
| new DefaultProxyRoutePlanner(new HttpHost(proxyHost, port))); | ||
| if (ds.getProxyUid() != null && ds.getProxyPwd() != null) { | ||
| BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||
| credentialsProvider.setCredentials( | ||
| new AuthScope(proxyHost, port), | ||
| new UsernamePasswordCredentials(ds.getProxyUid(), ds.getProxyPwd().toCharArray())); | ||
| httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); | ||
| httpClientBuilder.setProxyAuthenticationStrategy(DefaultAuthenticationStrategy.INSTANCE); | ||
| } | ||
| } | ||
| if (hasTrustStore) { | ||
| TrustManagerFactory tmf = | ||
| loadTrustManagerFactory( | ||
| ds.getSSLTrustStorePath(), | ||
| ds.getSSLTrustStorePassword(), | ||
| ds.getSSLTrustStoreType(), | ||
| ds.getSSLTrustStoreProvider()); | ||
| SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, tmf.getTrustManagers(), null); | ||
| httpClientBuilder.setConnectionManager( | ||
| PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setSSLSocketFactory(new SSLConnectionSocketFactory(sslContext)) | ||
| .build()); | ||
| } | ||
| final HttpTransport httpTransport = new Apache5HttpTransport(httpClientBuilder.build()); | ||
| return () -> httpTransport; | ||
| } | ||
|
|
||
| private TrustManagerFactory loadTrustManagerFactory( | ||
| String sslTrustStorePath, | ||
| String sslTrustStorePassword, | ||
| String sslTrustStoreType, | ||
| String sslTrustStoreProvider) | ||
| throws IOException, GeneralSecurityException { | ||
| try (FileInputStream stream = new FileInputStream(sslTrustStorePath)) { | ||
| String type = | ||
| (sslTrustStoreType != null && !sslTrustStoreType.trim().isEmpty()) | ||
| ? sslTrustStoreType.trim() | ||
| : KeyStore.getDefaultType(); | ||
| KeyStore trustStore = | ||
| (sslTrustStoreProvider != null && !sslTrustStoreProvider.trim().isEmpty()) | ||
| ? KeyStore.getInstance(type, sslTrustStoreProvider.trim()) | ||
| : KeyStore.getInstance(type); | ||
| char[] password = sslTrustStorePassword != null ? sslTrustStorePassword.toCharArray() : null; | ||
| trustStore.load(stream, password); | ||
| TrustManagerFactory tmf = | ||
| TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| tmf.init(trustStore); | ||
| return tmf; | ||
| } | ||
| } | ||
|
|
||
| private String verifyAndFetchLogs(String connectionUuid) throws Exception { | ||
| GoogleCredentials credentials = getCredentials(); | ||
|
|
||
| try (Logging logging = | ||
| LoggingOptions.newBuilder() | ||
| .setProjectId(PROJECT_ID) | ||
| .setCredentials(credentials) | ||
| .build() | ||
| .getService()) { | ||
| DataSource ds = DataSource.fromUrl(CONNECTION_URL); | ||
| GoogleCredentials credentials = getVerificationCredentials(ds); | ||
| String universeDomain = ds.getUniverseDomain(); | ||
|
|
||
| LoggingSettings.Builder settingsBuilder = | ||
| LoggingSettings.newBuilder() | ||
| .setCredentialsProvider(FixedCredentialsProvider.create(credentials)); | ||
| if (universeDomain != null) { | ||
| settingsBuilder.setUniverseDomain(universeDomain); | ||
| } | ||
| TransportChannelProvider channelProvider = createVerificationTransportChannelProvider(ds); | ||
| if (channelProvider != null) { | ||
| settingsBuilder.setTransportChannelProvider(channelProvider); | ||
| } | ||
|
|
||
| try (LoggingClient loggingClient = LoggingClient.create(settingsBuilder.build())) { | ||
| String filter = "labels.\"jdbc.connection_id\"=\"" + connectionUuid + "\""; | ||
|
|
||
| List<LogEntry> entries = fetchLogsWithRetry(logging, filter); | ||
| List<LogEntry> entries = fetchLogsWithRetry(loggingClient, filter); | ||
| assertFalse(entries.isEmpty(), "Telemetry logs should be exported to GCP"); | ||
|
|
||
| String traceId = null; | ||
| String hexSpanId = null; | ||
| for (LogEntry entry : entries) { | ||
| if (entry.getTrace() != null) { | ||
| if (!entry.getTrace().isEmpty()) { | ||
| traceId = entry.getTrace(); | ||
| hexSpanId = entry.getSpanId(); | ||
| hexSpanId = entry.getSpanId().isEmpty() ? null : entry.getSpanId(); | ||
| break; | ||
| } | ||
| } | ||
|
|
@@ -358,7 +443,7 @@ private String verifyAndFetchLogs(String connectionUuid) throws Exception { | |
|
|
||
| // Verify Connection UUID label correlation on all entries | ||
| for (LogEntry entry : entries) { | ||
| assertEquals(connectionUuid, entry.getLabels().get("jdbc.connection_id")); | ||
| assertEquals(connectionUuid, entry.getLabelsMap().get("jdbc.connection_id")); | ||
| } | ||
|
|
||
| return traceId; | ||
|
|
@@ -371,18 +456,20 @@ private Trace verifyAndFetchTrace(String traceId) throws Exception { | |
| hexTraceId = traceId.substring(traceId.lastIndexOf("/traces/") + 8); | ||
| } | ||
|
|
||
| GoogleCredentials credentials = getCredentials(); | ||
| DataSource ds = DataSource.fromUrl(CONNECTION_URL); | ||
| GoogleCredentials credentials = getVerificationCredentials(ds); | ||
|
|
||
| TraceServiceSettings.Builder settingsBuilder = | ||
| TraceServiceSettings.newBuilder() | ||
| .setCredentialsProvider(FixedCredentialsProvider.create(credentials)); | ||
|
|
||
| DataSource ds = DataSource.fromUrl(CONNECTION_URL); | ||
| String proxyHost = ds.getProxyHost(); | ||
| String proxyPortStr = ds.getProxyPort(); | ||
| if (proxyHost != null && proxyPortStr != null) { | ||
| settingsBuilder.setTransportChannelProvider( | ||
| createProxyTransportChannelProvider(proxyHost, Integer.parseInt(proxyPortStr))); | ||
| String universeDomain = ds.getUniverseDomain(); | ||
| if (universeDomain != null) { | ||
| settingsBuilder.setUniverseDomain(universeDomain); | ||
| } | ||
| TransportChannelProvider channelProvider = createVerificationTransportChannelProvider(ds); | ||
| if (channelProvider != null) { | ||
| settingsBuilder.setTransportChannelProvider(channelProvider); | ||
| } | ||
|
|
||
| try (TraceServiceClient traceClient = TraceServiceClient.create(settingsBuilder.build())) { | ||
|
|
@@ -392,20 +479,73 @@ private Trace verifyAndFetchTrace(String traceId) throws Exception { | |
| } | ||
| } | ||
|
|
||
| private TransportChannelProvider createProxyTransportChannelProvider(String host, int port) { | ||
| private TransportChannelProvider createVerificationTransportChannelProvider(DataSource ds) { | ||
| String proxyHost = ds.getProxyHost(); | ||
| String proxyPortStr = ds.getProxyPort(); | ||
| boolean hasProxy = proxyHost != null && proxyPortStr != null; | ||
| boolean hasTrustStore = | ||
| ds.getSSLTrustStorePath() != null && !ds.getSSLTrustStorePath().trim().isEmpty(); | ||
| if (!hasProxy && !hasTrustStore) { | ||
| return null; | ||
| } | ||
| return createTransportChannelProvider( | ||
| hasProxy ? proxyHost : null, | ||
| hasProxy ? Integer.valueOf(proxyPortStr) : null, | ||
| ds.getProxyUid(), | ||
| ds.getProxyPwd(), | ||
| ds.getSSLTrustStorePath(), | ||
| ds.getSSLTrustStorePassword(), | ||
| ds.getSSLTrustStoreType(), | ||
| ds.getSSLTrustStoreProvider()); | ||
| } | ||
|
|
||
| private TransportChannelProvider createTransportChannelProvider( | ||
| String host, | ||
| Integer port, | ||
| String proxyUid, | ||
| String proxyPwd, | ||
| String sslTrustStorePath, | ||
| String sslTrustStorePassword, | ||
| String sslTrustStoreType, | ||
| String sslTrustStoreProvider) { | ||
| return InstantiatingGrpcChannelProvider.newBuilder() | ||
| .setChannelConfigurator( | ||
| builder -> | ||
| builder -> { | ||
| if (host != null && port != null) { | ||
| builder.proxyDetector( | ||
| new ProxyDetector() { | ||
| @Override | ||
| public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) { | ||
| return HttpConnectProxiedSocketAddress.newBuilder() | ||
| .setProxyAddress(new InetSocketAddress(host, port)) | ||
| .setTargetAddress((InetSocketAddress) socketAddress) | ||
| .build(); | ||
| HttpConnectProxiedSocketAddress.Builder proxyBuilder = | ||
| HttpConnectProxiedSocketAddress.newBuilder() | ||
| .setProxyAddress(new InetSocketAddress(host, port)) | ||
| .setTargetAddress((InetSocketAddress) socketAddress); | ||
| if (proxyUid != null && proxyPwd != null) { | ||
| proxyBuilder.setUsername(proxyUid); | ||
| proxyBuilder.setPassword(proxyPwd); | ||
| } | ||
| return proxyBuilder.build(); | ||
| } | ||
| })) | ||
| }); | ||
| } | ||
| if (sslTrustStorePath != null | ||
| && !sslTrustStorePath.trim().isEmpty() | ||
| && builder instanceof NettyChannelBuilder) { | ||
| try { | ||
| TrustManagerFactory tmf = | ||
| loadTrustManagerFactory( | ||
| sslTrustStorePath, | ||
| sslTrustStorePassword, | ||
| sslTrustStoreType, | ||
| sslTrustStoreProvider); | ||
| SslContext sslContext = GrpcSslContexts.forClient().trustManager(tmf).build(); | ||
| ((NettyChannelBuilder) builder).sslContext(sslContext); | ||
| } catch (IOException | GeneralSecurityException e) { | ||
| throw new IllegalStateException("Failed to configure test SSL TrustStore", e); | ||
| } | ||
| } | ||
|
Comment on lines
+531
to
+546
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. If if (sslTrustStorePath != null && !sslTrustStorePath.trim().isEmpty()) {
if (builder instanceof NettyChannelBuilder) {
try {
TrustManagerFactory tmf =
loadTrustManagerFactory(
sslTrustStorePath,
sslTrustStorePassword,
sslTrustStoreType,
sslTrustStoreProvider);
SslContext sslContext = GrpcSslContexts.forClient().trustManager(tmf).build();
((NettyChannelBuilder) builder).sslContext(sslContext);
} catch (IOException | GeneralSecurityException e) {
throw new IllegalStateException("Failed to configure test SSL TrustStore", e);
}
} else {
throw new IllegalStateException(
"SSL TrustStore configured but the channel builder is not an instance of NettyChannelBuilder: "
+ builder.getClass().getName());
}
} |
||
| return builder; | ||
| }) | ||
| .build(); | ||
| } | ||
|
|
||
|
|
@@ -438,16 +578,21 @@ private <T> T pollWithRetry(java.util.concurrent.Callable<T> task) throws Interr | |
| return null; | ||
| } | ||
|
|
||
| private List<LogEntry> fetchLogsWithRetry(Logging logging, String filter) | ||
| private List<LogEntry> fetchLogsWithRetry(LoggingClient loggingClient, String filter) | ||
| throws InterruptedException { | ||
| String boundedFilter = | ||
| filter + " AND timestamp >= \"" + Instant.now().minus(Duration.ofHours(24)) + "\""; | ||
| List<LogEntry> result = | ||
| pollWithRetry( | ||
| () -> { | ||
| Page<LogEntry> entriesPage = | ||
| logging.listLogEntries( | ||
| Logging.EntryListOption.filter(filter), Logging.EntryListOption.pageSize(50)); | ||
| ListLogEntriesRequest request = | ||
| ListLogEntriesRequest.newBuilder() | ||
| .addResourceNames("projects/" + PROJECT_ID) | ||
| .setFilter(boundedFilter) | ||
| .setPageSize(50) | ||
| .build(); | ||
| List<LogEntry> entries = new ArrayList<>(); | ||
| entriesPage.iterateAll().forEach(entries::add); | ||
| loggingClient.listLogEntries(request).iterateAll().forEach(entries::add); | ||
| return entries.isEmpty() ? null : entries; | ||
| }); | ||
| return result != null ? result : new ArrayList<>(); | ||
|
|
||
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.
The local variable
hexSpanIdis assigned but never read or used anywhere in the method. It can be safely removed to clean up the code.