Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(mailgun): skip per-domain 401/403 instead of failing the sync (#63902)

Tom Owersf1820c1a46a4
diff --git a/posthog/temporal/data_imports/sources/mailgun/mailgun.py b/posthog/temporal/data_imports/sources/mailgun/mailgun.py
index 7f4e9000c397..e2806f623df2 100644
--- a/posthog/temporal/data_imports/sources/mailgun/mailgun.py
+++ b/posthog/temporal/data_imports/sources/mailgun/mailgun.py
@@ -209,17 +209,26 @@ def _fetch_page(url: str, api_key: str, logger: FilteringBoundLogger) -> dict[st
return response.json()
+# Per-domain statuses that skip the domain instead of failing the fan-out. The account lists
+# every domain on it (`/v4/domains`), but individual domains may be unqueryable: 400 for
+# disabled / unverified / sandbox-like domains, and 401/403 when the key lacks access to a
+# specific domain (e.g. a subaccount domain, or a restricted key). A global credential failure
+# 401s the `/v4/domains` listing itself, which never reaches this fan-out and stays non-retryable.
+SKIPPABLE_DOMAIN_STATUS_CODES = frozenset({400, 401, 403})
+
+
def _is_skippable_domain_error(config: MailgunEndpointConfig, domain: Optional[str], error: requests.HTTPError) -> bool:
"""Whether a failed domain-scoped request should skip that domain instead of failing the sync.
The domain fan-out lists every domain on the account (`/v4/domains`), including ones that
- can't be queried for events/suppressions — disabled, unverified, or sandbox-like domains.
- Those reject the domain-scoped request with a 400, so one bad domain would otherwise abort
- the whole fan-out and strand every other domain on the account."""
+ can't be queried for events/suppressions — disabled, unverified, sandbox-like, or domains
+ the key has no access to. Those reject the domain-scoped request with a 400/401/403, so one
+ bad domain would otherwise abort the whole fan-out and strand every other domain on the
+ account."""
if not config.domain_scoped or domain is None:
return False
response = error.response
- return response is not None and response.status_code == 400
+ return response is not None and response.status_code in SKIPPABLE_DOMAIN_STATUS_CODES
def get_domain_names(api_key: str, base_url: str, logger: FilteringBoundLogger) -> list[str]:
diff --git a/posthog/temporal/data_imports/sources/mailgun/tests/test_mailgun.py b/posthog/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
index 2a40f97f53d4..77091c888891 100644
--- a/posthog/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
+++ b/posthog/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
@@ -463,12 +463,15 @@ class TestGetRows:
assert saved_states[-1].next_url is None
assert saved_states[-1].pending_domains == []
+ @pytest.mark.parametrize("status_code", [400, 401, 403])
@mock.patch("posthog.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
- def test_domain_scoped_400_skips_domain_and_continues_fan_out(self, mock_session):
- # The account lists a domain that can't be queried for events (disabled / unverified):
- # its 400 must skip the domain, not abort the whole fan-out, so b.com still imports.
+ def test_domain_scoped_access_error_skips_domain_and_continues_fan_out(self, mock_session, status_code):
+ # The account lists a domain that can't be queried (disabled / unverified for 400, or a
+ # domain the key has no access to for 401/403). It must skip that domain, not abort the
+ # whole fan-out, so b.com still imports. A global credential failure 401s the /v4/domains
+ # listing instead, which never reaches the fan-out and stays non-retryable.
domains_page = {"items": [{"name": "a.com"}, {"name": "b.com"}]}
- a_bad = _error_response(400, f"{US_BASE}/v3/a.com/events")
+ a_bad = _error_response(status_code, f"{US_BASE}/v3/a.com/events")
b_events = _paging_page([{"id": "e2", "timestamp": 1700000100.5}], None)
mock_session.return_value.get.side_effect = [
_response(domains_page),
@@ -487,6 +490,16 @@ class TestGetRows:
assert saved_states[-1].next_url is None
assert saved_states[-1].pending_domains == []
+ @mock.patch("posthog.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
+ def test_domain_listing_401_still_raises(self, mock_session):
+ # A 401 on the /v4/domains listing is a global credential failure, not a single bad
+ # domain — it must surface (and stay non-retryable) rather than be skipped per-domain.
+ mock_session.return_value.get.return_value = _error_response(401, f"{US_BASE}/v4/domains")
+
+ manager = _make_manager()
+ with pytest.raises(requests.HTTPError, match="401 Client Error"):
+ list(get_rows("key", "us", "events", mock.MagicMock(), manager))
+
@mock.patch("posthog.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
def test_account_level_400_still_raises(self, mock_session):
# A 400 on a non-domain-scoped endpoint means our request is wrong, not a bad domain —
diff --git a/services/mcp/scripts/lib/tool-references.ts b/services/mcp/scripts/lib/tool-references.ts
index 0510bc706c27..1955656d4646 100644
--- a/services/mcp/scripts/lib/tool-references.ts
+++ b/services/mcp/scripts/lib/tool-references.ts
@@ -29,6 +29,7 @@ const REFERENCE_ALLOWLIST = new Set([
'comma-separated',
'skills-store', // the Skills store feature ("the skills-store tools")
'llma-alerts', // skills-store skill, not in this repo
+ 'secrets-and-integrations', // agent-platform runtime skill (secret-editor deep-link contract), not a repo skill
'text-embedding-3-small-1536', // embedding model name
'deep-dive', // "a deep-dive skill"
'team-shared',