Skip to content

Commit 31b54bb

Browse files
authored
use api-key in mail service (#8257)
Signed-off-by: Nikolay Chunosov <[email protected]>
1 parent b531cf2 commit 31b54bb

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

services/mail/pod-mail/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ It supports sending emails with multiple recipients, along with optional CC, BCC
99

1010
Environment variables should be set to configure the Mail Service:
1111
- `PORT`: The port on which the mail service listens for incoming HTTP requests.
12+
- `API_KEY`: An API key that clients must pass. The parameter is optional, should be provided when external access to the service is allowed.
1213

1314
Settings for SMTP or SES email service should be specified, simultaneous use of both protocols is not supported
1415

@@ -53,6 +54,8 @@ Send an email message.
5354
- `subject`: Required. String containing the email subject.
5455
- `html`: Optional. String containing HTML message body.
5556
- `from`: Optional. Sender's email address.
57+
- `headers`: Optional. An object or array of additional header fields.
58+
- `apiKey`: Required if the service started with `API_KEY`.
5659
- `attachments`: Optional. Array of objects, each object can have the following fields:
5760
- `filename`: Filename to be reported as the name of the attached file. Use of unicode is allowed.
5861
- `contentType`: Optional. Content type for the attachment, if not set will be derived from the filename property.

services/mail/pod-mail/src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ export const main = async (): Promise<void> => {
5555
}
5656

5757
export async function handleSendMail (client: MailClient, req: Request, res: Response): Promise<void> {
58-
// Skip auth check, since service should be internal
59-
const { from, to, subject, text, html, attachments } = req.body
58+
const { from, to, subject, text, html, attachments, headers, apiKey } = req.body
59+
if (process.env.API_KEY !== undefined && process.env.API_KEY !== apiKey) {
60+
res.status(401).send({ err: 'Unauthorized' })
61+
return
62+
}
6063
const fromAddress = from ?? config.source
6164
if (text === undefined) {
6265
res.status(400).send({ err: "'text' is missing" })
@@ -83,6 +86,9 @@ export async function handleSendMail (client: MailClient, req: Request, res: Res
8386
if (html !== undefined) {
8487
message.html = html
8588
}
89+
if (headers !== undefined) {
90+
message.headers = headers
91+
}
8692
if (attachments !== undefined) {
8793
message.attachments = getAttachments(attachments)
8894
}

0 commit comments

Comments
 (0)