Skip to content

Commit

Permalink
Removed logic that caused email duplication in new architecture (#2841)
Browse files Browse the repository at this point in the history
  • Loading branch information
betimshahini committed Feb 13, 2024
1 parent ea8f515 commit e0751e4
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 37 deletions.
2 changes: 2 additions & 0 deletions packages/types/headers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export const PlatformAccountURNHeader = 'rollup-account-urn'

export const AppAPIKeyHeader = 'APP-API-KEY'

export const RelayRecipientHeader = 'X-rollup-relay-recipient'
5 changes: 4 additions & 1 deletion platform/emaildistributor/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Environment } from './types'
import type { CloudflareEmailMessage } from '@proofzero/packages/types/email'
import { RelayRecipientHeader } from '@proofzero/types/headers'

export default {
async email(message: CloudflareEmailMessage, env: Environment) {
Expand Down Expand Up @@ -31,7 +32,9 @@ export default {
console.info(
`Forwarding to env suffix ${addressEnvSuffix} for ${message.to}`
)
await message.forward(targetEnvEmail)
const newHeaders = new Headers(message.headers)
newHeaders.append(RelayRecipientHeader, message.to)
await message.forward(targetEnvEmail, newHeaders)
}
},
}
3 changes: 1 addition & 2 deletions platform/emailinbounder/.dev.vars.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#This is only set in the CF environments, as a secret. Locally it can be set to anything
#JSON object containing mapping between env specific identifier in the email address
#and the target internal (hence, secret) email to be forwarded to for env-specific processor
SECRET_RELAY_DISTRIBUTION_MAP="{'local':'supersecretemail@example.com'}"
INTERNAL_RELAY_DISTRIBUTION_KEY = "local"
SECRET_RELAY_DISTRIBUTION_MAP="{'local':'supersecretemail@example.com'}"
66 changes: 32 additions & 34 deletions platform/emailinbounder/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CloudflareEmailMessage } from '@proofzero/packages/types/email'
import type { Environment } from './types'
import createCoreClient from '@proofzero/platform-clients/core'
import { RelayRecipientHeader } from '@proofzero/types/headers'

import {
generateTraceContextHeaders,
Expand All @@ -20,7 +21,15 @@ export default {
;({ done, value } = await reader.read())
}

return relay(content, env)
const recipient = message.headers.get(RelayRecipientHeader)
if (!recipient) console.error('Received email without the expected headers')
else if (
!recipient.endsWith(
`.${env.INTERNAL_EMAIL_DISTRIBUTION_KEY}@${env.INTERNAL_RELAY_DKIM_DOMAIN}`
)
)
console.warn('Received email for wrong environment')
else return relay(recipient, content, env)
},
}

Expand All @@ -42,7 +51,7 @@ interface DKIM {
dkim_private_key: string
}

const relay = async (message: string, env: Environment) => {
const relay = async (recipient: string, message: string, env: Environment) => {
const postalMime = new PostalMime()
const email = await postalMime.parse(message)

Expand All @@ -52,41 +61,30 @@ const relay = async (message: string, env: Environment) => {
dkim_private_key: env.SECRET_RELAY_DKIM_PRIVATE_KEY,
}

const recipients = new Array<Address>()
.concat(email.to || [])
.concat(email.cc || [])
.filter((recipient) =>
recipient.address.endsWith(
`.${env.INTERNAL_EMAIL_DISTRIBUTION_KEY}@${env.INTERNAL_RELAY_DKIM_DOMAIN}`
)
)

for (const recipient of recipients) {
const coreClient = getCoreClient(env)
const { sourceEmail, nickname } =
await coreClient.account.getSourceFromMaskedAddress.query({
maskedEmail: recipient.address,
})
if (!sourceEmail) continue

const from: MailChannelAddress = {
name: `Rollup Hidden email from ${email.from.name}`,
email: recipient.address,
}
const coreClient = getCoreClient(env)
const { sourceEmail, nickname } =
await coreClient.account.getSourceFromMaskedAddress.query({
maskedEmail: recipient,
})
if (!sourceEmail) return

const replyTo: MailChannelAddress = {
name: email.from.name,
email: email.from.address,
}
const from: MailChannelAddress = {
name: `Rollup Hidden email from ${email.from.name}`,
email: recipient,
}

const to: MailChannelAddress[] = [
{
name: nickname,
email: sourceEmail,
},
]
await send(email, from, to, replyTo, dkim)
const replyTo: MailChannelAddress = {
name: email.from.name,
email: email.from.address,
}

const to: MailChannelAddress[] = [
{
name: nickname,
email: sourceEmail,
},
]
await send(email, from, to, replyTo, dkim)
}

const send = async (
Expand Down
1 change: 1 addition & 0 deletions platform/emailinbounder/wrangler.current.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ services = [{ binding = "Core", service = "core-current" }]
[env.current.vars]
INTERNAL_RELAY_DKIM_DOMAIN = "rollup.email"
INTERNAL_RELAY_DKIM_SELECTOR = "mailchannels"
INTERNAL_EMAIL_DISTRIBUTION_KEY = "id"
1 change: 1 addition & 0 deletions platform/emailinbounder/wrangler.dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ services = [{ binding = "Core", service = "core-dev" }]
[env.dev.vars]
INTERNAL_RELAY_DKIM_DOMAIN = "rollup.email"
INTERNAL_RELAY_DKIM_SELECTOR = "mailchannels"
INTERNAL_EMAIL_DISTRIBUTION_KEY = "dev"
1 change: 1 addition & 0 deletions platform/emailinbounder/wrangler.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ services = [{ binding = "Core", service = "core-next" }]
[env.next.vars]
INTERNAL_RELAY_DKIM_DOMAIN = "rollup.email"
INTERNAL_RELAY_DKIM_SELECTOR = "mailchannels"
INTERNAL_EMAIL_DISTRIBUTION_KEY = "next"
1 change: 1 addition & 0 deletions platform/emailinbounder/wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ local_protocol = "http"
[vars]
INTERNAL_RELAY_DKIM_DOMAIN = "rollup.email"
INTERNAL_RELAY_DKIM_SELECTOR = "mailchannels"
INTERNAL_EMAIL_DISTRIBUTION_KEY = "local"

0 comments on commit e0751e4

Please sign in to comment.