|
31 | 31 |
|
32 | 32 | package com.google.auth.oauth2; |
33 | 33 |
|
| 34 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
34 | 35 | import static org.junit.jupiter.api.Assertions.assertTrue; |
35 | 36 | import static org.junit.jupiter.api.Assertions.fail; |
| 37 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
36 | 38 |
|
37 | 39 | import com.google.api.client.http.GenericUrl; |
38 | 40 | import com.google.api.client.http.HttpRequest; |
39 | 41 | import com.google.api.client.http.HttpRequestFactory; |
40 | 42 | import com.google.api.client.http.HttpResponse; |
| 43 | +import com.google.api.client.http.HttpTransport; |
41 | 44 | import com.google.api.client.http.UrlEncodedContent; |
42 | 45 | import com.google.api.client.http.javanet.NetHttpTransport; |
43 | 46 | import com.google.api.client.json.GenericJson; |
|
46 | 49 | import com.google.api.client.json.gson.GsonFactory; |
47 | 50 | import com.google.api.client.util.GenericData; |
48 | 51 | import com.google.auth.http.HttpCredentialsAdapter; |
| 52 | +import com.google.auth.http.HttpTransportFactory; |
| 53 | +import com.google.auth.mtls.MtlsHttpTransportFactory; |
| 54 | +import com.google.auth.mtls.X509Provider; |
49 | 55 | import com.google.auth.oauth2.ExternalAccountCredentials.SubjectTokenTypes; |
50 | 56 | import java.io.ByteArrayInputStream; |
51 | 57 | import java.io.File; |
52 | 58 | import java.io.FileInputStream; |
53 | 59 | import java.io.IOException; |
54 | 60 | import java.nio.charset.StandardCharsets; |
| 61 | +import java.security.KeyStore; |
55 | 62 | import java.time.Instant; |
56 | 63 | import java.util.HashMap; |
57 | 64 | import java.util.Map; |
@@ -281,6 +288,205 @@ void identityPoolCredentials_withProgrammaticAuth() throws IOException { |
281 | 288 | callGcs(identityPoolCredentials); |
282 | 289 | } |
283 | 290 |
|
| 291 | + /** |
| 292 | + * IdentityPoolCredentials (OIDC provider with certificate-bound workload and actor token): Uses |
| 293 | + * the service account to generate Google ID tokens for subject and actor tokens. Writes both |
| 294 | + * tokens to a temporary JSON file with subject_token and actor_token field names. Configures |
| 295 | + * certificate_config_location pointing to the certificate config. Exchanges the tokens over the |
| 296 | + * mTLS STS endpoint (https://sts.mtls.googleapis.com/v1/token) and calls GCS. |
| 297 | + */ |
| 298 | + @Test |
| 299 | + void identityPoolCredentials_withCertificateBoundWorkloadAndActorToken() throws Exception { |
| 300 | + String certConfigPath = getMtlsCertificateConfigPath(); |
| 301 | + String subjectToken = generateGoogleIdToken(OIDC_AUDIENCE); |
| 302 | + String actorToken = generateGoogleIdToken(OIDC_AUDIENCE); |
| 303 | + |
| 304 | + File tokenFile = |
| 305 | + File.createTempFile( |
| 306 | + "ITWorkloadIdentityFederation_cert_actor", /* suffix= */ null, /* directory= */ null); |
| 307 | + tokenFile.deleteOnExit(); |
| 308 | + try { |
| 309 | + GenericJson tokenJson = new GenericJson(); |
| 310 | + tokenJson.setFactory(OAuth2Utils.JSON_FACTORY); |
| 311 | + tokenJson.put("subject_token", subjectToken); |
| 312 | + tokenJson.put("actor_token", actorToken); |
| 313 | + |
| 314 | + OAuth2Utils.writeInputStreamToFile( |
| 315 | + new ByteArrayInputStream(tokenJson.toPrettyString().getBytes(StandardCharsets.UTF_8)), |
| 316 | + tokenFile.getAbsolutePath()); |
| 317 | + |
| 318 | + GenericJson config = new GenericJson(); |
| 319 | + config.put("type", "external_account"); |
| 320 | + config.put("audience", OIDC_AUDIENCE); |
| 321 | + config.put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 322 | + config.put("actor_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 323 | + config.put("token_url", "https://sts.mtls.googleapis.com/v1/token"); |
| 324 | + config.put( |
| 325 | + "service_account_impersonation_url", |
| 326 | + String.format( |
| 327 | + "https://iamcredentials.mtls.googleapis.com/v1/projects/-/serviceAccounts/%s:generateAccessToken", |
| 328 | + clientEmail)); |
| 329 | + |
| 330 | + GenericJson credentialSource = new GenericJson(); |
| 331 | + credentialSource.put("file", tokenFile.getAbsolutePath()); |
| 332 | + |
| 333 | + GenericJson format = new GenericJson(); |
| 334 | + format.put("type", "json"); |
| 335 | + format.put("subject_token_field_name", "subject_token"); |
| 336 | + format.put("actor_token_field_name", "actor_token"); |
| 337 | + credentialSource.put("format", format); |
| 338 | + |
| 339 | + GenericJson certificate = new GenericJson(); |
| 340 | + certificate.put("certificate_config_location", certConfigPath); |
| 341 | + credentialSource.put("certificate", certificate); |
| 342 | + |
| 343 | + config.put("credential_source", credentialSource); |
| 344 | + |
| 345 | + IdentityPoolCredentials identityPoolCredentials = |
| 346 | + (IdentityPoolCredentials) |
| 347 | + ExternalAccountCredentials.fromJson(config, OAuth2Utils.HTTP_TRANSPORT_FACTORY); |
| 348 | + |
| 349 | + KeyStore keyStore = new X509Provider(certConfigPath).getKeyStore(); |
| 350 | + HttpTransport mtlsTransport = new MtlsHttpTransportFactory(keyStore).create(); |
| 351 | + callGcs( |
| 352 | + identityPoolCredentials, |
| 353 | + mtlsTransport, |
| 354 | + "https://storage.mtls.googleapis.com/storage/v1/b/"); |
| 355 | + } finally { |
| 356 | + tokenFile.delete(); |
| 357 | + } |
| 358 | + } |
| 359 | + |
| 360 | + /** |
| 361 | + * IdentityPoolCredentials (OIDC provider with programmatic mTLS and actor token): Uses the |
| 362 | + * service account to generate Google ID tokens for subject and actor tokens via suppliers. |
| 363 | + * Configures mTLS transport using MtlsHttpTransportFactory with KeyStore loaded from the mTLS |
| 364 | + * certificate config. Exchanges the tokens over mTLS STS endpoint and calls GCS. |
| 365 | + */ |
| 366 | + @Test |
| 367 | + void identityPoolCredentials_withProgrammaticMtlsAndActorToken() throws Exception { |
| 368 | + IdentityPoolSubjectTokenSupplier tokenSupplier = |
| 369 | + context -> generateGoogleIdToken(OIDC_AUDIENCE); |
| 370 | + IdentityPoolActorTokenSupplier actorSupplier = context -> generateGoogleIdToken(OIDC_AUDIENCE); |
| 371 | + |
| 372 | + X509Provider x509Provider = new X509Provider(getMtlsCertificateConfigPath()); |
| 373 | + HttpTransportFactory transportFactory = |
| 374 | + new MtlsHttpTransportFactory(x509Provider.getKeyStore()); |
| 375 | + |
| 376 | + IdentityPoolCredentials credentials = |
| 377 | + IdentityPoolCredentials.newBuilder() |
| 378 | + .setSubjectTokenSupplier(tokenSupplier) |
| 379 | + .setActorTokenSupplier(actorSupplier) |
| 380 | + .setActorTokenType(SubjectTokenTypes.JWT.value) |
| 381 | + .setAudience(OIDC_AUDIENCE) |
| 382 | + .setSubjectTokenType(SubjectTokenTypes.JWT) |
| 383 | + .setTokenUrl("https://sts.mtls.googleapis.com/v1/token") |
| 384 | + .setServiceAccountImpersonationUrl( |
| 385 | + String.format( |
| 386 | + "https://iamcredentials.mtls.googleapis.com/v1/projects/-/serviceAccounts/%s:generateAccessToken", |
| 387 | + clientEmail)) |
| 388 | + .setHttpTransportFactory(transportFactory) |
| 389 | + .build(); |
| 390 | + |
| 391 | + callGcs( |
| 392 | + credentials, |
| 393 | + transportFactory.create(), |
| 394 | + "https://storage.mtls.googleapis.com/storage/v1/b/"); |
| 395 | + } |
| 396 | + |
| 397 | + /** |
| 398 | + * IdentityPoolCredentials (OIDC provider with certificate-bound workload and actor token, direct |
| 399 | + * STS): Exchanges tokens directly over the mTLS STS endpoint |
| 400 | + * (https://sts.mtls.googleapis.com/v1/token) without service account impersonation. |
| 401 | + */ |
| 402 | + @Test |
| 403 | + void identityPoolCredentials_directSts_withCertificateBoundWorkloadAndActorToken() |
| 404 | + throws IOException { |
| 405 | + String certConfigPath = getMtlsCertificateConfigPath(); |
| 406 | + String subjectToken = generateGoogleIdToken(OIDC_AUDIENCE); |
| 407 | + String actorToken = generateGoogleIdToken(OIDC_AUDIENCE); |
| 408 | + |
| 409 | + File tokenFile = |
| 410 | + File.createTempFile( |
| 411 | + "ITWorkloadIdentityFederation_direct_cert_actor", |
| 412 | + /* suffix= */ null, |
| 413 | + /* directory= */ null); |
| 414 | + tokenFile.deleteOnExit(); |
| 415 | + try { |
| 416 | + GenericJson tokenJson = new GenericJson(); |
| 417 | + tokenJson.setFactory(OAuth2Utils.JSON_FACTORY); |
| 418 | + tokenJson.put("subject_token", subjectToken); |
| 419 | + tokenJson.put("actor_token", actorToken); |
| 420 | + |
| 421 | + OAuth2Utils.writeInputStreamToFile( |
| 422 | + new ByteArrayInputStream(tokenJson.toPrettyString().getBytes(StandardCharsets.UTF_8)), |
| 423 | + tokenFile.getAbsolutePath()); |
| 424 | + |
| 425 | + GenericJson config = new GenericJson(); |
| 426 | + config.put("type", "external_account"); |
| 427 | + config.put("audience", OIDC_AUDIENCE); |
| 428 | + config.put("subject_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 429 | + config.put("actor_token_type", "urn:ietf:params:oauth:token-type:jwt"); |
| 430 | + config.put("token_url", "https://sts.mtls.googleapis.com/v1/token"); |
| 431 | + |
| 432 | + GenericJson credentialSource = new GenericJson(); |
| 433 | + credentialSource.put("file", tokenFile.getAbsolutePath()); |
| 434 | + |
| 435 | + GenericJson format = new GenericJson(); |
| 436 | + format.put("type", "json"); |
| 437 | + format.put("subject_token_field_name", "subject_token"); |
| 438 | + format.put("actor_token_field_name", "actor_token"); |
| 439 | + credentialSource.put("format", format); |
| 440 | + |
| 441 | + GenericJson certificate = new GenericJson(); |
| 442 | + certificate.put("certificate_config_location", certConfigPath); |
| 443 | + credentialSource.put("certificate", certificate); |
| 444 | + |
| 445 | + config.put("credential_source", credentialSource); |
| 446 | + |
| 447 | + IdentityPoolCredentials identityPoolCredentials = |
| 448 | + (IdentityPoolCredentials) |
| 449 | + ExternalAccountCredentials.fromJson(config, OAuth2Utils.HTTP_TRANSPORT_FACTORY); |
| 450 | + |
| 451 | + AccessToken accessToken = identityPoolCredentials.refreshAccessToken(); |
| 452 | + assertNotNull(accessToken); |
| 453 | + assertNotNull(accessToken.getTokenValue()); |
| 454 | + } finally { |
| 455 | + tokenFile.delete(); |
| 456 | + } |
| 457 | + } |
| 458 | + |
| 459 | + /** |
| 460 | + * IdentityPoolCredentials (OIDC provider with programmatic mTLS and actor token, direct STS): |
| 461 | + * Uses suppliers for subject and actor tokens, configuring MtlsHttpTransportFactory. Exchanges |
| 462 | + * directly over mTLS STS endpoint without service account impersonation. |
| 463 | + */ |
| 464 | + @Test |
| 465 | + void identityPoolCredentials_directSts_withProgrammaticMtlsAndActorToken() throws Exception { |
| 466 | + IdentityPoolSubjectTokenSupplier tokenSupplier = |
| 467 | + context -> generateGoogleIdToken(OIDC_AUDIENCE); |
| 468 | + IdentityPoolActorTokenSupplier actorSupplier = context -> generateGoogleIdToken(OIDC_AUDIENCE); |
| 469 | + |
| 470 | + X509Provider x509Provider = new X509Provider(getMtlsCertificateConfigPath()); |
| 471 | + HttpTransportFactory transportFactory = |
| 472 | + new MtlsHttpTransportFactory(x509Provider.getKeyStore()); |
| 473 | + |
| 474 | + IdentityPoolCredentials credentials = |
| 475 | + IdentityPoolCredentials.newBuilder() |
| 476 | + .setSubjectTokenSupplier(tokenSupplier) |
| 477 | + .setActorTokenSupplier(actorSupplier) |
| 478 | + .setActorTokenType(SubjectTokenTypes.JWT.value) |
| 479 | + .setAudience(OIDC_AUDIENCE) |
| 480 | + .setSubjectTokenType(SubjectTokenTypes.JWT) |
| 481 | + .setTokenUrl("https://sts.mtls.googleapis.com/v1/token") |
| 482 | + .setHttpTransportFactory(transportFactory) |
| 483 | + .build(); |
| 484 | + |
| 485 | + AccessToken accessToken = credentials.refreshAccessToken(); |
| 486 | + assertNotNull(accessToken); |
| 487 | + assertNotNull(accessToken.getTokenValue()); |
| 488 | + } |
| 489 | + |
284 | 490 | private GenericJson buildIdentityPoolCredentialConfig() throws IOException { |
285 | 491 | String idToken = generateGoogleIdToken(OIDC_AUDIENCE); |
286 | 492 |
|
@@ -382,24 +588,41 @@ private GenericJson buildAwsCredentialConfig() { |
382 | 588 | return config; |
383 | 589 | } |
384 | 590 |
|
| 591 | + private String getMtlsCertificateConfigPath() { |
| 592 | + String certConfigPath = System.getenv("GOOGLE_API_CERTIFICATE_CONFIG"); |
| 593 | + assumeTrue( |
| 594 | + certConfigPath != null && !certConfigPath.isEmpty(), |
| 595 | + "Skipping mTLS test: GOOGLE_API_CERTIFICATE_CONFIG env variable is not set."); |
| 596 | + return certConfigPath; |
| 597 | + } |
| 598 | + |
385 | 599 | private void callGcs(GoogleCredentials credentials) throws IOException { |
| 600 | + callGcs(credentials, new NetHttpTransport(), "https://storage.googleapis.com/storage/v1/b/"); |
| 601 | + } |
| 602 | + |
| 603 | + private void callGcs( |
| 604 | + GoogleCredentials credentials, HttpTransport transport, String storageBaseUrl) |
| 605 | + throws IOException { |
386 | 606 | String bucketName = System.getenv("GCS_BUCKET"); |
387 | 607 | if (bucketName == null) { |
388 | 608 | fail("GCS bucket name not set through GCS_BUCKET env variable."); |
389 | 609 | } |
390 | 610 |
|
391 | | - String url = "https://storage.googleapis.com/storage/v1/b/" + bucketName; |
| 611 | + String url = storageBaseUrl + bucketName; |
392 | 612 |
|
393 | 613 | HttpCredentialsAdapter credentialsAdapter = new HttpCredentialsAdapter(credentials); |
394 | | - HttpRequestFactory requestFactory = |
395 | | - new NetHttpTransport().createRequestFactory(credentialsAdapter); |
| 614 | + HttpRequestFactory requestFactory = transport.createRequestFactory(credentialsAdapter); |
396 | 615 | HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url)); |
397 | 616 |
|
398 | 617 | JsonObjectParser parser = new JsonObjectParser(GsonFactory.getDefaultInstance()); |
399 | 618 | request.setParser(parser); |
400 | 619 |
|
401 | 620 | HttpResponse response = request.execute(); |
402 | | - assertTrue(response.isSuccessStatusCode()); |
| 621 | + try { |
| 622 | + assertTrue(response.isSuccessStatusCode()); |
| 623 | + } finally { |
| 624 | + response.disconnect(); |
| 625 | + } |
403 | 626 | } |
404 | 627 |
|
405 | 628 | /** |
|
0 commit comments