2016-04-18 24 views
0

スクリプトを使用してIAMポリシーをバックアップまたは複製できるように、awot IAMポリシーの詳細をbotoで取得しようとしています。 私はboto 2と3のドキュメントを検索しましたが、設定されたポリシーのjsonデータを取得する可能性はありませんでした。botoを使用してIAMポリシー文書を取得する方法

(成功した)私がやった:

  • は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/*" 
          ] 
         } 
        ] 
    } 
    
  • 答えて

    1

    より良いサポートとドキュメントがあるよう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

    である私はちょうどあなたがすべて行ってもいいですboto3するすべてのコマンドに置き換え、CLIの下に簡単なチェックをしました。

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

    関数get_account_authorization_details()は、実際にポリシーの詳細を含んでいました。 – sarnu

    0

    私はあなたが次を使用できると思う:

    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

    3

    boto3に移動してください。

    ポリシー側からアプローチする:ポリシーARNを特定し、ARNを使用してポリシー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" 
        } 
    ] 
    

    あなたは、特にあなたの質問にアクション/声明を要求しました。私は 'Document'と 'Statement'プロパティを表示してその違いを表示しました。

    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

    関連する問題