Repository navigation
refactor(bigquery-jdbc): extract SSL TrustStore helpers and unify proxy checks #15340
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 |
|---|---|---|
|
|
@@ -44,7 +44,9 @@ | |
| import java.util.regex.Pattern; | ||
| import javax.net.ssl.SSLContext; | ||
| import javax.net.ssl.SSLSocket; | ||
| import javax.net.ssl.TrustManager; | ||
| import javax.net.ssl.TrustManagerFactory; | ||
| import javax.net.ssl.X509TrustManager; | ||
| import org.apache.hc.client5.http.auth.AuthScope; | ||
| import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; | ||
| import org.apache.hc.client5.http.impl.DefaultAuthenticationStrategy; | ||
|
|
@@ -71,6 +73,16 @@ final class BigQueryJdbcProxyUtility { | |
| HttpJsonConscryptUtils.configureConscryptSecurityProvider(new NetHttpTransport.Builder()) | ||
| .build(); | ||
|
|
||
| static final class SslConfig { | ||
| final SSLContext sslContext; | ||
| final X509TrustManager trustManager; | ||
|
|
||
| SslConfig(SSLContext sslContext, X509TrustManager trustManager) { | ||
| this.sslContext = sslContext; | ||
| this.trustManager = trustManager; | ||
| } | ||
| } | ||
|
|
||
| private BigQueryJdbcProxyUtility() {} | ||
|
|
||
| private static Provider createConscryptProvider() { | ||
|
|
@@ -100,11 +112,89 @@ protected void prepareSocket(SSLSocket socket) throws IOException { | |
| }; | ||
| } | ||
|
|
||
| static SslConfig createSslConfig( | ||
| String sslTrustStorePath, | ||
| String sslTrustStorePassword, | ||
| String sslTrustStoreType, | ||
| String sslTrustStoreProvider) { | ||
| return createSslConfig( | ||
| sslTrustStorePath, sslTrustStorePassword, sslTrustStoreType, sslTrustStoreProvider, null); | ||
| } | ||
|
|
||
| private static SslConfig createSslConfig( | ||
| String sslTrustStorePath, | ||
| String sslTrustStorePassword, | ||
| String sslTrustStoreType, | ||
| String sslTrustStoreProvider, | ||
| Provider sslProvider) { | ||
| try { | ||
| TrustManagerFactory trustManagerFactory = | ||
| createTrustManagerFactory( | ||
| sslTrustStorePath, | ||
| sslTrustStorePassword, | ||
| sslTrustStoreType, | ||
| sslTrustStoreProvider, | ||
| sslProvider); | ||
| TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); | ||
| X509TrustManager x509TrustManager = findX509TrustManager(trustManagers); | ||
|
|
||
| SSLContext sslContext = | ||
| sslProvider != null | ||
| ? SSLContext.getInstance("TLS", sslProvider) | ||
| : SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, trustManagers, null); | ||
| return new SslConfig(sslContext, x509TrustManager); | ||
| } catch (IOException | GeneralSecurityException e) { | ||
| throw new BigQueryJdbcRuntimeException("Failed to configure SSL TrustStore", e); | ||
| } | ||
| } | ||
|
|
||
| private static TrustManagerFactory createTrustManagerFactory( | ||
| String sslTrustStorePath, | ||
| String sslTrustStorePassword, | ||
| String sslTrustStoreType, | ||
| String sslTrustStoreProvider, | ||
| Provider trustManagerProvider) | ||
| throws IOException, GeneralSecurityException { | ||
| try (FileInputStream trustStoreStream = new FileInputStream(sslTrustStorePath)) { | ||
| KeyStore trustStore = loadKeyStore(sslTrustStoreType, sslTrustStoreProvider); | ||
| char[] trustStorePasswordChars = | ||
| sslTrustStorePassword != null ? sslTrustStorePassword.toCharArray() : null; | ||
| trustStore.load(trustStoreStream, trustStorePasswordChars); | ||
|
|
||
| TrustManagerFactory trustManagerFactory = | ||
| trustManagerProvider != null | ||
| ? TrustManagerFactory.getInstance( | ||
| TrustManagerFactory.getDefaultAlgorithm(), trustManagerProvider) | ||
| : TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init(trustStore); | ||
| return trustManagerFactory; | ||
| } | ||
| } | ||
|
|
||
| private static X509TrustManager findX509TrustManager(TrustManager[] trustManagers) | ||
| throws GeneralSecurityException { | ||
| if (trustManagers != null) { | ||
| for (TrustManager trustManager : trustManagers) { | ||
| if (trustManager instanceof X509TrustManager) { | ||
| return (X509TrustManager) trustManager; | ||
| } | ||
| } | ||
| } | ||
| throw new GeneralSecurityException("No X509TrustManager found in configured TrustStore"); | ||
| } | ||
|
|
||
| static Map<String, String> parseProxyProperties(DataSource ds, String callerClassName) { | ||
| LOG.finest("++enter++\t" + callerClassName); | ||
| Map<String, String> proxyProperties = new HashMap<>(); | ||
| String proxyHost = ds.getProxyHost(); | ||
| if (proxyHost != null) { | ||
| if (proxyHost.trim().isEmpty()) { | ||
| IllegalArgumentException ex = | ||
| new IllegalArgumentException("ProxyHost cannot be empty when specified."); | ||
| LOG.severe(ex.getMessage(), ex); | ||
| throw ex; | ||
| } | ||
| proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, proxyHost); | ||
| } | ||
| String proxyPort = ds.getProxyPort(); | ||
|
|
@@ -164,6 +254,18 @@ static Map<String, String> parseProxyProperties(String URL, String callerClassNa | |
| return parseProxyProperties(DataSource.fromUrl(URL), callerClassName); | ||
| } | ||
|
|
||
| static boolean hasProxy(Map<String, String> proxyProperties) { | ||
| return proxyProperties != null | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME) | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME); | ||
| } | ||
|
Comment on lines
+257
to
+261
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 helper method Please update static boolean hasProxy(Map<String, String> proxyProperties) {
return proxyProperties != null
&& proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
} |
||
|
|
||
| static boolean hasProxyAuth(Map<String, String> proxyProperties) { | ||
| return proxyProperties != null | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME) | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME); | ||
| } | ||
|
|
||
| static HttpTransportOptions getHttpTransportOptions( | ||
| Map<String, String> proxyProperties, | ||
| String sslTrustStorePath, | ||
|
|
@@ -175,9 +277,7 @@ static HttpTransportOptions getHttpTransportOptions( | |
| String callerClassName) { | ||
| LOG.finest("++enter++\t" + callerClassName); | ||
|
|
||
| boolean hasProxyOrSsl = | ||
| proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME) | ||
| || sslTrustStorePath != null; | ||
| boolean hasProxyOrSsl = hasProxy(proxyProperties) || sslTrustStorePath != null; | ||
|
|
||
| HttpTransportOptions.Builder httpTransportOptionsBuilder = | ||
| BigQueryOptions.getDefaultHttpTransportOptions().toBuilder(); | ||
|
|
@@ -213,10 +313,7 @@ private static HttpTransportFactory getHttpTransportFactory( | |
| String callerClassName) { | ||
| LOG.finest("++enter++\t" + callerClassName); | ||
| HttpClientBuilder httpClientBuilder = HttpClients.custom(); | ||
| boolean explicitProxySet = | ||
| proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME); | ||
|
|
||
| if (explicitProxySet) { | ||
| if (hasProxy(proxyProperties)) { | ||
| HttpHost proxyHostDetails = | ||
| new HttpHost( | ||
| proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME), | ||
|
|
@@ -229,34 +326,19 @@ private static HttpTransportFactory getHttpTransportFactory( | |
| httpClientBuilder.useSystemProperties(); | ||
|
|
||
| if (sslTrustStorePath != null) { | ||
| try (FileInputStream trustStoreStream = new FileInputStream(sslTrustStorePath)) { | ||
| KeyStore trustStore = loadKeyStore(sslTrustStoreType, sslTrustStoreProvider); | ||
| char[] trustStorePasswordChars = | ||
| sslTrustStorePassword != null ? sslTrustStorePassword.toCharArray() : null; | ||
| trustStore.load(trustStoreStream, trustStorePasswordChars); | ||
|
|
||
| TrustManagerFactory trustManagerFactory = | ||
| CONSCRYPT_PROVIDER != null | ||
| ? TrustManagerFactory.getInstance( | ||
| TrustManagerFactory.getDefaultAlgorithm(), CONSCRYPT_PROVIDER) | ||
| : TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init(trustStore); | ||
|
|
||
| SSLContext sslContext = | ||
| CONSCRYPT_PROVIDER != null | ||
| ? SSLContext.getInstance("TLS", CONSCRYPT_PROVIDER) | ||
| : SSLContext.getInstance("TLS"); | ||
| sslContext.init(null, trustManagerFactory.getTrustManagers(), null); | ||
|
|
||
| SSLConnectionSocketFactory sslSocketFactory = createSslConnectionSocketFactory(sslContext); | ||
| httpClientBuilder.setConnectionManager( | ||
| PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setSSLSocketFactory(sslSocketFactory) | ||
| .build()); | ||
| } catch (IOException | GeneralSecurityException e) { | ||
| throw new BigQueryJdbcRuntimeException( | ||
| "Failed to configure SSL TrustStore for HTTP transport", e); | ||
| } | ||
| SslConfig sslConfig = | ||
| createSslConfig( | ||
| sslTrustStorePath, | ||
| sslTrustStorePassword, | ||
| sslTrustStoreType, | ||
| sslTrustStoreProvider, | ||
| CONSCRYPT_PROVIDER); | ||
| SSLConnectionSocketFactory sslSocketFactory = | ||
| createSslConnectionSocketFactory(sslConfig.sslContext); | ||
| httpClientBuilder.setConnectionManager( | ||
| PoolingHttpClientConnectionManagerBuilder.create() | ||
| .setSSLSocketFactory(sslSocketFactory) | ||
| .build()); | ||
| } else if (CONSCRYPT_PROVIDER != null) { | ||
| try { | ||
| TrustManagerFactory trustManagerFactory = | ||
|
|
@@ -277,7 +359,6 @@ private static HttpTransportFactory getHttpTransportFactory( | |
| + e.getMessage()); | ||
| } | ||
| } | ||
| addAuthToProxyIfPresent(proxyProperties, httpClientBuilder, callerClassName); | ||
|
|
||
| CloseableHttpClient httpClient = httpClientBuilder.build(); | ||
| final HttpTransport httpTransport = new Apache5HttpTransport(httpClient); | ||
|
|
@@ -289,8 +370,7 @@ private static void addAuthToProxyIfPresent( | |
| HttpClientBuilder closeableHttpClientBuilder, | ||
| String callerClassName) { | ||
| LOG.finest("++enter++\t" + callerClassName); | ||
| if (proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME) | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME)) { | ||
| if (hasProxyAuth(proxyProperties)) { | ||
|
|
||
| AuthScope authScope = | ||
| new AuthScope( | ||
|
|
@@ -308,7 +388,7 @@ private static void addAuthToProxyIfPresent( | |
| proxyCredentialsProvider.setCredentials(authScope, usernamePasswordCredentials); | ||
| closeableHttpClientBuilder.setDefaultCredentialsProvider(proxyCredentialsProvider); | ||
| closeableHttpClientBuilder.setProxyAuthenticationStrategy( | ||
| DefaultAuthenticationStrategy.INSTANCE); // order of challenge? so it will show up | ||
| DefaultAuthenticationStrategy.INSTANCE); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -320,7 +400,7 @@ static TransportChannelProvider getTransportChannelProvider( | |
| String sslTrustStoreProvider, | ||
| String callerClassName) { | ||
| LOG.finest("++enter++\t" + callerClassName); | ||
| boolean hasProxy = proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME); | ||
| boolean hasProxy = hasProxy(proxyProperties); | ||
| boolean hasSsl = sslTrustStorePath != null; | ||
|
|
||
| if (!hasProxy && !hasSsl) { | ||
|
|
@@ -344,20 +424,14 @@ public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) { | |
| if (hasSsl | ||
| && managedChannelBuilder | ||
| instanceof io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) { | ||
| try (FileInputStream trustStoreStream = | ||
| new FileInputStream(sslTrustStorePath)) { | ||
| KeyStore trustStore = loadKeyStore(sslTrustStoreType, sslTrustStoreProvider); | ||
| char[] trustStorePasswordChars = | ||
| sslTrustStorePassword != null | ||
| ? sslTrustStorePassword.toCharArray() | ||
| : null; | ||
| trustStore.load(trustStoreStream, trustStorePasswordChars); | ||
|
|
||
| try { | ||
| TrustManagerFactory trustManagerFactory = | ||
| TrustManagerFactory.getInstance( | ||
| TrustManagerFactory.getDefaultAlgorithm()); | ||
| trustManagerFactory.init(trustStore); | ||
|
|
||
| createTrustManagerFactory( | ||
| sslTrustStorePath, | ||
| sslTrustStorePassword, | ||
| sslTrustStoreType, | ||
| sslTrustStoreProvider, | ||
| null); | ||
| SslContext grpcSslContext = | ||
| GrpcSslContexts.forClient().trustManager(trustManagerFactory).build(); | ||
| ((io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) | ||
|
|
@@ -393,8 +467,7 @@ private static HttpConnectProxiedSocketAddress getHttpConnectProxiedSocketAddres | |
| HttpConnectProxiedSocketAddress.newBuilder() | ||
| .setProxyAddress(new InetSocketAddress(proxyHost, proxyPort)) | ||
| .setTargetAddress(socketAddress); | ||
| if (proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME) | ||
| && proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME)) { | ||
| if (hasProxyAuth(proxyProperties)) { | ||
| builder.setUsername(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME)); | ||
| builder.setPassword(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME)); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -17,9 +17,11 @@ | |||||||||||||
| package com.google.cloud.bigquery.jdbc; | ||||||||||||||
|
|
||||||||||||||
| import static com.google.common.truth.Truth.assertThat; | ||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertThrows; | ||||||||||||||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||||||||||||||
|
|
||||||||||||||
| import com.google.api.client.http.GenericUrl; | ||||||||||||||
| import com.google.api.client.http.HttpResponse; | ||||||||||||||
|
|
@@ -146,6 +148,55 @@ public void testMissingPwdWithUidThrowsIllegalArgument() { | |||||||||||||
| null)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Test | ||||||||||||||
| public void testBlankHostThrowsIllegalArgument() { | ||||||||||||||
| assertThrows( | ||||||||||||||
| IllegalArgumentException.class, | ||||||||||||||
| () -> | ||||||||||||||
| BigQueryJdbcProxyUtility.parseProxyProperties( | ||||||||||||||
| "jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;" | ||||||||||||||
| + "ProjectId=MyBigQueryProject;OAuthType=3;" | ||||||||||||||
| + "ProxyHost= ;" | ||||||||||||||
| + "ProxyPort=3128;", | ||||||||||||||
| null)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Test | ||||||||||||||
| public void testHasProxyAndHasProxyAuth() { | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxy(null)); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxy(Collections.emptyMap())); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxyAuth(null)); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxyAuth(Collections.emptyMap())); | ||||||||||||||
|
|
||||||||||||||
| Map<String, String> hostOnly = new HashMap<>(); | ||||||||||||||
| hostOnly.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, "127.0.0.1"); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxy(hostOnly)); | ||||||||||||||
|
Comment on lines
+171
to
+173
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. Since
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| Map<String, String> proxyOnly = new HashMap<>(); | ||||||||||||||
| proxyOnly.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, "127.0.0.1"); | ||||||||||||||
| proxyOnly.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, "3128"); | ||||||||||||||
| assertTrue(BigQueryJdbcProxyUtility.hasProxy(proxyOnly)); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxyAuth(proxyOnly)); | ||||||||||||||
|
|
||||||||||||||
| Map<String, String> partialAuth = new HashMap<>(proxyOnly); | ||||||||||||||
| partialAuth.put(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME, "user"); | ||||||||||||||
| assertFalse(BigQueryJdbcProxyUtility.hasProxyAuth(partialAuth)); | ||||||||||||||
|
|
||||||||||||||
| Map<String, String> fullAuth = new HashMap<>(partialAuth); | ||||||||||||||
| fullAuth.put(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME, "pass"); | ||||||||||||||
| assertTrue(BigQueryJdbcProxyUtility.hasProxyAuth(fullAuth)); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Test | ||||||||||||||
| public void testCreateSslConfig_usesDefaultJdkProvider() throws Exception { | ||||||||||||||
| String trustStorePath = getTestResourcePath("test_truststore_withpass.jks"); | ||||||||||||||
| BigQueryJdbcProxyUtility.SslConfig sslConfig = | ||||||||||||||
| BigQueryJdbcProxyUtility.createSslConfig(trustStorePath, "testpassword", "JKS", null); | ||||||||||||||
| assertNotNull(sslConfig.sslContext); | ||||||||||||||
| assertNotNull(sslConfig.trustManager); | ||||||||||||||
| assertThat(sslConfig.sslContext.getProvider().getName()).isNotEqualTo("Conscrypt"); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| @Test | ||||||||||||||
| public void testGetHttpTransportOptionsWithAuthenticatedProxy() { | ||||||||||||||
| String connection_uri = | ||||||||||||||
|
|
||||||||||||||
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.
To prevent downstream code from performing redundant disk reads and to avoid race conditions during file rotation, we should store the truststore file content in memory within our data holder classes and avoid keeping or passing the file path
sslTrustStorePath.References