Aaron

Handle pagination;

@@ -31,22 +31,18 @@ import argparse @@ -31,22 +31,18 @@ import argparse
31 # ) 31 # )
32 ### 32 ###
33 33
34 -if __name__ == '__main__': 34 +def get_all_headers(data):
35 35
36 - # Parse CLI args  
37 - argp = argparse.ArgumentParser(description='Export AWS users to JSON')  
38 - argp.add_argument('--file', '-f', dest='outfile',  
39 - action='store', help='File to export data into')  
40 - argp.add_argument('--verbose-policies', '-p', dest='policies',  
41 - action='store_true', help='Export full policy details')  
42 - args = argp.parse_args() 36 + keys = []
43 37
44 - # Initialize client  
45 - client = boto3.client('iam') 38 + for item in data:
  39 + for key in item.keys():
  40 + if key not in keys:
  41 + keys.append(key)
46 42
47 - user_data = [] 43 + return keys
48 44
49 - user_list = client.list_users() 45 +def fetch_user_data(user_list):
50 46
51 for user in user_list['Users']: 47 for user in user_list['Users']:
52 48
@@ -92,6 +88,7 @@ if __name__ == '__main__': @@ -92,6 +88,7 @@ if __name__ == '__main__':
92 for policy in user_attached_policy_data['AttachedPolicies']: 88 for policy in user_attached_policy_data['AttachedPolicies']:
93 policy_data = client.get_policy(PolicyArn=policy['PolicyArn']) 89 policy_data = client.get_policy(PolicyArn=policy['PolicyArn'])
94 policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId']) 90 policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId'])
  91 + policy_doc = client.get_policy_version(PolicyArn=policy['PolicyArn'], VersionId=policy_data['Policy']['DefaultVersionId'])
95 policy_data['Policy']['PolicyDocument'] = policy_doc['PolicyVersion']['Document'] 92 policy_data['Policy']['PolicyDocument'] = policy_doc['PolicyVersion']['Document']
96 user['AttachedUserPolicies'].append(policy_data['Policy']) 93 user['AttachedUserPolicies'].append(policy_data['Policy'])
97 94
@@ -107,6 +104,30 @@ if __name__ == '__main__': @@ -107,6 +104,30 @@ if __name__ == '__main__':
107 104
108 user_data.append(user) 105 user_data.append(user)
109 106
  107 + return user_data
  108 +
  109 +if __name__ == '__main__':
  110 +
  111 + # Parse CLI args
  112 + argp = argparse.ArgumentParser(description='Export AWS users to JSON')
  113 + argp.add_argument('--file', '-f', dest='outfile',
  114 + action='store', help='File to export data into')
  115 + argp.add_argument('--verbose-policies', '-p', dest='policies',
  116 + action='store_true', help='Export full policy details')
  117 + args = argp.parse_args()
  118 +
  119 + # Initialize client
  120 + client = boto3.client('iam')
  121 +
  122 + user_data = []
  123 +
  124 + user_list = client.list_users()
  125 + user_data += fetch_user_data(user_list)
  126 +
  127 + while user_list['IsTruncated'] is True:
  128 + user_list = client.list_users(Marker=user_list['Marker'])
  129 + user_data += fetch_user_data(more_users)
  130 +
110 # Dump user data 131 # Dump user data
111 out = json.dumps(user_data, default=str, indent=2) 132 out = json.dumps(user_data, default=str, indent=2)
112 133