Ec2MetadataClient
IMDSv2 401 Unauthorized HTTP Status Code Exceptions
#5764
Labels
bug
This issue is a bug.
needs-review
This issue or PR needs review from the team.
p2
This is a standard priority issue
Describe the bug
Using
Ec2MetadataClient
to query the IMDSv2 service on an EC2 instance will eventually throw anSdkClientException
for a401 Unauthorized
HTTP status code as shown here.Regression Issue
Expected Behavior
This reproduction example should not print any errors. The
Ec2MetadataClient
should not use stale tokens, which could be the cause of the401 Unauthorized
response.Current Behavior
Let it run long enough and the reproduction example produces the following log:
Reproduction Steps
This reproduction example uses a short token TTL, but longer TTLs produce the same errors.
Possible Solution
This line checks if the current IMDSv2 token is expired before sending the request to the IMDS service, but there appears to be some sort of issue that causes a stale token to be used for the IMDS request. The subsequent call after a
401 Unauthorized
exception will refresh the token before the IMDS request, so I suspect a potential race condition (there may need to be some buffer time between the IMDS token TTL and the JVM-cached TTL) or some other timing issue.Additional Information/Context
No response
AWS Java SDK version used
2.29.43
JDK version used
OpenJDK 64-Bit Server VM Corretto-21.0.5.11.1
Operating System and version
Ubuntu 24.04.1 LTS
The text was updated successfully, but these errors were encountered: