Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Description
Given that the AWS API (called https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/defaultawssecuritycredentialssupplier.ts#L250) returns a JSON body with the correct tokens, but with a Content-Type of
text/plaininstead ofapplication/jsonand given that the gaxios call no longer specifies a responseType (https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/defaultawssecuritycredentialssupplier.ts#L245), gaxios tries to infer the return type from the header (text/plain), therefore simply returning a string instead of a object.The function calling this method to get the credential, assumes that the response is an object though, accessing fields such as
awsCreds.AccessKeyId(https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/defaultawssecuritycredentialssupplier.ts#L178), which evaluate to undefined, as the properties do not exist on a String.The rest of the code seems to assume that if an object is returned and no error is thrown, that the fields are correctly set and do not further validate anything. This leads to further API calls that rely on these values to simply use them, without validating if they are set, resulting in downstream errors.
This patch fixes this, by setting the
responseTypeback tojson(which it used to be set to) meaning that gaxios will try to parse it as a json object, even if the Content-Type header istext/plainand adding an explicit validation that the corresponding fields exist, throwing an error if they do not.Impact
If the response from the AWS Security Credentials API is invalid, it will throw an error.
Testing
No tests were added or changed. A test would need to be run on an AWS EC2 instance that is configured for Workload Identity Federation with a Google Cloud Project to run and cannot be run using the current setup (to my knowledge).
Additional Information
None
Checklist
Fixes #2139 🦕