forked from mostlygeek/cloudtrail-elasticsearch-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.sh.ts
executable file
·153 lines (139 loc) · 4.33 KB
/
import.sh.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
#!/usr/bin/env -S npx ts-node
/*
* Steps:
*
* - make sure indexes exist, create them if they don't
* - do an S3 list of stuff under a specific prefix
* - for each file, download it, unzip it and extract the json
* - extract the events and put them into ElasticSearch
* - record in elastic-search that the file was processed
* - repeat
*/
import AWS from 'aws-sdk';
import { URL } from 'url';
import { Client as ESClient } from '@elastic/elasticsearch';
import debug from 'debug';
import { version } from './package.json';
import {cloudtrailLogRecordExtractor} from "./import/extraction/cloudtrail-log-records";
import {convertCloudtrailToElasticsearch} from "./import/transformation/cloudtrail-to-elasticsearch";
import {elasticsearchLogRecordLoader} from "./import/load/elasticsearch-log-records";
import {batch} from "./import/common/batch";
import {Program} from "./import/types/input/program";
const d = {
ESError: debug("ElasticSearch:error"),
info: debug("info"),
error: debug("error")
};
const parseProgram = async () => {
const { validateProgram } = require('./import/types/input/program');
const args = require('yargs')
.env()
.option('--version', {
alias: '-v',
describe: 'Display the version number and exit.',
type: 'boolean',
})
.option('--bucket', {
alias: '-b',
describe: 'Bucket with cloudtrail logs',
demandOption: true,
type: 'string',
})
.option('--region', {
alias: '-r',
describe: 'The AWS region to use',
default: 'us-east-1',
type: 'string',
})
.option('--prefix', {
alias: '-p',
describe: 'The S3 prefix to search for logs',
default: '',
type: 'string',
})
.option('--parallelism', {
alias: '-w',
describe: 'Number of concurrent workers',
default: 5,
type: 'number',
})
.option('--batch-size', {
alias: '-s',
describe: 'Max number of records per batch',
default: 1_000,
type: 'number',
})
.option('--elasticsearch', {
alias: '-e',
describe: 'Elasticsearch base, e.g. https://host:9200',
demandOption: true,
type: 'string',
})
.option('--work-index', {
describe: 'Elasticsearch index to record imported logs',
default: 'cloudtrail-import-log',
type: 'string',
})
.option('--cloudtrail-index', {
describe: 'Elasticsearch index to import into',
default: 'cloudtrail',
type: 'string',
})
.option('--aws-access-key', {
alias: 'AWS_ACCESS_KEY',
describe: 'AWS Access Key (public)',
type: 'string',
})
.option('--aws-secret-key', {
alias: 'AWS_SECRET_KEY',
describe: 'AWS Secret Key (private)',
type: 'string',
})
.help()
.argv;
if(args.version) {
console.log(version);
process.exit(1);
}
return validateProgram(args) || process.exit(1);
};
const batchLogImport = batch(
cloudtrailLogRecordExtractor,
convertCloudtrailToElasticsearch,
elasticsearchLogRecordLoader
);
const run = async () => {
const program: Program = await parseProgram();
const ES = (() => {
const esUrl = new URL(program.elasticsearch);
esUrl.port = esUrl.port || '9200';
return new ESClient({
node: {
url: esUrl,
},
});
})();
const S3 = new AWS.S3({
region: program.region,
accessKeyId: program.AWS_ACCESS_KEY,
secretAccessKey: program.AWS_SECRET_KEY,
});
await batchLogImport(program.parallelism, program.batchSize)({
es: ES,
workIndex: program.workIndex,
s3: S3,
bucket: program.bucket,
prefix: program.prefix,
}, {
es: ES,
cloudtrailIndex: program.cloudtrailIndex,
});
};
// Run forever until explicitly closed with a `process.exit()`
setInterval(() => {}, 0x7FFFFFFF);
run()
.then(() => process.exit(0))
.catch(e => {
d.error("UNCAUGHT ERROR: %s", e);
process.exit(255);
});