【问题标题】:How to get policy document for aws managed policy of a iam user in python boto3?如何在 python boto3 中获取 iam 用户的 aws 托管策略的策略文档?
【发布时间】:2020-10-28 21:43:06
【问题描述】:

我可以通过“get_user_policy()”客户端检索内联策略的策略文档。有什么方法可以检索 IAM 用户的 AWS 托管策略的策略文档..?

import boto3
client = boto3.client('iam')
policy = iam.get_user_policy(UserName="<string>",PolicyName = "<string>")
doc = dict((k,response[k]) for k in ['PolicyDocument']if k in policy)
print(doc)

似乎我们可以使用它的 arn 来获取托管策略的策略文档。但我不确定如何获取附加到特定 IAM 用户的所有托管策略的 arn。

那么,如何在python中获取iam用户的aws管理策略的策略文档

提前致谢。

【问题讨论】:

    标签: python-3.x amazon-web-services boto3 amazon-iam policy


    【解决方案1】:

    我创建了一个名为 test1 的用户并附加了 IAMReadOnlyAccess 和 PowerUserAccess。问题是 AWS 托管策略和客户托管策略的 ARN 差异。 For more info.

    import boto3
    
    iam_res = boto3.resource('iam')
    user = iam_res.User('test1')
    
    policy_iterator = user.attached_policies.all()
    
    for each in policy_iterator:
        if each.arn.startswith('arn:aws:iam::aws'):
            print(each.default_version.document)
    

    这是输出。

    {'Statement': [{'Action': ['iam:GenerateCredentialReport',
                               'iam:GenerateServiceLastAccessedDetails',
                               'iam:Get*',
                               'iam:List*',
                               'iam:SimulateCustomPolicy',
                               'iam:SimulatePrincipalPolicy'],
                    'Effect': 'Allow',
                    'Resource': '*'}],
     'Version': '2012-10-17'}
    {'Statement': [{'Effect': 'Allow',
                    'NotAction': ['iam:*', 'organizations:*', 'account:*'],
                    'Resource': '*'},
                   {'Action': ['iam:CreateServiceLinkedRole',
                               'iam:DeleteServiceLinkedRole',
                               'iam:ListRoles',
                               'organizations:DescribeOrganization',
                               'account:ListRegions'],
                    'Effect': 'Allow',
                    'Resource': '*'}],
     'Version': '2012-10-17'}
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-12-06
        • 2017-10-22
        • 1970-01-01
        • 2018-11-28
        • 2021-11-12
        • 1970-01-01
        • 2016-06-05
        相关资源
        最近更新 更多