-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcognito-export.py
executable file
·105 lines (78 loc) · 3.21 KB
/
cognito-export.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /usr/bin/env python3
"""Cognito user data export
Usage: cognito-export.py [options] <user-pool-id>
Options:
-h --help Show this screen
--profile <name> The AWS credentials profile to use
--csv Export a CSV summary in addition to JSON data
"""
import subprocess
import json
import csv
from datetime import datetime
import os
from docopt import docopt
DATE_FORMAT = '%Y/%m/%d %H:%M:%S'
def pool_info(pool_id):
"""Split a user pool ID into region and ID"""
return pool_id.split('_')
class CognitoExport:
def __init__(self, pool_id, profile='default'):
"""Fetch all users using AWS CLI"""
self.users = []
options = {
'region': pool_info(pool_id)[0],
'profile': profile,
'user-pool-id': pool_id,
}
args = ['aws', 'cognito-idp', 'list-users']
for key, value in options.items():
args += ['--' + key, value]
done = False
token = None
# Loop through all pages of API results
while not done:
all_args = args + (['--pagination-token', token] if token is not None else [])
result = subprocess.run(all_args, capture_output=True)
data = json.loads(result.stdout.decode('utf-8'))
if 'PaginationToken' in data:
token = data['PaginationToken']
else:
done = True
self.users += [self.format(u) for u in data['Users']]
def format(self, user):
"""Reformat API data for a single user"""
attributes = {item['Name']: item['Value'] for item in user['Attributes']}
user['Attributes'] = attributes
return user
def export_json(self, path):
"""Export all user data to a JSON file"""
with open(path, 'w') as f:
json.dump(self.users, f, indent=4)
def export_csv(self, path):
"""Export default Cognito parameters to a CSV file"""
with open(path, 'w') as f:
writer = csv.writer(f)
writer.writerow(['ID', 'Name', 'Email', 'Phone', 'Created', 'Modified'])
for user in self.users:
writer.writerow([
user['Attributes']['sub'],
'{} {}'.format(user['Attributes'].get('given_name', ''),
user['Attributes'].get('family_name', '')),
user['Attributes'].get('email', ''),
user['Attributes'].get('phone_number', ''),
datetime.utcfromtimestamp(user['UserCreateDate']).strftime(DATE_FORMAT),
datetime.utcfromtimestamp(user['UserLastModifiedDate']).strftime(DATE_FORMAT),
])
if __name__ == '__main__':
args = docopt(__doc__)
print('Fetching data...')
data = CognitoExport(args['<user-pool-id>'], args['--profile'])
filename = 'cognito-{}-{}'.format(pool_info(args['<user-pool-id>'])[1],
datetime.now().strftime('%Y%m%d-%H%M'))
# Export JSON
data.export_json(os.path.join(filename + '.json'))
# Export CSV
if args['--csv']:
data.export_csv(os.path.join(os.getcwd(), filename + '.csv'))
print('Exported {} users'.format(len(data.users)))