I have to generate read only and write only tokens for a file in S3.
What I have tried so far:
- create an IAM role with read and write access to the bucket in reference
- create an STS client
- assume the IAM role created in step #1 by the STS client
- generate credentials using sts client
What this does is
- lets the user access the file in S3 with the token
- but this access is not limited to read only or write only
- also if the IAM role has access to more buckets , the token will be accessing all the bucket
Create STS client
AWSSecurityTokenServiceClient sts_client = (AWSSecurityTokenServiceClient) AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(Regions.DEFAULT_REGION).build();
Create assume role request
AssumeRoleRequest assumeRoleRequest = new AssumeRoleRequest()
.withRoleArn("arn:aws:iam::123456789123:role/iam-role-name")
.withDurationSeconds(7200)
.withRoleSessionName("session-role-"+System.currentTimeMillis());
Generate token request
GetSessionTokenRequest session_token_request = new GetSessionTokenRequest();
Generate tokens
GetSessionTokenResult session_token_result = sts_client.getSessionToken(session_token_request);
Create credentials
Credentials session_creds = session_token_result.getCredentials();
Create basic credentials
BasicSessionCredentials sessionCredentials = new BasicSessionCredentials(
session_creds.getAccessKeyId(),
session_creds.getSecretAccessKey(),
session_creds.getSessionToken());
expectation
- be able to generate read only and write only tokens
- be able to generate path specific tokens
- token be limited to only resource in reference and not to all the buckets attached in the IAM role
I found a solutions to this .
what is does is :