|
| 1 | +/** |
| 2 | + * Event handlers for attachment create, and av scan result |
| 3 | + */ |
| 4 | +import _ from 'lodash'; |
| 5 | +import Joi from 'joi'; |
| 6 | +import Promise from 'bluebird'; |
| 7 | +import config from 'config'; |
| 8 | +import util from '../../util'; |
| 9 | +import { BUS_API_EVENT } from '../../constants'; |
| 10 | +import { createEvent } from '../../services/busApi'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Payload for new unified BUS events like `avscan.projects.assets.result` with `resource=attachment` |
| 14 | + */ |
| 15 | +const attachmentScanResultPayloadSchema = Joi.object().keys({ |
| 16 | + url: Joi.string().required(), |
| 17 | + fileName: Joi.string().required(), |
| 18 | + isInfected: Joi.boolean().required(), |
| 19 | +}).unknown(true).required(); |
| 20 | + |
| 21 | +/** |
| 22 | + * Updates project activity fields. throws exceptions in case of error |
| 23 | + * @param {Object} app Application object used to interact with RMQ service |
| 24 | + * @param {String} topic Kafka topic |
| 25 | + * @param {Object} payload Message payload |
| 26 | + * @return {Promise} Promise |
| 27 | + */ |
| 28 | +async function attachmentScanResultKafkaHandler(app, topic, payload) { |
| 29 | + // Validate payload |
| 30 | + const result = Joi.validate(payload, attachmentScanResultPayloadSchema); |
| 31 | + if (result.error) { |
| 32 | + throw new Error(result.error); |
| 33 | + } |
| 34 | + const sourceBucket = config.get('attachmentsDMZS3Bucket'); |
| 35 | + // if the attachment is infected, move it to the quarantine s3 bucket, if not move it to the clean s3 bucket |
| 36 | + if (payload.isInfected) { |
| 37 | + // move to quarantine |
| 38 | + const destBucket = config.get('attachmentsQuarantineS3Bucket'); |
| 39 | + util.s3FileTransfer({ log: app.logger }, sourceBucket, payload.path, destBucket, payload.path) |
| 40 | + app.logger.debug(`Attachment ${payload.fileName} is infected, moving to quarantine`); |
| 41 | + } else { |
| 42 | + // move to clean |
| 43 | + const destBucket = config.get('attachmentsS3Bucket'); |
| 44 | + util.s3FileTransfer({ log: app.logger }, sourceBucket, payload.path, destBucket, payload.path) |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Payload for new unified BUS events like `avscan.action.scan` with `resource=attachment` |
| 50 | + */ |
| 51 | +const attachmentPayloadSchema = Joi.object().keys({ |
| 52 | + path: Joi.string().required(), |
| 53 | +}).unknown(true).required(); |
| 54 | + |
| 55 | +/** |
| 56 | + * Attachment Created BUS API event handler. |
| 57 | + * - requests av scan by posting a kafka message to `avscan.action.scan` topic |
| 58 | + * - throws exceptions in case of error |
| 59 | + * |
| 60 | + * @param {Object} app Application object |
| 61 | + * @param {String} topic Kafka topic |
| 62 | + * @param {Object} payload Message payload |
| 63 | + * @return {Promise} Promise |
| 64 | + */ |
| 65 | +async function attachmentCreatedKafkaHandler(app, topic, payload) { |
| 66 | + // Validate payload |
| 67 | + const result = Joi.validate(payload, attachmentPayloadSchema); |
| 68 | + if (result.error) { |
| 69 | + throw new Error(result.error); |
| 70 | + } |
| 71 | + const avScanPayload = { |
| 72 | + url: payload.path, |
| 73 | + fileName: payload.path.split('/').pop(), |
| 74 | + moveFile: false, |
| 75 | + callbackOption: 'kafka', |
| 76 | + callbackKafkaTopic: BUS_API_EVENT.PROJECT_ATTACHMENT_SCAN_RESULT, |
| 77 | + }; |
| 78 | + await createEvent( |
| 79 | + BUS_API_EVENT.AV_SCAN_REQUEST, |
| 80 | + avScanPayload, |
| 81 | + app.logger, |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +module.exports = { |
| 86 | + attachmentScanResultKafkaHandler, |
| 87 | + attachmentCreatedKafkaHandler, |
| 88 | +}; |
0 commit comments