This repository has been archived by the owner on Jun 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
343 lines (286 loc) · 9.37 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
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
/**
* @license
*
* Copyright (c) 2016-present, IBM Research.
*
* This source code is licensed under the Apache license found in the
* LICENSE.txt file in the root directory of this source tree.
*/
'use strict';
const isV6 = require('net').isIPv6;
const elastic = require('elasticsearch');
const ipaddr = require('ipaddr.js');
const dbg = require('debug')('jlocke');
const lodash = require('lodash');
const isPromise = require('is-promise');
const defaults = require('./defaults');
const ensureIndex = require('./lib/ensureIndex');
const today = require('./lib/today');
const errInit = 'URI not found, call "init" before';
let indexReady = false;
let db;
// We can't do it for each day in run time due to performance reasons.
let indexRequests;
let indexErrors;
const typeRequests = 'request';
const typeErrors = 'error';
let app = 'app';
function sendToDb(index, type, body) {
dbg('Inserting found request data in the DB', { index, type, body });
// TODO: Add pipelining
db.index({ index, type, body })
.then(() => dbg('New request info correctly inserted'))
.catch(err => {
throw Error(`Adding the requests info: ${err.message}`);
});
}
module.exports.init = async (uri, opts = {}) => {
if (!uri) {
throw new Error(errInit);
}
dbg(`Connecting to DB: ${uri}`);
const optsElastic = { host: uri };
if (opts.trace) {
optsElastic.log = 'trace';
}
if (opts.app) {
dbg(`"app" options passed: ${opts.app}`);
if (typeof opts.app !== 'string') {
throw new Error('Bad app name');
}
// eslint-disable-next-line prefer-destructuring
app = opts.app;
}
try {
db = new elastic.Client(optsElastic);
} catch (err) {
throw new Error(`Creating the Elastic client: ${err.message}`);
}
// Each new deploy indexes are created including the date in the name.
// We can't do it for each day in run time due to performance reasons.
const todayStr = today();
indexRequests = `${opts.indexRequests ||
defaults.indexes.api.name}-${todayStr}`;
indexErrors = `${opts.indexErrors ||
defaults.indexes.error.name}-${todayStr}`;
dbg('Creating proper indexes', {
indexRequests,
indexErrors,
});
// We don't drop if it already exists, ie: same day deploy.
try {
await Promise.all([
ensureIndex(db, indexRequests, typeRequests),
ensureIndex(db, indexErrors, typeErrors),
]);
} catch (err) {
throw Error(`Creating the indexes: ${err.message}`);
}
dbg('Indexes created');
indexReady = true;
};
module.exports.error = async (message, error, opts = {}) => {
if (!message) {
throw new Error('A custom message is mandatory');
}
if (!error) {
throw new Error('An error is mandatory');
}
dbg('New error', { message, opts });
// We don't use the Elastic until the index is created.
// TODO: Some packets could be lost during each deploy -> use a queue.
if (!indexReady) {
// eslint-disable-next-line no-console
console.warn('Error not logged (index not created)', error);
return;
}
const errorInfo = {
app,
message,
timestamp: new Date(),
errorMessage: error.message,
errorStack: error.stack,
};
if (opts.userId) {
errorInfo.userId = opts.userId;
dbg(`UserId passed: ${opts.userId}`);
}
await db.index({ index: indexErrors, type: typeErrors, body: errorInfo });
};
module.exports.express = (opts = {}) => {
dbg('Checking the passed options');
let only;
if (opts.only) {
if (typeof opts.only !== 'string' && !lodash.isArray(opts.only)) {
throw new Error('"only" should be string or array');
}
// Array or string supported.
// eslint-disable-next-line prefer-destructuring
only = opts.only;
if (typeof only === 'string') {
only = [only];
}
}
// To keep backward compatibility.
if (!opts.hideBody && opts.hide) {
// eslint-disable-next-line no-param-reassign
opts.hideBody = opts.hide;
}
if (opts.hideBody) {
if (typeof opts.hideBody !== 'object') {
throw new Error('"hide" should be an object');
}
if (
opts.hideBody.fun &&
(typeof opts.hideBody.fun !== 'function' || isPromise(opts.hideBody.fun))
) {
throw new Error('"hide" should be a function or a promise');
}
}
// TODO: Add also checks for subfields.
return (req, res, next) => {
dbg('New request');
next();
if (!indexReady) {
// eslint-disable-next-line no-console
console.warn('Request not logged (index not created)', req);
return;
}
// "originalUrl" is unique always present (vs "path" and "baseUrl").
if (only) {
const matchAny = !lodash.some(only, itemOnly => {
if (only && req.originalUrl.indexOf(itemOnly) === -1) {
return true;
}
return false;
});
// We don't want to debug the originalUrl because it includes the user token.
dbg('Request checked (hidden path)', {
path: req.path,
baseUrl: req.baseUrl,
matchAny,
});
if (!matchAny) {
return;
}
}
const reqInfo = {
app,
path: req.path,
method: req.method,
protocol: req.protocol,
originalUrl: req.originalUrl,
timestamp: new Date(),
};
if (req.headers['user-agent']) {
reqInfo.agent = req.headers['user-agent'];
}
// TODO: If not optional add at the object creation.
if (req.headers.host) {
reqInfo.host = req.headers.host;
}
if (opts.allHeaders) {
reqInfo.headers = req.headers;
}
if (req.ip) {
// TODO: Elastic only support v4 IP addresses, so we need to convert it.
// https://www.elastic.co/guide/en/elasticsearch/reference/current/ip.html
let goodIp = req.ip;
if (isV6(goodIp)) {
dbg('Detected v6 IP address, converting it to v4 ...');
const address = ipaddr.parse(goodIp);
goodIp = address.toIPv4Address().toString();
}
reqInfo.ip = goodIp;
dbg(`IP addr: ${reqInfo.ip}`);
}
if (req.params && Object.keys(req.params).length > 0) {
reqInfo.params = req.param;
dbg('Parameters found:', req.params);
}
if (req.userId) {
reqInfo.userId = req.userId;
dbg(`UserId passed: ${req.userId}`);
}
// We need to wait for the route to finish to get the correct statusCode and duration.
// https://nodejs.org/api/http.html#http_event_finish
res.on('finish', () => {
dbg('Request ended');
const duration = res.getHeader('x-response-time');
if (duration) {
reqInfo.duration = duration;
}
reqInfo.responseCode = res.statusCode;
// Adding the body.
if (
!req.body ||
Object.keys(req.body).length < 1 || // with non empty body
!opts.hideBody
) {
dbg('No "hide" or no body, sending ...');
sendToDb(indexRequests, typeRequests, reqInfo);
} else {
reqInfo.body = req.body;
// Hiding the options (if "hideBody")
// We use async stuff here so better inside this callback to
// avoid a mess.
// path field fun
// No No Yes -> Hide full body for all paths if fun
// Yes No No -> Hide full body for this path
// Yes No Yes -> Hide full body for this path if fun
// No Yes No -> Hide field for all paths
// No Yes Yes -> Hide field for all paths if fun
// Yes Yes No -> Hide field for this path
// Yes Yes Yes -> Hide field for this path if fun
const hidePath =
opts.hideBody.path && reqInfo.path.indexOf(opts.hideBody.path) !== -1;
if (opts.hideBody.fun) {
let condPromise = opts.hideBody.fun(req);
dbg('To hide (path):', { hidePath });
if (!isPromise(condPromise)) {
condPromise = Promise.resolve(condPromise);
}
condPromise
.then(hideFun => {
dbg('To hide (fun):', { hideFun });
if (hideFun) {
dbg('Deleting body');
// TODO: Try to avoid this delete to improve performance.
delete reqInfo.body; // "Hide full body"
}
sendToDb(indexRequests, typeRequests, reqInfo);
})
.catch(err => {
// eslint-disable-next-line no-console
console.error(
'Running the "hide.fun" promise, not storing' +
'the data due to privacy reasons',
err,
);
});
} else if (
!opts.hideBody.field && // "hide.field" not present
(!opts.hideBody.path || hidePath) // "path" is not blocking
) {
dbg('Deleting body');
// TODO: Try to avoid this delete to improve performance.
delete reqInfo.body;
sendToDb(indexRequests, typeRequests, reqInfo);
} else {
dbg('Checking if we need to delete any field');
const hideField =
opts.hideBody.field && reqInfo.body[opts.hideBody.field];
// Dropping specific fields.
if (
hideField && // only if field name matches.
(!opts.hideBody.path || hidePath)
) {
dbg(`Dropping hidden field: ${opts.hideBody.field}`);
delete reqInfo.body[opts.hideBody.field];
}
sendToDb(indexRequests, typeRequests, reqInfo);
}
}
});
};
};