-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathBigQueryJdbcOAuthUtilityTest.java
More file actions
492 lines (431 loc) · 20.1 KB
/
Copy pathBigQueryJdbcOAuthUtilityTest.java
File metadata and controls
492 lines (431 loc) · 20.1 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright 2023 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
*
* http://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.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.auth.oauth2.ImpersonatedCredentials;
import com.google.auth.oauth2.UserAuthorizer;
import com.google.auth.oauth2.UserCredentials;
import com.google.cloud.bigquery.exception.BigQueryJdbcRuntimeException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class BigQueryJdbcOAuthUtilityTest extends BigQueryJdbcBaseTest {
private static final int USER_AUTH_PORT = 53737;
private static final String EXPECTED_USER_AUTH_URL =
"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=client_id&redirect_uri=http://localhost:"
+ USER_AUTH_PORT
+ "&scope=https://www.googleapis.com/auth/bigquery&state=test_state&access_type=offline&prompt=consent&login_hint=test_user&include_granted_scopes=true";
@Test
public void testParseOAuthPropsWithSpecialChars() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyProject;OAuthType=0;OAuthServiceAcctEmail=dummy@email.com;"
+ "OAuthPvtKey=Key+With+Plus;"),
null);
assertEquals("Key+With+Plus", result.get("OAuthPvtKey"));
}
@Test
public void testParseOAuthPropsForAuthType0KeyfileOnly() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=0;"
+ "OAuthPvtKeyPath=C:\\SecureFiles\\ServiceKeyFile.p12;"),
null);
assertThat(result.get("OAuthType")).isEqualTo("GOOGLE_SERVICE_ACCOUNT");
assertThat(result.get("OAuthPvtKeyPath")).isEqualTo("C:\\SecureFiles\\ServiceKeyFile.p12");
}
@Test
public void testParseOAuthPropsForAuthType0ViaEmail() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=0;"
+ "OAuthServiceAcctEmail=dummytest@dummytest.iam.gserviceaccount.com;"
+ "OAuthPvtKey=RedactedKey;"),
null);
assertThat(result.get("OAuthType")).isEqualTo("GOOGLE_SERVICE_ACCOUNT");
assertThat(result.get("OAuthServiceAcctEmail"))
.isEqualTo("dummytest@dummytest.iam.gserviceaccount.com");
assertThat(result.get("OAuthPvtKey")).isEqualTo("RedactedKey");
}
@Test
public void testInvalidTokenUriForAuthType0() {
String connectionString =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=0;"
+ "OAuthServiceAcctEmail=dummytest@dummytest.iam.gserviceaccount.com;"
+ "OAuthPvtKey="
+ fake_pkcs8_key
+ ";"
+ "EndpointOverrides=OAuth2=brokenuri{};";
Map<String, String> oauthProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionString), null);
Map<String, String> overrideProperties =
DataSource.fromUrl(connectionString).getOverrideProperties();
try {
BigQueryJdbcOAuthUtility.getCredentials(oauthProperties, overrideProperties, false, null);
Assertions.fail();
} catch (BigQueryJdbcRuntimeException e) {
assertThat(e.getMessage()).contains("Validation failure");
}
}
@Test
public void testParseOAuthPropsForAuthType2() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=RedactedToken;"),
null);
assertThat(result.get("OAuthType")).isEqualTo("PRE_GENERATED_TOKEN");
assertThat(result.get("OAuthAccessToken")).isEqualTo("RedactedToken");
}
@Test
public void testParseOAuthPropsForAuthType3() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=3;ProjectId=MyBigQueryProject;"),
null);
assertThat(result.get("OAuthType")).isEqualTo("APPLICATION_DEFAULT_CREDENTIALS");
}
@Test
public void testParseOAuthPropsForDefaultAuthType() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=3"),
null);
assertThat(result.get("OAuthType")).isEqualTo("APPLICATION_DEFAULT_CREDENTIALS");
}
@Test
public void testGetCredentialsForPreGeneratedToken() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=RedactedToken;"),
null);
GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(authProperties, Collections.EMPTY_MAP, false, null);
assertThat(credentials).isNotNull();
}
@Test
public void testGetCredentialsForPreGeneratedTokenTPC() throws IOException {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;"
+ "OAuthAccessToken=RedactedToken;"
+ "universeDomain=testDomain;"),
null);
Map<String, String> stringStringMap = new HashMap<>();
stringStringMap.put(
BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, "testDomain");
Map<String, String> overrideProperties = new HashMap<>(stringStringMap);
GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(authProperties, overrideProperties, false, null);
assertThat(credentials.getUniverseDomain()).isEqualTo("testDomain");
}
@Test
@Disabled // For running locally only similar to our other JDBC tests.
public void testGetCredentialsForApplicationDefault() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=3;ProjectId=MyBigQueryProject;"),
null);
GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(authProperties, null, false, null);
assertThat(credentials).isNotNull();
}
@Test
public void testParseOAuthPropsForUserAuth() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=1;ProjectId=MyBigQueryProject;"
+ "OAuthClientId=client;OAuthClientSecret=secret;"),
null);
assertThat(authProperties.get("OAuthType")).isEqualTo("GOOGLE_USER_ACCOUNT");
assertThat(authProperties.get("OAuthClientId")).isEqualTo("client");
assertThat(authProperties.get("OAuthClientSecret")).isEqualTo("secret");
}
@Test
public void testParseOAuthPropsForUserAuthDefault() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=1;ProjectId=MyBigQueryProject;"),
null);
assertThat(authProperties.get("OAuthType")).isEqualTo("GOOGLE_USER_ACCOUNT");
assertThat(authProperties.get("OAuthClientId"))
.isEqualTo(BigQueryJdbcUrlUtility.DEFAULT_OAUTH_CLIENT_ID);
assertThat(authProperties.get("OAuthClientSecret"))
.isEqualTo(BigQueryJdbcUrlUtility.DEFAULT_OAUTH_CLIENT_SECRET);
}
@Test
public void testGenerateUserAuthURL() {
try {
HashMap<String, String> authProperties = new HashMap<>();
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_CLIENT_ID_PROPERTY_NAME, "client_id");
authProperties.put(BigQueryJdbcUrlUtility.OAUTH_CLIENT_SECRET_PROPERTY_NAME, "client_secret");
UserAuthorizer userAuthorizer =
BigQueryJdbcOAuthUtility.getUserAuthorizer(
authProperties, new HashMap<String, String>(), USER_AUTH_PORT, null);
String userId = "test_user";
String state = "test_state";
URI baseURI = URI.create("http://example.com/foo");
URL authURL = userAuthorizer.getAuthorizationUrl(userId, state, baseURI);
assertThat(authURL.toString()).isEqualTo(EXPECTED_USER_AUTH_URL);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Test
public void testGenerateUserAuthURLOverrideOauthEndpoint() {
try {
URI overrideTokenSeverURI = new URI("https://oauth2-gsprivateall.p.googleapis.com/token");
String connectionString =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=1;"
+ "OAuthClientId=client;OAuthClientSecret=secret;"
+ "EndpointOverrides=OAuth2="
+ overrideTokenSeverURI
+ ";";
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionString), null);
Map<String, String> overrideProperties =
DataSource.fromUrl(connectionString).getOverrideProperties();
UserAuthorizer userAuthorizer =
BigQueryJdbcOAuthUtility.getUserAuthorizer(
authProperties, overrideProperties, USER_AUTH_PORT, null);
assertThat(overrideTokenSeverURI).isEqualTo(userAuthorizer.toBuilder().getTokenServerUri());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Test
public void testParseOAuthPropsForRefreshToken() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=2;ProjectId=MyBigQueryProject;OAuthRefreshToken=token;"
+ "OAuthClientId=client;OAuthClientSecret=secret;"),
null);
assertThat(authProperties.get("OAuthType")).isEqualTo("PRE_GENERATED_TOKEN");
assertThat(authProperties.get("OAuthRefreshToken")).isEqualTo("token");
assertThat(authProperties.get("OAuthClientId")).isEqualTo("client");
assertThat(authProperties.get("OAuthClientSecret")).isEqualTo("secret");
}
@Test
public void testParseOverridePropsForRefreshTokenAuth() {
try {
String connectionString =
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "ProjectId=MyBigQueryProject;OAuthType=2;OAuthRefreshToken=token;"
+ "OAuthClientId=client;OAuthClientSecret=secret;"
+ "EndpointOverrides=Oauth2=https://oauth2-private.p.googleapis.com/token;";
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionString), null);
Map<String, String> overrideProperties =
DataSource.fromUrl(connectionString).getOverrideProperties();
UserCredentials userCredentials =
BigQueryJdbcOAuthUtility.getPreGeneratedRefreshTokenCredentials(
authProperties, overrideProperties, null);
assertThat(userCredentials.toBuilder().getTokenServerUri())
.isEqualTo(URI.create("https://oauth2-private.p.googleapis.com/token"));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
@Test
public void testParseBYOIDProps() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:433;OAuthType=4;"
+ "ProjectId=MyBigQueryProject;"
+ "BYOID_AudienceUri=//iam.googleapis.com/locations/global/workforcePools/pool-id/providers/provider-id;"
+ "BYOID_PoolUserProject=workforceProjectNumber;"
+ "BYOID_CredentialSource={\"file\": \"C:\\\\Token.txt\"};"
+ "BYOID_SA_Impersonation_Uri=testSA;"
+ "BYOID_SubjectTokenType=urn:ietf:params:oauth:tokentype:jwt;"
+ "BYOID_TokenUri=https://testuri.com/v1/token"),
null);
assertThat(result.get("BYOID_AudienceUri"))
.isEqualTo(
"//iam.googleapis.com/locations/global/workforcePools/pool-id/providers/provider-id");
assertThat(result.get("BYOID_PoolUserProject")).isEqualTo("workforceProjectNumber");
assertThat(result.get("BYOID_CredentialSource")).isEqualTo("{\"file\": \"C:\\\\Token.txt\"}");
assertThat(result.get("BYOID_SA_Impersonation_Uri")).isEqualTo("testSA");
assertThat(result.get("BYOID_SubjectTokenType"))
.isEqualTo("urn:ietf:params:oauth:tokentype:jwt");
assertThat(result.get("BYOID_TokenUri")).isEqualTo("https://testuri.com/v1/token");
}
@Test
public void testParseUserImpersonationDefault() {
String connectionUri =
getUriOAuthServiceAccount()
.append("ServiceAccountImpersonationEmail", "impersonated")
.toString();
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(DataSource.fromUrl(connectionUri), "");
assertEquals(
"impersonated",
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_EMAIL_PROPERTY_NAME));
assertEquals(
BigQueryJdbcOAuthUtility.BIGQUERY_SCOPE,
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_SCOPES_PROPERTY_NAME));
assertEquals(
BigQueryJdbcUrlUtility.DEFAULT_OAUTH_SA_IMPERSONATION_TOKEN_LIFETIME_VALUE,
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_TOKEN_LIFETIME_PROPERTY_NAME));
}
@Test
public void testParseUserImpersonationNonDefault() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
getUriOAuthServiceAccount()
.append("ServiceAccountImpersonationEmail", "impersonated")
.append("ServiceAccountImpersonationScopes", "scopes")
.append("ServiceAccountImpersonationTokenLifetime", 300)
.toString()),
"");
assertEquals(
"impersonated",
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_EMAIL_PROPERTY_NAME));
assertEquals(
"scopes", result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_SCOPES_PROPERTY_NAME));
assertEquals(
"300",
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_TOKEN_LIFETIME_PROPERTY_NAME));
}
@Test
public void testParseUserImpersonationForADC() {
Map<String, String> result =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
+ "ServiceAccountImpersonationEmail=impersonated@email.com;"),
"");
assertEquals("APPLICATION_DEFAULT_CREDENTIALS", result.get("OAuthType"));
assertEquals(
"impersonated@email.com",
result.get(BigQueryJdbcUrlUtility.OAUTH_SA_IMPERSONATION_EMAIL_PROPERTY_NAME));
}
@Test
public void testGetServiceAccountImpersonatedCredentialsForADC() throws Exception {
GoogleCredentials dummySourceCredentials = GoogleCredentials.newBuilder().build();
try (org.mockito.MockedStatic<GoogleCredentials> mockedCreds =
org.mockito.Mockito.mockStatic(GoogleCredentials.class)) {
mockedCreds.when(GoogleCredentials::getApplicationDefault).thenReturn(dummySourceCredentials);
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
"jdbc:bigquery://https://www.googleapis.com/bigquery/v2:443;"
+ "OAuthType=3;ProjectId=MyBigQueryProject;"
+ "ServiceAccountImpersonationEmail=impersonated@email.com;"),
"");
GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(
authProperties, java.util.Collections.EMPTY_MAP, false, null);
assertThat(credentials).isInstanceOf(ImpersonatedCredentials.class);
assertThat(((ImpersonatedCredentials) credentials).getSourceCredentials())
.isEqualTo(dummySourceCredentials);
}
}
@Test
public void testGetServiceAccountImpersonatedCredentials() {
Map<String, String> authProperties =
BigQueryJdbcOAuthUtility.parseOAuthProperties(
DataSource.fromUrl(
getUriOAuthServiceAccount()
.append("ServiceAccountImpersonationEmail", "impersonated")
.toString()),
"");
GoogleCredentials credentials =
BigQueryJdbcOAuthUtility.getCredentials(authProperties, Collections.EMPTY_MAP, false, null);
assertThat(credentials).isInstanceOf(ImpersonatedCredentials.class);
}
@Test
public void testPrivateKeyFromPkcs8() {
PrivateKey pk = BigQueryJdbcOAuthUtility.privateKeyFromPkcs8(fake_pkcs8_key);
assertNotNull(pk);
}
@Test
public void testPrivateKeyFromPkcs8_wrong() {
PrivateKey pk = BigQueryJdbcOAuthUtility.privateKeyFromPkcs8("");
assertNull(pk);
}
// Command to generate key:
// keytool -genkey -alias privatekey -keyalg RSA -keysize 2048 -storepass notasecret \
// -keypass notasecret -storetype pkcs12 -keystore ./fake.p12
@Test
public void testPrivateKeyFromP12Bytes() {
URL resource = BigQueryJdbcOAuthUtilityTest.class.getResource("/fake.p12");
try {
PrivateKey pk =
BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes(
Files.readAllBytes(Paths.get(resource.toURI())), "notasecret");
assertNotNull(pk);
} catch (Exception e) {
assertTrue(false);
}
}
@Test
public void testPrivateKeyFromP12Bytes_wrong_password() {
URL resource = BigQueryJdbcOAuthUtilityTest.class.getResource("/fake.p12");
try {
PrivateKey pk =
BigQueryJdbcOAuthUtility.privateKeyFromP12Bytes(
Files.readAllBytes(Paths.get(resource.toURI())), "fake");
assertNull(pk);
} catch (Exception e) {
assertTrue(false);
}
}
}