UNPKG

openhim-core

Version:

The OpenHIM core application that provides logging and routing of http requests

248 lines (226 loc) 8.58 kB
var Audit, AuditMeta, codeInArray, config, dgram, firstCharLowerCase, logger, net, parseAuditRecordFromXML, parseString, processAudit, processAuditMeta, sendTCPAudit, sendTLSAudit, sendUDPAudit, syslogParser, tls, tlsAuthentication, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; logger = require('winston'); syslogParser = require('glossy').Parse; parseString = require('xml2js').parseString; firstCharLowerCase = require('xml2js').processors.firstCharLowerCase; Audit = require('./model/audits').Audit; AuditMeta = require('./model/audits').AuditMeta; tlsAuthentication = require("./middleware/tlsAuthentication"); dgram = require('dgram'); tls = require('tls'); net = require('net'); config = require("./config/config"); config.auditing = config.get('auditing'); parseAuditRecordFromXML = function(xml, callback) { var csdCodeToCode, options, originalTextToDisplayName; csdCodeToCode = function(name) { if (name === 'csd-code') { return 'code'; } else { return name; } }; originalTextToDisplayName = function(name) { if (name === 'originalText') { return 'displayName'; } else { return name; } }; options = { mergeAttrs: true, explicitArray: false, tagNameProcessors: [firstCharLowerCase], attrNameProcessors: [firstCharLowerCase, csdCodeToCode, originalTextToDisplayName] }; return parseString(xml, options, function(err, result) { var ap, audit, i, j, len, len1, poi, ref, ref1; if (err) { return callback(err); } if (!(result != null ? result.auditMessage : void 0)) { return callback(new Error('Document is not a valid AuditMessage')); } audit = {}; if (result.auditMessage.eventIdentification) { audit.eventIdentification = result.auditMessage.eventIdentification; } audit.activeParticipant = []; if (result.auditMessage.activeParticipant) { if (result.auditMessage.activeParticipant instanceof Array) { ref = result.auditMessage.activeParticipant; for (i = 0, len = ref.length; i < len; i++) { ap = ref[i]; audit.activeParticipant.push(ap); } } else { audit.activeParticipant.push(result.auditMessage.activeParticipant); } } if (result.auditMessage.auditSourceIdentification) { audit.auditSourceIdentification = result.auditMessage.auditSourceIdentification; } audit.participantObjectIdentification = []; if (result.auditMessage.participantObjectIdentification) { if (result.auditMessage.participantObjectIdentification instanceof Array) { ref1 = result.auditMessage.participantObjectIdentification; for (j = 0, len1 = ref1.length; j < len1; j++) { poi = ref1[j]; audit.participantObjectIdentification.push(poi); } } else { audit.participantObjectIdentification.push(result.auditMessage.participantObjectIdentification); } } return callback(null, audit); }); }; codeInArray = function(code, arr) { return indexOf.call(arr.map(function(a) { return a.code; }), code) >= 0; }; exports.processAuditMeta = processAuditMeta = function(audit, callback) { return AuditMeta.findOne({}, function(err, auditMeta) { var activeParticipant, i, j, len, len1, participantObject, ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9; if (err) { logger.error(err); return callback(); } if (!auditMeta) { auditMeta = new AuditMeta(); } if (((ref = audit.eventIdentification) != null ? (ref1 = ref.eventTypeCode) != null ? ref1.code : void 0 : void 0) && !codeInArray(audit.eventIdentification.eventTypeCode.code, auditMeta.eventType)) { auditMeta.eventType.push(audit.eventIdentification.eventTypeCode); } if (((ref2 = audit.eventIdentification) != null ? (ref3 = ref2.eventID) != null ? ref3.code : void 0 : void 0) && !codeInArray(audit.eventIdentification.eventID.code, auditMeta.eventID)) { auditMeta.eventID.push(audit.eventIdentification.eventID); } if (audit.activeParticipant) { ref4 = audit.activeParticipant; for (i = 0, len = ref4.length; i < len; i++) { activeParticipant = ref4[i]; if (((ref5 = activeParticipant.roleIDCode) != null ? ref5.code : void 0) && !codeInArray(activeParticipant.roleIDCode.code, auditMeta.activeParticipantRoleID)) { auditMeta.activeParticipantRoleID.push(activeParticipant.roleIDCode); } } } if (audit.participantObjectIdentification) { ref6 = audit.participantObjectIdentification; for (j = 0, len1 = ref6.length; j < len1; j++) { participantObject = ref6[j]; if (((ref7 = participantObject.participantObjectIDTypeCode) != null ? ref7.code : void 0) && !codeInArray(participantObject.participantObjectIDTypeCode.code, auditMeta.participantObjectIDTypeCode)) { auditMeta.participantObjectIDTypeCode.push(participantObject.participantObjectIDTypeCode); } } } if (((ref8 = audit.auditSourceIdentification) != null ? ref8.auditSourceID : void 0) && (ref9 = audit.auditSourceIdentification.auditSourceID, indexOf.call(auditMeta.auditSourceID, ref9) < 0)) { auditMeta.auditSourceID.push(audit.auditSourceIdentification.auditSourceID); } return auditMeta.save(function(err) { if (err) { logger.error(err); } return callback(); }); }); }; exports.processAudit = processAudit = function(msg, callback) { var parsedMsg; if (callback == null) { callback = (function() {}); } parsedMsg = syslogParser.parse(msg); if (!parsedMsg || !parsedMsg.message) { logger.info('Invalid message received'); return callback(); } return parseAuditRecordFromXML(parsedMsg.message, function(xmlErr, result) { var audit; audit = new Audit(result); audit.rawMessage = msg; audit.syslog = parsedMsg; delete audit.syslog.originalMessage; delete audit.syslog.message; return audit.save(function(saveErr) { if (saveErr) { logger.error("An error occurred while processing the audit entry: " + saveErr); } if (xmlErr) { logger.info("Failed to parse message as an AuditMessage XML document: " + xmlErr); } return processAuditMeta(audit, callback); }); }); }; sendUDPAudit = function(msg, callback) { var client; client = dgram.createSocket('udp4'); return client.send(msg, 0, msg.length, config.auditing.auditEvents.port, config.auditing.auditEvents.host, function(err) { client.close(); return callback(err); }); }; sendTLSAudit = function(msg, callback) { return tlsAuthentication.getServerOptions(true, function(err, options) { var client; if (err) { return callback(err); } client = tls.connect(config.auditing.auditEvents.port, config.auditing.auditEvents.host, options, function() { if (!client.authorized) { return callback(client.authorizationError); } client.write(msg.length + " " + msg); return client.end(); }); client.on('error', function(err) { return logger.error(err); }); return client.on('close', function() { return callback(); }); }); }; sendTCPAudit = function(msg, callback) { var client; client = net.connect(config.auditing.auditEvents.port, config.auditing.auditEvents.host, function() { client.write(msg.length + " " + msg); return client.end(); }); client.on('error', function(err) { return logger.error; }); return client.on('close', function() { return callback(); }); }; exports.sendAuditEvent = function(msg, callback) { var done, ref; if (callback == null) { callback = (function() {}); } done = function(err) { if (err) { logger.error(err); } return callback(); }; if (((ref = config.auditing) != null ? ref.auditEvents : void 0) == null) { return done(new Error('Unable to record audit event: Missing config.auditing.auditEvents')); } switch (config.auditing.auditEvents["interface"]) { case 'internal': return processAudit(msg, done); case 'udp': return sendUDPAudit(msg, done); case 'tls': return sendTLSAudit(msg, done); case 'tcp': return sendTCPAudit(msg, done); default: return done(new Error("Invalid audit event interface '" + config.auditing.auditEvents["interface"] + "'")); } }; //# sourceMappingURL=auditing.js.map