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 @@ -49,6 +49,7 @@
import com.google.auth.ServiceAccountSigner;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.MetricsUtils.RequestType;
import com.google.common.base.Strings;
import com.google.common.io.BaseEncoding;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -168,10 +169,7 @@ private static String getSignature(
response, LOGGER_PROVIDER, "Received response for signature to sign the blob");
int statusCode = response.getStatusCode();
if (statusCode >= 400 && statusCode < HttpStatusCodes.STATUS_CODE_SERVER_ERROR) {
GenericData responseError = response.parseAs(GenericData.class);
Map<String, Object> error =
OAuth2Utils.validateMap(responseError, "error", PARSE_ERROR_MESSAGE);
String errorMessage = OAuth2Utils.validateString(error, "message", PARSE_ERROR_MESSAGE);
String errorMessage = parseErrorMessage(response);
throw new IOException(
String.format(
"Error code %s trying to sign provided bytes: %s", statusCode, errorMessage));
Expand All @@ -197,6 +195,48 @@ private static String getSignature(
return OAuth2Utils.validateString(responseData, "signedBlob", PARSE_ERROR_SIGNATURE);
}

/**
* Returns the error message from an IAM Credentials 4xx response, or falls back to the HTTP
* status message when the body is missing, empty, not JSON, or lacks {@code error.message}.
*
* <p>For example, {@code HttpURLConnection} (used by {@code NetHttpTransport}) discards the body
* of a 401 or 407 response to a request sent in streaming mode (every POST with content) and of
* any response with {@code Content-Length: 0}, and proxies can return HTML or other non-IAM
* bodies. Falling back preserves the HTTP status code in the caller's {@link IOException} instead
* of failing with a {@link NullPointerException}, {@link IllegalArgumentException}, or JSON parse
* error.
*/
private static String parseErrorMessage(HttpResponse response) {
GenericData responseError;
try {
if (response.getContent() == null) {
return getStatusMessageOrDefault(response);
}
responseError = response.parseAs(GenericData.class);
} catch (IOException | IllegalArgumentException e) {
// Empty body ("no JSON input found"), non-JSON body (for example, an HTML error page from a
// proxy), or unreadable stream.
return getStatusMessageOrDefault(response);
}
if (responseError != null) {
Object error = responseError.get("error");
if (error instanceof Map) {
Object message = ((Map<?, ?>) error).get("message");
if (message instanceof String && !((String) message).isEmpty()) {
return (String) message;
}
}
}
return getStatusMessageOrDefault(response);
}

private static String getStatusMessageOrDefault(HttpResponse response) {
String statusMessage = response.getStatusMessage();
return Strings.isNullOrEmpty(statusMessage)
? "(no error details in the response)"
: statusMessage;
}

/**
* Returns an IdToken issued to the serviceAccount with a specified targetAudience
*
Expand Down Expand Up @@ -257,10 +297,7 @@ static IdToken getIdToken(
LoggingUtils.logResponse(response, LOGGER_PROVIDER, "Received response for ID token request");
int statusCode = response.getStatusCode();
if (statusCode >= 400 && statusCode < HttpStatusCodes.STATUS_CODE_SERVER_ERROR) {
GenericData responseError = response.parseAs(GenericData.class);
Map<String, Object> error =
OAuth2Utils.validateMap(responseError, "error", PARSE_ERROR_MESSAGE);
String errorMessage = OAuth2Utils.validateString(error, "message", PARSE_ERROR_MESSAGE);
String errorMessage = parseErrorMessage(response);
throw new IOException(
String.format("Error code %s trying to getIDToken: %s", statusCode, errorMessage));
}
Expand Down
Loading
Loading