|
42 | 42 | import static org.junit.jupiter.api.Assertions.assertTrue; |
43 | 43 |
|
44 | 44 | import com.google.api.client.http.HttpTransport; |
| 45 | +import com.google.api.client.http.LowLevelHttpRequest; |
| 46 | +import com.google.api.client.http.LowLevelHttpResponse; |
45 | 47 | import com.google.api.client.json.GenericJson; |
| 48 | +import com.google.api.client.json.Json; |
46 | 49 | import com.google.api.client.json.JsonParser; |
| 50 | +import com.google.api.client.testing.http.MockHttpTransport; |
| 51 | +import com.google.api.client.testing.http.MockLowLevelHttpRequest; |
| 52 | +import com.google.api.client.testing.http.MockLowLevelHttpResponse; |
47 | 53 | import com.google.api.client.util.Clock; |
48 | 54 | import com.google.api.client.util.SecurityUtils; |
49 | 55 | import com.google.auth.TestUtils; |
|
59 | 65 | import java.math.BigDecimal; |
60 | 66 | import java.net.URI; |
61 | 67 | import java.security.KeyStore; |
| 68 | +import java.util.ArrayList; |
62 | 69 | import java.util.Arrays; |
63 | 70 | import java.util.Date; |
64 | 71 | import java.util.HashMap; |
@@ -634,6 +641,187 @@ void constructor_builder_defaultTokenUrlwithUniverseDomain() { |
634 | 641 | assertEquals("https://sts.testdomain.org/v1/token", credentials.getTokenUrl()); |
635 | 642 | } |
636 | 643 |
|
| 644 | + @Test |
| 645 | + void toBuilder_setUniverseDomain_updatesDefaultTokenUrl() { |
| 646 | + HashMap<String, Object> credentialSource = new HashMap<>(); |
| 647 | + credentialSource.put("file", "file"); |
| 648 | + |
| 649 | + IdentityPoolCredentials gduCredentials = |
| 650 | + IdentityPoolCredentials.newBuilder() |
| 651 | + .setHttpTransportFactory(transportFactory) |
| 652 | + .setAudience( |
| 653 | + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") |
| 654 | + .setSubjectTokenType("subjectTokenType") |
| 655 | + .setCredentialSource(new TestCredentialSource(credentialSource)) |
| 656 | + .build(); |
| 657 | + |
| 658 | + assertEquals(STS_URL, gduCredentials.getTokenUrl()); |
| 659 | + assertEquals(GOOGLE_DEFAULT_UNIVERSE, gduCredentials.getUniverseDomain()); |
| 660 | + |
| 661 | + // Retargeting a GDU credential must replace its defaulted sts.googleapis.com URL. |
| 662 | + IdentityPoolCredentials customUniverseCredentials = |
| 663 | + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 664 | + |
| 665 | + assertEquals("testdomain.org", customUniverseCredentials.getUniverseDomain()); |
| 666 | + assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); |
| 667 | + |
| 668 | + // Chained retargeting must also recompute because customUniverseCredentials still uses its |
| 669 | + // universe default. |
| 670 | + IdentityPoolCredentials secondUniverseCredentials = |
| 671 | + customUniverseCredentials.toBuilder().setUniverseDomain("other-universe.org").build(); |
| 672 | + |
| 673 | + assertEquals("other-universe.org", secondUniverseCredentials.getUniverseDomain()); |
| 674 | + assertEquals( |
| 675 | + "https://sts.other-universe.org/v1/token", secondUniverseCredentials.getTokenUrl()); |
| 676 | + } |
| 677 | + |
| 678 | + @Test |
| 679 | + void toBuilder_setUniverseDomain_updatesDefaultTokenUrlForAwsAndPluggableAuth() { |
| 680 | + // Verify other ExternalAccountCredentials subclasses inherit the same toBuilder() tokenUrl |
| 681 | + // reset. |
| 682 | + Map<String, Object> awsSourceMap = new HashMap<>(); |
| 683 | + awsSourceMap.put("environment_id", "aws1"); |
| 684 | + awsSourceMap.put( |
| 685 | + "regional_cred_verification_url", |
| 686 | + "https://sts.{region}.amazonaws.com?Action=GetCallerIdentity&Version=2011-06-15"); |
| 687 | + AwsCredentials awsCredentials = |
| 688 | + AwsCredentials.newBuilder() |
| 689 | + .setHttpTransportFactory(transportFactory) |
| 690 | + .setAudience("audience") |
| 691 | + .setSubjectTokenType("subjectTokenType") |
| 692 | + .setCredentialSource(new AwsCredentialSource(awsSourceMap)) |
| 693 | + .build(); |
| 694 | + AwsCredentials rebuiltAws = |
| 695 | + awsCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 696 | + assertEquals("https://sts.testdomain.org/v1/token", rebuiltAws.getTokenUrl()); |
| 697 | + |
| 698 | + Map<String, Object> executableMap = new HashMap<>(); |
| 699 | + executableMap.put("command", "/bin/echo token"); |
| 700 | + Map<String, Object> pluggableSourceMap = new HashMap<>(); |
| 701 | + pluggableSourceMap.put("executable", executableMap); |
| 702 | + PluggableAuthCredentials pluggableCredentials = |
| 703 | + PluggableAuthCredentials.newBuilder() |
| 704 | + .setHttpTransportFactory(transportFactory) |
| 705 | + .setAudience("audience") |
| 706 | + .setSubjectTokenType("subjectTokenType") |
| 707 | + .setCredentialSource(new PluggableAuthCredentialSource(pluggableSourceMap)) |
| 708 | + .build(); |
| 709 | + PluggableAuthCredentials rebuiltPluggable = |
| 710 | + pluggableCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 711 | + assertEquals("https://sts.testdomain.org/v1/token", rebuiltPluggable.getTokenUrl()); |
| 712 | + } |
| 713 | + |
| 714 | + @Test |
| 715 | + void toBuilder_fromGduJson_setUniverseDomain_updatesTokenUrl() throws IOException { |
| 716 | + // Standard gcloud credential files include token_url=https://sts.googleapis.com/v1/token; |
| 717 | + // switching universe domain via toBuilder() must still recompute it. |
| 718 | + GenericJson gduJson = buildJsonIdentityPoolCredential(); |
| 719 | + assertEquals(STS_URL, gduJson.get("token_url")); |
| 720 | + |
| 721 | + ExternalAccountCredentials gduCredentials = |
| 722 | + ExternalAccountCredentials.fromJson(gduJson, transportFactory); |
| 723 | + ExternalAccountCredentials customUniverseCredentials = |
| 724 | + (ExternalAccountCredentials) |
| 725 | + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 726 | + |
| 727 | + assertEquals("testdomain.org", customUniverseCredentials.getUniverseDomain()); |
| 728 | + assertEquals("https://sts.testdomain.org/v1/token", customUniverseCredentials.getTokenUrl()); |
| 729 | + } |
| 730 | + |
| 731 | + @Test |
| 732 | + void toBuilder_setUniverseDomain_preservesCustomTokenUrl() { |
| 733 | + HashMap<String, Object> credentialSource = new HashMap<>(); |
| 734 | + credentialSource.put("file", "file"); |
| 735 | + |
| 736 | + // Inherited custom URLs (e.g. mTLS STS) must not be overwritten when changing universe domain. |
| 737 | + IdentityPoolCredentials customUrlCredentials = |
| 738 | + IdentityPoolCredentials.newBuilder() |
| 739 | + .setHttpTransportFactory(transportFactory) |
| 740 | + .setAudience( |
| 741 | + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") |
| 742 | + .setSubjectTokenType("subjectTokenType") |
| 743 | + .setTokenUrl(STS_MTLS_URL) |
| 744 | + .setCredentialSource(new TestCredentialSource(credentialSource)) |
| 745 | + .build(); |
| 746 | + |
| 747 | + IdentityPoolCredentials rebuiltWithInheritedCustomUrl = |
| 748 | + customUrlCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 749 | + assertEquals("testdomain.org", rebuiltWithInheritedCustomUrl.getUniverseDomain()); |
| 750 | + assertEquals(STS_MTLS_URL, rebuiltWithInheritedCustomUrl.getTokenUrl()); |
| 751 | + |
| 752 | + IdentityPoolCredentials gduCredentials = |
| 753 | + IdentityPoolCredentials.newBuilder() |
| 754 | + .setHttpTransportFactory(transportFactory) |
| 755 | + .setAudience( |
| 756 | + "//iam.googleapis.com/locations/global/workforcePools/pool/providers/provider") |
| 757 | + .setSubjectTokenType("subjectTokenType") |
| 758 | + .setCredentialSource(new TestCredentialSource(credentialSource)) |
| 759 | + .build(); |
| 760 | + |
| 761 | + // Explicit setTokenUrl() on toBuilder() must take precedence in either setter order. |
| 762 | + String customUrl = "https://sts.custom.example.com/v1/token"; |
| 763 | + IdentityPoolCredentials setTokenUrlBeforeUniverse = |
| 764 | + gduCredentials.toBuilder() |
| 765 | + .setTokenUrl(customUrl) |
| 766 | + .setUniverseDomain("testdomain.org") |
| 767 | + .build(); |
| 768 | + assertEquals(customUrl, setTokenUrlBeforeUniverse.getTokenUrl()); |
| 769 | + |
| 770 | + IdentityPoolCredentials setTokenUrlAfterUniverse = |
| 771 | + gduCredentials.toBuilder() |
| 772 | + .setUniverseDomain("testdomain.org") |
| 773 | + .setTokenUrl(customUrl) |
| 774 | + .build(); |
| 775 | + assertEquals(customUrl, setTokenUrlAfterUniverse.getTokenUrl()); |
| 776 | + } |
| 777 | + |
| 778 | + @Test |
| 779 | + void toBuilder_setUniverseDomain_exchangesTokenAgainstUpdatedStsUrl() throws IOException { |
| 780 | + // End-to-end check that STS requests are actually sent to the recomputed universe URL. |
| 781 | + List<String> requestedUrls = new ArrayList<>(); |
| 782 | + HttpTransportFactory recordingTransportFactory = |
| 783 | + () -> |
| 784 | + new MockHttpTransport() { |
| 785 | + @Override |
| 786 | + public LowLevelHttpRequest buildRequest(String method, String url) { |
| 787 | + requestedUrls.add(url); |
| 788 | + return new MockLowLevelHttpRequest(url) { |
| 789 | + @Override |
| 790 | + public LowLevelHttpResponse execute() { |
| 791 | + return new MockLowLevelHttpResponse() |
| 792 | + .setContentType(Json.MEDIA_TYPE) |
| 793 | + .setContent( |
| 794 | + "{\"access_token\":\"customUniverseAccessToken\"," |
| 795 | + + "\"issued_token_type\":" |
| 796 | + + "\"urn:ietf:params:oauth:token-type:access_token\"," |
| 797 | + + "\"token_type\":\"Bearer\",\"expires_in\":3600}"); |
| 798 | + } |
| 799 | + }; |
| 800 | + } |
| 801 | + }; |
| 802 | + |
| 803 | + TestExternalAccountCredentials gduCredentials = |
| 804 | + (TestExternalAccountCredentials) |
| 805 | + TestExternalAccountCredentials.newBuilder() |
| 806 | + .setHttpTransportFactory(recordingTransportFactory) |
| 807 | + .setAudience("audience") |
| 808 | + .setSubjectTokenType("subjectTokenType") |
| 809 | + .setCredentialSource(new TestCredentialSource(FILE_CREDENTIAL_SOURCE_MAP)) |
| 810 | + .build(); |
| 811 | + |
| 812 | + TestExternalAccountCredentials customUniverseCredentials = |
| 813 | + (TestExternalAccountCredentials) |
| 814 | + gduCredentials.toBuilder().setUniverseDomain("testdomain.org").build(); |
| 815 | + |
| 816 | + StsTokenExchangeRequest stsRequest = |
| 817 | + StsTokenExchangeRequest.newBuilder("subjectToken", "subjectTokenType").build(); |
| 818 | + AccessToken accessToken = |
| 819 | + customUniverseCredentials.exchangeExternalCredentialForAccessToken(stsRequest); |
| 820 | + |
| 821 | + assertEquals("customUniverseAccessToken", accessToken.getTokenValue()); |
| 822 | + assertEquals(Arrays.asList("https://sts.testdomain.org/v1/token"), requestedUrls); |
| 823 | + } |
| 824 | + |
637 | 825 | @Test |
638 | 826 | void constructor_builder_subjectTokenTypeEnum() { |
639 | 827 | HashMap<String, Object> credentialSource = new HashMap<>(); |
|
0 commit comments