-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Universe Domain to Java-Core (#2329)
* feat: Add Java-Core Universe Domain changes * chore: Move validate universe domain logic to ServiceOptions * chore: Add javadocs * chore: Add tests * chore: Fix lint issues * chore: Add project id to tests * chore: Fix format issues * chore: Address PR comments * chore: Update Apiary to return rootHostUrl * chore: Use Google Auth Library v1.21.0 * chore: Add tests for normalizeEndpoint() * chore: Address PR comments * chore: Address PR comments * chore: Fix comments * chore: Address PR comments * chore: Address PR comments * chore: Add links * chore: Add format to match DEFAULT_HOST * chore: Fix failing tests * chore: Update javadocs * chore: Remove www. prefix
- Loading branch information
Showing
2 changed files
with
253 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
import static org.junit.Assert.assertNotEquals; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertThrows; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
|
@@ -55,6 +56,7 @@ public class ServiceOptionsTest { | |
private static GoogleCredentials credentials; | ||
private static GoogleCredentials credentialsWithProjectId; | ||
private static GoogleCredentials credentialsWithQuotaProject; | ||
private static GoogleCredentials credentialsNotInGDU; | ||
|
||
private static final String JSON_KEY = | ||
"{\n" | ||
|
@@ -81,7 +83,8 @@ public class ServiceOptionsTest { | |
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" | ||
+ " \"client_email\": \"[email protected]\",\n" | ||
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" | ||
+ " \"type\": \"service_account\"\n" | ||
+ " \"type\": \"service_account\",\n" | ||
+ " \"universe_domain\": \"googleapis.com\"\n" | ||
+ "}"; | ||
|
||
private static final String JSON_KEY_PROJECT_ID = | ||
|
@@ -110,7 +113,8 @@ public class ServiceOptionsTest { | |
+ " \"project_id\": \"someprojectid\",\n" | ||
+ " \"client_email\": \"[email protected]\",\n" | ||
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" | ||
+ " \"type\": \"service_account\"\n" | ||
+ " \"type\": \"service_account\",\n" | ||
+ " \"universe_domain\": \"googleapis.com\"\n" | ||
+ "}"; | ||
|
||
private static final String JSON_KEY_QUOTA_PROJECT_ID = | ||
|
@@ -140,13 +144,45 @@ public class ServiceOptionsTest { | |
+ " \"client_email\": \"[email protected]\",\n" | ||
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" | ||
+ " \"type\": \"service_account\",\n" | ||
+ " \"quota_project_id\": \"some-quota-project-id\"\n" | ||
+ " \"quota_project_id\": \"some-quota-project-id\",\n" | ||
+ " \"universe_domain\": \"googleapis.com\"\n" | ||
+ "}"; | ||
|
||
// Key added by copying the keys above and adding in the universe domain field | ||
private static final String JSON_KEY_NON_GDU = | ||
"{\n" | ||
+ " \"private_key_id\": \"somekeyid\",\n" | ||
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" | ||
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" | ||
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" | ||
+ "Bhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nknddadwkwewcVxHFhcZJO+XWf6ofLUXpRwiTZ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HBhhhp6mCcRpdQjV38H7JsyJ7lih/oNjECgYAt\\nkndj5uNl5SiuVxHFhcZJO+XWf6ofLUregtevZakGMn8EE1uVa" | ||
+ "2AY7eafmoU/nZPT\\n00YB0TBATdCbn/nBSuKDESkhSg9s2GEKQZG5hBmL5uCMfo09z3SfxZIhJdlerreP\\nJ7gSi" | ||
+ "dI12N+EZxYd4xIJh/HFDgp7RRO87f+WJkofMQKBgGTnClK1VMaCRbJZPriw\\nEfeFCoOX75MxKwXs6xgrw4W//AYG" | ||
+ "GUjDt83lD6AZP6tws7gJ2IwY/qP7+lyhjEqN\\nHtfPZRGFkGZsdaksdlaksd323423d+15/UvrlRSFPNj1tWQmNKk" | ||
+ "XyRDW4IG1Oa2p\\nrALStNBx5Y9t0/LQnFI4w3aG\\n-----END PRIVATE KEY-----\\n\",\n" | ||
+ " \"client_email\": \"[email protected]\",\n" | ||
+ " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" | ||
+ " \"type\": \"service_account\",\n" | ||
+ " \"universe_domain\": \"random.com\"\n" | ||
+ "}"; | ||
|
||
static { | ||
credentials = loadCredentials(JSON_KEY); | ||
credentialsWithProjectId = loadCredentials(JSON_KEY_PROJECT_ID); | ||
credentialsWithQuotaProject = loadCredentials(JSON_KEY_QUOTA_PROJECT_ID); | ||
credentialsNotInGDU = loadCredentials(JSON_KEY_NON_GDU); | ||
} | ||
|
||
static GoogleCredentials loadCredentials(String credentialFile) { | ||
|
@@ -471,6 +507,129 @@ public void testResponseHeaderDoesNotContainMetaDataFlavor() throws Exception { | |
assertThat(ServiceOptions.headerContainsMetadataFlavor(httpResponse)).isFalse(); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedEndpoint_noUniverseDomain() { | ||
TestServiceOptions options = TestServiceOptions.newBuilder().setProjectId("project-id").build(); | ||
assertThat(options.getResolvedHost("service")).isEqualTo("https://service.googleapis.com"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedEndpoint_emptyUniverseDomain() { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder().setUniverseDomain("").setProjectId("project-id").build(); | ||
IllegalArgumentException exception = | ||
assertThrows(IllegalArgumentException.class, () -> options.getResolvedHost("service")); | ||
assertThat(exception.getMessage()).isEqualTo("The universe domain cannot be empty"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedEndpoint_customUniverseDomain() { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setUniverseDomain("test.com") | ||
.setProjectId("project-id") | ||
.build(); | ||
assertThat(options.getResolvedHost("service")).isEqualTo("https://service.test.com"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedEndpoint_customUniverseDomain_customHost() { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setUniverseDomain("test.com") | ||
.setHost("https://service.random.com/") | ||
.setProjectId("project-id") | ||
.build(); | ||
assertThat(options.getResolvedHost("service")).isEqualTo("https://service.random.com/"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedApiaryHost_noUniverseDomain() { | ||
TestServiceOptions options = TestServiceOptions.newBuilder().setProjectId("project-id").build(); | ||
assertThat(options.getResolvedApiaryHost("service")) | ||
.isEqualTo("https://service.googleapis.com/"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedApiaryHost_customUniverseDomain_noHost() { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setUniverseDomain("test.com") | ||
.setHost(null) | ||
.setProjectId("project-id") | ||
.build(); | ||
assertThat(options.getResolvedApiaryHost("service")).isEqualTo("https://service.test.com/"); | ||
} | ||
|
||
@Test | ||
public void testGetResolvedApiaryHost_customUniverseDomain_customHost() { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setUniverseDomain("test.com") | ||
.setHost("https://service.random.com") | ||
.setProjectId("project-id") | ||
.build(); | ||
assertThat(options.getResolvedApiaryHost("service")).isEqualTo("https://service.test.com/"); | ||
} | ||
|
||
// No User Configuration = GDU, Default Credentials = GDU | ||
@Test | ||
public void testIsValidUniverseDomain_noUserUniverseDomainConfig_defaultCredentials() | ||
throws IOException { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setProjectId("project-id") | ||
.setHost("https://test.random.com") | ||
.setCredentials(credentials) | ||
.build(); | ||
assertThat(options.hasValidUniverseDomain()).isTrue(); | ||
} | ||
|
||
// No User Configuration = GDU, non Default Credentials = random.com | ||
// non-GDU Credentials could be any domain, the tests use random.com | ||
@Test | ||
public void testIsValidUniverseDomain_noUserUniverseDomainConfig_nonGDUCredentials() | ||
throws IOException { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setProjectId("project-id") | ||
.setHost("https://test.random.com") | ||
.setCredentials(credentialsNotInGDU) | ||
.build(); | ||
assertThat(options.hasValidUniverseDomain()).isFalse(); | ||
} | ||
|
||
// User Configuration = random.com, Default Credentials = GDU | ||
// User Credentials could be set to any domain, the tests use random.com | ||
@Test | ||
public void testIsValidUniverseDomain_userUniverseDomainConfig_defaultCredentials() | ||
throws IOException { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setProjectId("project-id") | ||
.setHost("https://test.random.com") | ||
.setUniverseDomain("random.com") | ||
.setCredentials(credentials) | ||
.build(); | ||
assertThat(options.hasValidUniverseDomain()).isFalse(); | ||
} | ||
|
||
// User Configuration = random.com, non Default Credentials = random.com | ||
// User Credentials and non GDU Credentials could be set to any domain, | ||
// the tests use random.com | ||
@Test | ||
public void testIsValidUniverseDomain_userUniverseDomainConfig_nonGDUCredentials() | ||
throws IOException { | ||
TestServiceOptions options = | ||
TestServiceOptions.newBuilder() | ||
.setProjectId("project-id") | ||
.setHost("https://test.random.com") | ||
.setUniverseDomain("random.com") | ||
.setCredentials(credentialsNotInGDU) | ||
.build(); | ||
assertThat(options.hasValidUniverseDomain()).isTrue(); | ||
} | ||
|
||
private HttpResponse createHttpResponseWithHeader(final Multimap<String, String> headers) | ||
throws Exception { | ||
HttpTransport mockHttpTransport = | ||
|