Skip to content

Commit 918f160

Browse files
committed
Reinstated TrustBoundaryProvider.java and addressed PR comments.
1 parent 5ccda63 commit 918f160

File tree

6 files changed

+73
-42
lines changed

6 files changed

+73
-42
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.google.api.client.http.HttpStatusCodes;
4242
import com.google.api.client.json.JsonObjectParser;
4343
import com.google.api.client.util.GenericData;
44+
import com.google.api.core.InternalApi;
4445
import com.google.auth.CredentialTypeForMetrics;
4546
import com.google.auth.Credentials;
4647
import com.google.auth.Retryable;
@@ -82,7 +83,7 @@
8283
* <p>These credentials use the IAM API to sign data. See {@link #sign(byte[])} for more details.
8384
*/
8485
public class ComputeEngineCredentials extends GoogleCredentials
85-
implements ServiceAccountSigner, IdTokenProvider {
86+
implements ServiceAccountSigner, IdTokenProvider, TrustBoundaryProvider {
8687

8788
static final String METADATA_RESPONSE_EMPTY_CONTENT_ERROR_MESSAGE =
8889
"Empty content from metadata token server request.";
@@ -706,12 +707,9 @@ public String getAccount() {
706707
return principal;
707708
}
708709

710+
@InternalApi
709711
@Override
710-
boolean supportsTrustBoundary() {
711-
return true;
712-
}
713-
714-
String getTrustBoundaryUrl() throws IOException {
712+
public String getTrustBoundaryUrl() throws IOException {
715713
return String.format(
716714
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
717715
getUniverseDomain(),

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,9 @@ TrustBoundary getTrustBoundary() {
339339
return trustBoundary;
340340
}
341341

342-
/**
343-
* Returns whether the credentials support trust boundary.
344-
*
345-
* @return {@code true} if the credentials support trust boundary, {@code false} otherwise.
346-
*/
347-
boolean supportsTrustBoundary() {
348-
return false;
349-
}
350-
351342
/**
352343
* Refreshes the trust boundary by making a call to the trust boundary URL.
353344
*
354-
* <p>This method is for internal use only and should not be called by users directly. It is used
355-
* to enforce security policies by ensuring that the credentials used to access Google Cloud APIs
356-
* are not used outside a trusted environment.
357-
*
358345
* @param newAccessToken The new access token to be used for the refresh.
359346
* @param trustBoundaryUrl The URL of the trust boundary service.
360347
* @param transportFactory The HTTP transport factory to be used for the refresh.
@@ -365,7 +352,7 @@ void refreshTrustBoundary(
365352
AccessToken newAccessToken, String trustBoundaryUrl, HttpTransportFactory transportFactory)
366353
throws IOException {
367354

368-
if (!supportsTrustBoundary()
355+
if (!(this instanceof TrustBoundaryProvider)
369356
|| !TrustBoundary.isTrustBoundaryEnabled()
370357
|| !isDefaultUniverseDomain()) {
371358
return;
@@ -456,17 +443,14 @@ static Map<String, List<String>> addQuotaProjectIdToRequestMetadata(
456443
@Override
457444
protected Map<String, List<String>> getAdditionalHeaders() {
458445
Map<String, List<String>> headers = new HashMap<>(super.getAdditionalHeaders());
459-
String quotaProjectId = this.getQuotaProjectId();
460-
if (quotaProjectId != null) {
461-
headers.put(QUOTA_PROJECT_ID_HEADER_KEY, Collections.singletonList(quotaProjectId));
462-
}
463446

464447
if (this.trustBoundary != null) {
465448
String headerValue = trustBoundary.isNoOp() ? "" : trustBoundary.getEncodedLocations();
466449
headers.put(TrustBoundary.TRUST_BOUNDARY_KEY, Collections.singletonList(headerValue));
467450
}
468451

469-
return Collections.unmodifiableMap(headers);
452+
String quotaProjectId = this.getQuotaProjectId();
453+
return addQuotaProjectIdToRequestMetadata(quotaProjectId, headers);
470454
}
471455

472456
/** Default constructor. */

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.google.api.client.http.json.JsonHttpContent;
4444
import com.google.api.client.json.JsonObjectParser;
4545
import com.google.api.client.util.GenericData;
46+
import com.google.api.core.InternalApi;
4647
import com.google.auth.CredentialTypeForMetrics;
4748
import com.google.auth.ServiceAccountSigner;
4849
import com.google.auth.http.HttpCredentialsAdapter;
@@ -93,7 +94,7 @@
9394
* </pre>
9495
*/
9596
public class ImpersonatedCredentials extends GoogleCredentials
96-
implements ServiceAccountSigner, IdTokenProvider {
97+
implements ServiceAccountSigner, IdTokenProvider, TrustBoundaryProvider {
9798

9899
private static final long serialVersionUID = -2133257318957488431L;
99100
private static final String RFC3339 = "yyyy-MM-dd'T'HH:mm:ssX";
@@ -325,12 +326,9 @@ public GoogleCredentials getSourceCredentials() {
325326
return sourceCredentials;
326327
}
327328

329+
@InternalApi
328330
@Override
329-
boolean supportsTrustBoundary() {
330-
return true;
331-
}
332-
333-
String getTrustBoundaryUrl() throws IOException {
331+
public String getTrustBoundaryUrl() throws IOException {
334332
return String.format(
335333
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
336334
getUniverseDomain(),

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import com.google.api.client.util.GenericData;
5252
import com.google.api.client.util.Joiner;
5353
import com.google.api.client.util.Preconditions;
54+
import com.google.api.core.InternalApi;
5455
import com.google.auth.CredentialTypeForMetrics;
5556
import com.google.auth.Credentials;
5657
import com.google.auth.RequestMetadataCallback;
@@ -89,7 +90,7 @@
8990
* <p>By default uses a JSON Web Token (JWT) to fetch access tokens.
9091
*/
9192
public class ServiceAccountCredentials extends GoogleCredentials
92-
implements ServiceAccountSigner, IdTokenProvider, JwtProvider {
93+
implements ServiceAccountSigner, IdTokenProvider, JwtProvider, TrustBoundaryProvider {
9394

9495
private static final long serialVersionUID = 7807543542681217978L;
9596
private static final String GRANT_TYPE = "urn:ietf:params:oauth:grant-type:jwt-bearer";
@@ -823,12 +824,9 @@ public boolean getUseJwtAccessWithScope() {
823824
return useJwtAccessWithScope;
824825
}
825826

827+
@InternalApi
826828
@Override
827-
boolean supportsTrustBoundary() {
828-
return true;
829-
}
830-
831-
String getTrustBoundaryUrl() throws IOException {
829+
public String getTrustBoundaryUrl() throws IOException {
832830
return String.format(
833831
OAuth2Utils.IAM_CREDENTIALS_ALLOWED_LOCATIONS_URL_FORMAT_SERVICE_ACCOUNT,
834832
getUniverseDomain(),

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,11 @@
5454
import javax.annotation.Nullable;
5555

5656
/**
57-
* Represents a trust boundary that can be used to restrict access to resources. This is an
58-
* experimental feature.
57+
* Represents the trust boundary configuration for a credential. This class holds the information
58+
* retrieved from the IAM `allowedLocations` endpoint. This data is then used to populate the
59+
* `x-allowed-locations` header in outgoing API requests, which in turn allows Google's
60+
* infrastructure to enforce regional security restrictions. This class does not perform any
61+
* client-side validation or enforcement.
5962
*/
6063
final class TrustBoundary {
6164

@@ -72,7 +75,7 @@ final class TrustBoundary {
7275
* @param encodedLocations The encoded string representation of the allowed locations.
7376
* @param locations A list of human-readable location strings.
7477
*/
75-
public TrustBoundary(String encodedLocations, List<String> locations) {
78+
TrustBoundary(String encodedLocations, List<String> locations) {
7679
this.encodedLocations = encodedLocations;
7780
this.locations =
7881
locations == null
@@ -177,13 +180,13 @@ static TrustBoundary refresh(
177180

178181
HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory();
179182
HttpRequest request = requestFactory.buildGetRequest(new GenericUrl(url));
180-
request.getHeaders().setAuthorization("Bearer " + accessToken.getTokenValue());
183+
// request.getHeaders().setAuthorization("Bearer " + accessToken.getTokenValue());
181184

182185
// Add the cached trust boundary header, if available.
183186
if (cachedTrustBoundary != null) {
184187
String headerValue =
185188
cachedTrustBoundary.isNoOp() ? "" : cachedTrustBoundary.getEncodedLocations();
186-
request.getHeaders().set(TRUST_BOUNDARY_KEY, headerValue);
189+
// request.getHeaders().set(TRUST_BOUNDARY_KEY, headerValue);
187190
}
188191

189192
// Add retry logic
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024, Google LLC
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are
6+
* met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above
11+
* copyright notice, this list of conditions and the following disclaimer
12+
* in the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* * Neither the name of Google LLC nor the names of its
16+
* contributors may be used to endorse or promote products derived from
17+
* this software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
package com.google.auth.oauth2;
33+
34+
import com.google.api.core.InternalApi;
35+
import java.io.IOException;
36+
37+
/**
38+
* An interface for providing trust boundary information. It is used to provide a common interface
39+
* for credentials that support trust boundary checks.
40+
*/
41+
@InternalApi
42+
interface TrustBoundaryProvider {
43+
44+
/**
45+
* Returns the trust boundary URI.
46+
*
47+
* @return The trust boundary URI.
48+
*/
49+
String getTrustBoundaryUrl() throws IOException;
50+
}

0 commit comments

Comments
 (0)