-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
240 lines (214 loc) · 7.18 KB
/
index.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
'use strict';
const AWS = require('aws-sdk');
const util = require('util');
const chalk = require('chalk');
class ServerlessPlugin {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.provider = this.serverless.getProvider('aws');
try {
this.servicename = this.serverless.service.getServiceName();
this.stage = this.provider.getStage();
this.region = this.provider.getRegion();
AWS.config.update({
region: this.region
});
this.cognitoIdp = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' });
this.stackname = this.servicename + '-' + this.stage;
this.hooks = {
// deploy
'after:aws:package:finalize:mergeCustomProviderResources': this.add_outputs.bind(this),
'after:deploy:deploy': this.process_deploy.bind(this),
// remove
'before:remove:remove': this.process_remove.bind(this)
};
} catch (error) {
this.serverless.cli.log(error.stack);
}
}
//===================================
// Remove: before:remove:remove
async process_remove() {
this.plugin_log('process_remove started.');
var that = this;
// get userpool names from serverless.yml in a array
var names = await this.get_sls_userpool_names();
if(!names) {
this.plugin_log('no userpools defined in serverless.yml to be to removed.');
return;
}
// get userpool strutures from aws
var userpools = await this.get_aws_cognito_userpools();
if(!userpools) {
this.plugin_log('no userpools on aws to be removed.');
return;
}
// process only the aws userpools that are defined on serverless.yml
var userpools2process = [];
userpools.forEach(function (userpool) {
if(names.includes(userpool.Name)) {
userpools2process.push(userpool);
}
});
if(userpools2process.length==0) {
this.plugin_log('no userpools to remove.');
}
for(var userpoolindex in userpools2process) {
var userpool = userpools2process[userpoolindex];
await this.delete_userpool_domain(userpool.Id, userpool.Name);
}
this.plugin_log('process_remove finished.');
}
async get_sls_userpool_names() {
if(this.serverless.service.resources===null || this.serverless.service.resources.Resources===null) {
return null;
}
var names = [];
var that = this;
var keys = [];
try {
keys = Object.keys(this.serverless.service.resources.Resources);
} catch (error) {
this.plugin_log(error.stack);
}
keys.forEach(function (value) {
var item = that.serverless.service.resources.Resources[value];
if (item.Type == 'AWS::Cognito::UserPool') {
names.push(item.Properties.UserPoolName);
}
});
return names;
}
async get_aws_cognito_userpools() {
var that = this;
var userpools = [];
var params = {
MaxResults: 1, /* required */
/* NextToken: 'STRING_VALUE' */
};
var hasNext = true;
while(hasNext) {
await this.cognitoIdp.listUserPools(params).promise().then(data => {
if(data.UserPools.length != 0) {
Array.prototype.push.apply(userpools,data.UserPools);
userpools.concat(data.UserPools);
if(data.NextToken) {
params.NextToken = data.NextToken;
} else {
hasNext = false;
}
} else {
hasNext = false;
}
}).catch(error => {
this.plugin_log(util.format('Error: %s, \'%s\'', error.code, error.message));
});
}
if(userpools.length==0) {
return null;
} else {
return userpools;
}
}
async delete_userpool_domain(userpoolid, domainname) {
var that = this;
this.plugin_log('Deleting user pool domain...');
this.plugin_log(`userpoolid: [${userpoolid}], domainname: [${domainname}]`);
try {
var params = {
Domain: domainname,
UserPoolId: userpoolid
};
await this.cognitoIdp.deleteUserPoolDomain(params).promise().then(data => {
that.plugin_log('domain deleted');
}).catch(error => {
that.plugin_log(util.format('Error: %s, \'%s\'', error.code, error.message));
});
this.plugin_log('done.');
} catch (error) {
this.plugin_log(error.stack);
}
}
//===================================
// Deploy: after:aws:package:finalize:mergeCustomProviderResources
async add_outputs() {
var resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources;
for (let key in resources) {
if (resources[key].Type === 'AWS::Cognito::UserPool') {
await this.add_poolid_outputs('UserPoolId' + key, key);
}
}
}
async add_poolid_outputs(name, value) {
var outputs = this.serverless.service.provider.compiledCloudFormationTemplate.Outputs;
outputs[name] = { Value: { Ref: value } };
}
//===================================
// Deploy: after:deploy:deploy
async process_deploy() {
this.plugin_log('process_deploy started.');
var that = this;
try {
var userpoolids = await this.get_deployed_userpool_id();
userpoolids.forEach(function(userpoolid) {
var resource = that.serverless.service.resources.Resources[userpoolid.name.substring(10)];
var domain = resource.Properties.UserPoolName;
userpoolid.domain = domain;
});
for (var index in userpoolids) {
await that.create_userpool_domain(userpoolids[index].id, userpoolids[index].domain);
}
} catch (error) {
this.plugin_log(error.stack);
}
this.plugin_log('process_deploy finished.');
}
async create_userpool_domain(userpoolid, domainname) {
this.plugin_log('Creating user pool domain...');
this.plugin_log(`userpoolid: [${userpoolid}], domainname: [${domainname}]`);
try {
var params = {
Domain: domainname,
UserPoolId: userpoolid,
};
var that = this;
await this.cognitoIdp.createUserPoolDomain(params).promise().then(data => {
that.plugin_log('domain created');
}).catch(error => {
that.plugin_log(util.format('Error: %s, \'%s\'', error.code, error.message));
that.plugin_log(error.stack);
});
this.plugin_log('done.');
} catch (error) {
this.plugin_log(error.stack);
}
}
async get_deployed_userpool_id() {
var user_pool_ids = [];
try {
var result = await this.fetch_CloudFormation_describeStacks();
var result_array = result.Stacks[0].Outputs;
result_array.forEach(function (item) {
if (item.OutputKey.startsWith('UserPoolId')) {
user_pool_ids.push({id:item.OutputValue,name:item.OutputKey});
}
});
return user_pool_ids;
} catch (error) {
this.plugin_log(error.stack);
}
}
async fetch_CloudFormation_describeStacks() {
try {
var params = { StackName: this.stackname };
return this.provider.request('CloudFormation', 'describeStacks', params);
} catch (error) {
this.plugin_log(error.stack);
}
}
async plugin_log(msg) {
this.serverless.cli.consoleLog(`${chalk.yellow('Plugin [aws-cognito-idp-userpool-domain]')}: ${msg}`);
}
}
module.exports = ServerlessPlugin;