Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How enable access to AWS STS AssumeRole

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:

  1. I created a role to access to S3 bucket.
  2. I ran a test over policy simulator and works fine
  3. I created a new group, and in it, i created a new policy that enables all sts actions, over all resources.
  4. I ran a test with the policy simulator, to sts assume role, pointing to the ARN of role created at step one; and it works fine
  5. I created a new user, and put it in group created at step 3
  6. With the credentials of the new user, i try to get a new credentials using sts asume role, but throw me an error that say my user is not authorized to perform sts:AssumeRole

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); }); 
like image 490
Vladimir Venegas Avatar asked Dec 26 '16 22:12

Vladimir Venegas


People also ask

How do you give AssumeRole permission?

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.

What is STS AssumeRole in AWS?

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.

How do I allow a user to assume AWS?

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.


2 Answers

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.

like image 146
Vladimir Venegas Avatar answered Oct 02 '22 10:10

Vladimir Venegas


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.

like image 41
smac2020 Avatar answered Oct 02 '22 10:10

smac2020