-
Notifications
You must be signed in to change notification settings - Fork 16
/
setup.js
468 lines (433 loc) · 15.3 KB
/
setup.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
var defenderApi = require('./utilities/ci_yard_api_client.js'),
{ S3Client,
GetBucketLocationCommand,
GetBucketLifecycleConfigurationCommand,
PutBucketLifecycleConfigurationCommand,
DeleteBucketLifecycleCommand
} = require('@aws-sdk/client-s3');
exports.createSource = async function (args) {
"use strict";
var sourceName = args.name,
sourceType = args.type,
params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
args: [
{ name: 'source.config.collection_type', value: sourceType },
{ name: 'name', value: sourceName }
]
};
if (args.customerId === "" || args.aims_access_key_id === "" || args.aims_secret_key === "") {
return { id: "" };
}
try {
const lifeCycleParams = {
name: args.name + "-rule",
bucket: args.s3.bucket
};
await updateBucketLifeCycle(lifeCycleParams, "add");
const result = await defenderApi.getSource(params);
const r = JSON.parse(result);
if (r.sources.length) {
console.log("Source '" + sourceName + "' already exists.");
console.log("Source: " + JSON.stringify(r.sources[0], null, 2));
if (r.sources[0]['source']['config']['collection_type'] === sourceType) {
return { id: r.sources[0]['source'].id };
} else {
return await createSourceImpl(args);
}
} else {
return await createSourceImpl(args);
}
} catch (err) {
console.log("Failed to create source. Error: '" + JSON.stringify(err) + "'");
return err;
}
};
exports.deleteSource = async function (args) {
"use strict";
var sourceName = args.name,
sourceType = args.type,
params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
args: [
{ name: 'source.config.collection_type', value: sourceType },
{ name: 'name', value: sourceName }
]
};
if (args.customerId === "" || args.aims_access_key_id === "" || args.aims_secret_key === "") {
return { id: "" };
}
try {
var lifeCycleParams = {
name: args.name + "-rule",
bucket: args.s3.bucket
};
await updateBucketLifeCycle(lifeCycleParams, "remove");
const result = await defenderApi.getSource(params);
var r = JSON.parse(result);
if (!r.sources.length) {
return { id: "" };
}
for (var i = 0; i < r.sources.length; i++) {
if (r.sources[0]['source']['config']['collection_type'] === sourceType) {
var source = r.sources[0]['source'];
args['sourceId'] = source.id;
args['credentialId'] = source.config.s3.credential.id;
args['policyId'] = source.config.policy.id;
return await deleteSourceImpl(args);
}
}
return { id: "" };
} catch (err) {
console.log("Error in deleteSource operation. Error: " + JSON.stringify(err));
return err;
}
};
async function createSourceImpl(args) {
"use strict";
console.log("Creating '" + args.name + "' source of '" + args.type + "' type.");
try {
const credentialId = await getCredentials(args, true);
const policyId = await getPolicy(args, true);
const result = await doCreateSource(args, credentialId, policyId);
console.log("Successfully created source. Result: '" + JSON.stringify(result) + "'");
return result;
} catch (err) {
console.log("Error creating source: " + JSON.stringify(err));
return err;
}
}
async function deleteSourceImpl(args) {
"use strict";
console.log("Deleting '" + args.sourceId + "' source.");
try {
await doDeleteSource(args);
await deletePolicy(args);
await deleteCredential(args);
return { id: args.sourceId };
} catch (err) {
console.log("Error deleting source: " + JSON.stringify(err));
return err;
}
}
async function getCredentials(args, createFlag) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
args: [
{ name: 'type', value: args.credentials.type },
{ name: 'name', value: args.name }
]
};
try {
const result = await defenderApi.getCredentials(params);
var r = JSON.parse(result);
if (r.credentials.length) {
var id = r.credentials[0]['credential'].id;
console.log("CredentialId: " + id);
return id;
} else if (!createFlag) {
return { message: "Credentials object doesn't exist", code: 404 };
}
return await createCredentials(args);
} catch (err) {
console.log("Error getting credentials: " + JSON.stringify(err));
return err;
}
}
async function createCredentials(args) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
name: args.name,
credentials: args.credentials
};
try {
const result = await defenderApi.createCredentials(params);
var id = JSON.parse(result)['credential'].id;
console.log("Created new credentials object: " + id);
return id;
} catch (err) {
console.log("Error creating credentials: " + JSON.stringify(err));
return err;
}
}
async function deleteCredential(args) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
id: args.credentialId
};
try {
await defenderApi.deleteCredential(params);
} catch (err) {
console.log("Failed to delete credential '" + args.credentialId + "'. Error: " + JSON.stringify(err));
return err;
}
}
async function getPolicy(args, createFlag) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
args: [
{ name: 'type', value: args.type },
{ name: 'name', value: args.name }
]
};
try {
const result = await defenderApi.getPolicy(params);
var r = JSON.parse(result);
if (r.policies.length) {
var id = r.policies[0]['policy'].id;
console.log("Policy Id: " + id);
return id;
} else if (!createFlag) {
return { message: "Policy object doesn't exist", code: 404 };
}
return await createPolicy(args);
} catch (err) {
console.log("Error getting policy: " + JSON.stringify(err));
return err;
}
}
async function createPolicy(args) {
"use strict";
var params = {};
switch (args.logFormat) {
case "AWS VPC Flow Logs":
params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
name: args.name,
type: args.type,
template_id: "BFE6243E-E57C-4ADE-B444-C5999E8FE3A7",
policy: {
default: "false"
}
};
break;
case "AWS IoT":
params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
name: args.name,
type: args.type,
policy: {
default: "false",
timestamp: {
format: "YYYY-MM-DD hh:mm:ss"
},
multiline: {
is_multiline: "false"
}
}
};
break;
default:
params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
name: args.name,
type: args.type,
policy: args.policy
};
}
console.log("Creating policy document: %s", JSON.stringify(params));
try {
const result = await defenderApi.createPolicy(params);
var id = JSON.parse(result)['policy'].id;
console.log("Created new policy object: " + id);
return id;
} catch (err) {
console.log("Error creating policy: " + JSON.stringify(err));
return err;
}
}
async function deletePolicy(args) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
id: args.policyId
};
try {
await defenderApi.deletePolicy(params);
} catch (err) {
console.log("Failed to delete policy '" + args.policyId + "'. Error: " + JSON.stringify(err));
return err;
}
}
async function doCreateSource(args, credentialId, policyId) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
name: args.name,
type: args.type,
source: {
config: {
time_zone: args[args.type]['time_zone'],
collection_method: "api",
collection_type: args.type,
[args.type]: {
bucket: args[args.type]['bucket'],
max_collection_interval: args[args.type]['max_collection_interval'] ? Number(args[args.type]['max_collection_interval']) : 300,
file_pattern: args[args.type]['file_pattern'],
credential: {
id: credentialId
}
},
policy: {
id: policyId
}
},
enabled: args[args.type]['enabled'],
name: args.name,
product_type: "lm",
type: "api"
}
};
console.log("Creating source: %s", JSON.stringify(params));
try {
const result = await defenderApi.createSource(params);
var newSource = JSON.parse(result).source;
console.log("Created source object. Id: '" + newSource.id + "'");
return { id: newSource.id };
} catch (err) {
console.log("Error creating source: " + JSON.stringify(err));
return err;
}
}
async function doDeleteSource(args) {
"use strict";
var params = {
customerId: args.customerId,
aims_access_key_id: args.aims_access_key_id,
aims_secret_key: args.aims_secret_key,
id: args.sourceId
};
try {
await defenderApi.deleteSource(params);
} catch (err) {
console.log("Failed to delete source '" + args.sourceId + "'. Error: " + JSON.stringify(err));
return err;
}
}
async function updateBucketLifeCycle(lifeCycleParams, operation) {
"use strict";
console.log("updateBucketLifeCycle called... params: '%s', operation: '%s'", JSON.stringify(lifeCycleParams), operation);
var index = lifeCycleParams.bucket.indexOf('/'),
bucketName = lifeCycleParams.bucket.slice(0, index),
prefix = lifeCycleParams.bucket.slice(index + 1);
var s3 = new S3Client({ region: lifeCycleParams.region });
try {
await setupS3endpoint();
const rules = await getS3Lifecycle();
await setupLifecycle(rules);
} catch (err) {
return err;
}
async function setupS3endpoint() {
try {
const data = await s3.send(new GetBucketLocationCommand({ "Bucket": bucketName }));
s3.config.endpoint = `https://${getS3Endpoint(data.LocationConstraint)}`;
console.log("Using '" + s3.config.endpoint + "' endpoint");
} catch (err) {
console.log("Failed to get '" + bucketName + "' bucket location. " +
"Error: " + JSON.stringify(err));
return err;
}
}
async function getS3Lifecycle() {
var params = { "Bucket": bucketName },
lifeCycleRule = {
"Prefix": prefix,
"Status": "Enabled",
"Expiration": { "Days": 1 },
"ID": lifeCycleParams.name
};
try {
const data = await s3.send(new GetBucketLifecycleConfigurationCommand(params));
for (var i = 0; i < data.Rules.length; i++) {
var rule = data.Rules[i];
if (rule.Prefix === lifeCycleRule.Prefix) {
if (operation === "add") {
return data.Rules;
} else {
if (rule.ID === lifeCycleRule.ID) {
data.Rules.splice(i, 1);
return data.Rules.length ? data.Rules : null;
} else {
return data.Rules;
}
}
}
}
if (operation === "add") {
data.Rules.push(lifeCycleRule);
return data.Rules;
} else {
return null;
}
} catch (err) {
if (err.name === "NoSuchLifecycleConfiguration") {
return (operation === "add") ? [lifeCycleRule] : null;
}
console.log("Failed to get '" + bucketName + "' bucket lifecycle. " +
"Error: " + JSON.stringify(err));
return err;
}
}
async function setupLifecycle(rules) {
if (!rules) {
if (operation === "add") {
return;
}
try {
await s3.send(new DeleteBucketLifecycleCommand({ "Bucket": bucketName }));
console.log("Successfully deleted lifecycle configuration on '" + bucketName + "' bucket.");
} catch (err) {
console.log("Failed to delete lifecycle configuration on '" + bucketName + "' bucket. " +
"Error: " + JSON.stringify(err));
return err;
}
} else {
var params = {
"Bucket": bucketName,
"LifecycleConfiguration": { "Rules": rules }
};
try {
await s3.send(new PutBucketLifecycleConfigurationCommand(params));
console.log("Successfully updated lifecycle configuration on '" + bucketName + "' bucket.");
} catch (err) {
console.log("Operation '" + operation + "'. Failed to set '" + bucketName + "' bucket lifecycle. " +
"Error: " + JSON.stringify(err));
return err;
}
}
}
}
function getS3Endpoint(region) {
"use strict";
if (!region || region === 'us-east-1' || region === '') {
return 's3.amazonaws.com';
}
return 's3-' + region + '.amazonaws.com';
}