-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add Universe Domain to Java-Core #2329
Changes from all commits
0397cc3
9ba6313
b0e7614
6cc86ac
c68e996
2c23732
db4965a
3d63297
5f8d48f
881b5c1
8fcf832
08bbe56
8e878f9
acb94a8
f4eaad3
6709f2c
ac02baf
793211c
5a2e6db
876b864
a6f3234
73db273
60c096b
f63e8cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you add source code comment how you generated this value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't generate this value. I copied it from above and added a new json entry for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add that "copied it from above and added a new json entry for universe_domain"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
"{\n" | ||
+ " \"private_key_id\": \"somekeyid\",\n" | ||
+ " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggS" | ||
+ "kAgEAAoIBAQC+K2hSuFpAdrJI\\nnCgcDz2M7t7bjdlsadsasad+fvRSW6TjNQZ3p5LLQY1kSZRqBqylRkzteMOyHg" | ||
+ "aR\\n0Pmxh3ILCND5men43j3h4eDbrhQBuxfEMalkG92sL+PNQSETY2tnvXryOvmBRwa/\\nQP/9dJfIkIDJ9Fw9N4" | ||
+ "Bhhhp6mCcRpdQjV38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pAS/vVI4dpOepP8++7y1u\\ncoOvtreXCX6XqfrWDtKIvv0vjl" | ||
+ "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 = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling it as host is itchy. Fine but ensure to explain this to library owners.
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/net/URL.html#getHost()