@mavogel/awscdk-rootmail 0.0.17 → 0.0.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.jsii +57 -19
- package/API.md +117 -2
- package/README.md +91 -2
- package/docs/img/cloudformation-tpl-min.png +0 -0
- package/integ-tests/functions/custom-ses-receive-function.ts +92 -0
- package/integ-tests/integ.rootmail-custom-santa.ts +143 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/RootmailIntegTestStack.assets.json +162 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/RootmailIntegTestStack.template.json +4541 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.assets.json +32 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.template.json +249 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.0649b5bfaa322d6bd173d2daef3177389cf0778b556b300ac6bf451704cc3a49/index.js +37981 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.157ba1713f7224463b621261792d18081588ff306a9bc3f75e6c468fabbe1a60/index.js +106 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.4c079063901e6478dbb5ace19e3123641ebf0da2ab648ecf52079c5bec154196/index.js +215 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.53e8368959effe94a6e51bef9c7c5bf0eaf5c4d64eece81632ca62aa34517978.bundle/index.js +31981 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.5cc92ed0cea39e2b8de2dbc527dfb5980a3af9564bd1084d840b9787c7d0467e/index.d.ts +14 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.5cc92ed0cea39e2b8de2dbc527dfb5980a3af9564bd1084d840b9787c7d0467e/index.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.78b64aaf9aebb4eacbdd42197be10e9d5894360a642f7dcf79aeb84bb3552377/index.js +61 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.7e8a681794eff5fda1841caa360113df5488ecf944055a69b8f1e5a687e269f5/index.js +83 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.9572b451243318765beb576ae7528096b3879783dbaab795c02c01d7f4e83438/index.js +47 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.b8448324ef2c7d5bfa362722df2d397079d5aabaae2749f09180ac5bedfa5c89/index.js +105 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.d2b520d86fd9d964c83dc0d5ce2e4456dbde5fa7ae0c791a71865cbdfe2fb855/index.js +85 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/cfn-response.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/consts.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/framework.js +3 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/outbound.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f2d30cfc360482320a52a4fcde8a70f3569df79ab30be24650fda58eb60052cf/util.js +1 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/asset.f9b17c134d24cf60e1d26c033b5f4f247b759149619ac0aac341b950a10ccf14/index.js +72 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/integ.json +27 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/manifest.json +742 -0
- package/integ-tests/integ.rootmail-custom-santa.ts.snapshot/tree.json +5893 -0
- package/integ-tests/integ.rootmail.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.assets.json +2 -2
- package/integ-tests/integ.rootmail.ts.snapshot/SetupTestIntegStackDefaultTestDeployAssert6EB6FF77.template.json +3 -3
- package/integ-tests/integ.rootmail.ts.snapshot/manifest.json +1 -1
- package/lib/index-cli-synth.js +22 -11
- package/lib/index.js +2 -1
- package/lib/rootmail.d.ts +5 -1
- package/lib/rootmail.js +3 -2
- package/lib/scripts/publish-assets.js +18 -22
- package/lib/ses-receive.d.ts +5 -1
- package/lib/ses-receive.js +36 -14
- package/package.json +11 -6
package/.jsii
CHANGED
|
@@ -3502,10 +3502,14 @@
|
|
|
3502
3502
|
"docs": {
|
|
3503
3503
|
"stability": "stable"
|
|
3504
3504
|
},
|
|
3505
|
-
"homepage": "https://github.com/
|
|
3505
|
+
"homepage": "https://github.com/MV-Consulting/awscdk-rootmail",
|
|
3506
3506
|
"jsiiVersion": "5.3.18 (build 4f1b2de)",
|
|
3507
3507
|
"keywords": [
|
|
3508
|
-
"
|
|
3508
|
+
"aws",
|
|
3509
|
+
"cdk",
|
|
3510
|
+
"construct",
|
|
3511
|
+
"rootmail",
|
|
3512
|
+
"ses"
|
|
3509
3513
|
],
|
|
3510
3514
|
"license": "Apache-2.0",
|
|
3511
3515
|
"metadata": {
|
|
@@ -3518,11 +3522,11 @@
|
|
|
3518
3522
|
},
|
|
3519
3523
|
"name": "@mavogel/awscdk-rootmail",
|
|
3520
3524
|
"readme": {
|
|
3521
|
-
"markdown": "# awscdk-rootmail\n\nA single rootmail box for all your AWS accounts. The cdk implementation and **adaption** of the [superwerker](https://superwerker.cloud/) rootmail feature. See [here](docs/adrs/rootmail.md) for a detailed Architectural Decision Record ([ADR](https://adr.github.io/))\n\n- [TL;DR](#tldr)\n- [Usage](#usage)\n - [Dependencies](#dependencies)\n - [Deploy](#deploy)\n - [CDK](#cdk)\n - [Cloudformation](#cloudformation)\n- [Solution design: Version 1 - Domain in the same AWS account](#solution-design-version-1---domain-in-the-same-aws-account)\n- [Solution design: Version 2 - external DNS provider](#solution-design-version-2---external-dns-provider)\n- [Uninstall](#uninstall)\n- [Known issues](#known-issues)\n- [Related projects](#related-projects)\n\n## TL;DR\nEach AWS account needs one unique email address (the so-called \"AWS account root user email address\").\n\nAccess to these email addresses must be adequately secured since they provide privileged access to AWS accounts, such as account deletion procedures.\n\nThis is why you only need 1 mailing list for the AWS Management (formerly *root*) account,\nwe recommend the following pattern `aws-roots+<uuid>@mycompany.test`\n\n> [!NOTE]\n> Maximum **64** characters are allowed for the whole address.\n\nAnd as you own the domain `mycompany.test` you can add a subdomain, e.g. `aws`, for which all EMails will then be received with this solution within this particular AWS Management account.\n\n## Usage\n### Dependencies\nAdministrative access to an AWS account and the following tools:\n```sh\nbrew install aws-cli node@18 esbuild\n```\n\n<details>\n <summary>... or a longer listing</summary>\n\n- Access to a development environment. It is recommended to use AWS Cloud9 to avoid having to set up the tools needed to deploy the solution. See [Getting started with AWS Cloud9](https://aws.amazon.com/cloud9/getting-started/).\n- Using Cloud9, all of the following have already been configured for you. If you choose not to use Cloud9, you will need to install the following.\n - AWS CLI. See [Installing, updating and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)\n - Set up the AWS CLI with IAM access credentials. See [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)\n - Node.js version 10.13.0 or later\n - AWS CDK version 2.90.0 or later. For installation instructions see [Getting Started with the AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_install)\n - Docker version 20.10.x or later **OR** [esbuild](https://esbuild.github.io/)\n</details>\n\n### Deploy\nYou can chose via embedding the construct in your cdk-app or use is directly via Cloudformation.\n#### CDK\n1. To start a new project we recommend using [projen](https://projen.io/).\n 1. Create a new projen project\n ```sh\n npx projen new awscdk-app-ts\n ```\n 2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file\n 3. Run `npm run projen` to install it\n2. In you `main.ts` file add the following\n```ts\nimport {\n App,\n Stack,\n aws_route53 as r53\n} from 'aws-cdk-lib';\nimport { Rootmail, SESReceiveStack } from '@mavogel/awscdk-rootmail';\nimport { Construct } from 'constructs';\n\nexport class MyStack extends Stack {\n constructor(scope: Construct, id: string, props: StackProps = {}) {\n super(scope, id, props);\n\n const domain = 'mycompany.test'\n\n const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {\n domainName: domain,\n });\n\n const rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the SAME account\n domain: domain;\n // 2. so the subdomain will be aws.mycompany.test and\n // wired / delegated automatically\n autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n });\n }\n}\n```\n2. run on your commandline\n```sh\nnpm run deploy\n```\n1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!\n2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.\n\n#### Cloudformation\nor use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).\n\n\n<details>\n <summary>... click to expand</summary>\n\nand fill out the parameters\n\n\n</details>\n\n## Solution design: Version 1 - Domain in the same AWS account\n\n\n1. You own a domain, e.g., `mycompany.test`, registered via `Route53` in the **same** AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the `TXT` and `CNAME` records for verifying the domain towards SES **and** adds the NS server entries automatically to the main domain `mycompany.test`.\n3. When the subdomain `aws.mycompany.test` is successfully propagated, the stack creates a verified Domain in AWS SES and adds a recipient rule for `root@aws.mycompany.test`. On a successful propagation you will get a mail as follows to the root Email address of the account you are installing the stack 👇\n\n4. Now, any mail going to `root+<any-string>@aws.mycompany.test` will be processed by OpsSanta 🎅🏽 Lambda function and also stored in the rootmail S3 bucket 🪣.\n5. The OpsSanta function verifies the verdicts (DKIM etc.) of the sender, also skips AWS Account welcome EMails, and processes all other EMails. If it is a password reset link EMail it stores the link in the parameter store (and does *not* create an OpsItem for now). For all other mails, which are not skipped an OpsItem is created to have a central location for all items.\n> [!NOTE]\n> You can also connect your Jira to the OpsCenter.\n6. The bucket where all mail to `root@aws.mycompany.test` are stored.\n7. The [SSM parameter store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) for the password reset links.\n\n8. The OpsItem which is created. It is open and shall be further processed either in the OpsCenter or any other issue tracker.\n\n\n> [!NOTE]\n> SES support alias, so mail to `root+random-string@aws.mycompany.test` will also be catched and forwarded.\n\n## Solution design: Version 2 - external DNS provider\n<details>\n <summary>open for details</summary>\n\n\n\n```ts\nconst rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the same account\n domain: 'mycompany.test';\n // 2. '' is the default, so you can also remove it\n // autowireDNSParentHostedZoneID: '',\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n});\n```\n\n1. You own a domain, e.g., `mycompany.test`. It can be at any registrar such as `godaddy`, also `Route53` itself in another AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the TXT and CNAME records (for DKIM etc.) for verifying the domain towards SES. **NOTE:** You must now add the NS server entries into the Domain provider which owns the main domain `mycompany.test`.\n3. items 3-7 are the same as in `v1`\n\n\n> [!NOTE]\n> After running `npm run deploy` you need to do the following steps manually:\n\n1. watch out for the hosted zone `aws.mycompany.test` to be created\n By default you have **2 hours** to wire the DNS!\n2. Then create the NS record in your domain `mycompany.test` for the subdomain `aws.mycompany.test`. Here for Route53 in AWS:\n\n3. You can test it yourself via\n```sh\ndig +short NS 8.8.8.8 aws.mycompany.test\n# should return something like\nns-1111.your-dns-provider-10.org.\nns-2222.your-dns-provider-21.co.uk.\nns-33.your-dns-provider-04.com.\nns-444.your-dns-provider-12.net.\n```\nand also by sending an EMail, e.g. from Gmail to `root@aws.mycompany.test`\n\n</details>\n\n## Uninstall\n1. Delete the stack, the custom resources will delete most resources\n2. And by design you need to manually delete the S3 Bucket containing the mails. This is to prevent accidental deletion of the mails. You can find the bucket name in the stack output.\n\n3. Furthermore, the cloudwatch log groups are not deleted, since they might contain valuable information. You can delete them manually.\n\n## Known issues\n- [jsii/2071](https://github.com/aws/jsii/issues/2071): so adding `compilerOptions.\"esModuleInterop\": true,` in `tsconfig.json` is not possible. See aws-cdk usage with[typescript](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/#Usage_with_TypeScript). So we needed to change import from `import AWS from 'aws-sdk';` -> `import * as AWS from 'aws-sdk';` to be able to compile.\n\n## Related projects\n- [aws-account-factory-email](https://github.com/aws-samples/aws-account-factory-email): a similar approach with SES, however you need to manually configure it upfront and also it about delivering root mails for a specific account to a specific mailing list and mainly decouples the real email address from the one of the AWS account. The main difference is that we do not *hide* or decouple the email address, but more make those as unique and unguessable/bruteforable as possible (with `uuids`)."
|
|
3525
|
+
"markdown": "# awscdk-rootmail\n\nA single rootmail box for all your AWS accounts. The cdk implementation and **adaption** of the [superwerker](https://superwerker.cloud/) rootmail feature. See [here](docs/adrs/rootmail.md) for a detailed Architectural Decision Record ([ADR](https://adr.github.io/))\n\n- [TL;DR](#tldr)\n- [Usage](#usage)\n - [Dependencies](#dependencies)\n - [Deploy](#deploy)\n - [CDK](#cdk)\n - [Cloudformation](#cloudformation)\n- [Solution design: Version 1 - Domain in the same AWS account](#solution-design-version-1---domain-in-the-same-aws-account)\n- [Solution design: Version 2 - external DNS provider](#solution-design-version-2---external-dns-provider)\n- [Uninstall](#uninstall)\n- [Known issues](#known-issues)\n- [Related projects](#related-projects)\n\n## TL;DR\nEach AWS account needs one unique email address (the so-called \"AWS account root user email address\").\n\nAccess to these email addresses must be adequately secured since they provide privileged access to AWS accounts, such as account deletion procedures.\n\nThis is why you only need 1 mailing list for the AWS Management (formerly *root*) account,\nwe recommend the following pattern `aws-roots+<uuid>@mycompany.test`\n\n> [!NOTE]\n> Maximum **64** characters are allowed for the whole address.\n\nAnd as you own the domain `mycompany.test` you can add a subdomain, e.g. `aws`, for which all EMails will then be received with this solution within this particular AWS Management account.\n\n## Usage\n### Dependencies\nAdministrative access to an AWS account and the following tools:\n```sh\nbrew install aws-cli node@18 esbuild\n```\n\n<details>\n <summary>... or a longer listing</summary>\n\n- Access to a development environment. It is recommended to use AWS Cloud9 to avoid having to set up the tools needed to deploy the solution. See [Getting started with AWS Cloud9](https://aws.amazon.com/cloud9/getting-started/).\n- Using Cloud9, all of the following have already been configured for you. If you choose not to use Cloud9, you will need to install the following.\n - AWS CLI. See [Installing, updating and uninstalling the AWS CLI version 2](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html)\n - Set up the AWS CLI with IAM access credentials. See [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)\n - Node.js version 10.13.0 or later\n - AWS CDK version 2.90.0 or later. For installation instructions see [Getting Started with the AWS CDK](https://docs.aws.amazon.com/cdk/latest/guide/getting_started.html#getting_started_install)\n - Docker version 20.10.x or later **OR** [esbuild](https://esbuild.github.io/)\n</details>\n\n### Deploy\nYou can chose via embedding the construct in your cdk-app or use is directly via Cloudformation.\n#### CDK\n1. To start a new project we recommend using [projen](https://projen.io/).\n 1. Create a new projen project\n ```sh\n npx projen new awscdk-app-ts\n ```\n 2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file\n 3. Run `npm run projen` to install it\n2. In you `main.ts` file add the following code\n```ts\nimport {\n App,\n Stack,\n aws_route53 as r53\n} from 'aws-cdk-lib';\nimport { Rootmail } from '@mavogel/awscdk-rootmail';\nimport { Construct } from 'constructs';\n\nexport class MyStack extends Stack {\n constructor(scope: Construct, id: string, props: StackProps = {}) {\n super(scope, id, props);\n\n const domain = 'mycompany.test'\n\n const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {\n domainName: domain,\n });\n\n const rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the SAME account\n domain: domain;\n // 2. so the subdomain will be aws.mycompany.test and\n // wired / delegated automatically\n autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n });\n }\n}\n```\n2. run on your commandline\n```sh\nnpm run deploy\n```\n1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!\n2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.\n\n### CDK with your own receiver function\nYou might also want to pass in you own function on what to do when an EMail is received\n\nfile `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated\n- `EMAIL_BUCKET`\n- `EMAIL_BUCKET_ARN`\n\nas well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.\n\n```ts\nimport { S3 } from '@aws-sdk/client-s3';\nimport { ParsedMail, simpleParser } from 'mailparser';\n// populated by default\nconst emailBucket = process.env.EMAIL_BUCKET;\nconst emailBucketArn = process.env.EMAIL_BUCKET_ARN;\nconst s3 = new S3();\n\n// SESEventRecordsToLambda\n// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html\nexport const handler = async (event: SESEventRecordsToLambda) => {\n for (const record of event.Records) {\n\n const id = record.ses.mail.messageId;\n const key = `RootMail/${id}`;\n const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });\n\n const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);\n\n let title = msg.subject;\n console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);\n // use the content of the email body\n const body = msg.html;\n // add your custom code here ...\n\n // dummy example: list s3 buckets\n const buckets = await s3.listBuckets({});\n if (!buckets.Buckets) {\n console.log('No buckets found');\n return;\n }\n console.log('Buckets:');\n for (const bucket of buckets.Buckets || []) {\n console.log(bucket.Name);\n }\n }\n\n};\n```\nand you create a separate `NodejsFunction` as follows with the additionally needed IAM permissions:\n```ts\nconst customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {\n functionName: PhysicalName.GENERATE_IF_NEEDED,\n entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),\n runtime: lambda.Runtime.NODEJS_18_X,\n logRetention: 1,\n timeout: Duration.seconds(30),\n});\n\n// Note: any additional permissions you need to add to the function yourself!\ncustomSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({\n actions: [\n 's3:List*',\n ],\n resources: ['*'],\n}))\n```\nand then pass it into the `Rootmail` Stack\n```ts\nexport class MyStack extends Stack {\n constructor(scope: Construct, id: string, props: StackProps = {}) {\n super(scope, id, props);\n\n const domain = 'mycompany.test'\n const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {\n domainName: domain,\n });\n\n const rootmail = new Rootmail(this, 'rootmail-stack', {\n domain: domain;\n autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,\n env: {\n region: 'eu-west-1',\n },\n customSesReceiveFunction: customSesReceiveFunction, // <- pass it in here\n });\n }\n}\n```\n\n#### Cloudformation\nor use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).\n\n\n<details>\n <summary>... click to expand</summary>\n\nand fill out the parameters\n\n\n</details>\n\n## Solution design: Version 1 - Domain in the same AWS account\n\n\n1. You own a domain, e.g., `mycompany.test`, registered via `Route53` in the **same** AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the `TXT` and `CNAME` records for verifying the domain towards SES **and** adds the NS server entries automatically to the main domain `mycompany.test`.\n3. When the subdomain `aws.mycompany.test` is successfully propagated, the stack creates a verified Domain in AWS SES and adds a recipient rule for `root@aws.mycompany.test`. On a successful propagation you will get a mail as follows to the root Email address of the account you are installing the stack 👇\n\n4. Now, any mail going to `root+<any-string>@aws.mycompany.test` will be processed by OpsSanta 🎅🏽 Lambda function and also stored in the rootmail S3 bucket 🪣.\n5. The OpsSanta function verifies the verdicts (DKIM etc.) of the sender, also skips AWS Account welcome EMails, and processes all other EMails. If it is a password reset link EMail it stores the link in the parameter store (and does *not* create an OpsItem for now). For all other mails, which are not skipped an OpsItem is created to have a central location for all items.\n> [!NOTE]\n> You can also connect your Jira to the OpsCenter.\n6. The bucket where all mail to `root@aws.mycompany.test` are stored.\n7. The [SSM parameter store](https://docs.aws.amazon.com/systems-manager/latest/userguide/systems-manager-parameter-store.html) for the password reset links.\n\n8. The OpsItem which is created. It is open and shall be further processed either in the OpsCenter or any other issue tracker.\n\n\n> [!NOTE]\n> SES support alias, so mail to `root+random-string@aws.mycompany.test` will also be catched and forwarded.\n\n## Solution design: Version 2 - external DNS provider\n<details>\n <summary>open for details</summary>\n\n\n\n```ts\nconst rootmail = new Rootmail(this, 'rootmail-stack', {\n // 1. a domain you own, registered via Route53 in the same account\n domain: 'mycompany.test';\n // 2. '' is the default, so you can also remove it\n // autowireDNSParentHostedZoneID: '',\n env: {\n // 3. or any other region SES is available\n region: 'eu-west-1',\n },\n});\n```\n\n1. You own a domain, e.g., `mycompany.test`. It can be at any registrar such as `godaddy`, also `Route53` itself in another AWS account.\n2. The stack creates a `Route53` public Hosted Zone for the subdomain, e.g., `aws.mycompany.test`. It also automatically adds the TXT and CNAME records (for DKIM etc.) for verifying the domain towards SES. **NOTE:** You must now add the NS server entries into the Domain provider which owns the main domain `mycompany.test`.\n3. items 3-7 are the same as in `v1`\n\n\n> [!NOTE]\n> After running `npm run deploy` you need to do the following steps manually:\n\n1. watch out for the hosted zone `aws.mycompany.test` to be created\n By default you have **2 hours** to wire the DNS!\n2. Then create the NS record in your domain `mycompany.test` for the subdomain `aws.mycompany.test`. Here for Route53 in AWS:\n\n3. You can test it yourself via\n```sh\ndig +short NS 8.8.8.8 aws.mycompany.test\n# should return something like\nns-1111.your-dns-provider-10.org.\nns-2222.your-dns-provider-21.co.uk.\nns-33.your-dns-provider-04.com.\nns-444.your-dns-provider-12.net.\n```\nand also by sending an EMail, e.g. from Gmail to `root@aws.mycompany.test`\n\n</details>\n\n## Uninstall\n1. Delete the stack, the custom resources will delete most resources\n2. And by design you need to manually delete the S3 Bucket containing the mails. This is to prevent accidental deletion of the mails. You can find the bucket name in the stack output.\n\n3. Furthermore, the cloudwatch log groups are not deleted, since they might contain valuable information. You can delete them manually.\n\n## Known issues\n- [jsii/2071](https://github.com/aws/jsii/issues/2071): so adding `compilerOptions.\"esModuleInterop\": true,` in `tsconfig.json` is not possible. See aws-cdk usage with[typescript](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/#Usage_with_TypeScript). So we needed to change import from `import AWS from 'aws-sdk';` -> `import * as AWS from 'aws-sdk';` to be able to compile.\n\n## Related projects\n- [aws-account-factory-email](https://github.com/aws-samples/aws-account-factory-email): a similar approach with SES, however you need to manually configure it upfront and also it about delivering root mails for a specific account to a specific mailing list and mainly decouples the real email address from the one of the AWS account. The main difference is that we do not *hide* or decouple the email address, but more make those as unique and unguessable/bruteforable as possible (with `uuids`)."
|
|
3522
3526
|
},
|
|
3523
3527
|
"repository": {
|
|
3524
3528
|
"type": "git",
|
|
3525
|
-
"url": "https://github.com/
|
|
3529
|
+
"url": "https://github.com/MV-Consulting/awscdk-rootmail"
|
|
3526
3530
|
},
|
|
3527
3531
|
"schema": "jsii/0.10.0",
|
|
3528
3532
|
"targets": {
|
|
@@ -3544,7 +3548,7 @@
|
|
|
3544
3548
|
},
|
|
3545
3549
|
"locationInModule": {
|
|
3546
3550
|
"filename": "src/rootmail.ts",
|
|
3547
|
-
"line":
|
|
3551
|
+
"line": 65
|
|
3548
3552
|
},
|
|
3549
3553
|
"parameters": [
|
|
3550
3554
|
{
|
|
@@ -3570,7 +3574,7 @@
|
|
|
3570
3574
|
"kind": "class",
|
|
3571
3575
|
"locationInModule": {
|
|
3572
3576
|
"filename": "src/rootmail.ts",
|
|
3573
|
-
"line":
|
|
3577
|
+
"line": 61
|
|
3574
3578
|
},
|
|
3575
3579
|
"name": "Rootmail",
|
|
3576
3580
|
"properties": [
|
|
@@ -3581,7 +3585,7 @@
|
|
|
3581
3585
|
"immutable": true,
|
|
3582
3586
|
"locationInModule": {
|
|
3583
3587
|
"filename": "src/rootmail.ts",
|
|
3584
|
-
"line":
|
|
3588
|
+
"line": 63
|
|
3585
3589
|
},
|
|
3586
3590
|
"name": "emailBucket",
|
|
3587
3591
|
"type": {
|
|
@@ -3595,7 +3599,7 @@
|
|
|
3595
3599
|
"immutable": true,
|
|
3596
3600
|
"locationInModule": {
|
|
3597
3601
|
"filename": "src/rootmail.ts",
|
|
3598
|
-
"line":
|
|
3602
|
+
"line": 62
|
|
3599
3603
|
},
|
|
3600
3604
|
"name": "hostedZoneParameterName",
|
|
3601
3605
|
"type": {
|
|
@@ -3615,7 +3619,7 @@
|
|
|
3615
3619
|
"kind": "interface",
|
|
3616
3620
|
"locationInModule": {
|
|
3617
3621
|
"filename": "src/rootmail.ts",
|
|
3618
|
-
"line":
|
|
3622
|
+
"line": 20
|
|
3619
3623
|
},
|
|
3620
3624
|
"name": "RootmailProps",
|
|
3621
3625
|
"properties": [
|
|
@@ -3628,13 +3632,30 @@
|
|
|
3628
3632
|
"immutable": true,
|
|
3629
3633
|
"locationInModule": {
|
|
3630
3634
|
"filename": "src/rootmail.ts",
|
|
3631
|
-
"line":
|
|
3635
|
+
"line": 24
|
|
3632
3636
|
},
|
|
3633
3637
|
"name": "domain",
|
|
3634
3638
|
"type": {
|
|
3635
3639
|
"primitive": "string"
|
|
3636
3640
|
}
|
|
3637
3641
|
},
|
|
3642
|
+
{
|
|
3643
|
+
"abstract": true,
|
|
3644
|
+
"docs": {
|
|
3645
|
+
"stability": "stable",
|
|
3646
|
+
"summary": "The custom SES receive function to use."
|
|
3647
|
+
},
|
|
3648
|
+
"immutable": true,
|
|
3649
|
+
"locationInModule": {
|
|
3650
|
+
"filename": "src/rootmail.ts",
|
|
3651
|
+
"line": 51
|
|
3652
|
+
},
|
|
3653
|
+
"name": "customSesReceiveFunction",
|
|
3654
|
+
"optional": true,
|
|
3655
|
+
"type": {
|
|
3656
|
+
"fqn": "aws-cdk-lib.aws_lambda.Function"
|
|
3657
|
+
}
|
|
3658
|
+
},
|
|
3638
3659
|
{
|
|
3639
3660
|
"abstract": true,
|
|
3640
3661
|
"docs": {
|
|
@@ -3646,7 +3667,7 @@
|
|
|
3646
3667
|
"immutable": true,
|
|
3647
3668
|
"locationInModule": {
|
|
3648
3669
|
"filename": "src/rootmail.ts",
|
|
3649
|
-
"line":
|
|
3670
|
+
"line": 58
|
|
3650
3671
|
},
|
|
3651
3672
|
"name": "setDestroyPolicyToAllResources",
|
|
3652
3673
|
"optional": true,
|
|
@@ -3664,7 +3685,7 @@
|
|
|
3664
3685
|
"immutable": true,
|
|
3665
3686
|
"locationInModule": {
|
|
3666
3687
|
"filename": "src/rootmail.ts",
|
|
3667
|
-
"line":
|
|
3688
|
+
"line": 31
|
|
3668
3689
|
},
|
|
3669
3690
|
"name": "subdomain",
|
|
3670
3691
|
"optional": true,
|
|
@@ -3682,7 +3703,7 @@
|
|
|
3682
3703
|
"immutable": true,
|
|
3683
3704
|
"locationInModule": {
|
|
3684
3705
|
"filename": "src/rootmail.ts",
|
|
3685
|
-
"line":
|
|
3706
|
+
"line": 38
|
|
3686
3707
|
},
|
|
3687
3708
|
"name": "totalTimeToWireDNS",
|
|
3688
3709
|
"optional": true,
|
|
@@ -3700,7 +3721,7 @@
|
|
|
3700
3721
|
"immutable": true,
|
|
3701
3722
|
"locationInModule": {
|
|
3702
3723
|
"filename": "src/rootmail.ts",
|
|
3703
|
-
"line":
|
|
3724
|
+
"line": 46
|
|
3704
3725
|
},
|
|
3705
3726
|
"name": "wireDNSToHostedZoneID",
|
|
3706
3727
|
"optional": true,
|
|
@@ -3724,7 +3745,7 @@
|
|
|
3724
3745
|
},
|
|
3725
3746
|
"locationInModule": {
|
|
3726
3747
|
"filename": "src/ses-receive.ts",
|
|
3727
|
-
"line":
|
|
3748
|
+
"line": 48
|
|
3728
3749
|
},
|
|
3729
3750
|
"parameters": [
|
|
3730
3751
|
{
|
|
@@ -3750,7 +3771,7 @@
|
|
|
3750
3771
|
"kind": "class",
|
|
3751
3772
|
"locationInModule": {
|
|
3752
3773
|
"filename": "src/ses-receive.ts",
|
|
3753
|
-
"line":
|
|
3774
|
+
"line": 47
|
|
3754
3775
|
},
|
|
3755
3776
|
"name": "SESReceive",
|
|
3756
3777
|
"symbolId": "src/ses-receive:SESReceive"
|
|
@@ -3817,6 +3838,23 @@
|
|
|
3817
3838
|
"primitive": "string"
|
|
3818
3839
|
}
|
|
3819
3840
|
},
|
|
3841
|
+
{
|
|
3842
|
+
"abstract": true,
|
|
3843
|
+
"docs": {
|
|
3844
|
+
"stability": "stable",
|
|
3845
|
+
"summary": "The custom SES receive function to use."
|
|
3846
|
+
},
|
|
3847
|
+
"immutable": true,
|
|
3848
|
+
"locationInModule": {
|
|
3849
|
+
"filename": "src/ses-receive.ts",
|
|
3850
|
+
"line": 39
|
|
3851
|
+
},
|
|
3852
|
+
"name": "customSesReceiveFunction",
|
|
3853
|
+
"optional": true,
|
|
3854
|
+
"type": {
|
|
3855
|
+
"fqn": "aws-cdk-lib.aws_lambda.Function"
|
|
3856
|
+
}
|
|
3857
|
+
},
|
|
3820
3858
|
{
|
|
3821
3859
|
"abstract": true,
|
|
3822
3860
|
"docs": {
|
|
@@ -3827,7 +3865,7 @@
|
|
|
3827
3865
|
"immutable": true,
|
|
3828
3866
|
"locationInModule": {
|
|
3829
3867
|
"filename": "src/ses-receive.ts",
|
|
3830
|
-
"line":
|
|
3868
|
+
"line": 44
|
|
3831
3869
|
},
|
|
3832
3870
|
"name": "setDestroyPolicyToAllResources",
|
|
3833
3871
|
"optional": true,
|
|
@@ -3839,6 +3877,6 @@
|
|
|
3839
3877
|
"symbolId": "src/ses-receive:SESReceiveProps"
|
|
3840
3878
|
}
|
|
3841
3879
|
},
|
|
3842
|
-
"version": "0.0.
|
|
3843
|
-
"fingerprint": "
|
|
3880
|
+
"version": "0.0.19",
|
|
3881
|
+
"fingerprint": "OTlKqgZKTk9D7yr98AoGwym8x9PLwHV/GxKs95Rj1YY="
|
|
3844
3882
|
}
|
package/API.md
CHANGED
|
@@ -56,14 +56,14 @@ You can chose via embedding the construct in your cdk-app or use is directly via
|
|
|
56
56
|
```
|
|
57
57
|
2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file
|
|
58
58
|
3. Run `npm run projen` to install it
|
|
59
|
-
2. In you `main.ts` file add the following
|
|
59
|
+
2. In you `main.ts` file add the following code
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
App,
|
|
63
63
|
Stack,
|
|
64
64
|
aws_route53 as r53
|
|
65
65
|
} from 'aws-cdk-lib';
|
|
66
|
-
import { Rootmail
|
|
66
|
+
import { Rootmail } from '@mavogel/awscdk-rootmail';
|
|
67
67
|
import { Construct } from 'constructs';
|
|
68
68
|
|
|
69
69
|
export class MyStack extends Stack {
|
|
@@ -97,6 +97,95 @@ npm run deploy
|
|
|
97
97
|
1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!
|
|
98
98
|
2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.
|
|
99
99
|
|
|
100
|
+
### CDK with your own receiver function
|
|
101
|
+
You might also want to pass in you own function on what to do when an EMail is received
|
|
102
|
+
|
|
103
|
+
file `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated
|
|
104
|
+
- `EMAIL_BUCKET`
|
|
105
|
+
- `EMAIL_BUCKET_ARN`
|
|
106
|
+
|
|
107
|
+
as well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
111
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
112
|
+
// populated by default
|
|
113
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
114
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
115
|
+
const s3 = new S3();
|
|
116
|
+
|
|
117
|
+
// SESEventRecordsToLambda
|
|
118
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
119
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
120
|
+
for (const record of event.Records) {
|
|
121
|
+
|
|
122
|
+
const id = record.ses.mail.messageId;
|
|
123
|
+
const key = `RootMail/${id}`;
|
|
124
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
125
|
+
|
|
126
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
127
|
+
|
|
128
|
+
let title = msg.subject;
|
|
129
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
130
|
+
// use the content of the email body
|
|
131
|
+
const body = msg.html;
|
|
132
|
+
// add your custom code here ...
|
|
133
|
+
|
|
134
|
+
// dummy example: list s3 buckets
|
|
135
|
+
const buckets = await s3.listBuckets({});
|
|
136
|
+
if (!buckets.Buckets) {
|
|
137
|
+
console.log('No buckets found');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.log('Buckets:');
|
|
141
|
+
for (const bucket of buckets.Buckets || []) {
|
|
142
|
+
console.log(bucket.Name);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
and you create a separate `NodejsFunction` as follows with the additionally needed IAM permissions:
|
|
149
|
+
```ts
|
|
150
|
+
const customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {
|
|
151
|
+
functionName: PhysicalName.GENERATE_IF_NEEDED,
|
|
152
|
+
entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),
|
|
153
|
+
runtime: lambda.Runtime.NODEJS_18_X,
|
|
154
|
+
logRetention: 1,
|
|
155
|
+
timeout: Duration.seconds(30),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Note: any additional permissions you need to add to the function yourself!
|
|
159
|
+
customSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({
|
|
160
|
+
actions: [
|
|
161
|
+
's3:List*',
|
|
162
|
+
],
|
|
163
|
+
resources: ['*'],
|
|
164
|
+
}))
|
|
165
|
+
```
|
|
166
|
+
and then pass it into the `Rootmail` Stack
|
|
167
|
+
```ts
|
|
168
|
+
export class MyStack extends Stack {
|
|
169
|
+
constructor(scope: Construct, id: string, props: StackProps = {}) {
|
|
170
|
+
super(scope, id, props);
|
|
171
|
+
|
|
172
|
+
const domain = 'mycompany.test'
|
|
173
|
+
const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {
|
|
174
|
+
domainName: domain,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const rootmail = new Rootmail(this, 'rootmail-stack', {
|
|
178
|
+
domain: domain;
|
|
179
|
+
autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,
|
|
180
|
+
env: {
|
|
181
|
+
region: 'eu-west-1',
|
|
182
|
+
},
|
|
183
|
+
customSesReceiveFunction: customSesReceiveFunction, // <- pass it in here
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
100
189
|
#### Cloudformation
|
|
101
190
|
or use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).
|
|
102
191
|
|
|
@@ -425,6 +514,7 @@ const rootmailProps: RootmailProps = { ... }
|
|
|
425
514
|
| **Name** | **Type** | **Description** |
|
|
426
515
|
| --- | --- | --- |
|
|
427
516
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.domain">domain</a></code> | <code>string</code> | Domain used for root mail feature. |
|
|
517
|
+
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.customSesReceiveFunction">customSesReceiveFunction</a></code> | <code>aws-cdk-lib.aws_lambda.Function</code> | The custom SES receive function to use. |
|
|
428
518
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.setDestroyPolicyToAllResources">setDestroyPolicyToAllResources</a></code> | <code>boolean</code> | Whether to set all removal policies to DESTROY. |
|
|
429
519
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.subdomain">subdomain</a></code> | <code>string</code> | Subdomain used for root mail feature. |
|
|
430
520
|
| <code><a href="#@mavogel/awscdk-rootmail.RootmailProps.property.totalTimeToWireDNS">totalTimeToWireDNS</a></code> | <code>aws-cdk-lib.Duration</code> | The total time to wait for the DNS records to be available/wired. |
|
|
@@ -444,6 +534,18 @@ Domain used for root mail feature.
|
|
|
444
534
|
|
|
445
535
|
---
|
|
446
536
|
|
|
537
|
+
##### `customSesReceiveFunction`<sup>Optional</sup> <a name="customSesReceiveFunction" id="@mavogel/awscdk-rootmail.RootmailProps.property.customSesReceiveFunction"></a>
|
|
538
|
+
|
|
539
|
+
```typescript
|
|
540
|
+
public readonly customSesReceiveFunction: Function;
|
|
541
|
+
```
|
|
542
|
+
|
|
543
|
+
- *Type:* aws-cdk-lib.aws_lambda.Function
|
|
544
|
+
|
|
545
|
+
The custom SES receive function to use.
|
|
546
|
+
|
|
547
|
+
---
|
|
548
|
+
|
|
447
549
|
##### `setDestroyPolicyToAllResources`<sup>Optional</sup> <a name="setDestroyPolicyToAllResources" id="@mavogel/awscdk-rootmail.RootmailProps.property.setDestroyPolicyToAllResources"></a>
|
|
448
550
|
|
|
449
551
|
```typescript
|
|
@@ -515,6 +617,7 @@ const sESReceiveProps: SESReceiveProps = { ... }
|
|
|
515
617
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.domain">domain</a></code> | <code>string</code> | Domain used for root mail feature. |
|
|
516
618
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.emailbucket">emailbucket</a></code> | <code>aws-cdk-lib.aws_s3.Bucket</code> | S3 bucket to store received emails. |
|
|
517
619
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.subdomain">subdomain</a></code> | <code>string</code> | Subdomain used for root mail feature. |
|
|
620
|
+
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.customSesReceiveFunction">customSesReceiveFunction</a></code> | <code>aws-cdk-lib.aws_lambda.Function</code> | The custom SES receive function to use. |
|
|
518
621
|
| <code><a href="#@mavogel/awscdk-rootmail.SESReceiveProps.property.setDestroyPolicyToAllResources">setDestroyPolicyToAllResources</a></code> | <code>boolean</code> | Whether to set all removal policies to DESTROY. |
|
|
519
622
|
|
|
520
623
|
---
|
|
@@ -555,6 +658,18 @@ Subdomain used for root mail feature.
|
|
|
555
658
|
|
|
556
659
|
---
|
|
557
660
|
|
|
661
|
+
##### `customSesReceiveFunction`<sup>Optional</sup> <a name="customSesReceiveFunction" id="@mavogel/awscdk-rootmail.SESReceiveProps.property.customSesReceiveFunction"></a>
|
|
662
|
+
|
|
663
|
+
```typescript
|
|
664
|
+
public readonly customSesReceiveFunction: Function;
|
|
665
|
+
```
|
|
666
|
+
|
|
667
|
+
- *Type:* aws-cdk-lib.aws_lambda.Function
|
|
668
|
+
|
|
669
|
+
The custom SES receive function to use.
|
|
670
|
+
|
|
671
|
+
---
|
|
672
|
+
|
|
558
673
|
##### `setDestroyPolicyToAllResources`<sup>Optional</sup> <a name="setDestroyPolicyToAllResources" id="@mavogel/awscdk-rootmail.SESReceiveProps.property.setDestroyPolicyToAllResources"></a>
|
|
559
674
|
|
|
560
675
|
```typescript
|
package/README.md
CHANGED
|
@@ -56,14 +56,14 @@ You can chose via embedding the construct in your cdk-app or use is directly via
|
|
|
56
56
|
```
|
|
57
57
|
2. Add `@mavogel/awscdk-rootmail` as a dependency to your project in the `.projenrc.ts` file
|
|
58
58
|
3. Run `npm run projen` to install it
|
|
59
|
-
2. In you `main.ts` file add the following
|
|
59
|
+
2. In you `main.ts` file add the following code
|
|
60
60
|
```ts
|
|
61
61
|
import {
|
|
62
62
|
App,
|
|
63
63
|
Stack,
|
|
64
64
|
aws_route53 as r53
|
|
65
65
|
} from 'aws-cdk-lib';
|
|
66
|
-
import { Rootmail
|
|
66
|
+
import { Rootmail } from '@mavogel/awscdk-rootmail';
|
|
67
67
|
import { Construct } from 'constructs';
|
|
68
68
|
|
|
69
69
|
export class MyStack extends Stack {
|
|
@@ -97,6 +97,95 @@ npm run deploy
|
|
|
97
97
|
1. No need to do anything, the NS records are **automatically** propagated as the parent Hosted Zone is in the same account!
|
|
98
98
|
2. The `hosted-zone-dkim-propagation-provider.is-complete-handler` Lambda function checks every 10 seconds if the DNS for the subdomain is propagated. Details are in the Cloudwatch log group.
|
|
99
99
|
|
|
100
|
+
### CDK with your own receiver function
|
|
101
|
+
You might also want to pass in you own function on what to do when an EMail is received
|
|
102
|
+
|
|
103
|
+
file `functions/custom-ses-receive-function.ts` which gets the 2 environment variables populated
|
|
104
|
+
- `EMAIL_BUCKET`
|
|
105
|
+
- `EMAIL_BUCKET_ARN`
|
|
106
|
+
|
|
107
|
+
as well as `s3:GetObject` on the `RootMail/*` objects in the created Rootmail `S3` bucket.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
111
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
112
|
+
// populated by default
|
|
113
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
114
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
115
|
+
const s3 = new S3();
|
|
116
|
+
|
|
117
|
+
// SESEventRecordsToLambda
|
|
118
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
119
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
120
|
+
for (const record of event.Records) {
|
|
121
|
+
|
|
122
|
+
const id = record.ses.mail.messageId;
|
|
123
|
+
const key = `RootMail/${id}`;
|
|
124
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
125
|
+
|
|
126
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
127
|
+
|
|
128
|
+
let title = msg.subject;
|
|
129
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
130
|
+
// use the content of the email body
|
|
131
|
+
const body = msg.html;
|
|
132
|
+
// add your custom code here ...
|
|
133
|
+
|
|
134
|
+
// dummy example: list s3 buckets
|
|
135
|
+
const buckets = await s3.listBuckets({});
|
|
136
|
+
if (!buckets.Buckets) {
|
|
137
|
+
console.log('No buckets found');
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
console.log('Buckets:');
|
|
141
|
+
for (const bucket of buckets.Buckets || []) {
|
|
142
|
+
console.log(bucket.Name);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
};
|
|
147
|
+
```
|
|
148
|
+
and you create a separate `NodejsFunction` as follows with the additionally needed IAM permissions:
|
|
149
|
+
```ts
|
|
150
|
+
const customSesReceiveFunction = new NodejsFunction(stackUnderTest, 'custom-ses-receive-function', {
|
|
151
|
+
functionName: PhysicalName.GENERATE_IF_NEEDED,
|
|
152
|
+
entry: path.join(__dirname, 'functions', 'custom-ses-receive-function.ts'),
|
|
153
|
+
runtime: lambda.Runtime.NODEJS_18_X,
|
|
154
|
+
logRetention: 1,
|
|
155
|
+
timeout: Duration.seconds(30),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
// Note: any additional permissions you need to add to the function yourself!
|
|
159
|
+
customSesReceiveFunction.addToRolePolicy(new iam.PolicyStatement({
|
|
160
|
+
actions: [
|
|
161
|
+
's3:List*',
|
|
162
|
+
],
|
|
163
|
+
resources: ['*'],
|
|
164
|
+
}))
|
|
165
|
+
```
|
|
166
|
+
and then pass it into the `Rootmail` Stack
|
|
167
|
+
```ts
|
|
168
|
+
export class MyStack extends Stack {
|
|
169
|
+
constructor(scope: Construct, id: string, props: StackProps = {}) {
|
|
170
|
+
super(scope, id, props);
|
|
171
|
+
|
|
172
|
+
const domain = 'mycompany.test'
|
|
173
|
+
const hostedZone = r53.HostedZone.fromLookup(this, 'rootmail-parent-hosted-zone', {
|
|
174
|
+
domainName: domain,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const rootmail = new Rootmail(this, 'rootmail-stack', {
|
|
178
|
+
domain: domain;
|
|
179
|
+
autowireDNSParentHostedZoneID: hostedZone.hostedZoneId,
|
|
180
|
+
env: {
|
|
181
|
+
region: 'eu-west-1',
|
|
182
|
+
},
|
|
183
|
+
customSesReceiveFunction: customSesReceiveFunction, // <- pass it in here
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
100
189
|
#### Cloudformation
|
|
101
190
|
or use it directly a Cloudformation template `yaml` from the URL [here](https://mvc-test4-bucket-eu-west-1.s3.eu-west-1.amazonaws.com/rootmail/0.0.13-DEVELOPMENT/rootmailStack.template.yaml).
|
|
102
191
|
|
|
Binary file
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { S3 } from '@aws-sdk/client-s3';
|
|
2
|
+
import { ParsedMail, simpleParser } from 'mailparser';
|
|
3
|
+
// from https://docs.aws.amazon.com/ses/latest/dg/receiving-email-action-lambda-event.html
|
|
4
|
+
export interface EventRecord {
|
|
5
|
+
eventSource: string;
|
|
6
|
+
eventVersion: string;
|
|
7
|
+
ses: Ses;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Ses {
|
|
11
|
+
mail: Mail;
|
|
12
|
+
receipt: Receipt;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Mail {
|
|
16
|
+
timestamp: string;
|
|
17
|
+
source: string;
|
|
18
|
+
messageId: string;
|
|
19
|
+
destination: string[];
|
|
20
|
+
headersTruncated: boolean;
|
|
21
|
+
headers: Header[];
|
|
22
|
+
commonHeaders: CommonHeaders;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface Header {
|
|
26
|
+
name: string;
|
|
27
|
+
value: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface CommonHeaders {
|
|
31
|
+
returnPath: string;
|
|
32
|
+
from: string[];
|
|
33
|
+
date: string;
|
|
34
|
+
to: string[];
|
|
35
|
+
messageId: string;
|
|
36
|
+
subject: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface Receipt {
|
|
40
|
+
timestamp: string;
|
|
41
|
+
processingTimeMillis: number;
|
|
42
|
+
recipients: string[];
|
|
43
|
+
spamVerdict: Verdict;
|
|
44
|
+
virusVerdict: Verdict;
|
|
45
|
+
spfVerdict: Verdict;
|
|
46
|
+
dkimVerdict: Verdict;
|
|
47
|
+
dmarcVerdict: Verdict;
|
|
48
|
+
action: Action;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Verdict {
|
|
52
|
+
status: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface Action {
|
|
56
|
+
type: string;
|
|
57
|
+
functionArn: string;
|
|
58
|
+
invocationType: string;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface SESEventRecordsToLambda {
|
|
62
|
+
Records: EventRecord[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const emailBucket = process.env.EMAIL_BUCKET;
|
|
66
|
+
const emailBucketArn = process.env.EMAIL_BUCKET_ARN;
|
|
67
|
+
const s3 = new S3();
|
|
68
|
+
|
|
69
|
+
export const handler = async (event: SESEventRecordsToLambda) => {
|
|
70
|
+
for (const record of event.Records) {
|
|
71
|
+
|
|
72
|
+
const id = record.ses.mail.messageId;
|
|
73
|
+
const key = `RootMail/${id}`;
|
|
74
|
+
const response = await s3.getObject({ Bucket: emailBucket as string, Key: key });
|
|
75
|
+
|
|
76
|
+
const msg: ParsedMail = await simpleParser(response.Body as unknown as Buffer);
|
|
77
|
+
|
|
78
|
+
let title = msg.subject;
|
|
79
|
+
console.log(`Title: ${title} from emailBucketArn: ${emailBucketArn}`);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// test custom permission to list s3 buckets
|
|
83
|
+
const buckets = await s3.listBuckets({});
|
|
84
|
+
if (!buckets.Buckets) {
|
|
85
|
+
console.log('No buckets found');
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
console.log('Buckets:');
|
|
89
|
+
for (const bucket of buckets.Buckets || []) {
|
|
90
|
+
console.log(bucket.Name);
|
|
91
|
+
}
|
|
92
|
+
};
|