0

In our application we are using InstanceProfileCredentialsProvider to access Amazon S3 buckets, but in some of our environments credentials are messed up.

AWS documentation (https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/java-dg-roles.html) reads:

If your application creates an AWS client using the default constructor, then the client will search for credentials using the default credentials provider chain, in the following order:

  1. In the Java system properties: aws.accessKeyId and aws.secretKey.
  2. In system environment variables: AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
  3. In the default credentials file (the location of this file varies by platform).
  4. Credentials delivered through the Amazon EC2 container service if the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI environment variable is set and security manager has permission to access the variable.
  5. In the instance profile credentials, which exist within the instance metadata associated with the IAM role for the EC2 instance.
  6. Web Identity Token credentials from the environment or container.

Is there a way to know where specifically InstanceProfileCredentialsProvider gets the credentials? From the look at its source code, it's quite discreet and doesn't share much details though either API or logging.

Alexander
  • 609
  • 6
  • 19

1 Answers1

1

InstanceProfileCredentialsProvider gets the credentials from the EC2 Instance Metadata Service. API service which continuously runs on the EC2 instance, if metadata endpoint is enabled.

API endpoint - http://169.254.169.254/latest/meta-data/latest/meta-data/iam/security-credentials/

https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/index.html?com/amazonaws/auth/InstanceProfileCredentialsProvider.html

In the above the case, your EC2 instance which is running the Java code, should have an attached IAM role to the EC2 instance, which has access to S3 resources.

Most likely the InstanceProfileCredentialsProvider is using EC2MetadataClient.java

https://github.com/aws/aws-sdk-java/blob/7b1e5b87b0bf03456df9e77716b14731adf9a7a7/aws-java-sdk-core/src/main/java/com/amazonaws/internal/EC2MetadataClient.java

Shivankar
  • 113
  • 1
  • 15