Skip to content

Example of role based access using S3Client? #1511

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

Closed
joannbrereton opened this issue Nov 15, 2019 · 2 comments
Closed

Example of role based access using S3Client? #1511

joannbrereton opened this issue Nov 15, 2019 · 2 comments
Labels
guidance Question that needs advice or information.

Comments

@joannbrereton
Copy link

Looking for an example for using role-based access to S3 files. I have the role defined and would be using it from java spring boot service running in K8S.

Expected Behavior

Would like code example like https://github.com/isuftin/s3-access-example/blob/master/src/main/java/gov/usgs/cida/aws/AwsS3AccessUtil.java only using the AWS SDK V2.

Current Behavior

Not sure which CredentialProvider to use, STSAssumeRoleCredentialsProvider? Also not sure how to create the initial StsClient? Is InstanceProfileCredentialProvider the right one to use? I'

Latest attempt is returning 403's.

  public AWSFileProvider(String roleName, String bucketName) {
        this.bucketName = bucketName;
        String roleSessionName = "WBCSession-" + Thread.currentThread().getId();
        AwsCredentialsProvider awsCredentialsProvider = roleCredentialsProvider(roleName,roleSessionName);

        this.client = S3Client.builder()
                .region(Region.US_EAST_1)
                .credentialsProvider(awsCredentialsProvider)
                .build();

    }

private StsAssumeRoleCredentialsProvider roleCredentialsProvider(String roleArn, String roleSessionName) {
        InstanceProfileCredentialsProvider instanceProfileCredentialsProvider = InstanceProfileCredentialsProvider.create();
        StsClient stsClient = StsClient.builder()
                .region(Region.US_EAST_1)
                .credentialsProvider(instanceProfileCredentialsProvider).build();

        AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder()
                .roleArn(roleArn)
                .roleSessionName(roleSessionName)
                .build();


        return StsAssumeRoleCredentialsProvider.builder()
                .refreshRequest(assumeRoleRequest)
                .stsClient(stsClient)
                .build();
    }

We also have a podAnnotation in our helm charts (used by K8S)

podAnnotations:
  iam.amazonaws.com/role: wbc-s3-allow-assume

Your Environment

  • AWS Java SDK version used:
  • JDK version used:
  • Operating System and version:

SDK2, Java 11, compiling on Linux based Jenkins, deploying to AWS K8S environment (EKS)

@klaytaybai klaytaybai added the guidance Question that needs advice or information. label Nov 19, 2019
@joannbrereton
Copy link
Author

Figured it out on my own...

   public AWSFileProvider(String roleName, String bucketName) {
        this.bucketName = bucketName;
        String roleSessionName = "WBCSession-" + Thread.currentThread().getId();
        AwsCredentialsProvider awsCredentialsProvider = roleCredentialsProvider(roleName,roleSessionName);

        this.client = S3Client.builder()
                .region(Region.US_EAST_1)
                .credentialsProvider(awsCredentialsProvider)
                .build();

     }

    private AwsCredentialsProvider roleCredentialsProvider(String roleArn, String roleSessionName) {
        AssumeRoleRequest assumeRoleRequest = AssumeRoleRequest.builder()
                .roleArn(roleArn)
                .roleSessionName(roleSessionName)
                .build();

        StsClient stsClient = StsClient.builder().region(Region.US_EAST_1).build();

        return StsAssumeRoleCredentialsProvider
                .builder()
                .stsClient(stsClient).refreshRequest(assumeRoleRequest)
                .asyncCredentialUpdateEnabled(true)
                .build();

    }

@nchmura4
Copy link

nchmura4 commented Jul 9, 2020

thank you for posting! this is a big help.

aws-sdk-java-automation pushed a commit that referenced this issue Jul 2, 2021
Allow backwards-incompatible ec2 changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants