-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
308 lines (261 loc) · 10.5 KB
/
index.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import * as sdk from 'aws-sdk';
import { RSA_PKCS1_OAEP_PADDING } from 'constants';
(async () => {
const organization = new sdk.Organizations();
const accounts = await organization.listAccounts().promise();
const sts = new sdk.STS();
const identity = await sts.getCallerIdentity().promise();
const regions = ['us-east-2', 'us-west-2', 'eu-west-2', 'eu-central-1', 'ap-northeast-1', 'ap-southeast-1', 'ap-southeast-2'];
await Promise.all(accounts.Accounts.map(async (account) => {
const role = `arn:aws:iam::${account.Id}:role/OrganizationAccountAccessRole`;
const assumedCreds = account.Id !== identity.Account ? await getCreds(role, account) : undefined;
const s3 = new sdk.S3(assumedCreds ? { credentials: assumedCreds } : undefined);
await deletAllBuckets(account, s3)
const iam = new sdk.IAM(assumedCreds ? { credentials: assumedCreds }: undefined);
await deleteRoles(iam);
for await(const region of regions) {
const cloudformation = new sdk.CloudFormation(assumedCreds ? { region, credentials: assumedCreds } : { region })
await deleteCIStacks(cloudformation);
await sleep(1000);
const amplify = new sdk.Amplify(assumedCreds ? { region, credentials: assumedCreds } : { region });
await deleteAmplifyApps(amplify);
await sleep(1000);
const es = new sdk.ES(assumedCreds ? { region, credentials: assumedCreds } : { region } );
await deleteEsDomains(es);
await sleep(1000);
}
}))
})();
// async function deleteLambdas(lambda: sdk.Lambda) {
// let isTruncated = false;
// let nextToken = undefined;
// do {
// const lambdaResult = await lambda.listFunctions({ Marker: nextToken }).promise();
// nextToken = lambdaResult.NextMarker;
// isTruncated = !!nextToken;
// for await (const lambdaFunction of lambdaResult.Functions){
// console.log('deleting domain' + lambdaFunction.FunctionName)
// await lambda.deleteFunction({ FunctionName: lambdaFunction.FunctionName }).promise();
// console.log('deleted domain ' + lambdaFunction.FunctionName)
// }
// }while(isTruncated);
// }
async function deleteEsDomains(es : sdk.ES) {
let isTruncated = false;
let nextToken = undefined;
do {
try{ const domains = await es.listDomainNames().promise();
for await (const domain of domains.DomainNames){
console.log('deleting domain ' + domain.DomainName)
try{
await sleep(200)
await es.deleteElasticsearchDomain({ DomainName : domain.DomainName }).promise();
console.log('deleted domain ' + domain.DomainName)
}catch(ex) {
console.log(ex.message);
console.log('failed to domain ' + domain.DomainName)
}
}
}catch(ex) {}
}while(isTruncated);
}
async function deleteRoles(iam: sdk.IAM) {
let nextToken = undefined;
let isTruncated = false;
do {
const roleResult = await iam.listRoles({ Marker: nextToken }).promise();
isTruncated = roleResult.IsTruncated
nextToken = roleResult.Marker;
for await(const role of roleResult.Roles) {
try{
await sleep(500);
const roledesc = await iam.listRoleTags({ RoleName: role.RoleName }).promise();
if(roledesc.Tags.length > 0 && roledesc.Tags.some(r => r.Key === 'user:Stack' || r.Key === 'user:Application')) {
console.log('Deleting Role ' + role.RoleName);
await detachRolePolicies(role, iam);
await iam.deleteRole({ RoleName: role.RoleName }).promise();
console.log('Deleted ' + role.RoleName);
await sleep(200)
}
}catch(ex) {
console.log(ex.message);
console.log('Failed to Delete' + role.RoleName)
}
}
}while(isTruncated);
}
async function detachRolePolicies(role: sdk.IAM.Role, iam: sdk.IAM) {
let isTruncated = false;
let nextToken = undefined;
do {
const rolePolicyResult = await iam.listRolePolicies({ RoleName: role.RoleName ,Marker: nextToken }).promise();
isTruncated = rolePolicyResult.IsTruncated;
nextToken = rolePolicyResult.Marker;
for await(const policyName of rolePolicyResult.PolicyNames){
await iam.deleteRolePolicy({ RoleName: role.RoleName, PolicyName: policyName }).promise();
}
} while(isTruncated);
isTruncated = false;
nextToken = undefined;
do {
const rolePolicyResult = await iam.listAttachedRolePolicies({ RoleName: role.RoleName, Marker: nextToken }).promise();
await sleep(300);
isTruncated = rolePolicyResult.IsTruncated;
nextToken = rolePolicyResult.Marker;
for await(const policyName of rolePolicyResult.AttachedPolicies){
await iam.detachRolePolicy({ PolicyArn: policyName.PolicyArn, RoleName: role.RoleName }).promise();
await sleep(300);
}
} while(isTruncated);
}
async function deleteAmplifyApps(amplify: sdk.Amplify) {
let isTruncated = false;
let nextToken = undefined;
do {
await sleep(300);
const appsResult = await amplify.listApps({ nextToken: undefined }).promise();
for await (const app of appsResult.apps){
console.log('Deleting App' + app.name);
await amplify.deleteApp({ appId: app.appId }).promise();
console.log('Deleted App' + app.name);
await sleep(300);
}
nextToken = appsResult.nextToken;
isTruncated = !!nextToken;
}while(isTruncated);
}
async function getCreds(role: string, account: sdk.Organizations.Account) {
const sts = new sdk.STS();
const creds = await sts.assumeRole({ RoleArn: role, RoleSessionName: `${account.Name}-delete`, DurationSeconds: 3600 }).promise();
return {
accessKeyId: creds.Credentials.AccessKeyId,
secretAccessKey: creds.Credentials.SecretAccessKey,
sessionToken: creds.Credentials.SessionToken
};
}
const deleteCIStacks = async(cloudformation: sdk.CloudFormation) => {
let isTruncated = false;
let nextToken = undefined;
do {
const stacksResult = await cloudformation.listStacks({
NextToken: nextToken,
StackStatusFilter: ["CREATE_COMPLETE", "ROLLBACK_FAILED", "ROLLBACK_COMPLETE", "DELETE_FAILED", "UPDATE_COMPLETE", "UPDATE_ROLLBACK_FAILED", "UPDATE_ROLLBACK_COMPLETE"]
}).promise();
await sleep(300);
isTruncated = !!stacksResult.NextToken;
nextToken = stacksResult.NextToken;
for await(const stack of stacksResult.StackSummaries) {
const stackdesc = await cloudformation.describeStacks({ StackName: stack.StackName }).promise();
await sleep(300);
if(stackdesc.Stacks && stackdesc.Stacks.length > 0 && !stackdesc.Stacks[0].ParentId && !stack.StackName.toLowerCase().includes('cdktoolkit')) {
console.log('Deleting stack ' + stack.StackName );
try{
await cloudformation.deleteStack({ StackName: stack.StackName }).promise();
await sleep(500)
} catch(ex){
console.log('Delete failed')
}
}
}
} while(isTruncated);
}
const sleep = async(time: number) => new Promise(resolve => setTimeout(resolve, time));
const deletAllBuckets = async(account: { Id?: string }, s3: sdk.S3 ) => {
const listBuckets = await s3.listBuckets().promise();
for await (const bucket of listBuckets.Buckets) {
if(bucket.Name.includes('do-not-delete') || bucket.Name.includes('cdktoolkit')){
console.log('skipping...' + account.Id +' : ' + bucket.Name);
continue;
}
console.log('Deleting... ' + account.Id +' : ' + bucket.Name);
try {
await emptyBucket(bucket.Name, null, s3);
await deleteVersionMarkers(bucket.Name, null, s3);
await s3.deleteBucket({ Bucket: bucket.Name }).promise();
console.log(`Deleted `)
}
catch(ex){
console.error(ex.message);
console.log('Failed Deleting...' + account.Id +' : ' + bucket.Name);
}
}
}
const emptyBucket = async (Bucket, NextContinuationToken, s3: sdk.S3, list = []) => {
if (NextContinuationToken || list.length === 0) {
return await s3
.listObjectsV2({ Bucket, ContinuationToken: NextContinuationToken })
.promise()
.then(async ({ Contents, NextContinuationToken }) => {
if (Contents.length) {
await s3
.deleteObjects({
Bucket,
Delete: {
Objects: Contents.map((item) => ({ Key: item.Key })),
},
})
.promise();
if (NextContinuationToken) {
console.log('deleted', NextContinuationToken);
}
return await emptyBucket(Bucket, NextContinuationToken, s3, [
...list,
...Contents.map((item) => item.Key),
]);
}
return list;
});
}
return list;
};
const deleteVersionMarkers = async (Bucket, NextKeyMarker, s3: sdk.S3, list = []) => {
if (NextKeyMarker || list.length === 0) {
return await s3
.listObjectVersions({ Bucket, KeyMarker: NextKeyMarker })
.promise()
.then(async ({ DeleteMarkers, Versions, NextKeyMarker }) => {
if (DeleteMarkers.length) {
await s3
.deleteObjects({
Bucket,
Delete: {
Objects: DeleteMarkers.map((item) => ({
Key: item.Key,
VersionId: item.VersionId,
})),
},
})
.promise();
if (NextKeyMarker) {
console.log('deleted', NextKeyMarker);
}
return await deleteVersionMarkers(Bucket, NextKeyMarker, s3, [
...list,
...DeleteMarkers.map((item) => item.Key),
]);
}
if (Versions.length) {
await s3
.deleteObjects({
Bucket,
Delete: {
Objects: Versions.map((item) => ({
Key: item.Key,
VersionId: item.VersionId,
})),
},
})
.promise();
if (NextKeyMarker) {
console.log('deleted', NextKeyMarker);
}
return await deleteVersionMarkers(Bucket, NextKeyMarker, s3, [
...list,
...Versions.map((item) => item.Key),
]);
}
return list;
});
}
return list;
};