Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.api.core.InternalApi;
import com.google.api.gax.core.GaxProperties;
import com.google.api.gax.retrying.RetrySettings;
import com.google.api.gax.rpc.EndpointContext;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.api.gax.rpc.HeaderProvider;
import com.google.api.gax.rpc.NoHeaderProvider;
Expand All @@ -49,6 +50,7 @@
import com.google.auth.oauth2.QuotaProjectIdProvider;
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.spi.ServiceRpcFactory;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
Expand Down Expand Up @@ -102,6 +104,7 @@ public abstract class ServiceOptions<
private final String projectId;
private final String universeDomain;
private final String host;
private volatile Boolean hasExplicitHost;
private final RetrySettings retrySettings;
private final String serviceRpcFactoryClassName;
private final String serviceFactoryClassName;
Expand Down Expand Up @@ -156,7 +159,10 @@ protected Builder() {}
protected Builder(ServiceOptions<ServiceT, OptionsT> options) {
projectId = options.projectId;
universeDomain = options.universeDomain;
host = options.host;
// Copy only an explicit host so toBuilder().setUniverseDomain(...) recomputes defaults.
if (options.hasExplicitHost()) {
host = options.host;
}
credentials = options.credentials;
retrySettings = options.retrySettings;
serviceFactory = options.serviceFactory;
Expand Down Expand Up @@ -221,10 +227,10 @@ public B setHost(String host) {
* Universe Domain value of `googleapis.com` and cloudasset.test.com would have a Universe
* Domain of `test.com`.
*
* <p>If this value is not set, the resolved UniverseDomain will default to `googleapis.com`.
* <p>If this value is not set, the {@code GOOGLE_CLOUD_UNIVERSE_DOMAIN} environment variable is
* used if set, otherwise the resolved UniverseDomain defaults to `googleapis.com`.
*
* @throws NullPointerException if {@code universeDomain} is {@code null}. The resolved
* universeDomain will be `googleapis.com` if this value is not set.
* @throws NullPointerException if {@code universeDomain} is {@code null}.
*/
public B setUniverseDomain(String universeDomain) {
this.universeDomain = checkNotNull(universeDomain);
Expand Down Expand Up @@ -362,7 +368,12 @@ protected ServiceOptions(
+ "or the environment. Please set a project ID using the builder.");
}
universeDomain = builder.universeDomain;
host = firstNonNull(builder.host, getDefaultHost());
hasExplicitHost = builder.host != null;
if (builder.host != null) {
host = builder.host;
} else {
host = getDefaultHost();
}
credentials = builder.credentials != null ? builder.credentials : defaultCredentials();
retrySettings = firstNonNull(builder.retrySettings, getDefaultRetrySettings());
serviceFactory =
Expand Down Expand Up @@ -658,6 +669,15 @@ public String getHost() {
return host;
}

/**
* Returns whether a host was explicitly configured via {@link Builder#setHost(String)} rather
* than defaulted by {@link #getDefaultHost()}.
*/
@InternalApi
public boolean hasExplicitHost() {
return Boolean.TRUE.equals(hasExplicitHost);
}

/** Returns the authentication credentials. */
public Credentials getCredentials() {
return credentials;
Expand Down Expand Up @@ -789,6 +809,9 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
input.defaultReadObject();
serviceFactory = newInstance(serviceFactoryClassName, ServiceFactory.class);
serviceRpcFactory = newInstance(serviceRpcFactoryClassName, ServiceRpcFactory.class);
if (hasExplicitHost == null) {
hasExplicitHost = !Objects.equals(host, getDefaultHost());
}
}

/**
Expand Down Expand Up @@ -898,28 +921,52 @@ public boolean getUseJwtAccessWithScope() {
return useJwtAccessWithScope;
}

/**
* Returns the resolved universe domain using the precedence:
*
* <ol>
* <li>Explicitly configured {@link #getUniverseDomain()}
* <li>{@code GOOGLE_CLOUD_UNIVERSE_DOMAIN} environment variable
* <li>Default {@link Credentials#GOOGLE_DEFAULT_UNIVERSE} ({@code googleapis.com})
* </ol>
*
* @throws IllegalArgumentException if the resolved universe domain is empty
*/
@InternalApi
public String resolveUniverseDomain() {
String resolved = universeDomain;
if (resolved == null) {
resolved = getUniverseDomainFromEnv();
}
if (resolved == null) {
return Credentials.GOOGLE_DEFAULT_UNIVERSE;
}
if (resolved.isEmpty()) {
throw new IllegalArgumentException("The universe domain cannot be empty");
}
return resolved;
}

@VisibleForTesting
String getUniverseDomainFromEnv() {
return System.getenv(EndpointContext.GOOGLE_CLOUD_UNIVERSE_DOMAIN);
}

/**
* Returns the resolved host for the Service to connect to Google Cloud
*
* <p>The resolved host will be in `https://{serviceName}.{resolvedUniverseDomain}` format. The
* resolvedUniverseDomain will be set to `googleapis.com` if universeDomain is null. The format is
* similar to the DEFAULT_HOST value in java-core.
* <p>The resolved host will be in `https://{serviceName}.{resolvedUniverseDomain}` format, where
* {@code resolvedUniverseDomain} is determined by {@link #resolveUniverseDomain()}. If the user
* explicitly configured a host other than {@code DEFAULT_HOST} via {@link
* Builder#setHost(String)}, that host is returned as-is.
*
* @see <a
* href="https://github.com/googleapis/google-cloud-java/blob/e50b96b70826f173e6d23278fea96c2af9b6e817/sdk-platform-java/java-core/google-cloud-core/src/main/java/com/google/cloud/ServiceOptions.java#L86">DEFAULT_HOST</a>
*/
@InternalApi
public String getResolvedHost(String serviceName) {
if (universeDomain != null && universeDomain.isEmpty()) {
throw new IllegalArgumentException("The universe domain cannot be empty");
}
String resolvedUniverseDomain =
universeDomain != null ? universeDomain : Credentials.GOOGLE_DEFAULT_UNIVERSE;
// The host value set to DEFAULT_HOST if the user didn't configure a host. If the
// user set a host the library uses that value, otherwise, construct the host for the user.
// The DEFAULT_HOST value is not a valid host for handwritten libraries and should be
// overriden to include the serviceName.
if (!DEFAULT_HOST.equals(host)) {
String resolvedUniverseDomain = resolveUniverseDomain();
if (hasExplicitHost() && !DEFAULT_HOST.equals(host)) {
return host;
}
return "https://" + serviceName + "." + resolvedUniverseDomain;
Expand All @@ -930,22 +977,20 @@ public String getResolvedHost(String serviceName) {
* removed in the future when Apiary clients can resolve their endpoints. Returns the host to be
* used as the rootUrl.
*
* <p>The resolved host will be in `https://{serviceName}.{resolvedUniverseDomain}/` format. The
* resolvedUniverseDomain will be set to `googleapis.com` if universeDomain is null.
*
* <p>The host value is set to DEFAULT_HOST if the user didn't configure a host. Returns the host
* value the user set, otherwise constructs the host for the user.
* <p>The resolved host will be in `https://{serviceName}.{resolvedUniverseDomain}/` format, where
* {@code resolvedUniverseDomain} is determined by {@link #resolveUniverseDomain()}. If the user
* explicitly configured a host other than {@code DEFAULT_HOST} via {@link
* Builder#setHost(String)}, that host is returned as-is.
*
* @see <a
* href="https://github.com/googleapis/google-api-java-client/blob/76765d5f9689be9d266a7d62fa6ffb4cabf701f5/google-api-client/src/main/java/com/google/api/client/googleapis/services/AbstractGoogleClient.java#L49">rootUrl</a>
*/
@InternalApi
public String getResolvedApiaryHost(String serviceName) {
if (!DEFAULT_HOST.equals(host)) {
String resolvedUniverseDomain = resolveUniverseDomain();
if (hasExplicitHost() && !DEFAULT_HOST.equals(host)) {
return host;
}
String resolvedUniverseDomain =
universeDomain != null ? universeDomain : Credentials.GOOGLE_DEFAULT_UNIVERSE;
return "https://" + serviceName + "." + resolvedUniverseDomain + "/";
}

Expand All @@ -959,14 +1004,13 @@ public String getResolvedApiaryHost(String serviceName) {
*/
@InternalApi
public boolean hasValidUniverseDomain() throws IOException {
String resolvedUniverseDomain = resolveUniverseDomain();
Credentials credentials = getCredentials();
// API keys carry no universe domain metadata. Callers are responsible for ensuring the API key
// belongs to the configured universe domain.
if (credentials instanceof ApiKeyCredentials) {
return true;
}
String resolvedUniverseDomain =
universeDomain != null ? universeDomain : Credentials.GOOGLE_DEFAULT_UNIVERSE;
// Fall back to GDU when credentials are null to match EndpointContext and HttpTransportOptions.
String credentialsUniverseDomain =
credentials != null ? credentials.getUniverseDomain() : Credentials.GOOGLE_DEFAULT_UNIVERSE;
Expand Down
Loading
Loading