Skip to content

Commit

Permalink
chore(ses): use NodeJS 14 in DropSpamReceiptRule (#13266)
Browse files Browse the repository at this point in the history
Closes #13223


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
jogold authored Mar 1, 2021
1 parent ad01099 commit 9663093
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 32 deletions.
59 changes: 54 additions & 5 deletions packages/@aws-cdk/aws-ses-actions/test/integ.actions.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
"Code": {
"ZipFile": "exports.handler = async (event) => event;"
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"FunctionServiceRole675BB04A",
"Arn"
]
},
"Handler": "index.handler",
"Runtime": "nodejs10.x"
},
"DependsOn": [
Expand Down Expand Up @@ -341,16 +341,51 @@
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = function dropSpamCode(event, _, callback) {\n console.log('Spam filter');\n const sesNotification = event.Records[0].ses;\n console.log('SES Notification:\\n', JSON.stringify(sesNotification, null, 2));\n // Check if any spam check failed\n if (sesNotification.receipt.spfVerdict.status === 'FAIL'\n || sesNotification.receipt.dkimVerdict.status === 'FAIL'\n || sesNotification.receipt.spamVerdict.status === 'FAIL'\n || sesNotification.receipt.virusVerdict.status === 'FAIL') {\n console.log('Dropping spam');\n // Stop processing rule set, dropping message\n callback(null, { disposition: 'STOP_RULE_SET' });\n }\n else {\n callback(null, null);\n }\n}"
"S3Bucket": {
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3Bucket6AFCBA5F"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086"
}
]
}
]
}
]
]
}
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"SingletonLambda224e77f9a32e4b4dac32983477abba16ServiceRole3037F5B4",
"Arn"
]
},
"Runtime": "nodejs10.x"
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
"SingletonLambda224e77f9a32e4b4dac32983477abba16ServiceRole3037F5B4"
Expand All @@ -372,5 +407,19 @@
}
}
}
},
"Parameters": {
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3Bucket6AFCBA5F": {
"Type": "String",
"Description": "S3 bucket for asset \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
},
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086": {
"Type": "String",
"Description": "S3 key for asset version \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
},
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34ArtifactHash6BE57680": {
"Type": "String",
"Description": "Artifact hash for asset \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
}
}
}
}
22 changes: 22 additions & 0 deletions packages/@aws-cdk/aws-ses/lib/drop-spam-handler/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable no-console */

// Adapted from https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-action-lambda-example-functions.html
export async function handler(event: AWSLambda.SESEvent): Promise<{ disposition: string } | null> {
console.log('Spam filter');

const sesNotification = event.Records[0].ses;
console.log('SES Notification: %j', sesNotification);

// Check if any spam check failed
if (sesNotification.receipt.spfVerdict.status === 'FAIL'
|| sesNotification.receipt.dkimVerdict.status === 'FAIL'
|| sesNotification.receipt.spamVerdict.status === 'FAIL'
|| sesNotification.receipt.virusVerdict.status === 'FAIL') {
console.log('Dropping spam');

// Stop processing rule set, dropping message
return { disposition: 'STOP_RULE_SET' };
}

return null;
}
27 changes: 3 additions & 24 deletions packages/@aws-cdk/aws-ses/lib/receipt-rule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { Aws, IResource, Lazy, Resource } from '@aws-cdk/core';
Expand Down Expand Up @@ -175,9 +176,9 @@ export class DropSpamReceiptRule extends CoreConstruct {
super(scope, id);

const fn = new lambda.SingletonFunction(this, 'Function', {
runtime: lambda.Runtime.NODEJS_10_X,
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromInline(`exports.handler = ${dropSpamCode}`),
code: lambda.Code.fromAsset(path.join(__dirname, 'drop-spam-handler')),
uuid: '224e77f9-a32e-4b4d-ac32-983477abba16',
});

Expand All @@ -203,25 +204,3 @@ export class DropSpamReceiptRule extends CoreConstruct {
});
}
}

// Adapted from https://docs.aws.amazon.com/ses/latest/DeveloperGuide/receiving-email-action-lambda-example-functions.html
/* eslint-disable no-console */
function dropSpamCode(event: any, _: any, callback: any) {
console.log('Spam filter');

const sesNotification = event.Records[0].ses;
console.log('SES Notification:\n', JSON.stringify(sesNotification, null, 2));

// Check if any spam check failed
if (sesNotification.receipt.spfVerdict.status === 'FAIL'
|| sesNotification.receipt.dkimVerdict.status === 'FAIL'
|| sesNotification.receipt.spamVerdict.status === 'FAIL'
|| sesNotification.receipt.virusVerdict.status === 'FAIL') {
console.log('Dropping spam');

// Stop processing rule set, dropping message
callback(null, { disposition: 'STOP_RULE_SET' });
} else {
callback(null, null);
}
}
55 changes: 52 additions & 3 deletions packages/@aws-cdk/aws-ses/test/integ.receipt.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,51 @@
"Type": "AWS::Lambda::Function",
"Properties": {
"Code": {
"ZipFile": "exports.handler = function dropSpamCode(event, _, callback) {\n console.log('Spam filter');\n const sesNotification = event.Records[0].ses;\n console.log('SES Notification:\\n', JSON.stringify(sesNotification, null, 2));\n // Check if any spam check failed\n if (sesNotification.receipt.spfVerdict.status === 'FAIL'\n || sesNotification.receipt.dkimVerdict.status === 'FAIL'\n || sesNotification.receipt.spamVerdict.status === 'FAIL'\n || sesNotification.receipt.virusVerdict.status === 'FAIL') {\n console.log('Dropping spam');\n // Stop processing rule set, dropping message\n callback(null, { disposition: 'STOP_RULE_SET' });\n }\n else {\n callback(null, null);\n }\n}"
"S3Bucket": {
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3Bucket6AFCBA5F"
},
"S3Key": {
"Fn::Join": [
"",
[
{
"Fn::Select": [
0,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086"
}
]
}
]
},
{
"Fn::Select": [
1,
{
"Fn::Split": [
"||",
{
"Ref": "AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086"
}
]
}
]
}
]
]
}
},
"Handler": "index.handler",
"Role": {
"Fn::GetAtt": [
"SingletonLambda224e77f9a32e4b4dac32983477abba16ServiceRole3037F5B4",
"Arn"
]
},
"Runtime": "nodejs10.x"
"Handler": "index.handler",
"Runtime": "nodejs14.x"
},
"DependsOn": [
"SingletonLambda224e77f9a32e4b4dac32983477abba16ServiceRole3037F5B4"
Expand Down Expand Up @@ -150,5 +185,19 @@
}
}
}
},
"Parameters": {
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3Bucket6AFCBA5F": {
"Type": "String",
"Description": "S3 bucket for asset \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
},
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34S3VersionKey02BA9086": {
"Type": "String",
"Description": "S3 key for asset version \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
},
"AssetParameters96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34ArtifactHash6BE57680": {
"Type": "String",
"Description": "Artifact hash for asset \"96d0b6be9a64ae309bf89a86f5515453f0fa1d07b4f6b37198051cc98e251f34\""
}
}
}

0 comments on commit 9663093

Please sign in to comment.