【问题标题】:How to get IAM Policy Document via boto如何通过 boto 获取 IAM 政策文件
【发布时间】:2016-08-09 21:45:09
【问题描述】:

我正在尝试通过 boto 获取 aws IAM 策略的详细信息,以便能够通过脚本备份或复制 IAM 策略。 我已经搜索了 boto 2 和 3 的文档,但没有发现任何可能获取已配置策略的 json 数据。

我(成功地)做了什么:

  • 通过 IAM 管理控制台创建了策略
  • 将其分配给角色
  • 使用它通过 boto 创建 ec2 实例

但我无法找到一种方法来检索关联的 JSON 数据(管理控制台中的“策略文档”)以将其放入 boto。

我对 boto 的尝试:

import boto.iam
REGION_NAME = 'eu-west-1'
iam_conn = boto.iam.connect_to_region(REGION_NAME)
arn = 'arn:myproperlyformattedarn'
p = iam_conn.get_policy(arn)
print p

结果:

{
    "get_policy_response": {
        "response_metadata": {
            "request_id": "XXXXX-XXXX-XXXX-XXXX-XXXX"
        },
        "get_policy_result": {
            "policy": {
                "update_date": "2016-04-15T12:51:21Z",
                "create_date": "2016-04-15T12:51:21Z",
                "is_attachable": "true",
                "policy_name": "My_Policy_Name",
                "default_version_id": "v1",
                "attachment_count": "1",
                "path": "/",
                "arn": "arn:aws:iam::123456789:policy/VerticaTest_GetConfigsFromS3",
                "policy_id": "XXXSOMELONGSTRINGXXXX"
            }
        }
    }
}

我所追求的是这样的(管理控制台中的策略文档):

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::mybucketname",
                "arn:aws:s3:::mybucketname/*"
            ]
        }
    ]
}

【问题讨论】:

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


    【解决方案1】:

    请移至boto3。

    从策略方面处理此问题:识别 Policy ARN,使用 ARN 识别 Policy DefaultVersionId,使用 ARN 和 DefaultVersionId 检索 PolicyDocument。

    import boto3
    import json
    
    arn = 'arn:aws:iam::aws:policy/AdministratorAccess'
    
    iam = boto3.client('iam')
    policy = iam.get_policy(
        PolicyArn = arn
    )
    policy_version = iam.get_policy_version(
        PolicyArn = arn, 
        VersionId = policy['Policy']['DefaultVersionId']
    )
    
    print(json.dumps(policy_version['PolicyVersion']['Document']))
    print(json.dumps(policy_version['PolicyVersion']['Document']['Statement']))
    

    运行此代码并将输出通过管道传输到“jq”。你会得到以下输出:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Action": "*",
          "Resource": "*",
          "Effect": "Allow"
        }
      ]
    }
    
    [
      {
        "Action": "*",
        "Resource": "*",
        "Effect": "Allow"
      }
    ]
    

    您在问题中明确要求了操作/声明。我打印了“文档”和“声明”属性以显示差异。

    http://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_policy http://boto3.readthedocs.io/en/latest/reference/services/iam.html#IAM.Client.get_policy_version

    【讨论】:

      【解决方案2】:

      请切换到 boto3,因为有更好的支持和文档。 与 boto3 文档一样,get_policy() 不会为您提供 policydocument。

      我能得到的最好的方法是 get_account_authorization_details()

      http://boto3.readthedocs.org/en/latest/reference/services/iam.html#IAM.Client.get_account_authorization_details

      我在 cli 下进行了快速检查,只需将所有命令替换为 boto3 即可。

      aws iam get-account-authorization-details --filter 'LocalManagedPolicy'
      

      【讨论】:

      • 函数 get_account_authorization_details() 实际上包含了策略详细信息。
      【解决方案3】:

      我认为您可以使用以下内容:

      get_policy(policy_arn)
      Get policy information.
      
      Parameters: policy_arn (string) – The ARN of the policy to get information for
      
      get_policy_version(policy_arn, version_id)¶
      Get policy information.
      
      Parameters: 
      policy_arn (string) – The ARN of the policy to get information for a specific version
      version_id (string) – The id of the version to get information for
      

      http://boto.cloudhackers.com/en/latest/ref/iam.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-24
        • 2020-03-30
        • 2019-05-16
        • 2020-07-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多