generated from mats16/codespaces-cdk-template
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #44 from supabase-community/refactor-smtp-and-jwt
chore: Refactoring
- Loading branch information
Showing
8 changed files
with
410 additions
and
367 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import * as path from 'path'; | ||
import * as cdk from 'aws-cdk-lib'; | ||
import * as iam from 'aws-cdk-lib/aws-iam'; | ||
import * as lambda from 'aws-cdk-lib/aws-lambda'; | ||
import { NodejsFunction } from 'aws-cdk-lib/aws-lambda-nodejs'; | ||
import { Secret } from 'aws-cdk-lib/aws-secretsmanager'; | ||
import * as cr from 'aws-cdk-lib/custom-resources'; | ||
import { Construct } from 'constructs'; | ||
import { WorkMailStack } from '../aws-workmail'; | ||
|
||
interface SesSmtpProps { | ||
region: string; | ||
email: string; | ||
workMailEnabled: cdk.CfnCondition; | ||
} | ||
|
||
export class SesSmtp extends Construct { | ||
secret: Secret; | ||
host: string; | ||
port: number; | ||
email: string; | ||
|
||
constructor(scope: Construct, id: string, props: SesSmtpProps) { | ||
super(scope, id); | ||
|
||
const { region, email, workMailEnabled } = props; | ||
|
||
/** IAM Policy to send email via Amazon SES */ | ||
const sendEmailPolicy = new iam.Policy(this, 'SendEmailPolicy', { | ||
statements: [ | ||
new iam.PolicyStatement({ | ||
actions: ['ses:SendRawEmail'], | ||
resources: ['*'], | ||
}), | ||
], | ||
}); | ||
|
||
/** IAM User to send email via Amazon SES */ | ||
const user = new iam.User(this, 'User'); | ||
user.attachInlinePolicy(sendEmailPolicy); | ||
|
||
/** SMTP username */ | ||
const accessKey = new iam.CfnAccessKey(this, 'AccessKey', { userName: user.userName }); | ||
|
||
/** Custom resource handler to generate a SMTP password */ | ||
const passwordFunction = new NodejsFunction(this, 'PasswordFunction', { | ||
description: 'Supabase - Generate SMTP Password Function', | ||
entry: path.resolve(__dirname, 'cr-smtp-password.ts'), | ||
runtime: lambda.Runtime.NODEJS_18_X, | ||
}); | ||
|
||
/** Custom resource provider to generate a SMTP password */ | ||
const passwordProvider = new cr.Provider(this, 'PasswordProvider', { onEventHandler: passwordFunction }); | ||
|
||
/** SMTP password */ | ||
const password = new cdk.CustomResource(this, 'Password', { | ||
resourceType: 'Custom::Password', | ||
serviceToken: passwordProvider.serviceToken, | ||
properties: { | ||
Region: region, | ||
SecretAccessKey: accessKey.attrSecretAccessKey, | ||
}, | ||
}); | ||
|
||
const stackId = cdk.Fn.select(2, cdk.Fn.split('/', cdk.Aws.STACK_ID)); | ||
|
||
/** Amazon WorkMail Stack */ | ||
const workMail = new WorkMailStack(this, 'WorkMail', { | ||
description: 'Amazon WorkMail for Test Domain', | ||
organization: { region: region, alias: stackId }, | ||
}); | ||
// Add condition | ||
(workMail.node.defaultChild as cdk.CfnStack).cfnOptions.condition = workMailEnabled; | ||
|
||
/** The mail user on WorkMail */ | ||
const workMailUser = workMail.organization.addUser('Supabase', password.getAttString('Password')); | ||
|
||
this.host = cdk.Fn.conditionIf(workMailEnabled.logicalId, `smtp.mail.${region}.awsapps.com`, `email-smtp.${region}.amazonaws.com`).toString(); | ||
this.port = 465; | ||
this.email = cdk.Fn.conditionIf(workMailEnabled.logicalId, workMailUser.getAtt('Email'), email).toString(); | ||
|
||
/** | ||
* SMTP username | ||
* | ||
* If WorkMail is enabled, use the WorkMail user's email address. | ||
*/ | ||
const username = cdk.Fn.conditionIf(workMailEnabled.logicalId, workMailUser.getAtt('Email'), accessKey.ref).toString(); | ||
|
||
this.secret = new Secret(this, 'Secret', { | ||
secretName: `${cdk.Aws.STACK_NAME}${id}Secret`, | ||
description: 'Supabase - SMTP Secret', | ||
secretObjectValue: { | ||
username: cdk.SecretValue.unsafePlainText(username), | ||
password: cdk.SecretValue.resourceAttribute(password.getAttString('Password')), | ||
host: cdk.SecretValue.unsafePlainText(this.host), | ||
}, | ||
}); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.