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 @@ -363,7 +363,7 @@
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a

Check failure on line 366 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

View workflow job for this annotation

GitHub Actions / bom-content-test

no tag name after @

Check failure on line 366 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

View workflow job for this annotation

GitHub Actions / BomContentAssertionsTest (Test for assertion logic in BomContentTest)

no tag name after @
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition
Expand All @@ -384,7 +384,7 @@
* external source for authentication to Google Cloud Platform, you must validate it before
* providing it to any Google API or library. Providing an unvalidated credential configuration to
* Google APIs can compromise the security of your systems and data. For more information, refer
* to {@see <a

Check failure on line 387 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

View workflow job for this annotation

GitHub Actions / bom-content-test

no tag name after @

Check failure on line 387 in google-auth-library-java/oauth2_http/java/com/google/auth/oauth2/ExternalAccountCredentials.java

View workflow job for this annotation

GitHub Actions / BomContentAssertionsTest (Test for assertion logic in BomContentTest)

no tag name after @
* href="https://cloud.google.com/docs/authentication/external/externally-sourced-credentials">documentation</a>}.
*
* @param credentialsStream the stream with the credential definition
Expand Down Expand Up @@ -522,8 +522,21 @@
&& ((String) credentialSource.get("environment_id")).startsWith("aws");
}

private boolean shouldBuildImpersonatedCredential() {
return this.serviceAccountImpersonationUrl != null && this.impersonatedCredentials == null;
@Nullable ImpersonatedCredentials getImpersonatedCredentials() {
if (this.serviceAccountImpersonationUrl == null) {
return null;
}
ImpersonatedCredentials local = this.impersonatedCredentials;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should impersonatedCredentials be declared volatile? Without volatile, the first check in this double-checked locking block can let a concurrent refreshAccessToken caller observe a non-null reference before its constructor writes finish.

if (local == null) {
synchronized (this) {
local = this.impersonatedCredentials;
if (local == null) {
local = this.buildImpersonatedCredentials();
this.impersonatedCredentials = local;
}
}
}
return local;
}

/**
Expand Down Expand Up @@ -552,11 +565,9 @@
StsTokenExchangeRequest stsTokenExchangeRequest, HttpTransportFactory cycleTransportFactory)
throws IOException {
// Handle service account impersonation if necessary.
if (this.shouldBuildImpersonatedCredential()) {
this.impersonatedCredentials = this.buildImpersonatedCredentials();
}
if (this.impersonatedCredentials != null) {
return this.impersonatedCredentials.refreshAccessToken();
ImpersonatedCredentials impersonated = getImpersonatedCredentials();
if (impersonated != null) {
return impersonated.refreshAccessToken();
}

StsRequestHandler.Builder requestHandler =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,12 @@ private static String extractField(GenericJson json, String fieldName) throws IO
+ " must be a String but was: "
+ value.getClass().getName());
}
return (String) value;
String token = (String) value;
if (token.trim().isEmpty()) {
throw new IOException(
"Invalid token field value. Empty token was found for field: " + fieldName);
}
return token;
}

/** Used primarily for UrlIdentityPoolSubjectTokenSupplier */
Expand All @@ -195,19 +200,7 @@ static String parseToken(
GenericJson fileContents =
parser.parseAndClose(in, StandardCharsets.UTF_8, GenericJson.class);

Object value = fileContents.get(targetFieldName);
if (value == null || Data.isNull(value)) {
throw new IOException(
"Invalid token field name. No token was found for field: " + targetFieldName);
}
if (!(value instanceof String)) {
throw new IOException(
"Token field value for "
+ targetFieldName
+ " must be a String but was: "
+ value.getClass().getName());
}
return (String) value;
return extractField(fileContents, targetFieldName);
}
}

Expand Down
Loading
Loading