AWS boto3:模拟给定权限json策略文件的IAM角色

时间:2020-05-05 17:08:16

标签: amazon-web-services boto3 amazon-iam

我正在尝试使用AWS IAM-Simulator with boto3

可以说我有 policy1.json,policy2.json 文件,它们是有效的AWS IAM策略json。可以说json1成立了:

{
"Version": "2023-12-15",
"Statement": [
    {
        "Effect": "Allow",
        "Action": [
            "ec2:CreateSnapshot",
            "ec2:CreateTags"
        ],
        "Resource": "*"
    },
    {
        "Effect": "Allow",
        "Action": "ec2:DeleteSnapshot",
        "Resource": "*",
        "Condition": {
            "StringLike": {
                "ec2:ResourceTag/my_cool_tag": "*"
            }
        }
    }
]

}

我想检查一个具有应用角色的用户,例如role_arn = 12345 可以执行策略json文件中指定的操作。

我希望它具有通用性,即能够接收任何有效的策略文件并模拟附加的 arn角色是否可以执行有/无policy.json中指定条件的所有操作文件(适用于任何文件)

有没有办法使用boto3做到这一点?我尝试阅读文档,但找不到任何可以做到这一点的示例

对于此示例,我想到了:

        ressult1 =iam_client.simulate_principal_policy(
        PolicySourceArn=12345,
        ActionNames=[
        "ec2:CreateSnapshot",
        "ec2:CreateTags"
    ])

    ressult2 =iam_client.simulate_principal_policy(
        PolicySourceArn=12345,
        ActionNames=[ec2:DeleteSnapshot],
        ContextEntries=[
            {"ContextKeyName": "ec2:ResourceTag/my_cool_tag", "ContextKeyValues": [""], "ContextKeyType": "string",}
        ])

但这不是通用的 想做这样的事情:

    with open('policy1.json') as json_file:
          policy_data = json.load(json_file)
          ressult =iam_client.simulate_principal_policy(
                     PolicySourceArn=12345,
                     policy_dict = policy_loaded)

在此感谢您的帮助。 谢谢!

0 个答案:

没有答案