-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·162 lines (133 loc) · 4.81 KB
/
cli.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const AWS = require('aws-sdk');
const cognitoIsp = new AWS.CognitoIdentityServiceProvider();
const bluebird = require('bluebird');
const fs = require('fs');
const sanitizeFilename = require('sanitize-filename');
const JSONStream = require('JSONStream');
const streamToPromise = require('stream-to-promise');
const debug = require('debug')('cognito-tool');
const mkdirp = bluebird.promisify(require('mkdirp'));
const assert = require('assert');
const cli = meow(`
Usage
$ cognito-tool backup-users <user-pool-id> <option> Backup all users in a single user pool
$ cognito-tool backup-all-users <options> Backup all users in all user pools for this account
$ cognito-tool restore <user-pool-id> --file <JSON_user_file> Restore users to a single user pool
AWS_ACCESS_KEY_ID , AWS_SECRET_ACCESS_KEY and AWS_REGION
(optional for assume role: AWS_SESSION_TOKEN)
is specified in env variables or ~/.aws/credentials
Options
--dir Path to export all pools, all users to (defaults to current dir), only for backup
--file Path to file with user details, only for retore
`);
const methods = {
'backup-users': backupUsersCli,
'backup-all-users': backupAllUsersCli,
'restore': restore,
};
const method = methods[cli.input[0]] || cli.showHelp();
bluebird.resolve(method.call(undefined, cli))
.catch(err => {
console.error(err.stack);
process.exit(1);
});
function backupUsersCli(cli) {
const userPoolId = cli.input[1];
const file = sanitizeFilename(getFilename(userPoolId));
if (!userPoolId) {
console.error('user-pool-id is required');
cli.showHelp();
}
return backupUsers(cognitoIsp, userPoolId, file);
}
function backupAllUsersCli(cli) {
const dir = cli.flags.dir || '.';
return mkdirp(dir)
.then(() => bluebird.mapSeries(listUserPools(), userPoolId => {
const file = sanitizeFilename(getFilename(userPoolId));
console.error(`Exporting ${userPoolId} to ${file}`);
return backupUsers(cognitoIsp, userPoolId, file);
}));
}
function getFilename(userPoolId) {
return `${userPoolId}.json`;
}
function listUserPools() {
return cognitoIsp.listUserPools({
MaxResults: 60
}).promise()
.then(data => {
assert(!data.NextToken, 'More than 60 user pools is not yet supported');
const userPools = data.UserPools;
debug({
userPools
});
return userPools.map(p => p.Id);
});
}
function backupUsers(cognitoIsp, userPoolId, file) {
const writeStream = fs.createWriteStream(file);
const stringify = JSONStream.stringify();
stringify.pipe(writeStream);
const params = {
UserPoolId: userPoolId
};
const page = () => {
debug(`Fetching users - page: ${params.PaginationToken || 'first'}`);
return bluebird.resolve(cognitoIsp.listUsers(params).promise())
.then(data => {
data.Users.forEach(item => stringify.write(item));
if (data.PaginationToken !== undefined) {
params.PaginationToken = data.PaginationToken;
return page();
}
});
}
return page()
.finally(() => {
stringify.end();
return streamToPromise(stringify);
})
.finally(() => writeStream.end());
}
function restore(cli) {
const userPoolId = cli.input[1];
const file = cli.flags.file
var new_attributes = [];
if (!userPoolId || !file) {
console.error('user-pool-id or input file is required');
cli.showHelp();
}
fs.readFile(file, 'utf8', function readFileCallback(err, data) {
if (err) {
console.log(err);
} else {
var users = JSON.parse(data);
users.forEach(function(user) {
// sub is non-mutable attribute
var attributes = user.Attributes;
attributes.forEach(function(attribute) {
if (attribute.Name != "sub") {
new_attributes.push(attribute);
}
});
// create users
var params = {
UserPoolId: userPoolId,
Username: user.Username,
DesiredDeliveryMediums: ['EMAIL'],
ForceAliasCreation: false,
TemporaryPassword: 'P@ssw@rd1234',
UserAttributes: new_attributes
};
cognitoIsp.adminCreateUser(params, function(err, data) {
if (err) console.log(err, err.stack);
else console.log(data);
});
});
}
});
}