AWS跨賬號授權訪問對方資源

假設A賬號要控制B賬號資源,但是B賬號不想給A賬號創建專用IAM用戶,這是就可以用role來給A賬號中的用戶受臨時權限。

具體方案爲:
(1)    在B賬號(被控制賬號)中創建個角色,選擇Role for Cross-Account Access,然後選擇Provide access between AWS accounts you own,然後在Account ID裏寫上被授權賬號,也就是A賬號的AccountID
(2)    然後在B賬號中給這個角色授權,這個跟給IAM用戶授權方法一樣
(3)    然後在A賬號選擇允許執行跨賬號權限的組或者用戶,然後授權給這些組或者用戶,授權json如下:

{
  "Version": "2012-10-17",
  "Statement": [{
      "Effect": "Deny",
      "Action": "sts:AssumeRole",
      "Resource": "arn:aws:iam::AccounId-A:role/role-name"
    }]
}

這個是給A賬號中的用戶或者組執行AssumeRole(成爲某角色)的權限
(4)    然後A賬號中的用戶,就可以通過AssumeRole的API來執行B賬號中這個角色的權限
(5)    A中的用戶控制A中的資源,還是原來的方法,A中的用戶控制B中的資源時,需要先使用AssumeRole API獲取角色的臨時權限

給個python調用的例子

#!/usr/bin/env python
import boto
from boto.sts import STSConnection
from boto.s3.connection import S3Connection





## 需要提前寫好A賬號中用戶的boto的授權配置文件~/.boto sts_connection = STSConnection() assumedRoleObject = sts_connection.assume_role( role_arn="arn:aws:iam::account-of-role-to-assume:role/name-of-role", role_session_name="AssumeRoleSession1" ) # Use the temporary credentials returned by AssumeRole to call Amazon S3 # and list all buckets in the account that owns the role (the trusting account) ## AssumeRole API 返回角色提供的臨時AccessKey Pairs s3_connection = S3Connection( aws_access_key_id=assumedRoleObject.credentials.access_key, aws_secret_access_key=assumedRoleObject.credentials.secret_key, security_token=assumedRoleObject.credentials.session_token ) bucketList = s3_connection.get_all_buckets() for bucket in bucketList: print bucket.name
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章