Skip to content

Commit 0c28dc6

Browse files
committed
Const string for Service Account Lookup IAM URI. Network call removed from locking. Other doc changes.
1 parent b897b68 commit 0c28dc6

File tree

7 files changed

+60
-50
lines changed

7 files changed

+60
-50
lines changed

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,10 @@ public HttpTransportFactory getTransportFactory() {
713713

714714
@Override
715715
public String getTrustBoundaryUrl() throws IOException {
716-
if (principal == null) {
717-
principal = getDefaultServiceAccount();
718-
}
719716
return String.format(
720-
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s/allowedLocations",
721-
getUniverseDomain(), principal);
717+
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
718+
getUniverseDomain(),
719+
getAccount());
722720
}
723721

724722
/**

oauth2_http/java/com/google/auth/oauth2/GoogleCredentials.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ String getFileType() {
107107
private final String universeDomain;
108108
private final boolean isExplicitUniverseDomain;
109109

110-
private transient TrustBoundary trustBoundary;
110+
private TrustBoundary trustBoundary;
111111

112112
protected final String quotaProjectId;
113113

@@ -336,6 +336,7 @@ public GoogleCredentials createWithQuotaProject(String quotaProject) {
336336
return this.toBuilder().setQuotaProjectId(quotaProject).build();
337337
}
338338

339+
@VisibleForTesting
339340
public TrustBoundary getTrustBoundary() {
340341
return trustBoundary;
341342
}
@@ -400,25 +401,38 @@ protected void refreshTrustBoundaries(AccessToken newAccessToken) throws IOExcep
400401
}
401402

402403
TrustBoundaryProvider provider = (TrustBoundaryProvider) this;
404+
TrustBoundary cachedTrustBoundary;
405+
403406
synchronized (lock) {
404-
// Refresh trust boundaries only if the cached value is not NO_OP.
405-
if (this.trustBoundary == null || !this.trustBoundary.isNoOp()) {
406-
try {
407-
this.trustBoundary =
408-
TrustBoundary.refresh(
409-
provider.getTransportFactory(),
410-
provider.getTrustBoundaryUrl(),
411-
newAccessToken,
412-
this.trustBoundary);
413-
} catch (IOException e) {
414-
// If refresh fails, check for cached value.
415-
if (this.trustBoundary == null) {
416-
// No cached value, so fail hard.
417-
throw new IOException(
418-
"Failed to refresh trust boundary and no cached value is available.", e);
419-
}
420-
}
407+
// Do not refresh if the cached value is already NO_OP.
408+
if (this.trustBoundary != null && this.trustBoundary.isNoOp()) {
409+
return;
410+
}
411+
cachedTrustBoundary = this.trustBoundary;
412+
}
413+
414+
TrustBoundary newTrustBoundary;
415+
try {
416+
newTrustBoundary =
417+
TrustBoundary.refresh(
418+
provider.getTransportFactory(),
419+
provider.getTrustBoundaryUrl(),
420+
newAccessToken,
421+
cachedTrustBoundary);
422+
} catch (IOException e) {
423+
// If refresh fails, check for a cached value.
424+
if (cachedTrustBoundary == null) {
425+
// No cached value, so fail hard.
426+
throw new IOException(
427+
"Failed to refresh trust boundary and no cached value is available.", e);
421428
}
429+
430+
return;
431+
}
432+
433+
// A lock is required to safely update the shared field.
434+
synchronized (lock) {
435+
this.trustBoundary = newTrustBoundary;
422436
}
423437
}
424438

oauth2_http/java/com/google/auth/oauth2/ImpersonatedCredentials.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,9 @@ public HttpTransportFactory getTransportFactory() {
333333
@Override
334334
public String getTrustBoundaryUrl() throws IOException {
335335
return String.format(
336-
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s/allowedLocations",
337-
getUniverseDomain(), getAccount());
336+
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
337+
getUniverseDomain(),
338+
getAccount());
338339
}
339340

340341
int getLifetime() {

oauth2_http/java/com/google/auth/oauth2/OAuth2Utils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public class OAuth2Utils {
9393
static final URI TOKEN_SERVER_URI = URI.create("https://oauth2.googleapis.com/token");
9494

9595
static final URI TOKEN_REVOKE_URI = URI.create("https://oauth2.googleapis.com/revoke");
96+
97+
public static final String IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT =
98+
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s/allowedLocations";
9699
static final URI USER_AUTH_URI = URI.create("https://accounts.google.com/o/oauth2/auth");
97100

98101
static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

oauth2_http/java/com/google/auth/oauth2/ServiceAccountCredentials.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,8 +831,9 @@ public HttpTransportFactory getTransportFactory() {
831831
@Override
832832
public String getTrustBoundaryUrl() throws IOException {
833833
return String.format(
834-
"https://iamcredentials.%s/v1/projects/-/serviceAccounts/%s/allowedLocations",
835-
getUniverseDomain(), getClientEmail());
834+
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
835+
getUniverseDomain(),
836+
getAccount());
836837
}
837838

838839
@VisibleForTesting
@@ -1160,7 +1161,6 @@ public static class Builder extends GoogleCredentials.Builder {
11601161
private int lifetime = DEFAULT_LIFETIME_IN_SECONDS;
11611162
private boolean useJwtAccessWithScope = false;
11621163
private boolean defaultRetriesEnabled = true;
1163-
private TrustBoundary trustBoundary;
11641164

11651165
protected Builder() {}
11661166

@@ -1179,7 +1179,6 @@ protected Builder(ServiceAccountCredentials credentials) {
11791179
this.lifetime = credentials.lifetime;
11801180
this.useJwtAccessWithScope = credentials.useJwtAccessWithScope;
11811181
this.defaultRetriesEnabled = credentials.defaultRetriesEnabled;
1182-
this.trustBoundary = credentials.getTrustBoundary();
11831182
}
11841183

11851184
@CanIgnoreReturnValue

oauth2_http/java/com/google/auth/oauth2/TrustBoundary.java

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Google LLC
2+
* Copyright 2025, Google LLC
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -44,6 +44,7 @@
4444
import com.google.api.client.util.ExponentialBackOff;
4545
import com.google.api.client.util.Key;
4646
import com.google.auth.http.HttpTransportFactory;
47+
import com.google.common.annotations.VisibleForTesting;
4748
import com.google.common.base.MoreObjects;
4849
import java.io.IOException;
4950
import java.util.Collections;
@@ -55,7 +56,7 @@
5556
* Represents a trust boundary that can be used to restrict access to resources. This is an
5657
* experimental feature.
5758
*/
58-
public final class TrustBoundary {
59+
final class TrustBoundary {
5960

6061
static final String TRUST_BOUNDARY_KEY = "x-allowed-locations";
6162
static final String GOOGLE_AUTH_TRUST_BOUNDARY_ENABLED_ENV_VAR =
@@ -111,27 +112,19 @@ public String toString() {
111112
}
112113
}
113114

114-
// expose this setter only for testing purposes
115-
public static void setEnvironmentProviderForTest(@Nullable EnvironmentProvider provider) {
115+
@VisibleForTesting
116+
static void setEnvironmentProviderForTest(@Nullable EnvironmentProvider provider) {
116117
environmentProvider = provider == null ? SystemEnvironmentProvider.getInstance() : provider;
117118
}
118119

119-
static boolean isTrustBoundaryEnabled() throws IOException {
120-
String tbEnabled = environmentProvider.getEnv(GOOGLE_AUTH_TRUST_BOUNDARY_ENABLED_ENV_VAR);
121-
if (tbEnabled == null) {
120+
static boolean isTrustBoundaryEnabled() {
121+
String trustBoundaryEnabled =
122+
environmentProvider.getEnv(GOOGLE_AUTH_TRUST_BOUNDARY_ENABLED_ENV_VAR);
123+
if (trustBoundaryEnabled == null) {
122124
return false;
123125
}
124-
String lowercasedTbEnabled = tbEnabled.toLowerCase();
125-
if ("true".equals(lowercasedTbEnabled) || "1".equals(tbEnabled)) {
126-
return true;
127-
}
128-
if ("false".equals(lowercasedTbEnabled) || "0".equals(tbEnabled)) {
129-
return false;
130-
}
131-
throw new IOException(
132-
String.format(
133-
"Invalid value for %s environment variable: \"%s\". Supported values are 'true', '1', 'false', or '0'.",
134-
GOOGLE_AUTH_TRUST_BOUNDARY_ENABLED_ENV_VAR, tbEnabled));
126+
String lowercasedTrustBoundaryEnabled = trustBoundaryEnabled.toLowerCase();
127+
return "true".equals(lowercasedTrustBoundaryEnabled) || "1".equals(trustBoundaryEnabled);
135128
}
136129

137130
static TrustBoundary refresh(
@@ -141,11 +134,11 @@ static TrustBoundary refresh(
141134
@Nullable TrustBoundary cachedTrustBoundary)
142135
throws IOException {
143136
if (accessToken == null) {
144-
throw new IOException("The provided access token is null.");
137+
throw new IllegalArgumentException("The provided access token is null.");
145138
}
146139
if (accessToken.getExpirationTime() != null
147140
&& accessToken.getExpirationTime().before(new Date())) {
148-
throw new IOException("The provided access token is expired.");
141+
throw new IllegalArgumentException("The provided access token is expired.");
149142
}
150143

151144
HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory();

oauth2_http/java/com/google/auth/oauth2/TrustBoundaryProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2024, Google LLC
2+
* Copyright 2025, Google LLC
33
*
44
* Redistribution and use in source and binary forms, with or without
55
* modification, are permitted provided that the following conditions are
@@ -31,10 +31,12 @@
3131

3232
package com.google.auth.oauth2;
3333

34+
import com.google.api.core.BetaApi;
3435
import com.google.auth.http.HttpTransportFactory;
3536
import java.io.IOException;
3637

3738
/** An interface for credentials that support trust boundaries. This is an experimental feature. */
39+
@BetaApi
3840
public interface TrustBoundaryProvider {
3941

4042
/** Returns the transport factory. */

0 commit comments

Comments
 (0)