Skip to content

Commit 29c061e

Browse files
authored
fix: S2A gRPC flow creates ComputeEngineCredentials via newBuilder. (#3651)
@rockspore pointed out that the credential should be created from scratch because when using [toBuilder](https://github.com/googleapis/google-auth-library-java/blob/main/oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java#L648) the underlying [access token is copied](https://github.com/googleapis/google-auth-library-java/blob/37d228410e99799e4a7be8650fe472ea712c9b4d/oauth2_http/java/com/google/auth/oauth2/OAuth2Credentials.java#L657). This was confirmed to be a bug with local testing which: - deployed a GAE app, the app performs the below two actions sequentially - create Google API client ( `allowedHardBoundAccessTokens` empty in GrpcProvider) and then ping the API, logs show the bearer token is used, obtained from making call to MDS - create a Google API client ( `allowedHardBoundAccessTokens` contains `MTLS_S2A` in GrpcProvider) and then ping the API, logs show the bearer token is used. A call to MDS is **not** made. This is likely because the credential and channel have different lifetimes.
1 parent fe002fa commit 29c061e

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

gax-java/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -1200,11 +1200,20 @@ boolean isDirectPathBoundTokenEnabled() {
12001200
CallCredentials createHardBoundTokensCallCredentials(
12011201
ComputeEngineCredentials.GoogleAuthTransport googleAuthTransport,
12021202
ComputeEngineCredentials.BindingEnforcement bindingEnforcement) {
1203+
ComputeEngineCredentials.Builder credsBuilder =
1204+
((ComputeEngineCredentials) credentials).toBuilder();
12031205
// We only set scopes and HTTP transport factory from the original credentials because
1204-
// only those are used in gRPC CallCredentials to fetch request metadata.
1206+
// only those are used in gRPC CallCredentials to fetch request metadata. We create a new
1207+
// credential
1208+
// via {@code newBuilder} as opposed to {@code toBuilder} because we don't want a reference to
1209+
// the
1210+
// access token held by {@code credentials}; we want this new credential to fetch a new access
1211+
// token
1212+
// from MDS using the {@param googleAuthTransport} and {@param bindingEnforcement}.
12051213
return MoreCallCredentials.from(
1206-
((ComputeEngineCredentials) this.credentials)
1207-
.toBuilder()
1214+
ComputeEngineCredentials.newBuilder()
1215+
.setScopes(credsBuilder.getScopes())
1216+
.setHttpTransportFactory(credsBuilder.getHttpTransportFactory())
12081217
.setGoogleAuthTransport(googleAuthTransport)
12091218
.setBindingEnforcement(bindingEnforcement)
12101219
.build());

0 commit comments

Comments
 (0)