-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
427 lines (389 loc) · 10.3 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
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
const winston = require('winston');
const async = require('async');
const moment = require('moment');
const useragent = require('useragent');
const express = require('express');
const Webtask = require('webtask-tools');
const app = express();
const Request = require('request');
const memoizer = require('lru-memoizer');
const httpRequest = require('request');
const metadata = require('./webtask.json');
const lawgs = require('lawgs');
let lawger = null;
function lastLogCheckpoint(req, res) {
let ctx = req.webtaskContext;
let required_settings = ['AUTH0_DOMAIN', 'AUTH0_CLIENT_ID', 'AUTH0_CLIENT_SECRET', 'AWS_ACCESS_KEY', 'AWS_SECRET_KEY', 'AWS_REGION'];
let missing_settings = required_settings.filter((setting) => !ctx.data[setting]);
if (!lawger) {
let aws_access_key = ctx.data.AWS_ACCESS_KEY;
let aws_secret_key = ctx.data.AWS_SECRET_KEY;
let aws_region = ctx.data.AWS_REGION;
let log_group_name = ctx.data.LOG_GROUP || 'AUTH0_GROUP';
var log_stream_name = ctx.data.LOG_STREAM || ctx.data.AUTH0_DOMAIN;
lawgs.config({
aws: {
accessKeyId: aws_access_key,
secretAccessKey: aws_secret_key,
region: aws_region
}
});
lawger = lawgs.getOrCreate(log_group_name);
}
if (missing_settings.length) {
return res.status(400).send({ message: 'Missing settings: ' + missing_settings.join(', ') });
}
// If this is a scheduled task, we'll get the last log checkpoint from the previous run and continue from there.
req.webtaskContext.storage.get((err, data) => {
let startFromId = ctx.data.START_FROM ? ctx.data.START_FROM : null;
let startCheckpointId = typeof data === 'undefined' ? startFromId : data.checkpointId;
// Start the process.
async.waterfall([
(callback) => {
const getLogs = (context) => {
console.log(`Logs from: ${context.checkpointId || 'Start'}.`);
let take = Number.parseInt(ctx.data.BATCH_SIZE);
take = take > 100 ? 100 : take;
context.logs = context.logs || [];
getLogsFromAuth0(req.webtaskContext.data.AUTH0_DOMAIN, req.access_token, take, context.checkpointId, (logs, err) => {
if (err) {
return callback({ error: err, message: 'Error getting logs from Auth0' });
}
if (logs && logs.length) {
logs.forEach((l) => context.logs.push(l));
context.checkpointId = context.logs[context.logs.length - 1]._id;
}
console.log(`Total logs: ${context.logs.length}.`);
return callback(null, context);
});
};
getLogs({ checkpointId: startCheckpointId });
},
(context, callback) => {
const min_log_level = parseInt(ctx.data.LOG_LEVEL) || 0;
const log_matches_level = (log) => {
if (logTypes[log.type]) {
return logTypes[log.type].level >= min_log_level;
}
return true;
};
const types_filter = (ctx.data.LOG_TYPES && ctx.data.LOG_TYPES.split(',')) || [];
const log_matches_types = (log) => {
if (!types_filter || !types_filter.length) return true;
return log.type && types_filter.indexOf(log.type) >= 0;
};
context.logs = context.logs
.filter(l => l.type !== 'sapi' && l.type !== 'fapi')
.filter(log_matches_level)
.filter(log_matches_types);
callback(null, context);
},
(context, callback) => {
console.log('Uploading blobs...');
var now = Date.now();
async.eachLimit(context.logs, 100, (log, cb) => {
const date = moment(log.date);
const url = `${date.format('YYYY/MM/DD')}/${date.format('HH')}/${log._id}.json`;
var body = {};
body.post_date = now;
body.message = JSON.stringify(log);
try {
lawger.log(log_stream_name, body.message);
}
catch (err) {
return cb(err);
}
return cb();
}, (err) => {
if (err) {
return callback({ error: err, message: 'Error sending logs to CloudWatch' });
}
console.log('Upload complete.');
return callback(null, context);
});
}
], function (err, context) {
if (err) {
console.log('Job failed.', err);
return req.webtaskContext.storage.set({ checkpointId: startCheckpointId }, { force: 1 }, (error) => {
if (error) {
return res.status(500).send({ error: error, message: 'Error storing startCheckpoint' });
}
res.status(500).send(err);
});
}
console.log('Job complete.');
return req.webtaskContext.storage.set({
checkpointId: context.checkpointId,
totalLogsProcessed: context.logs.length
}, { force: 1 }, (error) => {
if (error) {
return res.status(500).send({error: error, message: 'Error storing checkpoint' });
}
res.sendStatus(200);
});
});
});
}
const logTypes = {
's': {
event: 'Success Login',
level: 1 // Info
},
'seacft': {
event: 'Success Exchange',
level: 1 // Info
},
'seccft': {
event: 'Success Exchange (Client Credentials)',
level: 1 // Info
},
'feacft': {
event: 'Failed Exchange',
level: 3 // Error
},
'feccft': {
event: 'Failed Exchange (Client Credentials)',
level: 3 // Error
},
'f': {
event: 'Failed Login',
level: 3 // Error
},
'w': {
event: 'Warnings During Login',
level: 2 // Warning
},
'du': {
event: 'Deleted User',
level: 1 // Info
},
'fu': {
event: 'Failed Login (invalid email/username)',
level: 3 // Error
},
'fp': {
event: 'Failed Login (wrong password)',
level: 3 // Error
},
'fc': {
event: 'Failed by Connector',
level: 3 // Error
},
'fco': {
event: 'Failed by CORS',
level: 3 // Error
},
'con': {
event: 'Connector Online',
level: 1 // Info
},
'coff': {
event: 'Connector Offline',
level: 3 // Error
},
'fcpro': {
event: 'Failed Connector Provisioning',
level: 4 // Critical
},
'ss': {
event: 'Success Signup',
level: 1 // Info
},
'fs': {
event: 'Failed Signup',
level: 3 // Error
},
'cs': {
event: 'Code Sent',
level: 0 // Debug
},
'cls': {
event: 'Code/Link Sent',
level: 0 // Debug
},
'sv': {
event: 'Success Verification Email',
level: 0 // Debug
},
'fv': {
event: 'Failed Verification Email',
level: 0 // Debug
},
'scp': {
event: 'Success Change Password',
level: 1 // Info
},
'fcp': {
event: 'Failed Change Password',
level: 3 // Error
},
'sce': {
event: 'Success Change Email',
level: 1 // Info
},
'fce': {
event: 'Failed Change Email',
level: 3 // Error
},
'scu': {
event: 'Success Change Username',
level: 1 // Info
},
'fcu': {
event: 'Failed Change Username',
level: 3 // Error
},
'scpn': {
event: 'Success Change Phone Number',
level: 1 // Info
},
'fcpn': {
event: 'Failed Change Phone Number',
level: 3 // Error
},
'svr': {
event: 'Success Verification Email Request',
level: 0 // Debug
},
'fvr': {
event: 'Failed Verification Email Request',
level: 3 // Error
},
'scpr': {
event: 'Success Change Password Request',
level: 0 // Debug
},
'fcpr': {
event: 'Failed Change Password Request',
level: 3 // Error
},
'fn': {
event: 'Failed Sending Notification',
level: 3 // Error
},
'sapi': {
event: 'API Operation'
},
'fapi': {
event: 'Failed API Operation'
},
'limit_wc': {
event: 'Blocked Account',
level: 4 // Critical
},
'limit_ui': {
event: 'Too Many Calls to /userinfo',
level: 4 // Critical
},
'api_limit': {
event: 'Rate Limit On API',
level: 4 // Critical
},
'sdu': {
event: 'Successful User Deletion',
level: 1 // Info
},
'fdu': {
event: 'Failed User Deletion',
level: 3 // Error
},
'fapi': {
event: 'Failed API Operation',
level: 3 // Error
},
'limit_wc': {
event: 'Blocked Account',
level: 3 // Error
},
'limit_mu': {
event: 'Blocked IP Address',
level: 3 // Error
},
'slo': {
event: 'Success Logout',
level: 1 // Info
},
'flo': {
event: ' Failed Logout',
level: 3 // Error
},
'sd': {
event: 'Success Delegation',
level: 1 // Info
},
'fd': {
event: 'Failed Delegation',
level: 3 // Error
}
};
function getLogsFromAuth0 (domain, token, take, from, cb) {
var url = `https://${domain}/api/v2/logs`;
Request({
method: 'GET',
url: url,
json: true,
qs: {
take: take,
from: from,
sort: 'date:1',
per_page: take
},
headers: {
Authorization: `Bearer ${token}`,
Accept: 'application/json'
}
}, (err, res, body) => {
if (err) {
console.log('Error getting logs', err);
cb(null, err);
} else {
cb(body);
}
});
}
const getTokenCached = memoizer({
load: (apiUrl, audience, clientId, clientSecret, cb) => {
Request({
method: 'POST',
url: apiUrl,
json: true,
body: {
audience: audience,
grant_type: 'client_credentials',
client_id: clientId,
client_secret: clientSecret
}
}, (err, res, body) => {
if (err) {
cb(null, err);
} else {
cb(body.access_token);
}
});
},
hash: (apiUrl) => apiUrl,
max: 100,
maxAge: 1000 * 60 * 60
});
app.use(function (req, res, next) {
var apiUrl = `https://${req.webtaskContext.data.AUTH0_DOMAIN}/oauth/token`;
var audience = `https://${req.webtaskContext.data.AUTH0_DOMAIN}/api/v2/`;
var clientId = req.webtaskContext.data.AUTH0_CLIENT_ID;
var clientSecret = req.webtaskContext.data.AUTH0_CLIENT_SECRET;
getTokenCached(apiUrl, audience, clientId, clientSecret, function (access_token, err) {
if (err) {
console.log('Error getting access_token', err);
return next(err);
}
req.access_token = access_token;
next();
});
});
app.get('/', lastLogCheckpoint);
app.post('/', lastLogCheckpoint);
// This endpoint would be called by webtask-gallery when the extension is installed as custom-extension
app.get('/meta', (req, res) => {
res.status(200).send(metadata);
});
module.exports = Webtask.fromExpress(app);