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(data-warehouse): say why Mailgun rejected a key during source setup (#95543)

Tom Owersd1af6f9ba35f
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/mailgun.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/mailgun.py
index e90d654554dc..4f395fddc926 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/mailgun.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/mailgun.py
@@ -282,7 +282,21 @@ def get_domain_names(api_key: str, base_url: str, logger: FilteringBoundLogger)
skip += config.page_size
-def validate_credentials(api_key: str, region: str) -> bool:
+# Mailgun keys are scoped to one region, so a good key checked against the wrong region is
+# rejected exactly like a bad key. Naming both leaves something to act on either way.
+INVALID_KEY_ERROR = (
+ "Mailgun rejected the API key. Check that you copied the private API key from Settings > API "
+ "security, and that the region matches where your account is hosted."
+)
+FORBIDDEN_ERROR = (
+ "Mailgun denied access with this API key. Use a key that can read your sending domains, then try again."
+)
+UNREACHABLE_ERROR = (
+ "Couldn't reach Mailgun to check your API key. This is usually temporary, so try again in a few minutes."
+)
+
+
+def validate_credentials(api_key: str, region: str) -> tuple[bool, str | None]:
"""Confirm the private API key is valid for the region. A 1-item domain listing is a cheap probe."""
try:
response = make_tracked_session().get(
@@ -291,9 +305,16 @@ def validate_credentials(api_key: str, region: str) -> bool:
auth=("api", api_key),
timeout=10,
)
- return response.status_code == 200
except Exception:
- return False
+ return False, UNREACHABLE_ERROR
+
+ if response.status_code == 200:
+ return True, None
+ if response.status_code == 403:
+ return False, FORBIDDEN_ERROR
+ if response.status_code >= 500:
+ return False, UNREACHABLE_ERROR
+ return False, INVALID_KEY_ERROR
def get_rows(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
index 753e5f4c34e7..d284f01c9bbf 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/source.py
@@ -216,10 +216,7 @@ Note: Mailgun only retains events for a limited period (1 day on free plans, up
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_mailgun_credentials(config.api_key, config.region):
- return True, None
-
- return False, "Invalid Mailgun API key or region"
+ return validate_mailgun_credentials(config.api_key, config.region)
def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MailgunResumeConfig]:
return ResumableSourceManager[MailgunResumeConfig](inputs, MailgunResumeConfig)
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
index 2e07572e4666..d475c8c49361 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun.py
@@ -12,7 +12,10 @@ import requests
from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.arrow_utils import table_from_py_list
from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import WebhookCreationResult
from products.warehouse_sources.backend.temporal.data_imports.sources.mailgun.mailgun import (
+ FORBIDDEN_ERROR,
+ INVALID_KEY_ERROR,
MAX_RETRY_AFTER_SECONDS,
+ UNREACHABLE_ERROR,
MailgunResumeConfig,
MailgunRetryableError,
_epoch_to_datetime,
@@ -275,22 +278,23 @@ class TestValidateCredentials:
@pytest.mark.parametrize(
"status_code, expected",
[
- (200, True),
- (401, False),
- (403, False),
- (500, False),
+ (200, (True, None)),
+ (401, (False, INVALID_KEY_ERROR)),
+ (404, (False, INVALID_KEY_ERROR)),
+ (403, (False, FORBIDDEN_ERROR)),
+ (500, (False, UNREACHABLE_ERROR)),
],
)
@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
def test_validate_credentials_status_mapping(self, mock_session, status_code, expected):
mock_session.return_value.get.return_value = _response({}, status_code)
- assert validate_credentials("key", "us") is expected
+ assert validate_credentials("key", "us") == expected
@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.mailgun.mailgun.make_tracked_session")
def test_validate_credentials_swallows_exceptions(self, mock_session):
mock_session.return_value.get.side_effect = Exception("boom")
- assert validate_credentials("key", "us") is False
+ assert validate_credentials("key", "us") == (False, UNREACHABLE_ERROR)
@pytest.mark.parametrize(
"region, expected_host",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
index 188dbc93382c..e95fb8e219ce 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailgun/tests/test_mailgun_source.py
@@ -96,22 +96,19 @@ class TestMailgunSource:
assert self.source.get_schemas(self.config, self.team_id, names=["nope"]) == []
@pytest.mark.parametrize(
- "mock_return, expected_valid, expected_message",
+ "mock_return",
[
- (True, True, None),
- (False, False, "Invalid Mailgun API key or region"),
+ (True, None),
+ (False, "Mailgun rejected the API key."),
],
)
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.mailgun.source.validate_mailgun_credentials"
)
- def test_validate_credentials(self, mock_validate, mock_return, expected_valid, expected_message):
+ def test_validate_credentials(self, mock_validate, mock_return):
mock_validate.return_value = mock_return
- is_valid, error_message = self.source.validate_credentials(self.config, self.team_id)
-
- assert is_valid is expected_valid
- assert error_message == expected_message
+ assert self.source.validate_credentials(self.config, self.team_id) == mock_return
mock_validate.assert_called_once_with(self.config.api_key, self.config.region)
def test_version_declaration_defaults_to_v4_with_v3_supported(self):