|
| 1 | +import * as path from 'path'; |
| 2 | +import * as cdk from 'aws-cdk-lib'; |
| 3 | +import * as iam from 'aws-cdk-lib/aws-iam'; |
| 4 | +import * as lambda from 'aws-cdk-lib/aws-lambda'; |
| 5 | +import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; |
| 6 | +import { Secret } from 'aws-cdk-lib/aws-secretsmanager'; |
| 7 | +import * as cr from 'aws-cdk-lib/custom-resources'; |
| 8 | +import { Construct } from 'constructs'; |
| 9 | +import { WorkMailStack } from '../aws-workmail'; |
| 10 | + |
| 11 | +interface SesSmtpProps { |
| 12 | + region: string; |
| 13 | + email: string; |
| 14 | + workMailEnabled: cdk.CfnCondition; |
| 15 | +} |
| 16 | + |
| 17 | +export class SesSmtp extends Construct { |
| 18 | + secret: Secret; |
| 19 | + host: string; |
| 20 | + port: number; |
| 21 | + email: string; |
| 22 | + |
| 23 | + constructor(scope: Construct, id: string, props: SesSmtpProps) { |
| 24 | + super(scope, id); |
| 25 | + |
| 26 | + const { region, email, workMailEnabled } = props; |
| 27 | + |
| 28 | + /** IAM Policy to send email via Amazon SES */ |
| 29 | + const sendEmailPolicy = new iam.Policy(this, 'SendEmailPolicy', { |
| 30 | + statements: [ |
| 31 | + new iam.PolicyStatement({ |
| 32 | + actions: ['ses:SendRawEmail'], |
| 33 | + resources: ['*'], |
| 34 | + }), |
| 35 | + ], |
| 36 | + }); |
| 37 | + |
| 38 | + /** IAM User to send email via Amazon SES */ |
| 39 | + const user = new iam.User(this, 'User'); |
| 40 | + user.attachInlinePolicy(sendEmailPolicy); |
| 41 | + |
| 42 | + /** SMTP username */ |
| 43 | + const accessKey = new iam.CfnAccessKey(this, 'AccessKey', { userName: user.userName }); |
| 44 | + |
| 45 | + /** Custom resource handler to generate a SMTP password */ |
| 46 | + const passwordFunction = new NodejsFunction(this, 'PasswordFunction', { |
| 47 | + description: 'Supabase - Generate SMTP Password Function', |
| 48 | + entry: path.resolve(__dirname, 'cr-smtp-password.ts'), |
| 49 | + runtime: lambda.Runtime.NODEJS_18_X, |
| 50 | + }); |
| 51 | + |
| 52 | + /** Custom resource provider to generate a SMTP password */ |
| 53 | + const passwordProvider = new cr.Provider(this, 'PasswordProvider', { onEventHandler: passwordFunction }); |
| 54 | + |
| 55 | + /** SMTP password */ |
| 56 | + const password = new cdk.CustomResource(this, 'Password', { |
| 57 | + resourceType: 'Custom::Password', |
| 58 | + serviceToken: passwordProvider.serviceToken, |
| 59 | + properties: { |
| 60 | + Region: region, |
| 61 | + SecretAccessKey: accessKey.attrSecretAccessKey, |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + const stackId = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Aws.STACK_ID)); |
| 66 | + |
| 67 | + /** Amazon WorkMail Stack */ |
| 68 | + const workMail = new WorkMailStack(this, 'WorkMail', { |
| 69 | + description: 'Amazon WorkMail for Test Domain', |
| 70 | + organization: { region: region, alias: stackId }, |
| 71 | + }); |
| 72 | + // Add condition |
| 73 | + (workMail.node.defaultChild as cdk.CfnStack).cfnOptions.condition = workMailEnabled; |
| 74 | + |
| 75 | + /** The mail user on WorkMail */ |
| 76 | + const workMailUser = workMail.organization.addUser('Supabase', password.getAttString('Password')); |
| 77 | + |
| 78 | + this.host = cdk.Fn.conditionIf(workMailEnabled.logicalId, `smtp.mail.${region}.awsapps.com`, `email-smtp.${region}.amazonaws.com`).toString(); |
| 79 | + this.port = 465; |
| 80 | + this.email = cdk.Fn.conditionIf(workMailEnabled.logicalId, workMailUser.getAtt('Email'), email).toString(); |
| 81 | + |
| 82 | + /** |
| 83 | + * SMTP username |
| 84 | + * |
| 85 | + * If WorkMail is enabled, use the WorkMail user's email address. |
| 86 | + */ |
| 87 | + const username = cdk.Fn.conditionIf(workMailEnabled.logicalId, workMailUser.getAtt('Email'), accessKey.ref).toString(); |
| 88 | + |
| 89 | + this.secret = new Secret(this, 'Secret', { |
| 90 | + secretName: `${cdk.Aws.STACK_NAME}${id}Secret`, |
| 91 | + description: 'Supabase - SMTP Secret', |
| 92 | + secretObjectValue: { |
| 93 | + username: cdk.SecretValue.unsafePlainText(username), |
| 94 | + password: cdk.SecretValue.resourceAttribute(password.getAttString('Password')), |
| 95 | + host: cdk.SecretValue.unsafePlainText(this.host), |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments