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 all commits
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
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ the email forwarding mapping from original destinations to new destination.

- For Role, choose "Basic Execution Role" under Create New Role. In the popup,
give the role a name (e.g., LambdaSesForwarder). Configure the role policy to
the following:
the following:
( Note: Action `s3:DeleteObject` is only needed when using the
`emailCleanupOnS3` option.)
```
{
"Version": "2012-10-17",
Expand All @@ -88,7 +90,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 Expand Up @@ -162,7 +165,9 @@ likely need to adjust the bucket policy statement with one like this:
}
```

8. Optionally set the S3 lifecycle for this bucket to delete/expire objects
8. Optionally, to clean up saved email files on the AWS S3 Bucket, you can either:
- Set the `emailCleanupOnS3` option in the script configuration to true (this will delete files on a succesful process).
- Set the S3 lifecycle for this bucket to delete/expire objects
after a few days to clean up the saved emails.

## Extending
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
39 changes: 37 additions & 2 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,37 @@ exports.sendMessage = function(data) {
});
};

/**
* Clean up (delete) the S3 email object if `data.config.emailCleanupOnS3` equals true
*
* @param {object} data - Data bundle with context, email, etc.
*
* @return {object} - Promise resolved with data.
*/
exports.emailCleanupOnS3 = function(data) {
if (!data.config.emailCleanupOnS3) {
return Promise.resolve(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 @@ -297,7 +331,8 @@ exports.handler = function(event, context, callback, overrides) {
exports.transformRecipients,
exports.fetchMessage,
exports.processMessage,
exports.sendMessage
exports.sendMessage,
exports.emailCleanupOnS3
];
var data = {
event: event,
Expand Down
76 changes: 76 additions & 0 deletions test/emailCleanupOnS3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

/* global describe, it */

var assert = require("assert");

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

describe('index.js', function() {
describe('#emailCleanupOnS3()', function() {
it('should simply return data when emailCleanupOnS3 is false',
function(done) {
var data = {
config: {
emailCleanupOnS3: false
}
};
index.emailCleanupOnS3(data)
.then(function() {
assert.ok(true, "emailCleanupOnS3 returned successfully");
done();
});
});

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: false
}
};
index.handler(event, context, callback, overrides);
Expand Down