Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mailer-16: feat: add secrets invoke from aws secrets #22

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"registry": "https://npm.pkg.github.com"
},
"dependencies": {
"@aws-sdk/client-secrets-manager": "^3.583.0",
"axios": "^1.7.2",
"ejs": "^3.1.10"
}
Expand Down
8 changes: 8 additions & 0 deletions src/config/aws.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { AwsRegion } from '../strategies/secretStrategies/secrets';

const awsConfig = {
region: process.env.AWS_REGION as AwsRegion,
secretManagerName: process.env.SECRET_MANAGER_NAME as string
};

export default awsConfig;
34 changes: 28 additions & 6 deletions src/config/mail.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
export default {
from: process.env.MAIL_FROM_ADDRESS,
name: process.env.MAIL_FROM_NAME,
mailServer: {
host: process.env.MAIL_SERVER_HOST || '',
password: process.env.MAIL_PASSWORD || ''
import getSecret from '../strategies/secretStrategies/secretStrategy';
import storageProvider, {
MAIL_CONFIG_KEYS
} from '../providers/awsConfigStorage.provider';

const getMailConfig = async () => {
if (!storageProvider.size) {
const [from, name, host, password] = await Promise.all(
Object.keys(MAIL_CONFIG_KEYS).map(getSecret)
);

storageProvider.set(MAIL_CONFIG_KEYS.MAIL_FROM_ADDRESS, from);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_FROM_NAME, name);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_SERVER_HOST, host);
storageProvider.set(MAIL_CONFIG_KEYS.MAIL_PASSWORD, password);
}

const mailConfig = {
from: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_FROM_ADDRESS),
name: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_FROM_NAME),
mailServer: {
host: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_SERVER_HOST),
password: storageProvider.get(MAIL_CONFIG_KEYS.MAIL_PASSWORD)
}
};

return mailConfig;
};

export default getMailConfig;
10 changes: 10 additions & 0 deletions src/providers/awsConfigStorage.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const MAIL_CONFIG_KEYS = {
MAIL_FROM_ADDRESS: 'MAIL_FROM_ADDRESS' as const,
MAIL_FROM_NAME: 'MAIL_FROM_NAME' as const,
MAIL_SERVER_HOST: 'MAIL_SERVER_HOST' as const,
MAIL_PASSWORD: 'MAIL_PASSWORD' as const
};

type MAIL_CONFIG_KEYS = keyof typeof MAIL_CONFIG_KEYS;

export default new Map<MAIL_CONFIG_KEYS, string>();
6 changes: 4 additions & 2 deletions src/services/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { render } from 'ejs';

import en from '../lang/en';

import mailConfig from '../config/mail.config';
import getMailConfig from '../config/mail.config';

import { sendMail } from './mailServer';

Expand Down Expand Up @@ -82,7 +82,7 @@ class Mail {
return this;
}

send() {
async send() {
if (!this.mailNode.to.length) {
throw new Error(en.error.mail.to);
}
Expand All @@ -91,6 +91,8 @@ class Mail {
throw new Error(en.error.mail.subject);
}

const mailConfig = await getMailConfig();

if (!this.mailNode.fromAddress) {
this.from(mailConfig.from, mailConfig.name);
}
Expand Down
4 changes: 3 additions & 1 deletion src/services/mailServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';

import mailConfig from '../config/mail.config';
import getMailConfig from '../config/mail.config';

import type { IMailNode } from './mail';

Expand All @@ -20,6 +20,8 @@ export const createPayload = (payload: IMailNode) => {
};

export const sendMail = async (payload: IMailNode) => {
const mailConfig = await getMailConfig();

return await http.post(mailConfig.mailServer.host, createPayload(payload), {
headers: {
'x-api-key': mailConfig.mailServer.password
Expand Down
34 changes: 34 additions & 0 deletions src/strategies/secretStrategies/awsSecretStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SecretsManager } from '@aws-sdk/client-secrets-manager';

import awsConfig from '../../config/aws.config';
import { IMailerSecretKey, IMailerSecretKeys } from './secrets';

/**
* Fetches secrets from AWS Secret Manager.
*/
async function fetchSecrets(): Promise<Record<IMailerSecretKey, string>> {
const client = new SecretsManager({
region: awsConfig.region
});

const data = await client.getSecretValue({
SecretId: awsConfig.secretManagerName
});

try {
return JSON.parse(data.SecretString || '');
} catch {
throw new Error('Invalid secret string');
}
}

/**
* Get the secret value from the AWS Secret Manager.
*/
async function getSecrets(key: IMailerSecretKey) {
const awsSecretValues = await fetchSecrets();

return awsSecretValues[key] || '';
}

export { getSecrets };
7 changes: 7 additions & 0 deletions src/strategies/secretStrategies/envSecretStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { IMailerSecretKey, IMailerSecretKeys } from './secrets';

function getSecrets(key: IMailerSecretKey): IMailerSecretKeys[typeof key] {
return process.env[key] || '';
}

export { getSecrets };
21 changes: 21 additions & 0 deletions src/strategies/secretStrategies/secretStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as AwsSecret from './awsSecretStrategy';
import * as EnvSecret from './envSecretStrategy';
import type { IMailerSecretKey, IMailerSecretKeys } from './secrets';

async function getSecret(
key: IMailerSecretKey
): Promise<IMailerSecretKeys[IMailerSecretKey]> {
const awsSecret = AwsSecret.getSecrets(key);

if (awsSecret) {
return awsSecret;
}

const envSecret = EnvSecret.getSecrets(key);

if (envSecret) {
return envSecret;
}
}

export default getSecret;
38 changes: 38 additions & 0 deletions src/strategies/secretStrategies/secrets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export type AwsRegion =
| 'us-east-2' // US East (Ohio)
| 'us-east-1' // US East (N. Virginia)
| 'us-west-1' // US West (N. California)
| 'us-west-2' // US West (Oregon)
| 'af-south-1' // Africa (Cape Town)
| 'ap-east-1' // Asia Pacific (Hong Kong)
| 'ap-south-1' // Asia Pacific (Mumbai)
| 'ap-northeast-3' // Asia Pacific (Osaka)
| 'ap-northeast-2' // Asia Pacific (Seoul)
| 'ap-southeast-1' // Asia Pacific (Singapore)
| 'ap-southeast-2' // Asia Pacific (Sydney)
| 'ap-northeast-1' // Asia Pacific (Tokyo)
| 'ca-central-1' // Canada (Central)
| 'eu-central-1' // Europe (Frankfurt)
| 'eu-west-1' // Europe (Ireland)
| 'eu-west-2' // Europe (London)
| 'eu-south-1' // Europe (Milan)
| 'eu-west-3' // Europe (Paris)
| 'eu-north-1' // Europe (Stockholm)
| 'me-south-1' // Middle East (Bahrain)
| 'sa-east-1' // South America (Sao Paulo)
| 'us-gov-east-1' // AWS GovCloud (US-East)
| 'us-gov-west-1'; // AWS GovCloud (US-West)

export interface IMailerSecretKeys {
MAIL_FROM_ADDRESS: string;
MAIL_FROM_NAME: string;
MAIL_SERVER_HOST: string;
MAIL_PASSWORD: string;
}

export interface IAwsCredentials {
AWS_REGION: AwsRegion;
SECRET_MANAGER_NAME: string;
}

export type IMailerSecretKey = keyof IMailerSecretKeys;
Loading
Loading