From 77a8ea69c61aff0a57efb838a963165b355d9ff6 Mon Sep 17 00:00:00 2001 From: Benoit Beraud Date: Thu, 14 Jul 2016 16:58:51 +0200 Subject: [PATCH] Handle SES create receipt rule --- README.md | 20 +++++- aws/ses.js | 39 ++++++++++++ ...e_cloudformation_helper_functions.template | 61 ++++++++++++++++++- test/aws/s3.putObject.template | 2 +- test/aws/ses.createReceiptRule.template | 61 +++++++++++++++++++ 5 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 aws/ses.js create mode 100644 test/aws/ses.createReceiptRule.template diff --git a/README.md b/README.md index 2741ff9..66c9485 100644 --- a/README.md +++ b/README.md @@ -300,6 +300,24 @@ SnsSubscribeFunctionArn [sns.subscribe.template](test/aws/sns.subscribe.template) +### Create a SES Receipt Rule + +Allows to create an SES Receipt Rule inside an existing SES Rule set (active or not). +Mirrors the [SES.CreateReceipRule API method](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SES.html#createReceiptRule-property). +This will delete the rule when the corresponding stack is deleted. + +#### Paramters + +See the reference above or the example below for full list of parameters. All parameters are directly passed 'as is' except boolean which are converted. + +#### Reference Output Name +SesCreateReceiptRuleFunctionArn + +#### Example/Test Template +[ses.createReceiptRule.template](test/aws/ses.createReceiptRule.template) + + + ## Deployment (contributors) After making changes (i.e. adding a new helper function), please do the following: @@ -318,4 +336,4 @@ After making changes (i.e. adding a new helper function), please do the followin ## License Copyright 2016 Gilt Groupe, Inc. -Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 \ No newline at end of file +Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0 diff --git a/aws/ses.js b/aws/ses.js new file mode 100644 index 0000000..78120a6 --- /dev/null +++ b/aws/ses.js @@ -0,0 +1,39 @@ +var Promise = require('bluebird'), + AWS = require('aws-sdk'), + base = require('lib/base'), + helpers = require('lib/helpers'), + ses = Promise.promisifyAll(new AWS.SES()); + +// Exposes the SES.createReceiptRule API method +function CreateReceiptRule(event, context) { + base.Handler.call(this, event, context); +} +CreateReceiptRule.prototype = Object.create(base.Handler.prototype); +CreateReceiptRule.prototype.handleCreate = function() { + var p = this.event.ResourceProperties; + delete p.ServiceToken; + p.Rule.Enabled = ("true" === p.Rule.Enabled ); + p.Rule.ScanEnabled = ("true" === p.Rule.ScanEnabled ); + return ses.createReceiptRuleAsync(p) + .then(function() { + return { + RuleSetName : p.RuleSetName, + RuleName : p.Rule.Name + } + }); +} +CreateReceiptRule.prototype.handleDelete = function(referenceData) { + return Promise.try(function() { + if (referenceData) { + return ses.deleteReceiptRuleAsync({ + RuleSetName : referenceData.RuleSetName, + RuleName : referenceData.RuleName + }); + } + }); +} +exports.createReceiptRule = function(event, context) { + console.log(JSON.stringify(event)); + handler = new CreateReceiptRule(event, context); + handler.handle(); +} diff --git a/create_cloudformation_helper_functions.template b/create_cloudformation_helper_functions.template index 7c8e831..8cc46b3 100644 --- a/create_cloudformation_helper_functions.template +++ b/create_cloudformation_helper_functions.template @@ -383,6 +383,61 @@ "DependsOn": [ "SnsSubscribeFunctionRole" ] + }, + "SesCreateReceiptRuleFunctionRole": { + "Type": "AWS::IAM::Role", + "Properties": { + "AssumeRolePolicyDocument": { + "Version" : "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "Service": [ "lambda.amazonaws.com" ] + }, + "Action": [ "sts:AssumeRole" ] + } + ] + }, + "ManagedPolicyArns": [ + { "Ref": "RoleBasePolicy" } + ], + "Policies": [ + { + "PolicyName": "SESReceiptRuleModifier", + "PolicyDocument": { + "Version" : "2012-10-17", + "Statement": [ + { + "Effect": "Allow", + "Action": [ + "ses:CreateReceiptRule", + "ses:DeleteReceiptRule" + ], + "Resource": "*" + } + ] + } + } + ] + } + }, + "SesCreateReceiptRuleFunction": { + "Type": "AWS::Lambda::Function", + "Properties": { + "Code": { + "S3Bucket": "com.gilt.public.backoffice", + "S3Key": "lambda_functions/cloudformation-helpers.zip" + }, + "Description": "Used to create SES receipt rules.", + "Handler": "aws/ses.createReceiptRule", + "Role": {"Fn::GetAtt" : [ "SesCreateReceiptRuleFunctionRole", "Arn" ] }, + "Runtime": "nodejs4.3", + "Timeout": 30 + }, + "DependsOn": [ + "SesCreateReceiptRuleFunctionRole" + ] } }, "Outputs": { @@ -409,6 +464,10 @@ "S3PutObjectFunctionArn": { "Description": "The ARN of the S3PutObjectFunction, for use in other CloudFormation templates.", "Value": { "Fn::GetAtt" : ["S3PutObjectFunction", "Arn"] } + }, + "SesCreateReceiptRuleFunctionArn": { + "Description": "The ARN of the SesCreateReceiptRuleFunction, for use in other CloudFormation templates.", + "Value": { "Fn::GetAtt" : ["SesCreateReceiptRuleFunction", "Arn"] } } } -} \ No newline at end of file +} diff --git a/test/aws/s3.putObject.template b/test/aws/s3.putObject.template index 74a60e2..8045050 100644 --- a/test/aws/s3.putObject.template +++ b/test/aws/s3.putObject.template @@ -51,4 +51,4 @@ ] } } -} \ No newline at end of file +} diff --git a/test/aws/ses.createReceiptRule.template b/test/aws/ses.createReceiptRule.template new file mode 100644 index 0000000..0b78e13 --- /dev/null +++ b/test/aws/ses.createReceiptRule.template @@ -0,0 +1,61 @@ +{ + "AWSTemplateFormatVersion": "2010-09-09", + "Parameters": { + "CFHelperStackName": { + "Type": "String", + "Description": "The name of the stack where you installed the CloudFormation helper functions. See https://github.com/gilt/cloudformation-helpers." + }, + "RuleSetName": { + "Type": "String", + "Description": "The name of the rule set where to create the rule. Must already exist." + }, + "S3Bucket": { + "Type": "String", + "Description": "The name of the S3 bucket where to put the object. Must already exist." + }, + "MailRecipient" :{ + "Type": "String", + "Description": "Email used to receive mails in the configured rule" + } + }, + "Resources": { + "CFHelperStack": { + "Type": "AWS::CloudFormation::Stack", + "Properties": { + "TemplateURL": "https://s3.amazonaws.com/com.gilt.public.backoffice/cloudformation_templates/lookup_stack_outputs.template" + } + }, + "CFHelper": { + "Type": "Custom::CFHelper", + "Properties": { + "ServiceToken": { "Fn::GetAtt" : ["CFHelperStack", "Outputs.LookupStackOutputsArn"] }, + "StackName": { "Ref": "CFHelperStackName" } + }, + "DependsOn": [ + "CFHelperStack" + ] + }, + "SesCreateReceiptRule": { + "Type": "Custom::SesCreateReceiptRule", + "Properties": { + "ServiceToken": { "Fn::GetAtt" : ["CFHelper", "SesCreateReceiptRuleFunctionArn"] }, + "Rule" : { + "Name": "Test-SESRule", + "Recipients" : [{ "Ref": "MailRecipient" }], + "Enabled" : true, + "ScanEnabled" : true, + "Actions" : [{ + "S3Action": { + "BucketName": { "Ref": "S3Bucket" }, + "ObjectKeyPrefix": "incoming_mails/" + } + }] + }, + "RuleSetName" :{ "Ref": "RuleSetName" } + }, + "DependsOn": [ + "CFHelper" + ] + } + } +}