阅读背景:

如何从另一个AWS角色承担AWS角色?

来源:互联网 

I have two AWS account - lets say A and B.

我有两个AWS账户 - 比方说A和B.

In account B, I have a role defined that allow access to another role from account A. Lets call it Role-B

在帐户B中,我定义了一个角色,允许从帐户A访问另一个角​​色。让我们将其称为Role-B

{
  "Version": "2012-10-17",
  "Statement": [
  {
     "Effect": "Allow",
     "Principal": {
         "AWS": "arn:aws:iam::********:role/RoleA"
     },
    "Action": "sts:AssumeRole"
  }]
}

In account A, I have defined a role that allows the root user to assume role. Lets call it Role-A

在帐户A中,我定义了一个允许root用户担任角色的角色。让我们称之为Role-A

{
  "Version": "2012-10-17",
  "Statement": [
  {
     "Effect": "Allow",
     "Principal": {
         "AWS": "arn:aws:iam::********:root"
     },
    "Action": "sts:AssumeRole"
  }]
}

Role A has the following policy attached to it

角色A附加了以下策略

 {
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::****:role/RoleB",
            "Effect": "Allow"
       }]
 }

As a user in account A, I assumed the Role-A. Now using this temporary credential, I want to assume the Role-B and access the resource owned by account B. I have the below code

作为帐户A中的用户,我假设了角色-A。现在使用这个临时凭证,我想假设角色B并访问帐户B拥有的资源。我有以下代码

client = boto3.client('sts')

firewall_role_object = client.assume_role(
    RoleArn=INTERMEDIARY_IAM_ROLE_ARN,
    RoleSessionName=str("default"),
    DurationSeconds=3600)

firewall_credentials = firewall_role_object['Credentials']

firewall_client = boto3.client(
    'sts',
    aws_access_key_id=firewall_credentials['AccessKeyId'],
    aws_secret_access_key=firewall_credentials['SecretAccessKey'],
    aws_session_token=firewall_credentials['SessionToken'], )

optimizely_role_object = firewall_client.assume_role(
    RoleArn=CUSTOMER_IAM_ROLE_ARN,
    RoleSessionName=str("default"),
    DurationSeconds=3600)

print(optimizely_role_object['Credentials'])

This code works for the set of roles I got from my client but is not working for the roles I defined between two of the AWS account I have access to.

此代码适用于我从客户端获得的一组角色,但不适用于我在我有权访问的两个AWS账户之间定义的角色。

1 个解决方案

#1


5  

Finally got this working. The above configuration is correct. There was a spelling mistake in the policy.

终于搞定了这个。以上配置是正确的。政策中存在拼写错误。

I will keep this question here for it may help someone who want to achieve double hop authentication using roles.

我会在这里保留这个问题,因为它可以帮助那些想要使用角色实现双跳认证的人。


分享到: