Skip to content

The agent that grows with you

fix(email): warn about a missing authserv_id once per account, even via reconnect

kshitijk4poor8b66a51036c1
diff --git a/plugins/platforms/email/adapter.py b/plugins/platforms/email/adapter.py
index d722ecaa06f..5fb71417812 100644
--- a/plugins/platforms/email/adapter.py
+++ b/plugins/platforms/email/adapter.py
@@ -455,6 +455,9 @@ class EmailAdapter(BasePlatformAdapter):
# adapter per retry; without this connect(is_reconnect=True) would re-mark the mailbox seen and skip
# mail that arrived during the outage. Keyed by address (multiplex runs several accounts); same-process only.
_seen_uids_snapshot: Dict[str, set] = {}
+ # Accounts already warned about a missing authserv_id pin. Per address, not per first connect: an account whose
+ # first connect fails is brought up by the reconnect watcher (is_reconnect=True) and must still warn once.
+ _missing_pin_warned: set = set()
def __init__(self, config: PlatformConfig):
super().__init__(config, Platform.EMAIL)
@@ -607,7 +610,8 @@ class EmailAdapter(BasePlatformAdapter):
return self._fail("[Email] %s", message, "email_missing_configuration", message, retryable=False)
if not self._probe_imap(is_reconnect) or not self._probe_smtp():
return False
- if not is_reconnect and self._require_authenticated_sender and not self._authserv_id:
+ if self._require_authenticated_sender and not self._authserv_id and self._address not in EmailAdapter._missing_pin_warned:
+ EmailAdapter._missing_pin_warned.add(self._address)
logger.warning("[Email] %s: %s.%s", self._address, _MISSING_AUTHSERV_REASON, _MISSING_AUTHSERV_HINT)
self._running = True
self._poll_task = asyncio.create_task(self._poll_loop())
diff --git a/tests/gateway/test_email.py b/tests/gateway/test_email.py
index 0f9abd35abe..9c4b06df722 100644
--- a/tests/gateway/test_email.py
+++ b/tests/gateway/test_email.py
@@ -379,7 +379,9 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):
def test_only_a_missing_auth_results_header_warns_with_the_opt_out_hint(self):
"""A granted sender's mail with no Authentication-Results suggests a server that never stamps it, so the drop
warns with the opt-out hint; no stamp from the pinned authserv-id warns to check authserv_id; a listed sender's
- failing verdict warns without a hint; forged stranger mail under open access stays at debug."""
+ failing verdict warns without a hint; forged stranger mail under open access stays at debug. A missing
+ authserv_id pin warns once per account from connect(), including when the account first comes up via a
+ reconnect after a failed initial connect."""
import asyncio
from gateway.config import PlatformConfig
from plugins.platforms.email.adapter import _MISSING_AUTHSERV_REASON, _UNTRUSTED_AUTHSERV_REASON, EmailAdapter
@@ -394,22 +396,28 @@ class TestDispatchDefersToGatewayAuthorization(unittest.TestCase):
self.assertIn("authserv_id", logs.output[0])
self.assertNotIn("require_authenticated_sender", logs.output[0])
# No pin drops every message of the account, so connect() names the fix (pin, or the explicit opt-out) once
- # per account at startup: each unpinned mailbox warns, a reconnect retry does not, pinned/opted-out stay quiet.
- def connect(address, extra=None, is_reconnect=False):
+ # per account: the first successful connect warns even when it is a reconnect after a failed startup, later
+ # reconnects do not, each unpinned mailbox warns once, pinned/opted-out stay quiet.
+ EmailAdapter._missing_pin_warned.clear()
+
+ def connect(address, extra=None, is_reconnect=False, fail=False):
with patch.dict(os.environ, {"EMAIL_ADDRESS": address, "EMAIL_PASSWORD": "secret",
"EMAIL_IMAP_HOST": "imap.test.com", "EMAIL_SMTP_HOST": "smtp.test.com"}):
adapter = EmailAdapter(PlatformConfig(enabled=True, extra=extra or {}))
imap = MagicMock()
imap.uid.return_value = ("OK", [b""])
- with patch("imaplib.IMAP4_SSL", return_value=imap), patch.object(adapter, "_connect_smtp"):
- self.assertTrue(asyncio.run(adapter.connect(is_reconnect=is_reconnect)))
+ imap_patch = patch("imaplib.IMAP4_SSL", side_effect=OSError("down")) if fail else patch("imaplib.IMAP4_SSL", return_value=imap)
+ with imap_patch, patch.object(adapter, "_connect_smtp"):
+ self.assertEqual(asyncio.run(adapter.connect(is_reconnect=is_reconnect)), not fail)
with self.assertLogs(adapter_log, level="WARNING") as logs:
- for address in ("[email protected]", "[email protected]"):
- connect(address)
+ connect("[email protected]", fail=True)
+ connect("[email protected]", is_reconnect=True)
connect("[email protected]", is_reconnect=True)
+ connect("[email protected]")
connect("[email protected]", {"authserv_id": "mx.ourserver.com"})
connect("[email protected]", {"require_authenticated_sender": False})
+ logs.output[:] = [line for line in logs.output if _MISSING_AUTHSERV_REASON in line]
self.assertEqual([line.split(": ")[0] for line in logs.output],
["WARNING:plugins.platforms.email.adapter:[Email] [email protected]",
"WARNING:plugins.platforms.email.adapter:[Email] [email protected]"], logs.output)