: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(mailerlite): stop blaming a working api key for an unverified probe (#107378)
Tom Owersed24d0b94b93
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/mailerlite.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/mailerlite.pyindex 7fabe594c144..9c05d11d2621 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/mailerlite.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/mailerlite.py@@ -159,14 +159,39 @@ def mailerlite_source()-def validate_credentials(api_key: str, path: str = "/subscribers") -> bool:- """Confirm the API key is genuine with one cheap probe against a list endpoint."""- ok, _status = validate_via_probe(+INVALID_KEY_ERROR = (+ "Your MailerLite API key is invalid or has been revoked. Create a new key in your "+ "MailerLite account settings, then try again."+)+MISSING_PERMISSION_ERROR = (+ "Your MailerLite API key is missing the read permissions needed for this data. "+ "Grant them in your MailerLite account settings, then try again."+)+UNVERIFIED_KEY_ERROR = (+ "Couldn't check your MailerLite API key because MailerLite didn't respond. Try again in a few minutes."+)+++def validate_credentials(api_key: str, path: str = "/subscribers") -> tuple[bool, str | None]:+ """Probe the API key with one cheap GET against a list endpoint, as ``(is_valid, error)``.++ Only a deterministic 4xx proves the key itself is bad. A 403 means the key is genuine but+ unscoped, and a 429, a 5xx or a transport failure never verified it at all — blaming the key+ there sends the user off to replace one that works.+ """+ _ok, status = validate_via_probe(lambda: make_tracked_session(redact_values=(api_key,)),f"{MAILERLITE_BASE_URL}{path}?limit=1",headers={"Authorization": f"Bearer {api_key}", "Accept": "application/json"},)- return ok++ if status == 200:+ return True, None+ if status == 403:+ return False, MISSING_PERMISSION_ERROR+ if status is not None and 400 <= status < 500 and status != 429:+ return False, INVALID_KEY_ERROR+ return False, UNVERIFIED_KEY_ERROR# Envelope keys a webhook delivery adds around the subscriber object. `event` (flat payloads) anddiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/source.pyindex 379cc680ee5a..97b82a3f4d36 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/source.py@@ -182,10 +182,7 @@ Copy the `secret` from the response into the field below. MailerLite never retur) -> tuple[bool, str | None]:endpoint_config = MAILERLITE_ENDPOINTS.get(schema_name) if schema_name else Nonepath = endpoint_config.path if endpoint_config else "/subscribers"- if validate_mailerlite_credentials(config.api_key, path):- return True, None-- return False, "Invalid MailerLite API key"+ return validate_mailerlite_credentials(config.api_key, path)def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[MailerLiteResumeConfig]:return ResumableSourceManager[MailerLiteResumeConfig](inputs, MailerLiteResumeConfig)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.pyindex 5c648b8e9138..ccfa8a6ccd19 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite.py@@ -15,7 +15,10 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.resfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.resumable import ResumableSourceManagerfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.webhook_s3 import WebhookSourceManagerfrom products.warehouse_sources.backend.temporal.data_imports.sources.mailerlite.mailerlite import (+ INVALID_KEY_ERROR,MAILERLITE_BASE_URL,+ MISSING_PERMISSION_ERROR,+ UNVERIFIED_KEY_ERROR,MailerLiteResumeConfig,_webhook_table_transformer,create_webhook,@@ -255,18 +258,25 @@ class TestApiVersionHeader:class TestValidateCredentials:@pytest.mark.parametrize(- ("status_code", "expected"),- [(200, True), (401, False), (403, False), (500, False)],+ ("status_code", "expected_ok", "expected_error"),+ [+ (200, True, None),+ (401, False, INVALID_KEY_ERROR),+ (400, False, INVALID_KEY_ERROR),+ (403, False, MISSING_PERMISSION_ERROR),+ (429, False, UNVERIFIED_KEY_ERROR),+ (500, False, UNVERIFIED_KEY_ERROR),+ ],)- def test_status_maps_to_bool(self, status_code: int, expected: bool) -> None:+ def test_status_maps_to_message(self, status_code: int, expected_ok: bool, expected_error: str | None) -> None:with patch(MAILERLITE_SESSION_PATCH) as MockSession:MockSession.return_value.get.return_value = _make_response({}, status_code=status_code)- assert validate_credentials("key") is expected+ assert validate_credentials("key") == (expected_ok, expected_error)- def test_exception_returns_false(self) -> None:+ def test_exception_does_not_blame_the_key(self) -> None:with patch(MAILERLITE_SESSION_PATCH) as MockSession:MockSession.return_value.get.side_effect = Exception("boom")- assert validate_credentials("key") is False+ assert validate_credentials("key") == (False, UNVERIFIED_KEY_ERROR)class TestMailerLiteSourceResponse:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite_source.pyindex 03ccba5e4ca3..34a3237d244c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mailerlite/tests/test_mailerlite_source.py@@ -32,22 +32,20 @@ def _config() -> MailerLiteSourceConfig:class TestMailerLiteSourceClass:@pytest.mark.parametrize(- ("valid", "expected_ok"),- [(True, True), (False, False)],+ "probe_result",+ [(True, None), (False, "probe rejected the key")],)- def test_validate_credentials(self, valid: bool, expected_ok: bool) -> None:+ def test_validate_credentials_passes_the_probe_result_through(self, probe_result: tuple[bool, str | None]) -> None:with patch("products.warehouse_sources.backend.temporal.data_imports.sources.mailerlite.source.validate_mailerlite_credentials",- return_value=valid,+ return_value=probe_result,):- ok, error = MailerLiteSource().validate_credentials(_config(), team_id=1)- assert ok is expected_ok- assert (error is None) is expected_ok+ assert MailerLiteSource().validate_credentials(_config(), team_id=1) == probe_resultdef test_validate_credentials_uses_schema_path(self) -> None:with patch("products.warehouse_sources.backend.temporal.data_imports.sources.mailerlite.source.validate_mailerlite_credentials",- return_value=True,+ return_value=(True, None),) as mock_validate:MailerLiteSource().validate_credentials(_config(), team_id=1, schema_name="groups")assert mock_validate.call_args.args == ("test-key", "/groups")