From ba5d1408d95ed54328cef16647b71d7a801cfeca Mon Sep 17 00:00:00 2001 From: seeebiii Date: Tue, 14 Nov 2023 21:25:57 +0100 Subject: [PATCH] fix: ensure catch-all is working properly --- src/email-forwarding-rule.ts | 5 ++-- test/email-forwarding-rule.test.ts | 41 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/email-forwarding-rule.ts b/src/email-forwarding-rule.ts index 66816fb..35e143c 100644 --- a/src/email-forwarding-rule.ts +++ b/src/email-forwarding-rule.ts @@ -129,12 +129,13 @@ export class EmailForwardingRule extends Construct { scanEnabled: true, receiptRuleName: props.id + '-rule-set', tlsPolicy: TlsPolicy.REQUIRE, - recipients: Object.keys(forwardMapping), + // catch all recipients may not start with '@' and just include the domain + recipients: Object.keys(forwardMapping).map(val => val.startsWith('@') ? val.substring(1) : val), }); } private convertForwardMappingToMap(props: EmailForwardingRuleProps) { - const forwardMapping: { [key: string]: string[] } = {}; + const forwardMapping: Record = {}; props.emailMapping.forEach((val) => { let email = val.receiveEmail; if (!email && val.receivePrefix) { diff --git a/test/email-forwarding-rule.test.ts b/test/email-forwarding-rule.test.ts index a06434d..aa6c4b1 100644 --- a/test/email-forwarding-rule.test.ts +++ b/test/email-forwarding-rule.test.ts @@ -183,4 +183,45 @@ describe('email forwarding rule', () => { }, })); }); + + it('ensure catch-all email is properly configured', () => { + const app = new App(); + const stack = new Stack(app, 'TestStack'); + + const ruleId = 'example.org-id'; + const receiptRuleSet = new ReceiptRuleSet(stack, 'example', {}); + + new EmailForwardingRule(stack, 'ExampleRule', { + domainName: 'example.org', + fromPrefix: 'noreply', + id: ruleId, + emailMapping: [{ + receiveEmail: '@example.org', + targetEmails: ['admin+hello@gmail.com'], + }], + ruleSet: receiptRuleSet, + }); + + Template.fromStack(stack).hasResource('AWS::SES::ReceiptRule', Match.objectLike({ + Properties: { + Rule: Match.objectLike({ + Name: `${ruleId}-rule-set`, + Actions: Match.arrayWith([{ + S3Action: Match.objectLike({ + ObjectKeyPrefix: 'inbox/', + }), + }, { + LambdaAction: Match.anyValue(), + }]), + Recipients: ['example.org'], + }), + }, + })); + + Template.fromStack(stack).hasResource('AWS::SSM::Parameter', Match.objectLike({ + Properties: { + Value: JSON.stringify({ '@example.org': ['admin+hello@gmail.com'] }), + }, + })); + }); });