diff --git a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java index 53cc57fa7..10fa111d4 100644 --- a/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java +++ b/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java @@ -109,13 +109,23 @@ protected AbstractGoogleClient(Builder builder) { * Universe Domain Env Var if set, 3. Default to the Google Default Universe */ private String determineUniverseDomain(Builder builder) { - String resolvedUniverseDomain = builder.universeDomain; + return determineUniverseDomain( + builder.universeDomain, System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN)); + } + + @VisibleForTesting + static String determineUniverseDomain(String configuredUniverseDomain, String envUniverseDomain) { + String resolvedUniverseDomain = configuredUniverseDomain; + if (resolvedUniverseDomain == null) { + resolvedUniverseDomain = envUniverseDomain; + } if (resolvedUniverseDomain == null) { - resolvedUniverseDomain = System.getenv(GOOGLE_CLOUD_UNIVERSE_DOMAIN); + return Credentials.GOOGLE_DEFAULT_UNIVERSE; } - return resolvedUniverseDomain == null - ? Credentials.GOOGLE_DEFAULT_UNIVERSE - : resolvedUniverseDomain; + if (resolvedUniverseDomain.isEmpty()) { + throw new IllegalArgumentException("The universe domain value cannot be empty."); + } + return resolvedUniverseDomain; } /** @@ -130,14 +140,12 @@ private String determineEndpoint(Builder builder) { "mTLS is not supported in any universe other than googleapis.com"); } // If the serviceName is null, we cannot construct a valid resolved endpoint. Simply return - // the rootUrl as this was custom rootUrl passed in. - if (builder.isUserConfiguredEndpoint || builder.serviceName == null) { + // the rootUrl as this was custom rootUrl passed in. An mTLS rootUrl is also returned as is + // because mTLS is only supported in the default universe. + if (builder.isUserConfiguredEndpoint || builder.serviceName == null || mtlsEnabled) { return builder.rootUrl; } - if (mtlsEnabled) { - return "https://" + builder.serviceName + ".mtls." + universeDomain + "/"; - } - return "https://" + builder.serviceName + "." + universeDomain + "/"; + return "https://" + builder.serviceName + "." + universeDomain + builder.port + "/"; } /** @@ -410,10 +418,11 @@ public abstract static class Builder { /** * Regex pattern to check if the URL passed in matches the default endpoint configured from a - * discovery doc. Follows the format of `https://{serviceName}(.mtls).googleapis.com/` + * discovery doc. Follows the format of `https://{serviceName}(.mtls).googleapis.com(:{port})/` */ Pattern defaultEndpointRegex = - Pattern.compile("https://([a-zA-Z]*)(\\.mtls)?\\.googleapis.com/?"); + Pattern.compile( + "https://([a-zA-Z0-9-]+(?:\\.[a-zA-Z0-9-]+)*?)(\\.mtls)?\\.googleapis\\.com(:\\d+)?/?"); /** * Whether the user has configured an endpoint via {@link #setRootUrl(String)}. This is added in @@ -432,6 +441,9 @@ public abstract static class Builder { /** The parsed serviceName value from the rootUrl from the Discovery Doc. */ String serviceName; + /** Optional port (including leading colon) parsed from the default rootUrl. */ + String port = ""; + /** * Returns an instance of a new builder. * @@ -458,6 +470,9 @@ protected Builder( // a custom endpoint this.isUserConfiguredEndpoint = !matches; this.serviceName = matches ? matcher.group(1) : null; + if (matches && matcher.group(3) != null) { + this.port = matcher.group(3); + } } /** Builds a new instance of {@link AbstractGoogleClient}. */ diff --git a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java index bcb6a3775..805448dba 100644 --- a/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java +++ b/google-api-client/src/test/java/com/google/api/client/googleapis/services/AbstractGoogleClientTest.java @@ -353,7 +353,133 @@ public void testIsUserSetEndpoint_regionalEndpoint() { JSON_OBJECT_PARSER, null) .setApplicationName("Test Application"); - assertTrue(clientBuilder.isUserConfiguredEndpoint); + + assertFalse(clientBuilder.isUserConfiguredEndpoint); + assertEquals("us-east-4.coolservice", clientBuilder.getServiceName()); + } + + @Test + public void testBuild_serviceNameWithDigitsAndCustomUniverseDomain_rewritesRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, "https://area120tables.googleapis.com/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://area120tables.random.com/", client.getRootUrl()); + } + + @Test + public void testBuild_serviceNameWithHyphensAndCustomUniverseDomain_rewritesRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, + "https://admin-remotebuildexecution.googleapis.com/", + "", + JSON_OBJECT_PARSER, + null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://admin-remotebuildexecution.random.com/", client.getRootUrl()); + } + + @Test + public void testBuild_multiLabelSubdomainAndCustomUniverseDomain_rewritesRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, + "https://storage.us-central1.rep.googleapis.com/", + "", + JSON_OBJECT_PARSER, + null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://storage.us-central1.rep.random.com/", client.getRootUrl()); + } + + @Test + public void testBuild_rootUrlWithPortAndCustomUniverseDomain_rewritesHostAndPreservesPort() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, "https://searchads360.googleapis.com:443/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://searchads360.random.com:443/", client.getRootUrl()); + } + + @Test + public void testBuild_mtlsSubdomainWithPortInDefaultUniverse_preservesRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, + "https://storage.us-central1.rep.mtls.googleapis.com:443/", + "", + JSON_OBJECT_PARSER, + null) + .setApplicationName("Test Application"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://storage.us-central1.rep.mtls.googleapis.com:443/", client.getRootUrl()); + } + + @Test + public void testBuild_emptyServiceNameWithCustomUniverseDomain_doesNotRewriteRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, "https://.googleapis.com/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://.googleapis.com/", client.getRootUrl()); + } + + @Test + public void testBuild_unescapedDotInRootUrlWithCustomUniverseDomain_doesNotRewriteRootUrl() { + AbstractGoogleClient.Builder builder = + new MockGoogleClient.Builder( + TRANSPORT, "https://test.googleapisXcom/", "", JSON_OBJECT_PARSER, null) + .setApplicationName("Test Application") + .setUniverseDomain("random.com"); + + AbstractGoogleClient client = builder.build(); + + assertEquals("https://test.googleapisXcom/", client.getRootUrl()); + } + + @Test + public void testDetermineUniverseDomain_emptyEnvVar_throwsIllegalArgumentException() { + IllegalArgumentException exception = + assertThrows( + IllegalArgumentException.class, + new ThrowingRunnable() { + @Override + public void run() { + AbstractGoogleClient.determineUniverseDomain(null, ""); + } + }); + + assertEquals("The universe domain value cannot be empty.", exception.getMessage()); + } + + @Test + public void testDetermineUniverseDomain_configuredValueOverridesEmptyEnvVar_returnsConfigured() { + String universeDomain = AbstractGoogleClient.determineUniverseDomain("random.com", ""); + + assertEquals("random.com", universeDomain); } @Test