div*_*eep 6 amazon-web-services aws-iam
我想知道如何simulate-principal-policy将 AWS CLI 用于代入角色。
为了提供一些上下文,作为我的应用程序启动的一部分,我想确保应用程序具有访问它需要的所有 AWS 资源的必要权限。我通过获取调用者身份aws sts get-caller-identity并使用返回的调用者身份作为simulate-principal-policy请求的策略源来实现这一点。
当我们的应用程序在 EC2 上运行时,它使用一个假定的角色。所以,get-caller-identity返回一个假定的角色 arn。
如果我尝试simulate-principal-policy使用我的用户 arn 作为策略源 arn 执行,则该命令可以正常工作。
aws iam simulate-principal-policy --action-names "sqs:Receivemessage" --policy-source-arn "arn:aws:sts::123456789021:user/divesh"
但是,尝试使用假定的角色执行上述命令会报告错误。
aws iam simulate-principal-policy --action-names "sqs:Receivemessage" --policy-source-arn "arn:aws:sts::123456789021:assumed-role/development/development-session"
An error occurred (InvalidInput) when calling the SimulatePrincipalPolicy operation: Invalid Entity Arn: arn:aws:sts::123456789021:assumed-role/development/development-session does not clearly define entity type and name.
我们的应用程序在 Kubernetes 集群上运行,并使用kiam将 IAM 角色与 Pod 相关联。
小智 2
您的请求的问题在于您使用的是“配置文件 ARN”而不是“角色 ARN”。要获取角色 Arn,您可以执行以下操作:
Instance Profile Arn:arn:aws:sts::123456789021:assumed-role/development/development-session变成development/development-session
aws iam get-instance-profile --instance-profile-name Instance Profile Arn
{
   "InstanceProfile":{
      "Roles":[
         {
            "Arn":"arn:aws:iam::992863558783:role/YourRole"
         }
      ]
   }
}
aws iam simulate-principal-policy --action-names "sqs:Receivemessage" --policy-source-arn "arn:aws:iam::992863558783:role/YourRole"
在 Python 中,脚本如下所示:
import boto3
iam= boto3.client('iam')
profileArn = 'arn:aws:sts::123456789021:assumed-role/development/development-session'
iamProfileName = iamInstanceProfileArn.split(':assumed-role/')[1]
profile = iam.get_instance_profile(InstanceProfileName=iamProfileName)
policySourceArns = []
for role in profile['InstanceProfile']['Roles']:
    policySourceArns.append(role['Arn'])
retval = iam.simulate_principal_policy(
    PolicySourceArn = policySourceArns[0],
    ActionNames = ['sqs:Receivemessage']
)
| 归档时间: | 
 | 
| 查看次数: | 1084 次 | 
| 最近记录: |