I am getting an error when calling to assume role
method of STS. It says that the user is not authorized to perform sts:AsumeRole
on resource xxx
.
I did the following:
What am I doing wrong?
Policy in Group
{ "Version": "2012-10-17", "Statement": [ { "Sid": "some-large-id", "Effect": "Allow", "Action": [ "sts:*" ], "Resource": [ "*" ] } ] }
Policy in role
{ "Version": "2012-10-17", "Statement": [ { "Sid": "another-large-id", "Effect": "Allow", "Action": [ "s3:PutObject" ], "Resource": [ "arn:aws:s3:::my-bucket-name/*" ] } ] }
And finally calling like this
let policy = { "Version": "2012-10-17", "Statement": [ { "Sid": "new-custom-id", "Effect": "Allow", "Action": ["s3:PutObject"], "Resource": ["arn:aws:s3:::my-bucket-name/*"] } ] }; let params = { DurationSeconds: 3600, ExternalId: 'some-value', Policy: JSON.stringify(policy), RoleArn: "arn:aws:iam::NUMBER:role/ROLE-NAME", //Cheked, role is the same that step one RoleSessionName: this.makeNewSessionId() }; let sts = new AWS.STS({ apiVersion: '2012-08-10' }); sts.assumeRole(params, (err, data) => { if(err) console.log(err); else console.log(data); });
One way to accomplish this is to create a new role and specify the desired permissions in that role's permissions policy. Another way to accomplish this is to call the AssumeRole API and include session policies in the optional Policy parameter as part of the API operation.
PDF. Returns a set of temporary security credentials that you can use to access AWS resources that you might not normally have access to. These temporary credentials consist of an access key ID, a secret access key, and a security token.
Grant permission to a developer to assume an IAM role given by a Amazon WorkDocs customer. If you are a developer with an administrative AWS account, you can grant a user permission to switch to a role by creating a new policy and attaching it to the user.
There is a step that was missing: set trust relationship on role created in step one. No matter what privileges the user had, if the trust relationship is not set, STS will refuse the request.
Troubleshooting IAM Roles explain how it works.
On the role that you want to assume, for example using the STS Java V2 API (not Node), you need to set a trust relationship. In the trust relationship, specify the user to trust. For example:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "<Specify the ARN of your IAM user you are using in this code example>" }, "Action": "sts:AssumeRole" } ] }
Now you can, for example, run a Java program to invoke the assumeRole operation.
package com.example.sts; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.sts.StsClient; import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; import software.amazon.awssdk.services.sts.model.StsException; import software.amazon.awssdk.services.sts.model.AssumeRoleResponse; import software.amazon.awssdk.services.sts.model.Credentials; import java.time.Instant; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.Locale; /** * To make this code example work, create a Role that you want to assume. * Then define a Trust Relationship in the AWS Console. YOu can use this as an example: * * { * "Version": "2012-10-17", * "Statement": [ * { * "Effect": "Allow", * "Principal": { * "AWS": "<Specify the ARN of your IAM user you are using in this code example>" * }, * "Action": "sts:AssumeRole" * } * ] * } * * For more information, see "Editing the Trust Relationship for an Existing Role" in the AWS Directory Service guide. */ public class AssumeRole { public static void main(String[] args) { String roleArn = "arn:aws:iam::000540000000:role/s3role" ; // args[0]; String roleSessionName = "mysession101"; // args[1]; Region region = Region.US_EAST_1; StsClient stsClient = StsClient.builder() .region(region) .build(); try { AssumeRoleRequest roleRequest = AssumeRoleRequest.builder() .roleArn(roleArn) .roleSessionName(roleSessionName) .build(); AssumeRoleResponse roleResponse = stsClient.assumeRole(roleRequest); Credentials myCreds = roleResponse.credentials(); //Display the time when the temp creds expire Instant exTime = myCreds.expiration(); // Convert the Instant to readable date DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT ) .withLocale( Locale.US) .withZone( ZoneId.systemDefault() ); formatter.format( exTime ); System.out.println("The temporary credentials expire on " + exTime ); } catch (StsException e) { System.err.println(e.getMessage()); System.exit(1); } } }
Without setting the Trust Relationship, this code does not work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With