-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
502 lines (450 loc) · 17.1 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
var logger = require('./lib/logger.js');
var agentVersion = require('./package.json').version;
var agentDescription = 'ACDP Submitter for Node.js version ' + agentVersion;
logger.info('Starting ' + agentDescription);
logger.debug(
'Loading ACDP-Submitter from %s',
__dirname
);
var async = require('async');
var uuid = require('node-uuid');
var constants = require('./lib/constants');
var Request = require('./lib/model/Request.js');
var classifier = require('./lib/classifier');
var Consumer = require('./lib/model/Consumer');
var Producer = require('./lib/model/Producer');
var L3ep = require('./lib/model/NetworkEndpoint');
var L4ep = require('./lib/model/TransportEndpoint');
var L7ep = require('./lib/model/ApplicationEndpoint');
var config = require('./lib/configloader');
var crypto = require('./lib/crypto');
// some config variables which need to be set at startup
if (config.get('application:instanceid:random')) {
config.set('application:instanceid:value', uuid.v4());
}
config.set('submitter:version', agentDescription);
// user supplied config, e.g. jwe key
var initialized = false;
/**
*
* @param options
* @param callback
* @returns {*}
*/
var init = function (options, callback) {
var errors = [];
// do some sync stuff, fill errors[] if errors happen (may partially work)
if (!initialized) {
if (options) {
if (options.key) {
try {
var key = JSON.stringify(options.key);
config.set('encryption:jwekey', key);
logger.debug('Supplied key has been added to the configuration.');
} catch (e) {
var keyParsingError = new Error('Supplied key is not a valid JSON structure.');
logger.error(keyParsingError);
errors.push(keyParsingError);
}
}
//TODO other options
} else {
var noOptionsSuppliedError = new Error('No initialization options were supplied.');
logger.error(noOptionsSuppliedError);
errors.push(noOptionsSuppliedError);
}
} else {
var noOptionsSuppliedError = new Error('User-supplied config has already been initialized!');
logger.error(noOptionsSuppliedError);
errors.push(noOptionsSuppliedError);
}
initialized = true;
if (errors.length > 0) {
logger.trace("CONFIGURATION AFTER USER-INIT: " + JSON.stringify(config.get()));
if (callback) callback(errors);
return errors;
} else {
logger.trace("CONFIGURATION AFTER USER-INIT: " + JSON.stringify(config.get()));
var okmsg = "everything went fine";
if (callback) callback(null, okmsg);
return okmsg;
}
};
/**
* Takes an array of demands and starts sending them
* @param {Array} rawDemands
* @param callback
*/
var rawDemands = function (rawDemands, callback) {
if (rawDemands.constructor === Array) {
rawDemandStarter(rawDemands, function (err, result) {
if (err) {
logger.error(err);
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
})
} else {
if (callback) callback(new Error('Supplied data is not an array.'), null);
}
};
var produce = function (acdpShorthands, callback) {
if (typeof acdpShorthands === 'object' && acdpShorthands !== null) {
shorthandStarter(acdpShorthands, producerParser, function (err, result) {
if (err) {
logger.error(err);
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
});
}
};
var consume = function (acdpShorthands, callback) {
if (typeof acdpShorthands === 'object' && acdpShorthands !== null) {
shorthandStarter(acdpShorthands, consumerParser, function (err, result) {
if (err) {
logger.error(err);
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
});
}
};
var rawDemandStarter = function (demands, callback) {
var reqObj = new Request();
demands.forEach(function (item) {
reqObj.addDemand(item);
});
reqObj.validateAndSend(function (err, result) {
if (err) {
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
})
};
var shorthandStarter = function (acdpShorthands, parser, callback) {
var reqObj = new Request();
if (acdpShorthands.constructor === Array) {
async.each(acdpShorthands, function (acdpShorthand, callback) {
parser(acdpShorthand, function (err, consumerOrProducer) {
if (err) {
callback(err)
} else {
reqObj.addDemand(consumerOrProducer);
callback();
}
});
}, function (err) {
if (err) {
if (callback) callback(err)
} else {
reqObj.validateAndSend(function (err, result) {
if (err) {
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
})
}
});
} else {
parser(acdpShorthands, function (err, consumerOrProducer) {
if (err) {
if (callback) callback(err)
} else {
reqObj.addDemand(consumerOrProducer);
reqObj.validateAndSend(function (err, result) {
if (err) {
if (callback) callback(err, null);
} else {
if (callback) callback(null, result);
}
})
}
});
}
};
var producerParser = function (shorthand, callback) {
if (Object.keys(shorthand).length > 2) {
throw new Error('Invalid number of key-value pairs');
}
// if we have only one key value pair...
if (Object.keys(shorthand).length == 1) {
resolveShorthand(shorthand, function (err, result) {
if (err) {
callback(err);
} else {
var consObj = {};
switch (result.type) {
case "PORT":
consObj = new Producer();
var l3ep = new L3ep();
l3ep.addSpecial(constants.SPECIAL_UNKNOWN);
consObj.addConsumer(l3ep);
consObj.addProduct(result.l4ep);
callback(null, consObj);
break;
case "APP":
result.l4ep.addSpecial(constants.SPECIAL_UNKNOWN); // do this because the caller only specified an App, no port
consObj = new Consumer();
consObj.addProduct(result.l4ep);
consObj.addProducer(result.l7ep);
callback(null, consObj);
break;
case "IP":
result.l4ep.addSpecial(constants.SPECIAL_UNKNOWN); // do this because the caller only specified an IP-Address
consObj = new Consumer();
consObj.addProducer(result.l3ep);
consObj.addProduct(result.l4ep);
callback(null, consObj);
break;
default:
var errMsg = 'Invalid shorthand notation: ' + JSON.stringify(shorthand);
if (callback) callback(new Error(errMsg), null);
}
}
});
}
// We have exactly two key value pairs
if (Object.keys(shorthand).length == 2) {
var objects = {};
async.forEachOf(shorthand, function (value, key, callback) {
var tmpObj = {};
tmpObj[key] = value;
resolveShorthand(tmpObj, function (err, result) {
if (err) {
return callback(err);
} else {
objects[result.type] = result;
callback();
}
})
}, function (err) {
if (err) {
callback(err)
} else {
var validObject = false;
if (objects.FOR && objects.PORT) {
var prodObj = new Producer();
prodObj.addProduct(objects.PORT.l4ep);
if (objects.FOR.nested.l3ep) prodObj.addConsumer(objects.FOR.nested.l3ep);
if (objects.FOR.nested.l7ep) prodObj.addConsumer(objects.FOR.nested.l7ep);
validObject = true;
callback(null, prodObj);
}
if (!validObject) callback(new Error("No allowed shorthand notation"));
}
});
}
};
var consumerParser = function (shorthand, callback) {
if (Object.keys(shorthand).length > 2) {
throw new Error('Invalid number of key-value pairs');
}
// We have only one key value pair
if (Object.keys(shorthand).length == 1) {
resolveShorthand(shorthand, function (err, result) {
if (err) {
callback(err);
} else {
var consObj = {};
switch (result.type) {
case "URL":
consObj = new Consumer();
consObj.addProducer(result.l3ep);
consObj.addProduct(result.l4ep);
callback(null, consObj);
break;
case "APP":
consObj = new Consumer();
var l4ep = new L4ep();
l4ep.addSpecial(constants.SPECIAL_UNKNOWN); // do this because the caller only specified an App, no port
consObj.addProduct(l4ep);
consObj.addProducer(result.l7ep);
callback(null, consObj);
break;
case "IP":
consObj = new Consumer();
var l4ep = new L4ep();
l4ep.addSpecial(constants.SPECIAL_UNKNOWN); // do this because the caller only specified an IP-Address
consObj.addProducer(result.l3ep);
consObj.addProduct(l4ep);
callback(null, consObj);
break;
default:
var errMsg = 'Invalid shorthand notation: ' + JSON.stringify(shorthand);
if (callback) callback(new Error(errMsg), null);
}
}
});
}
// We have exactly two key value pairs
if (Object.keys(shorthand).length == 2) {
var objects = {};
async.forEachOf(shorthand, function (value, key, callback) {
var tmpObj = {};
tmpObj[key] = value;
resolveShorthand(tmpObj, function (err, result) {
if (err) {
return callback(err);
} else {
objects[result.type] = result;
callback();
}
})
}, function (err) {
if (err) {
callback(err)
} else {
var valid = false;
if (objects.IP && objects.PORT) {
valid = true;
var consObj = new Consumer();
consObj.addProduct(objects.PORT.l4ep);
consObj.addProducer(objects.IP.l3ep);
callback(null, consObj);
}
if (objects.APP && objects.PORT) {
valid = true;
var consObj = new Consumer();
consObj.addProduct(objects.PORT.l4ep);
consObj.addProducer(objects.APP.l7ep);
callback(null, consObj);
}
if (objects.APP && objects.SPECIAL) {
valid = true;
var consObj = new Consumer();
var l4ep = new L4ep();
l4ep.addSpecial(objects.SPECIAL.special);
consObj.addProduct(l4ep);
consObj.addProducer(objects.APP.l7ep);
callback(null, consObj);
}
// TODO: implement {"tcp":443, "from": {"app": "App-with-some-name"}}
// TODO: implement {"tcp":443, "from": {"ip": "an IP address"}}
if (!valid) callback(new Error("No allowed shorthand notation"));
}
});
}
};
var resolveShorthand = function (result, callback) {
for (var key in result) {
var value = result[key];
switch (key.toUpperCase()) {
case "URL":
classifier.objectsFromUrl(value, function (err, results) {
if (err) {
callback(err, null);
} else {
var l3Obj = results[0];
var l4Obj = results[1];
var retObj = {
type: "URL",
l3ep: l3Obj,
l4ep: l4Obj
};
callback(null, retObj);
}
});
break;
case "APP":
var l7Obj = new L7ep();
l7Obj.addApplication(value);
var retObj = {
type: "APP",
l7ep: l7Obj
};
callback(null, retObj);
break;
case "TCP":
var parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
callback(new Error("Value is not a number."));
} else {
var l4Obj = new L4ep();
l4Obj.addPort(constants.TCP, parseInt(value, 10));
var retObj = {
type: "PORT",
l4ep: l4Obj
};
callback(null, retObj);
}
break;
case "UDP":
var parsedValue = parseInt(value, 10);
if (isNaN(parsedValue)) {
callback(new Error("Value is not a number."));
} else {
var l4Obj = new L4ep();
l4Obj.addPort(constants.UDP, parsedValue);
var retObj = {
type: "PORT",
l4ep: l4Obj
};
callback(null, retObj);
}
break;
case "IP":
var l3Obj = new L3ep();
l3Obj.addIpAddr(value);
var retObj = {
type: "IP",
l3ep: l3Obj
};
callback(null, retObj);
break;
case "SPECIAL":
var specialObj = {"type": value};
var retObj = {
type: "SPECIAL",
special: specialObj
};
callback(null, retObj);
break;
case "FOR":
case "FROM":
if (typeof value === 'object' && value !== null) {
async.waterfall([
function (callback) {
if (typeof value === 'object' && value !== null) {
resolveShorthand(value, function (err, result) {
if (err) {
callback(err)
} else {
callback(null, result);
}
});
}
},
function (nested, callback) {
var retObj = {};
retObj.type = key.toUpperCase();
retObj.nested = nested;
callback(null, retObj);
}
], function (err, result) {
if (err) {
callback(err)
}
callback(null, result);
});
} else {
callback(new Error("FOR and FROM keywords can only be used together with an object as a value."));
}
break;
default:
var errMsg = 'Invalid key supplied: ' + key;
if (callback) callback(new Error(errMsg), null);
}
}
};
module.exports = {
init: init,
produce: produce,
consume: consume,
rawDemands: rawDemands
};