Aaron

Update aws_users_export.py

... ... @@ -31,12 +31,23 @@ import argparse
# )
###
def get_all_headers(data):
keys = []
for item in data:
for key in item.keys():
if key not in keys:
keys.append(key)
return keys
if __name__ == '__main__':
# Parse CLI args
argp = argparse.ArgumentParser(description='Export AWS users to CSV')
argp = argparse.ArgumentParser(description='Export AWS users to JSON')
argp.add_argument('--file', '-f', dest='outfile',
action='store', help='File to export JSON data into')
action='store', help='File to export data into')
argp.add_argument('--verbose-policies', '-p', dest='policies',
action='store_true', help='Export full policy details')
args = argp.parse_args()
... ... @@ -44,16 +55,15 @@ if __name__ == '__main__':
# Initialize client
client = boto3.client('iam')
user_data = {}
user_data = []
user_list = client.list_users()
for user in user_list['Users']:
user_data[user['UserName']] = user
# Get Group Data
group_data = client.list_groups_for_user(UserName=user['UserName'])
user_data[user['UserName']]['Groups'] = []
user['Groups'] = []
# Get inline and attached group policies
for group in group_data['Groups']:
... ... @@ -62,49 +72,52 @@ if __name__ == '__main__':
group_attached_policy_data = client.list_attached_group_policies(GroupName=group['GroupName'])
if args.policies:
group['InlineGroupPolicies'] = {}
group['InlineGroupPolicies'] = []
for policy in group_inline_policy_data['PolicyNames']:
policy_data = client.get_group_policy(GroupName=group['GroupName'], PolicyName=policy)
group['InlineGroupPolicies'][policy] = policy_data['PolicyDocument']
#group['InlineGroupPolicies'][policy] = policy_data['PolicyDocument']
group['InlineGroupPolicies'].append(policy_data['PolicyDocument'])
group['AttachedGroupPolicies'] = {}
group['AttachedGroupPolicies'] = []
for policy in group_attached_policy_data['AttachedPolicies']:
policy_data = client.get_policy(PolicyArn=policy['PolicyArn'])
policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId'])
group['AttachedGroupPolicies'][policy['PolicyName']] = policy_data['Policy']
group['AttachedGroupPolicies'][policy['PolicyName']]['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
policy_data['Policy']['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
group['AttachedGroupPolicies'].append(policy_data['Policy'])
else:
group['InlineGroupPolicies'] = group_inline_policy_data['PolicyNames']
group['AttachedGroupPolicies'] = group_attached_policy_data['AttachedPolicies']
user_data[user['UserName']]['Groups'].append(group)
user['Groups'].append(group)
user_inline_policy_data = client.list_user_policies(UserName=user['UserName'])
user_attached_policy_data = client.list_attached_user_policies(UserName=user['UserName'])
if args.policies:
user_data[user['UserName']]['InlineUserPolicies'] = {}
user['InlineUserPolicies'] = []
for policy in user_inline_policy_data['PolicyNames']:
policy_data = client.get_user_policy(UserName=user['UserName'], PolicyName=policy)
user_data[user['UserName']]['InlineUserPolicies'][policy] = policy_data['PolicyDocument']
user['InlineUserPolicies'].append(policy_data['PolicyDocument'])
user_data[user['UserName']]['AttachedUserPolicies'] = {}
user['AttachedUserPolicies'] = []
for policy in user_attached_policy_data['AttachedPolicies']:
policy_data = client.get_policy(PolicyArn=policy['PolicyArn'])
policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId'])
policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId'])
user_data[user['UserName']]['AttachedUserPolicies'][policy['PolicyName']] = policy_data['Policy']
user_data[user['UserName']]['AttachedUserPolicies'][policy['PolicyName']]['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
policy_data['Policy']['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
user['AttachedUserPolicies'].append(policy_data['Policy'])
else:
user_data[user['UserName']]['InlineUserPolicies'] = user_inline_policy_data['PolicyNames']
user_data[user['UserName']]['AttachedUserPolicies'] = user_attached_policy_data['AttachedPolicies']
user['InlineUserPolicies'] = user_inline_policy_data['PolicyNames']
user['AttachedUserPolicies'] = user_attached_policy_data['AttachedPolicies']
tag_data = client.list_user_tags(UserName=user['UserName'])
user_data[user['UserName']]['Tags'] = tag_data['Tags']
user['Tags'] = tag_data['Tags']
mfa_data = client.list_mfa_devices(UserName=user['UserName'])
user_data[user['UserName']]['MFADevices'] = mfa_data['MFADevices']
user['MFADevices'] = mfa_data['MFADevices']
user_data.append(user)
# Dump user data
out = json.dumps(user_data, default=str, indent=2)
... ... @@ -115,4 +128,4 @@ if __name__ == '__main__':
f.write(out)
else:
# Print to stdout
print(out)
\ No newline at end of file
print(out)
... ...