Skip to content

EC2 IMDS Changes to Support Account ID #6176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

S-Saranya1
Copy link
Contributor

@S-Saranya1 S-Saranya1 commented Jun 12, 2025

Add support for extracting account ID from Instance Metadata Credentials Provider

Motivation and Context

Add support in IMDS credentials provider to retrieve accountID.

Modifications

  • Added support for new /latest/meta-data/iam/security-credentials-extended/ IMDS endpoint
  • Added fallback logic to legacy endpoint '/latest/meta-data/iam/security-credentials/{ROLE}'
  • Added support for AccountId to extract it from IMDS response.

Testing

Added unit tests using WireMock to verify:

  • Successful retrieval of credentials with account ID from extended endpoint
  • Proper fallback to legacy endpoint when extended endpoint returns 404

Performed integration testing on EC2:

  • Verified successful account ID resolution on allowlisted instances
  • Confirmed fallback to legacy endpoint on non-allowlisted instances

Screenshots (if appropriate)

Tested Code:

public class ImdsAccountIdTest {
    public static void main(String[] args) {
        try {
            InstanceProfileCredentialsProvider provider = InstanceProfileCredentialsProvider.create();
            AwsCredentials credentials = provider.resolveCredentials();
            System.out.println("Successfully resolved credentials!");
            System.out.println("Access Key ID: " + credentials.accessKeyId().substring(0, 5) + "...");
            System.out.println("Account ID: " + credentials.accountId().orElse("Not available"));

        } catch (Exception e) {
            System.err.println("Error: " + e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
    }
}

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

License

  • I confirm that this pull request can be released under the Apache 2 license

@S-Saranya1 S-Saranya1 requested a review from a team as a code owner June 12, 2025 15:49
-created a new integration test file for IMDS extended url separating it from legacy
-Included the status code to the fallback logic
- Removed the duplicate test files
- Make ApiVersion Volatile
-Adding additional tests
-Updating to use AtomicReference
Updating the debug logging message
Modified the fallback logic in refresh credentials
Copy link
Contributor

@alextwoods alextwoods left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants