Access S3 buckets using SSE-KMS encryption in an EC2 IAM Role environment (AWS SDK for Java)

The other day, I participated in an event

** "By the way, I wrote an article to encrypt and upload to an S3 bucket, but I used an access key to authenticate!" ** (Not in line with security best practices)

I noticed that, so I decided to supplement it.

** Encrypting data uploaded to S3 using AWS SDK for Java / SSE-KMS ** is a continuation.

1. Set up an IAM Role for EC2

· In IAM, click ** Create New Role **. 01_iam1.png

-Select ** "Amazon EC2" **. 02_iam2.png

-Check ** "Amazon S3 Full Access" ** and click ** [Next Step] **. 03_iam3.png

-Enter ** role name ** and click ** [Create Role] **. 04_iam4.png

-Select the ** encryption key ** used for S3 encryption. 05_kms1.png

-Click ** [Add] ** of ** Key User **. 06_kms2.png

-Select the ** IAM role you created ** and click ** [Attach] **. 07_kms3.png

-Assign the created IAM role to the ** target EC2 instance **. 08_ec2_1.png

-Enter / select the ** IAM role name ** and click ** Apply **. 09_ec2_2.png

-If the process is successful, start (start) the target EC2 instance and check that the IAM Role is applied. 11_ec2_4.png

2. Java code changes

For Java code, simply unspecify credentials.

change point


public class S3Access {

    private static final String ENDPOINT_URL = "https://s3-ap-northeast-1.amazonaws.com";
    private static final String REGION       = "ap-northeast-1";
//  private static final String ACCESS_KEY   = "【access key】";
//  private static final String SECRET_KEY   = "[Secret key]";
    private static final String KMS_KEY_ID   = "[KMS key ID]";

(Omission)

    //--------------------------------------------------
    //Client generation
    //--------------------------------------------------
    private AmazonS3 getClient(String bucketName) throws Exception {

//      //Authentication information
//      AWSCredentials credentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);

        //Client settings
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTPS);  //protocol
        clientConfig.setConnectionTimeout(10000);   //Connection timeout(ms) 

        //Endpoint setting
        EndpointConfiguration endpointConfiguration = new EndpointConfiguration(ENDPOINT_URL, REGION);

        //Client generation
        AmazonS3 client = AmazonS3ClientBuilder.standard()
//                      .withCredentials(new AWSStaticCredentialsProvider(credentials))
                        .withClientConfiguration(clientConfig)
                        .withEndpointConfiguration(endpointConfiguration).build();

(Omission)

** Delete the line commented out with "//" at the beginning of the line **.

Recommended Posts

Access S3 buckets using SSE-KMS encryption in an EC2 IAM Role environment (AWS SDK for Java)
I tried using an extended for statement in Java
Try Spark Submit to EMR using AWS SDK for Java
Upload / download / bulk delete data to S3 using Amazon S3 Client Builder with AWS SDK for Java
How to publish an application using AWS (3) EC2 instance environment construction
AWS SDK for Java 1.11.x and 2.x
Deleting AWS S3 Objects in Java
Renamed folders in AWS S3 (Java)
Get a list of S3 files with ListObjectsV2Request (AWS SDK for Java)
[AWS SDK for Java] Set a retry policy on the S3 client
Using Java with AWS Lambda-Implementation-Stop / Launch EC2
Map without using an array in java
Modern Java environment for Windows using Chocolatey
Use without preparing an authentication file when using Firebase Admin SDK in Java