我试图通过boto获取aws IAM策略的详细信息,以便能够通过脚本备份或复制IAM策略。 我搜索了boto 2和3的文档,但没有找到任何获取已配置策略的json数据的可能性。
我(成功)做了什么:
但我无法找到一种方法来检索关联的JSON数据(管理控制台中的“策略文档”)以获取它。
我尝试用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/*"
]
}
]
}
答案 0 :(得分:8)
请转到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"
}
]
您在问题中特别请求了操作/声明。我打印了“文档”和“声明”属性以显示差异。
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
答案 1 :(得分:1)
请切换到boto3,因为有更好的支持和文档。 与boto3文档中一样,get_policy()不会给你policydocument。
我能得到的最好的是get_account_authorization_details()
我在cli下做了一个快速检查,只需将所有命令替换为boto3然后你就可以了。
aws iam get-account-authorization-details --filter 'LocalManagedPolicy'
答案 2 :(得分: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