-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
executable file
·297 lines (244 loc) · 8.84 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
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
#! /usr/bin/env node
'use strict';
const allContainers = require('docker-allcontainers');
const dns = require('dns');
const eos = require('end-of-stream');
const eventsFactory = require('docker-event-log');
const logFactory = require('docker-loghose');
const net = require('net');
const os = require('os');
const statsFactory = require('docker-stats');
const through = require('through2');
const tls = require('tls');
const winston = require('winston');
const { Command } = require('commander');
const UUID_REGEX = /^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/;
// Winston logger initialized after CLI arg parsing
let LOGGER;
function connect(opts) {
let stream;
const endpoint = `${opts.region}${opts.server}`;
dns.lookup(endpoint, (err, address, family) => {
if (err) {
// crash if unable to resolve DNS to avoid possible zombie container
throw new Error(`Failed to resolve DNS. error: ${err}`);
} else {
LOGGER.debug(`Successfully resolved DNS. address: "${address}" family: "${family}"`)
}
});
if (opts.secure) {
LOGGER.info(`Establishing secure connection to "${endpoint}:${opts.port}"`);
stream = tls.connect(opts.port, endpoint, onSecure);
} else {
LOGGER.info(`Establishing plain-text connection to "${endpoint}:${opts.port}"`);
stream = net.createConnection(opts.port, endpoint);
}
function onSecure() {
if (!stream.authorized) {
// let's just crash if we are not secure
throw new Error('Secure connection is not authorized');
}
LOGGER.debug('Secure connection established');
}
return stream;
}
function start(opts) {
let out;
let noRestart = () => void 0;
const filter = through.obj(function (obj, enc, cb) {
LOGGER.debug(`Got an event with encoding "${enc}":`, obj);
LOGGER.debug('Enriching log with --add contents')
obj = addAll(opts.add, obj);
function addAll(proto, obj) {
let ret = {
...(obj || {}),
...(proto|| {}),
}
LOGGER.debug('Returning enriched log:', ret);
return ret;
}
LOGGER.debug('Getting correct token for obj...')
const token = (() => {
if (obj.line) {
LOGGER.debug(`Using logs token: ${opts.logstoken}`);
return opts.logstoken;
} else if (obj.type) {
LOGGER.debug(`Using events token: ${opts.eventstoken}`);
return opts.eventstoken;
} else if (obj.stats) {
LOGGER.debug(`Using stats token: ${opts.statstoken}`);
return opts.statstoken;
} else {
LOGGER.error('Unable to figure out correct token to use, skipping log...');
}
})();
if (token) {
LOGGER.debug(`Stringifying object and prepending log token: ${token}`);
this.push(token);
this.push(' ');
this.push(JSON.stringify(obj));
this.push('\n');
}
LOGGER.debug('Finished processing log message, calling callback...');
cb();
});
LOGGER.debug('Getting all containers events...')
const events = allContainers(opts);
let streamsOpened = 0;
opts.events = events;
const createLogHose = (condition, factory) => {
if (!condition()) {
LOGGER.debug(`Condition for log stream creation not met: ${condition}`);
return;
}
LOGGER.debug('Creating hose')
const hose = factory(opts);
LOGGER.debug('Calling pipe')
hose.pipe(filter);
streamsOpened++;
LOGGER.debug('Log stream created');
return hose;
};
LOGGER.debug('Creating log hose');
const loghose = createLogHose(() => opts.logs !== false, logFactory);
LOGGER.debug('Creating statistics hose');
const statshose = createLogHose(() => opts.stats !== false, statsFactory);
LOGGER.debug('Creating events hose');
const eventshose = createLogHose(() => opts.dockerEvents !== false, eventsFactory);
pipe();
function pipe() {
LOGGER.debug('Starting data pipe...');
if (out) {
LOGGER.debug('Unpiping filter...');
filter.unpipe(out);
}
LOGGER.debug('Connecting to ingestion');
out = connect(opts);
LOGGER.debug('Calling filter pipe...')
filter.pipe(out, { end: false });
LOGGER.debug('Setting noRestart')
// automatically reconnect on socket failure
noRestart = eos(out, pipe);
}
// destroy out if all streams are destroyed
loghose && eos(loghose, () => {
LOGGER.debug('Closing log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
statshose && eos(statshose, () => {
LOGGER.debug('Closing stats log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
eventshose && eos(eventshose, () => {
LOGGER.debug('Closing Docker events log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
function streamClosed(streamsOpened) {
LOGGER.debug(`Stream closed. ${streamsOpened} streams remain opened.`);
if (streamsOpened <= 0) {
noRestart();
out.destroy();
}
}
return loghose;
}
function parse_args(process_args) {
const program = new Command();
program
// Required since region is a property on Commander Command
// https://github.com/tj/commander.js#avoiding-option-name-clashes
.storeOptionsAsProperties(false)
.name('r7insight_docker')
.version(process.env.npm_package_version)
.requiredOption('-r, --region <REGION>', 'The region to forward your logs to')
.option('-a, --add <NAME>=<VALUE>', 'Add KVPs to the data being published', [ 'host=' + os.hostname() ])
.option('-i, --statsinterval <STATS_INTERVAL>', 'Downsample stats send to Insight Platform', 30)
.option('-j, --json', 'Stream logs in JSON format', false)
.option('-e, --eventstoken <EVENTS_TOKEN>', 'Specify log token for forwarding events', process.env.INSIGHT_EVENTSTOKEN)
.option('-l, --logstoken <LOGS_TOKEN>', 'Specify log token for logs', process.env.INSIGHT_LOGSTOKEN)
.option('-k, --statstoken <STATS_TOKEN>', 'Specify log token for forwarding statistics', process.env.INSIGHT_STATSTOKEN)
.option('-t, --token <TOKEN>', 'Specify token to use', process.env.INSIGHT_TOKEN)
.option('-v, --log-level <LEVEL>', 'Define application log level', process.env.INSIGHT_LOG_LEVEL || 'info')
.option('--matchByName <REGEX>', 'Forward logs for containers whose name matches <REGEX>')
.option('--matchByImage <REGEX>', 'Forward logs for containers whose image matches <REGEX>')
.option('--skipByName <REGEX>', 'Do not forward logs for containers whose name matches <REGEX>')
.option('--skipByImage <REGEX>', 'Do not forward logs for containers whose image matches <REGEX>')
.option('--no-docker-events', 'Do not stream Docker events')
.option('--no-logs', 'Do not stream logs')
.option('--no-stats', 'Do not stream statistics')
.option('--no-secure', 'Send logs un-encrypted; no TSL/SSL')
.option('--port <PORT>', 'Specify port to forward logs to. Default depends on whether secure is set', undefined)
.option('--server <SERVER>', 'Specify server to forward logs to', '.data.logs.insight.rapid7.com')
.parse(process_args);
return program.opts();
}
function cli(process_args) {
let args = parse_args(process_args);
LOGGER = winston.createLogger({
level: args.logLevel,
format: winston.format.combine(
winston.format.simple(),
winston.format.splat(),
),
transports: [
new winston.transports.Console(),
],
})
LOGGER.info('Starting...');
LOGGER.debug('Initial configuration:', args);
if (!(args.logs || args.stats || args.dockerEvents)) {
throw new Error('You need to enable either logs, stats or events.');
}
if (args.token) {
args.logstoken = args.logstoken || args.token;
args.statstoken = args.statstoken || args.token;
args.eventstoken = args.eventstoken || args.token;
}
if (args.logs && !UUID_REGEX.test(args.logstoken)) {
throw new Error('Logs enabled but log token not supplied or not valid UUID!');
} else if (args.stats && !UUID_REGEX.test(args.statstoken)) {
throw new Error('Stats enabled but stats token not supplied or not valid UUID!');
} else if (args.dockerEvents && !UUID_REGEX.test(args.eventstoken)) {
throw new Error('Events enabled but events token not supplied or not valid UUID!');
}
const getPort = () => {
if (args.port == undefined) {
if (args.secure) {
return 443;
}
return 80;
}
// TODO: support service names
return parseInt(args.port);
};
args.port = getPort();
LOGGER.info(`Using port ${args.port}`);
if (isNaN(args.port)) {
throw new Error(`Port must be a number`);
}
LOGGER.info('Processing --add flag');
if (args.add && !Array.isArray(args.add)) {
args.add = [args.add];
}
args.add = args.add.reduce((acc, arg) => {
arg = arg.split('=');
acc[arg[0]] = arg[1];
return acc
}, {});
LOGGER.debug('Add after processing:', args.add);
utils.start(args);
}
const utils = {
start,
parse_args,
};
module.exports = {
cli,
utils,
};
if (require.main === module) {
cli(process.argv);
}