Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional email cleanup on S3 at end of process #73

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log for aws-lambda-ses-forwarder

## 4.3.0 [2017/3/10]

- Added optional `cleanupS3Email` step at the end of process

## 4.2.0 [2017/6/20]

- Removing `Message-ID` header from messages to fix `InvalidParameterValue:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ the email forwarding mapping from original destinations to new destination.
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::S3-BUCKET-NAME/*"
}
Expand Down
3 changes: 2 additions & 1 deletion example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ exports.handler = function(event, context, callback) {
"[email protected]": [
"[email protected]"
]
}
},
emailCleanupS3: false
}
};
LambdaForwarder.handler(event, context, callback, overrides);
Expand Down
52 changes: 43 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ console.log("AWS Lambda SES Forwarder // @arithmetric // Version 4.2.0");
//
// To match a mailbox name on all domains, use a key without the "at" symbol
// and domain part of an email address (i.e. `info`).
//
// - emailCleanupOnS3: true to delete email from S3 bucket
var defaultConfig = {
fromEmail: "[email protected]",
subjectPrefix: "",
Expand All @@ -46,7 +48,8 @@ var defaultConfig = {
"info": [
"[email protected]"
]
}
},
emailCleanupOnS3: false
};

/**
Expand Down Expand Up @@ -280,6 +283,34 @@ exports.sendMessage = function(data) {
});
};

/**
* Clean up (delete) the S3 email object
*
* @param {object} data - Data bundle with context, email, etc.
*
* @return {object} - Promise resolved with data.
*/
exports.emailCleanupOnS3 = function(data) {
data.log({level: "info", message: "Deleting email at s3://" +
data.config.emailBucket + '/' + data.config.emailKeyPrefix +
data.email.messageId});
return new Promise(function(resolve, reject) {
data.s3.deleteObject({
Bucket: data.config.emailBucket,
Key: data.config.emailKeyPrefix + data.email.messageId
}, function(err, result) {
if (err) {
data.log({level: "error", message: "deleteObject() returned error:",
error: err, stack: err.stack});
return reject(new Error('Error: Email cleanup on S3 failed.'));
}
data.log({level: "info", message: "emailCleanupOnS3() successful.",
result: result});
resolve(data);
});
});
};

/**
* Handler function to be invoked by AWS Lambda with an inbound SES email as
* the event.
Expand All @@ -291,14 +322,6 @@ exports.sendMessage = function(data) {
* configuration, SES object, and S3 object.
*/
exports.handler = function(event, context, callback, overrides) {
var steps = overrides && overrides.steps ? overrides.steps :
[
exports.parseEvent,
exports.transformRecipients,
exports.fetchMessage,
exports.processMessage,
exports.sendMessage
];
var data = {
event: event,
callback: callback,
Expand All @@ -309,6 +332,17 @@ exports.handler = function(event, context, callback, overrides) {
s3: overrides && overrides.s3 ?
overrides.s3 : new AWS.S3({signatureVersion: 'v4'})
};
var steps = overrides && overrides.steps ? overrides.steps :
[
exports.parseEvent,
exports.transformRecipients,
exports.fetchMessage,
exports.processMessage,
exports.sendMessage
];
if (data.config.emailCleanupOnS3) {
steps.push(exports.emailCleanupOnS3);
}
Promise.series(steps, data)
.then(function(data) {
data.log({level: "info", message: "Process finished successfully."});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aws-lambda-ses-forwarder",
"version": "4.2.0",
"version": "4.3.0",
"description": "Serverless email forwarding using AWS Lambda and SES",
"main": "index.js",
"scripts": {
Expand Down
62 changes: 62 additions & 0 deletions test/emailCleanupOnS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

/* global describe, it */

var assert = require("assert");

var index = require("../index");

describe('index.js', function() {
describe('#emailCleanupOnS3()', function() {
it('should invoke the AWS S3 SDK to delete the email object',
function(done) {
var data = {
config: {
emailBucket: "bucket",
emailKeyPrefix: "prefix/",
emailCleanupOnS3: true
},
context: {},
email: {
messageId: "abc"
},
log: console.log,
s3: {
deleteObject: function(options, callback) {
callback(null);
}
}
};
index.emailCleanupOnS3(data)
.then(function() {
assert.ok(true, "emailCleanupOnS3 returned successfully");
done();
});
});

it('should result in failure if the AWS S3 SDK cannot delete the object',
function(done) {
var data = {
config: {
emailBucket: "bucket",
emailKeyPrefix: "prefix/",
emailCleanupOnS3: true
},
context: {},
email: {
messageId: "abc"
},
log: console.log,
s3: {
deleteObject: function(options, callback) {
callback(true);
}
}
};
index.emailCleanupOnS3(data)
.catch(function(err) {
assert.ok(err, "emailCleanupOnS3 aborted operation");
done();
});
});
});
});
6 changes: 5 additions & 1 deletion test/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ describe('index.js', function() {
},
getObject: function(options, callback) {
callback(null, {Body: "email data"});
},
deleteObject: function(options, callback) {
callback(null, {});
}
},
ses: {
Expand All @@ -35,7 +38,8 @@ describe('index.js', function() {
"[email protected]": [
"[email protected]"
]
}
},
emailCleanupOnS3: true
}
};
index.handler(event, context, callback, overrides);
Expand Down