gpt4 book ai didi

python - 我们如何获取 IAM 用户、他们的组和策略?

转载 作者:太空狗 更新时间:2023-10-29 22:05:22 25 4
gpt4 key购买 nike

我需要获取所有 aws 用户、他们相应的组、策略以及是否为他们激活了 mfa。谁能告诉我如何通过 aws cli 或 boto 完成。

我有一个脚本,它只获取 aws 中的所有用户。

  import boto3
from boto3 import *
import argparse

access_key = ''
secret_key = ''

def get_iam_uses_list():
client = boto3.client('iam',
aws_access_key_id=access_key,
aws_secret_access_key=secret_key)
my_list=list()
iam_all_users = client.list_users(MaxItems=200)
for user in iam_all_users['Users']:
my_list.append(user['UserName'])
#

for i in my_list:
print i

# print "read complete"
#
# for i in my_list:
# iam_user_policy=client.list_attached_user_policies(UserName=i)
# for policy in iam_user_policy['AttachedPolicies']:
# print "%s \t %s" %(i, policy['PolicyName'])

def main():
parser = argparse.ArgumentParser()
parser.add_argument('access_key', help='Access Key');
parser.add_argument('secret_key', help='Secret Key');
args = parser.parse_args()
global access_key
global secret_key
access_key = args.access_key
secret_key = args.secret_key
get_iam_uses_list()

if __name__ =='__main__':main()

最佳答案

在这里,我使用 boto 命令执行四个操作 -

  1. 列出所有用户
  2. 列出附加到每个用户的策略
  3. 列出添加到每个用户的角色
  4. List Mfa devices to see if MFA has been configured by User or not(这里我不是检查MFA是否未启用,而是检查设备是否已被用户配置。)

获取与 AWS 账户的 IAM 连接

import boto3
client = boto3.client('iam',aws_access_key_id="XXX",aws_secret_access_key="XXX")

获取 IAM 用户这将打印所有用户名。如果您还想打印其他详细信息,您可以自定义。

users = client.list_users()
for key in users['Users']:
print key['UserName']

获取附加到每个用户的策略列表

for key in users['Users']:
List_of_Policies = client.list_user_policies(UserName=key['UserName'])
for key in List_of_Policies['PolicyNames']:
print key['PolicyName']

获取附加到每个用户的组列表

for key in users['Users']:
List_of_Groups = client.list_groups_for_user(UserName=key['UserName'])
for key in List_of_Groups['Groups']:
print key['GroupName']

检查是否配置了 MFA 设备

for key in users['Users']:
List_of_MFA_Devices = client.list_mfa_devices(UserName=key['UserName'])
for key in List_of_MFA_Devices['MFADevices']:
print key

您可以进一步检查 List_of_MFA_Devices['MFADevices'] 是否为空。如果为空,则 MFA 设备未配置。

如果您想将输出添加为 Dict 列表,其中每个索引将包含具有 userName、Groups、Policy、isMFA_flag_configured 或 not 值对的字典。使用以下代码 -

import boto3
client = boto3.client('iam',aws_access_key_id="XXXX",aws_secret_access_key="YYY")
users = client.list_users()
user_list = []
for key in users['Users']:
result = {}
Policies = []
Groups=[]

result['userName']=key['UserName']
List_of_Policies = client.list_user_policies(UserName=key['UserName'])

result['Policies'] = List_of_Policies['PolicyNames']

List_of_Groups = client.list_groups_for_user(UserName=key['UserName'])

for Group in List_of_Groups['Groups']:
Groups.append(Group['GroupName'])
result['Groups'] = Groups

List_of_MFA_Devices = client.list_mfa_devices(UserName=key['UserName'])

if not len(List_of_MFA_Devices['MFADevices']):
result['isMFADeviceConfigured']=False
else:
result['isMFADeviceConfigured']=True
user_list.append(result)

for key in user_list:
print key

上述代码的输出 -

{'userName': 'user1', 'Groups': ['grp1','grp2'], 'Policies':['policy1','policy2], 'isMFADeviceConfigured': False/True}

{'userName': 'user2', 'Groups': ['grp1','grp2'], 'Policies': ['policy1','policy2], 'isMFADeviceConfigured': False/True}

关于python - 我们如何获取 IAM 用户、他们的组和策略?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com