This repository has been archived by the owner on Apr 3, 2024. It is now read-only.
forked from rapid7/r7insight_docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·278 lines (229 loc) · 7.04 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
#! /usr/bin/env node
'use strict';
const tls = require('tls');
const net = require('net');
const eos = require('end-of-stream');
const through = require('through2');
const minimist = require('minimist');
const allContainers = require('docker-allcontainers');
const statsFactory = require('docker-stats');
const logFactory = require('docker-loghose');
const eventsFactory = require('docker-event-log');
const os = require('os');
let debugLogging = !!process.env.INSIGHT_DOCKER_DEBUG;
const logDebug = (...args) => {
if (!debugLogging) {
return;
}
console.log(...args);
};
function connect(opts) {
let stream;
const endpoint = `${opts.region}${opts.server}`;
if (opts.secure) {
logDebug(`Establishing secure connection to ${endpoint}:${opts.port}...`);
stream = tls.connect(opts.port, endpoint, onSecure);
} else {
logDebug(`Establishing plain-text connection to ${endpoint}:${opts.port}...`);
stream = net.createConnection(opts.port, endpoint);
}
function onSecure() {
// let's just crash if we are not secure
if (!stream.authorized) {
logDebug('Connection is not secure!');
throw new Error('secure connection not authorized');
}
}
return stream;
}
function start(opts) {
debugLogging = opts.debug;
const logsToken = opts.logstoken || opts.token;
const statsToken = opts.statstoken || opts.token;
const eventsToken = opts.eventstoken || opts.token;
let out;
let noRestart = () => void 0;
const filter = through.obj(function (obj, enc, cb) {
logDebug(`Got an event with encoding "${enc}":`, obj);
obj = addAll(opts.add, obj);
const token = (() => {
const {
line,
type,
stats
} = obj;
if (line) {
logDebug('Using logs token:', logsToken);
return logsToken;
} else if (type) {
logDebug('Using events token:', eventsToken);
return eventsToken;
} else if (stats) {
logDebug('Using stats token:', statsToken);
return statsToken;
}
throw new Error('Configuration did not result in a log token being set.');
})();
if (token) {
logDebug('Prepending log token:', token);
this.push(token);
this.push(' ');
this.push(JSON.stringify(obj));
this.push('\n');
}
logDebug('Finished processing event:', obj);
cb();
});
const events = allContainers(opts);
let streamsOpened = 0;
opts.events = events;
const createLogHose = (condition, factory) => {
logDebug('Creating log stream with factory:', factory);
if (!condition()) {
logDebug('Condition for log stream creation not met:', String(condition));
return;
}
const hose = factory(opts);
hose.pipe(filter);
streamsOpened++;
logDebug('Log stream created');
return hose;
};
const loghose = createLogHose(() => opts.logs !== false && logsToken, logFactory);
const stats = createLogHose(() => opts.stats !== false && statsToken, statsFactory);
const dockerEvents = createLogHose(() => opts.dockerEvents !== false && eventsToken, eventsFactory);
if (!stats && !loghose && !dockerEvents) {
throw new Error(`You should enable either stats, logs or dockerEvents, \
this might be due to missing log token.`);
}
pipe();
// destroy out if all streams are destroyed
loghose && eos(loghose, () => {
logDebug('Closing log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
stats && eos(stats, () => {
logDebug('Closing stats log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
dockerEvents && eos(dockerEvents, () => {
logDebug('Closing Docker events log stream');
streamsOpened--;
streamClosed(streamsOpened);
});
return loghose;
function addAll(proto, obj) {
if (!proto) {
return;
}
const newObj = {...obj};
for (const key in proto) {
if (proto.hasOwnProperty(key)) {
logDebug(`Adding key "${key}" with value "${proto[key]}"`);
newObj[key] = proto[key];
}
}
return newObj;
}
function pipe() {
logDebug('Starting data pipe...');
if (out) {
filter.unpipe(out);
}
out = connect(opts);
filter.pipe(out, { end: false });
// automatically reconnect on socket failure
noRestart = eos(out, pipe);
}
function streamClosed(streamsOpened) {
if (streamsOpened <= 0) {
noRestart();
out.destroy();
}
}
}
function cli(process_args) {
const argv = minimist(process_args.slice(2), {
boolean: ['json', 'secure', 'stats', 'logs', 'dockerEvents', 'debug'],
string: ['token', 'region', 'logstoken', 'statstoken', 'eventstoken', 'server', 'port'],
alias: {
'token': 't',
'region': 'r',
'logstoken': 'l',
'newline': 'n',
'statstoken': 'k',
'eventstoken': 'e',
'secure': 's',
'json': 'j',
'statsinterval': 'i',
'add': 'a',
'debug': 'd'
},
default: {
json: false,
secure: true,
newline: true,
stats: true,
logs: true,
dockerEvents: true,
statsinterval: 30,
add: [ 'host=' + os.hostname() ],
debug: !!process.env.INSIGHT_DOCKER_DEBUG,
token: process.env.INSIGHT_TOKEN,
logstoken: process.env.INSIGHT_LOGSTOKEN || process.env.INSIGHT_TOKEN,
statstoken: process.env.INSIGHT_STATSTOKEN || process.env.INSIGHT_TOKEN,
eventstoken: process.env.INSIGHT_EVENTSTOKEN || process.env.INSIGHT_TOKEN,
server: '.data.logs.insight.rapid7.com',
port: undefined
}
});
if (argv.help || !(argv.token || argv.logstoken || argv.statstoken || argv.eventstoken) || !(argv.region)) {
console.log('Usage: r7insight_docker [-l LOGSTOKEN] [-k STATSTOKEN] [-e EVENTSTOKEN]\n' +
' [-t TOKEN] [--no-secure] [--json]\n' +
' [-r REGION]\n' +
' [--no-newline] [--no-stats] [--no-logs] [--no-dockerEvents]\n' +
' [-i STATSINTERVAL] [-a KEY=VALUE]\n' +
' [--matchByImage REGEXP] [--matchByName REGEXP]\n' +
' [--skipByImage REGEXP] [--skipByName REGEXP]\n' +
' [--server HOSTNAME] [--port PORT]\n' +
' [--debug]\n' +
' [--help]');
process.exit(1);
}
const getPort = () => {
if (argv.port == undefined) {
if (argv.secure) {
return 443;
}
return 80;
}
// TODO: support service names
return parseInt(argv.port);
};
argv.port = getPort();
if (isNaN(argv.port)) {
console.log('port must be a number');
process.exit(1);
}
if (argv.add && !Array.isArray(argv.add)) {
argv.add = [argv.add];
}
argv.add = argv.add.reduce((acc, arg) => {
arg = arg.split('=');
acc[arg[0]] = arg[1];
return acc
}, {});
utils.start(argv);
}
const utils = {
start,
};
module.exports = {
cli,
utils,
};
if (require.main === module) {
cli(process.argv);
}