fix: use credentials manager instead config
Signed-off-by: Heikki Hellgren <heikki.hellgren@op.fi>
This commit is contained in:
+2
-11
@@ -58,18 +58,9 @@ export interface Config {
|
||||
*/
|
||||
apiVersion?: string;
|
||||
/**
|
||||
* SES region to use
|
||||
* AWS account ID to use
|
||||
*/
|
||||
region?: string;
|
||||
/**
|
||||
* AWS access key id
|
||||
*/
|
||||
accessKeyId?: string;
|
||||
/**
|
||||
* AWS secret access key
|
||||
* @visibility secret
|
||||
*/
|
||||
secretAccessKey?: string;
|
||||
accountId?: string;
|
||||
}
|
||||
| {
|
||||
transport: 'sendmail';
|
||||
|
||||
@@ -32,13 +32,14 @@
|
||||
"test": "backstage-cli package test"
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-ses": "^3.556.0",
|
||||
"@aws-sdk/credential-provider-node": "^3.350.0",
|
||||
"@aws-sdk/client-ses": "3.554.0",
|
||||
"@aws-sdk/types": "^3.347.0",
|
||||
"@backstage/backend-common": "workspace:^",
|
||||
"@backstage/backend-plugin-api": "workspace:^",
|
||||
"@backstage/catalog-client": "workspace:^",
|
||||
"@backstage/catalog-model": "workspace:^",
|
||||
"@backstage/config": "workspace:^",
|
||||
"@backstage/integration-aws-node": "workspace:^",
|
||||
"@backstage/plugin-notifications-common": "workspace:^",
|
||||
"@backstage/plugin-notifications-node": "workspace:^",
|
||||
"@backstage/types": "workspace:^",
|
||||
|
||||
+45
-3
@@ -45,7 +45,7 @@ describe('NotificationsEmailProcessor', () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should create smtp transport', () => {
|
||||
it('should create smtp transport', async () => {
|
||||
const processor = new NotificationsEmailProcessor(
|
||||
logger,
|
||||
new ConfigReader({
|
||||
@@ -66,6 +66,20 @@ describe('NotificationsEmailProcessor', () => {
|
||||
auth,
|
||||
);
|
||||
|
||||
await processor.postProcess(
|
||||
{
|
||||
origin: 'plugin',
|
||||
id: '1234',
|
||||
user: 'user:default/mock',
|
||||
created: new Date(),
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
{
|
||||
recipients: { type: 'entity', entityRef: 'user:default/mock' },
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
);
|
||||
|
||||
expect(processor).toBeInstanceOf(NotificationsEmailProcessor);
|
||||
expect(createTransport as jest.Mock).toHaveBeenCalledWith({
|
||||
host: 'localhost',
|
||||
@@ -75,7 +89,7 @@ describe('NotificationsEmailProcessor', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it('should create ses transport', () => {
|
||||
it('should create ses transport', async () => {
|
||||
const processor = new NotificationsEmailProcessor(
|
||||
logger,
|
||||
new ConfigReader({
|
||||
@@ -93,13 +107,27 @@ describe('NotificationsEmailProcessor', () => {
|
||||
auth,
|
||||
);
|
||||
|
||||
await processor.postProcess(
|
||||
{
|
||||
origin: 'plugin',
|
||||
id: '1234',
|
||||
user: 'user:default/mock',
|
||||
created: new Date(),
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
{
|
||||
recipients: { type: 'entity', entityRef: 'user:default/mock' },
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
);
|
||||
|
||||
expect(processor).toBeInstanceOf(NotificationsEmailProcessor);
|
||||
expect(createTransport as jest.Mock).toHaveBeenCalledWith({
|
||||
SES: expect.anything(),
|
||||
});
|
||||
});
|
||||
|
||||
it('should create sendmail transport', () => {
|
||||
it('should create sendmail transport', async () => {
|
||||
const processor = new NotificationsEmailProcessor(
|
||||
logger,
|
||||
new ConfigReader({
|
||||
@@ -117,6 +145,20 @@ describe('NotificationsEmailProcessor', () => {
|
||||
auth,
|
||||
);
|
||||
|
||||
await processor.postProcess(
|
||||
{
|
||||
origin: 'plugin',
|
||||
id: '1234',
|
||||
user: 'user:default/mock',
|
||||
created: new Date(),
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
{
|
||||
recipients: { type: 'entity', entityRef: 'user:default/mock' },
|
||||
payload: { title: 'notification' },
|
||||
},
|
||||
);
|
||||
|
||||
expect(processor).toBeInstanceOf(NotificationsEmailProcessor);
|
||||
expect(createTransport as jest.Mock).toHaveBeenCalledWith({
|
||||
sendmail: true,
|
||||
|
||||
+53
-33
@@ -33,9 +33,10 @@ import { createSendmailTransport, createSmtpTransport } from './transports';
|
||||
import { createSesTransport } from './transports/ses';
|
||||
import { UserEntity } from '@backstage/catalog-model';
|
||||
import { compact } from 'lodash';
|
||||
import { DefaultAwsCredentialsManager } from '@backstage/integration-aws-node';
|
||||
|
||||
export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
private readonly transportter: any;
|
||||
private transporter: any;
|
||||
private readonly broadcastConfig?: Config;
|
||||
private readonly sender: string;
|
||||
private readonly format: string;
|
||||
@@ -44,41 +45,11 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
|
||||
constructor(
|
||||
private readonly logger: LoggerService,
|
||||
config: Config,
|
||||
private readonly config: Config,
|
||||
private readonly catalog: CatalogClient,
|
||||
private readonly auth: AuthService,
|
||||
private readonly cache?: CacheService,
|
||||
) {
|
||||
const transportConfig = config.getConfig('notifications.email.transport');
|
||||
const transport = transportConfig.getString('transport');
|
||||
if (transport === 'smtp') {
|
||||
this.transportter = createSmtpTransport({
|
||||
transport: 'smtp',
|
||||
hostname: transportConfig.getString('hostname'),
|
||||
port: transportConfig.getNumber('port'),
|
||||
secure: transportConfig.getOptionalBoolean('secure'),
|
||||
requireTls: transportConfig.getOptionalBoolean('requireTls'),
|
||||
username: transportConfig.getOptionalString('username'),
|
||||
password: transportConfig.getOptionalString('password'),
|
||||
});
|
||||
} else if (transport === 'ses') {
|
||||
this.transportter = createSesTransport({
|
||||
transport: 'ses',
|
||||
apiVersion: transportConfig.getOptionalString('apiVersion'),
|
||||
region: transportConfig.getOptionalString('region'),
|
||||
accessKeyId: transportConfig.getOptionalString('accessKeyId'),
|
||||
secretAccessKey: transportConfig.getOptionalString('secretAccessKey'),
|
||||
});
|
||||
} else if (transport === 'sendmail') {
|
||||
this.transportter = createSendmailTransport({
|
||||
transport: 'sendmail',
|
||||
path: transportConfig.getOptionalString('path'),
|
||||
newline: transportConfig.getOptionalString('newline'),
|
||||
});
|
||||
} else {
|
||||
throw new Error(`Unsupported transport: ${transport}`);
|
||||
}
|
||||
|
||||
this.broadcastConfig = config.getOptionalConfig(
|
||||
'notifications.email.broadcastConfig',
|
||||
);
|
||||
@@ -90,6 +61,46 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
config.getOptionalNumber('notifications.email.cacheTtl') ?? 3_600_000;
|
||||
}
|
||||
|
||||
private async getTransporter() {
|
||||
if (this.transporter) {
|
||||
return this.transporter;
|
||||
}
|
||||
const transportConfig = this.config.getConfig(
|
||||
'notifications.email.transport',
|
||||
);
|
||||
const transport = transportConfig.getString('transport');
|
||||
if (transport === 'smtp') {
|
||||
this.transporter = createSmtpTransport({
|
||||
transport: 'smtp',
|
||||
hostname: transportConfig.getString('hostname'),
|
||||
port: transportConfig.getNumber('port'),
|
||||
secure: transportConfig.getOptionalBoolean('secure'),
|
||||
requireTls: transportConfig.getOptionalBoolean('requireTls'),
|
||||
username: transportConfig.getOptionalString('username'),
|
||||
password: transportConfig.getOptionalString('password'),
|
||||
});
|
||||
} else if (transport === 'ses') {
|
||||
const awsCredentialsManager = DefaultAwsCredentialsManager.fromConfig(
|
||||
this.config,
|
||||
);
|
||||
this.transporter = await createSesTransport({
|
||||
transport: 'ses',
|
||||
credentialsManager: awsCredentialsManager,
|
||||
apiVersion: transportConfig.getOptionalString('apiVersion'),
|
||||
accountId: transportConfig.getOptionalString('accountId'),
|
||||
});
|
||||
} else if (transport === 'sendmail') {
|
||||
this.transporter = createSendmailTransport({
|
||||
transport: 'sendmail',
|
||||
path: transportConfig.getOptionalString('path'),
|
||||
newline: transportConfig.getOptionalString('newline'),
|
||||
});
|
||||
} else {
|
||||
throw new Error(`Unsupported transport: ${transport}`);
|
||||
}
|
||||
return this.transporter;
|
||||
}
|
||||
|
||||
getName(): string {
|
||||
return 'Email';
|
||||
}
|
||||
@@ -190,6 +201,8 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
notification: Notification,
|
||||
options: NotificationSendOptions,
|
||||
): Promise<void> {
|
||||
this.transporter = await this.getTransporter();
|
||||
|
||||
let emails: string[] = [];
|
||||
try {
|
||||
emails = await this.getRecipientEmails(notification, options);
|
||||
@@ -198,6 +211,13 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
return;
|
||||
}
|
||||
|
||||
if (emails.length === 0) {
|
||||
this.logger.info(
|
||||
`No email recipients found for notification: ${notification.id}, skipping`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: add template support for content either HTML or text
|
||||
const contentParts: string[] = [];
|
||||
if (notification.payload.description) {
|
||||
@@ -220,7 +240,7 @@ export class NotificationsEmailProcessor implements NotificationProcessor {
|
||||
|
||||
for (const email of emails) {
|
||||
try {
|
||||
await this.transportter.sendMail({ ...mailOptions, to: email });
|
||||
await this.transporter.sendMail({ ...mailOptions, to: email });
|
||||
} catch (e) {
|
||||
this.logger.error(`Failed to send email to ${email}: ${e}`);
|
||||
}
|
||||
|
||||
@@ -15,25 +15,21 @@
|
||||
*/
|
||||
import { SesTransportConfig } from '../../types';
|
||||
import { createTransport } from 'nodemailer';
|
||||
import * as aws from '@aws-sdk/client-ses';
|
||||
import { defaultProvider } from '@aws-sdk/credential-provider-node';
|
||||
import { SendRawEmailCommand, SES } from '@aws-sdk/client-ses';
|
||||
|
||||
export const createSesTransport = (config: SesTransportConfig) => {
|
||||
const ses = new aws.SES([
|
||||
export const createSesTransport = async (config: SesTransportConfig) => {
|
||||
const credentials = await config.credentialsManager.getCredentialProvider({
|
||||
accountId: config.accountId,
|
||||
});
|
||||
const ses = new SES([
|
||||
{
|
||||
region: config.region,
|
||||
apiVersion: config.apiVersion ?? '2010-12-01',
|
||||
defaultProvider,
|
||||
credentials:
|
||||
config.accessKeyId && config.secretAccessKey
|
||||
? {
|
||||
accessKeyId: config.accessKeyId,
|
||||
secretAccessKey: config.secretAccessKey,
|
||||
}
|
||||
: undefined,
|
||||
credentials: credentials.sdkCredentialProvider,
|
||||
region: credentials.stsRegion,
|
||||
accountId: credentials.accountId,
|
||||
},
|
||||
]);
|
||||
return createTransport({
|
||||
SES: { ses, aws },
|
||||
SES: { ses, aws: { SendRawEmailCommand } },
|
||||
});
|
||||
};
|
||||
|
||||
@@ -13,6 +13,8 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { AwsCredentialsManager } from '@backstage/integration-aws-node';
|
||||
|
||||
export interface TransportConfig {
|
||||
transport: 'smtp' | 'ses' | 'sendmail';
|
||||
}
|
||||
@@ -52,19 +54,8 @@ export interface SesTransportConfig extends TransportConfig {
|
||||
* SES ApiVersion to use, defaults to 2010-12-01
|
||||
*/
|
||||
apiVersion?: string;
|
||||
/**
|
||||
* SES region to use
|
||||
*/
|
||||
region?: string;
|
||||
/**
|
||||
* AWS access key id
|
||||
*/
|
||||
accessKeyId?: string;
|
||||
/**
|
||||
* AWS secret access key
|
||||
* @visibility secret
|
||||
*/
|
||||
secretAccessKey?: string;
|
||||
accountId?: string;
|
||||
credentialsManager: AwsCredentialsManager;
|
||||
}
|
||||
|
||||
export interface SendmailTransportConfig extends TransportConfig {
|
||||
|
||||
Reference in New Issue
Block a user