gpt4 book ai didi

amazon-web-services - 使用假定的角色模拟主要策略

转载 作者:行者123 更新时间:2023-12-04 11:58:06 25 4
gpt4 key购买 nike

我想知道如何使用 simulate-principal-policy 使用 AWS CLI 来扮演一个假定的角色。

为了提供一些上下文,作为我的应用程序启动的一部分,我想确保应用程序具有访问它需要的所有 AWS 资源的必要权限。我通过使用 aws sts get-caller-identity 获取调用者身份并使用返回的调用者身份作为 simulate-principal-policy 请求的策略源 arn 来做到这一点。

当我们的应用程序在 EC2 上运行时,它使用一个假定的角色。所以,get-caller-identity 返回一个假定的角色 arn。

如果我尝试使用我的用户 arn 作为策略源 arn 执行 simulate-principal-policy,则该命令可以正常工作。

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。

最佳答案

您的请求的问题在于您使用的是“配置文件 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
  • 在结果文档中找到角色 Arn:

  • {
    "InstanceProfile":{
    "Roles":[
    {
    "Arn":"arn:aws:iam::992863558783:role/YourRole"
    }
    ]
    }
    }
  • 在模拟主体策略中使用此 ARN
  • 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']
    )

    关于amazon-web-services - 使用假定的角色模拟主要策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52941478/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com