Aaron

Handle pagination;

... ... @@ -31,22 +31,18 @@ import argparse
# )
###
if __name__ == '__main__':
def get_all_headers(data):
# Parse CLI args
argp = argparse.ArgumentParser(description='Export AWS users to JSON')
argp.add_argument('--file', '-f', dest='outfile',
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()
keys = []
# Initialize client
client = boto3.client('iam')
for item in data:
for key in item.keys():
if key not in keys:
keys.append(key)
user_data = []
return keys
user_list = client.list_users()
def fetch_user_data(user_list):
for user in user_list['Users']:
... ... @@ -92,6 +88,7 @@ if __name__ == '__main__':
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'])
policy_data['Policy']['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
user['AttachedUserPolicies'].append(policy_data['Policy'])
... ... @@ -107,6 +104,30 @@ if __name__ == '__main__':
user_data.append(user)
return user_data
if __name__ == '__main__':
# Parse CLI args
argp = argparse.ArgumentParser(description='Export AWS users to JSON')
argp.add_argument('--file', '-f', dest='outfile',
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()
# Initialize client
client = boto3.client('iam')
user_data = []
user_list = client.list_users()
user_data += fetch_user_data(user_list)
while user_list['IsTruncated'] is True:
user_list = client.list_users(Marker=user_list['Marker'])
user_data += fetch_user_data(more_users)
# Dump user data
out = json.dumps(user_data, default=str, indent=2)
... ...