Skip to content
Snippets Groups Projects
Commit a29f0a64 authored by aurieh's avatar aurieh
Browse files

Scan, WIP

parent 9f4898db
No related branches found
No related tags found
No related merge requests found
...@@ -124,53 +124,55 @@ function fireInfectedNotification (data) { ...@@ -124,53 +124,55 @@ function fireInfectedNotification (data) {
* > POST /scan * > POST /scan
* Scan the file specified in the S3 event in the body. * Scan the file specified in the S3 event in the body.
*/ */
module.exports = function scan (req, res, next) { module.exports = function* scan () {
debug('received data from SQS'); debug('received data from SQS');
// Reject incoming data // Reject incoming data
function rejectData (msg) { function rejectData (msg) {
res.status(400).json({ this.status = 400;
this.body = {
code: 400, code: 400,
message: msg message: msg
}); };
} }
// Test incoming data // Test incoming data
if (req.body.Event === 's3:TestEvent') return rejectData('test event'); if (this.req.body.Event === 's3:TestEvent') return rejectData('test event');
if (!Array.isArray(req.body.Records)) return rejectData('invalid S3 message structure'); if (!Array.isArray(this.req.body.Records)) return rejectData('invalid S3 message structure');
if (req.body.Records.length !== 1) return rejectData('records count !== 1'); if (this.req.body.Records.length !== 1) return rejectData('records count !== 1');
for (const item of req.body.Records) { for (const item of this.req.body.Records) {
if (typeof item !== 'object' || item === null) return rejectData('invalid item in records'); if (typeof item !== 'object' || item === null) return rejectData('invalid item in records');
if (typeof item.eventName !== 'string' || item.eventName.indexOf('ObjectCreated') === -1) return rejectData('invalid event type on record'); if (typeof item.eventName !== 'string' || item.eventName.indexOf('ObjectCreated') === -1) return rejectData('invalid event type on record');
} }
// Promise chain for processing the scan // Promise chain for processing the scan
Promise.resolve(req.body) try {
.then(getObject) const data = yield Promise.resolve(this.req.body)
.then(writeTempFile) .then(getObject)
.then(clamScan) .then(writeTempFile)
// TODO: .then(uploadInfectedToS3) .then(clamScan)
.then(unlinkTempFile) // TODO: .then(uploadInfectedToS3)
.then(deleteInfectedFromS3) .then(unlinkTempFile)
.then(fireInfectedNotification) .then(deleteInfectedFromS3)
.then(data => { .then(fireInfectedNotification);
res.status(200).json({ this.status = 200;
processed: true, this.body = {
infected: data.isInfected || false, processed: true,
permanentlyDeleted: data.wasPermanentlyDeleted || false infected: data.isInfected || false,
}); permanentlyDeleted: data.wasPermanentlyDeleted || false
}) };
.catch(err => { } catch (err) {
console.error(err.stack); console.error(err.stack);
res.status(500).json({ this.status = 500;
code: 500, this.body = {
message: 'internal server error', code: 500,
data: { message: 'internal server error',
type: err.type, data: {
name: err.name, type: err.type,
arguments: err.arguments, name: err.name,
message: err.message arguments: err.arguments,
} message: err.message
}); }
}); };
}
}; };
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment