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(postmark): distinguish token, permission, and unreachable errors on validate (#73846)

Tom Owersb6552fed86f0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/postmark.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/postmark.py
index 689cfee4d134..0bfc4a1823c0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/postmark.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/postmark.py
@@ -84,17 +84,18 @@ def _get_headers(server_token: str) -> dict[str, str]:
}
-def validate_credentials(server_token: str) -> bool:
+def validate_credentials(server_token: str) -> tuple[bool, int | None]:
# /message-streams is a cheap read-only call any valid server token can make. Postmark
- # returns 401 (ErrorCode 10) for an invalid/missing token and 200 otherwise.
- ok, _status = validate_via_probe(
+ # returns 401 (ErrorCode 10) for an invalid/missing token and 200 otherwise. Return the
+ # status so the caller can tell a rejected token (401) from a permissions problem (403) or
+ # an unreachable/erroring API (None/5xx), rather than reporting them all as "invalid token".
+ return validate_via_probe(
# `X-Postmark-Server-Token` is not in the sample-capture header denylist, so mask the
# token by value to keep it out of any captured HTTP sample.
lambda: make_tracked_session(redact_values=(server_token,)),
f"{POSTMARK_BASE_URL}/message-streams",
headers=_get_headers(server_token),
)
- return ok
def postmark_source(
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/source.py
index 8e1c1de9e5e9..dc8cf3d16f1f 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/source.py
@@ -112,10 +112,21 @@ The token grants read access to the following server-level resources:
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_postmark_credentials(config.server_token):
+ is_valid, status = validate_postmark_credentials(config.server_token)
+ if is_valid:
return True, None
- return False, "Invalid Postmark server API token"
+ if status == 403:
+ return (
+ False,
+ "Your Postmark server API token doesn't have the required permissions. Please check the token and try again.",
+ )
+ if status is None or status == 429 or status >= 500:
+ return (
+ False,
+ "Couldn't reach Postmark to verify your token. Please try again in a moment.",
+ )
+ return False, "Invalid Postmark server API token. Please check the token and try again."
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark.py
index fe904a19f414..0129b917e956 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark.py
@@ -80,7 +80,8 @@ class TestValidateCredentials:
@mock.patch(POSTMARK_SESSION_PATCH)
def test_validate_credentials(self, _name: str, status_code: int, expected: bool, mock_session: mock.MagicMock):
mock_session.return_value.get.return_value = mock.MagicMock(status_code=status_code)
- assert validate_credentials("test-token") is expected
+ # The status flows back to the caller so it can distinguish rejection causes.
+ assert validate_credentials("test-token") == (expected, status_code)
# The probe session masks the token by value so it never lands in a captured HTTP sample.
assert mock_session.call_args.kwargs["redact_values"] == ("test-token",)
@@ -90,9 +91,9 @@ class TestValidateCredentials:
assert get_kwargs["headers"]["X-Postmark-Server-Token"] == "test-token"
@mock.patch(POSTMARK_SESSION_PATCH)
- def test_validate_credentials_network_error_returns_false(self, mock_session: mock.MagicMock):
+ def test_validate_credentials_network_error_returns_none_status(self, mock_session: mock.MagicMock):
mock_session.return_value.get.side_effect = Exception("boom")
- assert validate_credentials("test-token") is False
+ assert validate_credentials("test-token") == (False, None)
class TestFlatEndpoint:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark_source.py
index d478c42ae437..d1023a3a0ccd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/postmark/tests/test_postmark_source.py
@@ -1,5 +1,7 @@
from unittest import mock
+from parameterized import parameterized
+
from posthog.schema import SourceFieldInputConfig, SourceFieldInputConfigType
from products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManager
@@ -68,7 +70,7 @@ class TestPostmarkSource:
"products.warehouse_sources.backend.temporal.data_imports.sources.postmark.source.validate_postmark_credentials"
)
def test_validate_credentials_success(self, mock_validate):
- mock_validate.return_value = True
+ mock_validate.return_value = (True, 200)
is_valid, error_message = self.source.validate_credentials(self.config, self.team_id)
@@ -76,16 +78,26 @@ class TestPostmarkSource:
assert error_message is None
mock_validate.assert_called_once_with(self.config.server_token)
+ @parameterized.expand(
+ [
+ ("invalid_token_401", 401, "Invalid Postmark server API token"),
+ ("missing_permissions_403", 403, "doesn't have the required permissions"),
+ ("unreachable_none", None, "Couldn't reach Postmark"),
+ ("server_error_500", 500, "Couldn't reach Postmark"),
+ ("rate_limited_429", 429, "Couldn't reach Postmark"),
+ ]
+ )
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.postmark.source.validate_postmark_credentials"
)
- def test_validate_credentials_failure(self, mock_validate):
- mock_validate.return_value = False
+ def test_validate_credentials_failure_maps_status(self, _name, status, expected_substring, mock_validate):
+ mock_validate.return_value = (False, status)
is_valid, error_message = self.source.validate_credentials(self.config, self.team_id)
assert is_valid is False
- assert error_message == "Invalid Postmark server API token"
+ assert error_message is not None
+ assert expected_substring in error_message
def test_get_resumable_source_manager(self):
inputs = mock.MagicMock()