简体   繁体   English

亚马逊 iam 在 boto3 中获取内联政策声明

[英]amazon iam get inline policy statement in boto3

I'am using boto3 list_role_policies to get all inline policies but I'm getting only PolicyNames.我正在使用 boto3 list_role_policies来获取所有内联策略,但我只获取 PolicyNames。 I would like to obtain the json statement as well.我也想获取 json 语句。

Something like this:像这样的东西:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "mobileanalytics:PutEvents",
                "cognito-sync:*",
                "cognito-identity:*"
            ],
            "Resource": [
                "*"
            ]
        }
    ]
}

Please, remember I don't want managed policies, only Inline .请记住,我不想要托管策略,只想要Inline

Thanx in advance.提前谢谢。

You can use iam.Role.policies to iterate all inline policies for a specific role.您可以使用iam.Role.policies迭代特定角色的所有内联策略。 You will get iterable of iam.RolePolicy instances which have policy_document attribute with policy JSON.您将获得具有策略 JSON 的policy_document属性的iam.RolePolicy实例的迭代。

import boto3

iam = boto3.resource('iam')

role_name = 'RoleName'
role = iam.Role(role_name)
for policy in role.policies.all():
    print(policy.name)
    print(policy.policy_document)

You can use the result of list_role_policies() to call get_role_policy() on each of the policy names returned:您可以使用list_role_policies()的结果对返回的每个策略名称调用get_role_policy()

Retrieves the specified inline policy document that is embedded with the specified IAM role.检索嵌入了指定 IAM 角色的指定内联策略文档。

response = client.get_role_policy(
    RoleName='string',
    PolicyName='string'
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM