Skip to content

Commit

Permalink
Do not fail if SMTP provider is not configured (#8228)
Browse files Browse the repository at this point in the history
Now that we have webhooks, it is valid to not have an SMTP provider
configured. In this case, we log an error and send a fake email instead.
  • Loading branch information
scotttrinh authored Jan 17, 2025
1 parent a9c3dd7 commit a91d693
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 102 deletions.
51 changes: 33 additions & 18 deletions edb/server/protocol/auth_ext/email.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import asyncio
import urllib.parse
import random
import logging

from email.message import EmailMessage
from typing import Any, Coroutine
from edb.server import tenant, smtp
from edb import errors

from . import util, ui


logger = logging.getLogger("edb.server.ext.auth")


async def send_password_reset_email(
db: Any,
tenant: tenant.Tenant,
Expand All @@ -30,12 +36,7 @@ async def send_password_reset_email(
reset_url=reset_url,
**email_args,
)
smtp_provider = smtp.SMTP(db)
coro = smtp_provider.send(
msg,
test_mode=test_mode,
)
await _protected_send(coro, tenant)
await _maybe_send_message(msg, tenant, db, test_mode)


async def send_verification_email(
Expand Down Expand Up @@ -70,12 +71,7 @@ async def send_verification_email(
verify_url=verify_url,
**email_args,
)
smtp_provider = smtp.SMTP(db)
coro = smtp_provider.send(
msg,
test_mode=test_mode,
)
await _protected_send(coro, tenant)
await _maybe_send_message(msg, tenant, db, test_mode)


async def send_magic_link_email(
Expand All @@ -100,12 +96,7 @@ async def send_magic_link_email(
link=link,
**email_args,
)
smtp_provider = smtp.SMTP(db)
coro = smtp_provider.send(
msg,
test_mode=test_mode,
)
await _protected_send(coro, tenant)
await _maybe_send_message(msg, tenant, db, test_mode)


async def send_fake_email(tenant: tenant.Tenant) -> None:
Expand All @@ -116,6 +107,30 @@ async def noop_coroutine() -> None:
await _protected_send(coro, tenant)


async def _maybe_send_message(
msg: EmailMessage,
tenant: tenant.Tenant,
db: Any,
test_mode: bool,
) -> None:
try:
smtp_provider = smtp.SMTP(db)
except errors.ConfigurationError as e:
logger.debug(
"ConfigurationError while instantiating SMTP provider, "
f"sending fake email instead: {e}"
)
smtp_provider = None
if smtp_provider is None:
coro = send_fake_email(tenant)
else:
coro = smtp_provider.send(
msg,
test_mode=test_mode,
)
await _protected_send(coro, tenant)


async def _protected_send(
coro: Coroutine[Any, Any, None], tenant: tenant.Tenant
) -> None:
Expand Down
Loading

0 comments on commit a91d693

Please sign in to comment.