This repository was archived by the owner on Mar 23, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathBigQueryJdbcProxyUtility.java
More file actions
306 lines (275 loc) · 13.6 KB
/
BigQueryJdbcProxyUtility.java
File metadata and controls
306 lines (275 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery.jdbc;
import static com.google.cloud.bigquery.storage.v1.stub.BigQueryReadStubSettings.defaultGrpcTransportProviderBuilder;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.v5.Apache5HttpTransport;
import com.google.api.gax.rpc.TransportChannelProvider;
import com.google.auth.http.HttpTransportFactory;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import com.google.cloud.http.HttpTransportOptions;
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.netty.handler.ssl.SslContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.security.GeneralSecurityException;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;
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.CloseableHttpClient;
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.routing.HttpRoutePlanner;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory;
import org.apache.hc.core5.http.HttpHost;
final class BigQueryJdbcProxyUtility {
private static final BigQueryJdbcCustomLogger LOG =
new BigQueryJdbcCustomLogger(BigQueryJdbcProxyUtility.class.getName());
static final String validPortRegex =
"^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$";
private BigQueryJdbcProxyUtility() {}
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) {
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, proxyHost);
}
String proxyPort = ds.getProxyPort();
if (proxyPort != null) {
if (!Pattern.compile(validPortRegex).matcher(proxyPort).find()) {
throw new IllegalArgumentException(
String.format(
"Illegal port number provided %s. Please provide a valid port number.", proxyPort));
}
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, proxyPort);
}
String proxyUid = ds.getProxyUid();
if (proxyUid != null) {
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME, proxyUid);
}
String proxyPwd = ds.getProxyPwd();
if (proxyPwd != null) {
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME, proxyPwd);
}
boolean isMissingProxyHostOrPortWhenProxySet =
(proxyHost == null && proxyPort != null) || (proxyHost != null && proxyPort == null);
if (isMissingProxyHostOrPortWhenProxySet) {
throw new IllegalArgumentException(
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
+ " occurs.");
}
boolean isMissingProxyUidOrPwdWhenAuthSet =
(proxyUid == null && proxyPwd != null) || (proxyUid != null && proxyPwd == null);
if (isMissingProxyUidOrPwdWhenAuthSet) {
throw new IllegalArgumentException(
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
}
boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null;
if (isProxyAuthSetWithoutProxySettings) {
throw new IllegalArgumentException(
"Proxy authentication provided via connection string with no proxy host or port set.");
}
return proxyProperties;
}
static Map<String, String> parseProxyProperties(String URL, String callerClassName) {
return parseProxyProperties(DataSource.fromUrl(URL), callerClassName);
}
static HttpTransportOptions getHttpTransportOptions(
Map<String, String> proxyProperties,
String sslTrustStorePath,
String sslTrustStorePassword,
Integer connectTimeout,
Integer readTimeout,
String callerClassName) {
LOG.finest("++enter++\t" + callerClassName);
boolean hasProxyOrSsl =
proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME)
|| sslTrustStorePath != null;
boolean hasTimeoutConfig = connectTimeout != null || readTimeout != null;
if (!hasProxyOrSsl && !hasTimeoutConfig) {
return null;
}
HttpTransportOptions.Builder httpTransportOptionsBuilder = HttpTransportOptions.newBuilder();
if (hasProxyOrSsl) {
httpTransportOptionsBuilder.setHttpTransportFactory(
getHttpTransportFactory(
proxyProperties, sslTrustStorePath, sslTrustStorePassword, callerClassName));
}
if (connectTimeout != null) {
httpTransportOptionsBuilder.setConnectTimeout(connectTimeout);
}
if (readTimeout != null) {
httpTransportOptionsBuilder.setReadTimeout(readTimeout);
}
return httpTransportOptionsBuilder.build();
}
private static HttpTransportFactory getHttpTransportFactory(
Map<String, String> proxyProperties,
String sslTrustStorePath,
String sslTrustStorePassword,
String callerClassName) {
LOG.finest("++enter++\t" + callerClassName);
HttpClientBuilder httpClientBuilder = HttpClients.custom();
boolean explicitProxySet =
proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
if (explicitProxySet) {
HttpHost proxyHostDetails =
new HttpHost(
proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME),
Integer.parseInt(
proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME)));
HttpRoutePlanner httpRoutePlanner = new DefaultProxyRoutePlanner(proxyHostDetails);
httpClientBuilder.setRoutePlanner(httpRoutePlanner);
addAuthToProxyIfPresent(proxyProperties, httpClientBuilder, callerClassName);
} else {
httpClientBuilder.useSystemProperties();
}
if (sslTrustStorePath != null) {
try (FileInputStream trustStoreStream = new FileInputStream(sslTrustStorePath)) {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] trustStorePasswordChars =
sslTrustStorePassword != null ? sslTrustStorePassword.toCharArray() : null;
trustStore.load(trustStoreStream, trustStorePasswordChars);
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
httpClientBuilder.setConnectionManager(
PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(sslSocketFactory)
.build());
} catch (IOException | GeneralSecurityException e) {
throw new BigQueryJdbcRuntimeException(e);
}
}
addAuthToProxyIfPresent(proxyProperties, httpClientBuilder, callerClassName);
CloseableHttpClient httpClient = httpClientBuilder.build();
final HttpTransport httpTransport = new Apache5HttpTransport(httpClient);
return () -> httpTransport;
}
private static void addAuthToProxyIfPresent(
Map<String, String> proxyProperties,
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)) {
AuthScope authScope =
new AuthScope(
proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME),
Integer.parseInt(
proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME)));
UsernamePasswordCredentials usernamePasswordCredentials =
new UsernamePasswordCredentials(
proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME),
proxyProperties
.get(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME)
.toCharArray());
BasicCredentialsProvider proxyCredentialsProvider = new BasicCredentialsProvider();
proxyCredentialsProvider.setCredentials(authScope, usernamePasswordCredentials);
closeableHttpClientBuilder.setDefaultCredentialsProvider(proxyCredentialsProvider);
closeableHttpClientBuilder.setProxyAuthenticationStrategy(
DefaultAuthenticationStrategy.INSTANCE); // order of challenge? so it will show up
}
}
static TransportChannelProvider getTransportChannelProvider(
Map<String, String> proxyProperties,
String sslTrustStorePath,
String sslTrustStorePassword,
String callerClassName) {
LOG.finest("++enter++\t" + callerClassName);
boolean hasProxy = proxyProperties.containsKey(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
boolean hasSsl = sslTrustStorePath != null;
if (!hasProxy && !hasSsl) {
return null;
}
TransportChannelProvider transportChannelProvider =
defaultGrpcTransportProviderBuilder()
.setChannelConfigurator(
managedChannelBuilder -> {
if (hasProxy) {
managedChannelBuilder.proxyDetector(
new ProxyDetector() {
@Override
public ProxiedSocketAddress proxyFor(SocketAddress socketAddress) {
return getHttpConnectProxiedSocketAddress(
(InetSocketAddress) socketAddress, proxyProperties);
}
});
}
if (hasSsl
&& managedChannelBuilder
instanceof io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder) {
try (FileInputStream trustStoreStream =
new FileInputStream(sslTrustStorePath)) {
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] trustStorePasswordChars =
sslTrustStorePassword != null
? sslTrustStorePassword.toCharArray()
: null;
trustStore.load(trustStoreStream, trustStorePasswordChars);
TrustManagerFactory trustManagerFactory =
TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(trustStore);
SslContext grpcSslContext =
GrpcSslContexts.forClient().trustManager(trustManagerFactory).build();
((io.grpc.netty.shaded.io.grpc.netty.NettyChannelBuilder)
managedChannelBuilder)
.sslContext(grpcSslContext);
} catch (IOException | GeneralSecurityException e) {
throw new BigQueryJdbcRuntimeException(e);
}
}
return managedChannelBuilder;
})
.build();
return transportChannelProvider;
}
private static HttpConnectProxiedSocketAddress getHttpConnectProxiedSocketAddress(
InetSocketAddress socketAddress, Map<String, String> proxyProperties) {
String proxyHost = proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
int proxyPort =
Integer.parseInt(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME));
HttpConnectProxiedSocketAddress.Builder builder =
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)) {
builder.setUsername(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME));
builder.setPassword(proxyProperties.get(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME));
}
return builder.build();
}
}