: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(tawk-to): stop reporting every validation failure as an invalid api key (#118969)
Tom Owers770784d46686
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/source.pyindex 432d8f16c981..c0bb8045d4d9 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/source.py@@ -1,5 +1,7 @@from typing import Optional, cast+import requests+from products.warehouse_sources.backend.facade.codebase_detection import detectfrom products.warehouse_sources.backend.facade.source_config import (DataWarehouseSourceCategory,@@ -23,7 +25,9 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.seTAWK_TO_ENDPOINTS,)from products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.tawk_to import (+ TawkToCredentialsError,TawkToResumeConfig,+ TawkToRetryableError,tawk_to_source,validate_credentials as validate_tawk_to_credentials,)@@ -126,10 +130,13 @@ tawk.to's REST API is available by request: submit the [REST API Access Requestschema_name: Optional[str] = None,api_version: str | None = None,) -> tuple[bool, str | None]:- if validate_tawk_to_credentials(config.api_key):+ try:+ validate_tawk_to_credentials(config.api_key)return True, None-- return False, "Invalid tawk.to API key"+ except TawkToCredentialsError as e:+ return False, str(e)+ except (TawkToRetryableError, requests.RequestException):+ return False, "PostHog couldn't reach tawk.to to check your API key. Wait a few minutes, then try again."def get_resumable_source_manager(self, inputs: SourceInputs) -> ResumableSourceManager[TawkToResumeConfig]:return ResumableSourceManager[TawkToResumeConfig](inputs, TawkToResumeConfig)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tawk_to.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tawk_to.pyindex 6a18aa376d7e..c16faaec773f 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tawk_to.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tawk_to.py@@ -29,6 +29,20 @@ class TawkToApiError(Exception):"""The API answered 2xx but with `{"ok": false, ...}` in the body."""+class TawkToCredentialsError(Exception):+ """tawk.to rejected the API key, with a message the user can act on."""+++INVALID_API_KEY_MESSAGE = (+ "Your tawk.to API key is invalid or has been revoked. Generate a new REST API key in your tawk.to profile, "+ "then reconnect."+)+API_KEY_FORBIDDEN_MESSAGE = (+ "Your tawk.to API key can't read your properties. Check that tawk.to approved your REST API access and that "+ "the key has read access, then reconnect."+)++@dataclasses.dataclassclass TawkToResumeConfig:# Row offset within the property currently being paged through.@@ -85,18 +99,26 @@ def _post(def validate_credentials(api_key: str) -> bool:"""Confirm the API key is valid. `property.list` with an empty body is the cheapest- authenticated probe (an invalid key gets `{"ok": false, "error": "auth_error"}` with 401)."""- try:- response = make_tracked_session(redact_values=(api_key,), capture=False).post(- f"{TAWK_TO_BASE_URL}/property.list",- json={},- auth=(api_key, ""),- headers={"Accept": "application/json"},- timeout=10,- )- return response.status_code == 200 and response.json().get("ok") is True- except Exception:- return False+ authenticated probe (an invalid key gets `{"ok": false, "error": "auth_error"}` with 401).++ Returns True for a working key and raises `TawkToCredentialsError` when tawk.to rejects it.+ A rate limit or 5xx raises `TawkToRetryableError`, and transport errors propagate, so the+ caller doesn't report a transient failure as a bad key."""+ response = make_tracked_session(redact_values=(api_key,), capture=False).post(+ f"{TAWK_TO_BASE_URL}/property.list",+ json={},+ auth=(api_key, ""),+ headers={"Accept": "application/json"},+ timeout=10,+ )+ if response.status_code == 429 or response.status_code >= 500:+ raise TawkToRetryableError(f"tawk.to API error (retryable): status={response.status_code}")+ # tawk.to grants REST API access by request, so a key can authenticate and still be refused data.+ if response.status_code == 403:+ raise TawkToCredentialsError(API_KEY_FORBIDDEN_MESSAGE)+ if response.ok and response.json().get("ok") is True:+ return True+ raise TawkToCredentialsError(INVALID_API_KEY_MESSAGE)def _list_property_ids(session: requests.Session, api_key: str, logger: FilteringBoundLogger) -> list[str]:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to.pyindex 6a48797dd71f..712c0aa82dbb 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to.py@@ -3,6 +3,8 @@ from typing import Anyimport pytestfrom unittest import mock+import requests+from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (ScriptedResponse,SourceDriver,@@ -16,9 +18,13 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.se)from products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.source import TawkToSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.tawk_to import (+ API_KEY_FORBIDDEN_MESSAGE,+ INVALID_API_KEY_MESSAGE,PAGE_SIZE,TawkToApiError,+ TawkToCredentialsError,TawkToResumeConfig,+ TawkToRetryableError,get_rows,validate_credentials,)@@ -54,22 +60,34 @@ def _page(items: list[dict[str, Any]], total: int) -> dict[str, Any]:class TestValidateCredentials:+ def test_accepts_a_working_key(self):+ with scripted_network(always(_response({"ok": True, "data": []}))):+ assert validate_credentials("key") is True+@pytest.mark.parametrize(- "status_code, body, expected",+ "status_code, body, expected_message",[- (200, {"ok": True, "data": []}, True),- (200, {"ok": False, "error": "auth_error"}, False),- (401, {"ok": False, "error": "auth_error", "message": "invalid_auth"}, False),- (500, {}, False),+ (200, {"ok": False, "error": "auth_error"}, INVALID_API_KEY_MESSAGE),+ (401, {"ok": False, "error": "auth_error", "message": "invalid_auth"}, INVALID_API_KEY_MESSAGE),+ (403, {"ok": False, "error": "forbidden"}, API_KEY_FORBIDDEN_MESSAGE),],)- def test_status_and_body_mapping(self, status_code, body, expected):+ def test_rejected_key_raises_credentials_error(self, status_code, body, expected_message):with scripted_network(always(_response(body, status_code))):- assert validate_credentials("key") is expected-- def test_swallows_exceptions(self):- with scripted_network(always(Exception("boom"))):- assert validate_credentials("key") is False+ with pytest.raises(TawkToCredentialsError) as exc:+ validate_credentials("key")+ assert str(exc.value) == expected_message++ @pytest.mark.parametrize("status_code", [429, 500, 503])+ def test_rate_limit_and_server_errors_are_retryable(self, status_code):+ with scripted_network(always(_response({}, status_code))):+ with pytest.raises(TawkToRetryableError):+ validate_credentials("key")++ def test_transport_errors_propagate(self):+ with scripted_network(always(requests.ConnectionError("boom"))):+ with pytest.raises(requests.ConnectionError):+ validate_credentials("key")class TestGetRows:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to_source.pyindex bbacbc6bf750..23f4f6d26eed 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tawk_to/tests/test_tawk_to_source.py@@ -1,8 +1,14 @@import pytestfrom unittest import mock+import requests+from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.tawkto import TawkToSourceConfigfrom products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.source import TawkToSource+from products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.tawk_to import (+ TawkToCredentialsError,+ TawkToRetryableError,+)MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.tawk_to.source"@@ -18,20 +24,25 @@ class TestTawkToSource:assert [schema.name for schema in schemas] == ["chats"]@pytest.mark.parametrize(- "mock_return, expected_valid, expected_message",+ "side_effect, expected_valid, expected_message",[- (True, True, None),- (False, False, "Invalid tawk.to API key"),+ (None, True, None),+ (TawkToCredentialsError("rejected"), False, "rejected"),+ (TawkToRetryableError("status=503"), False, "couldn't reach tawk.to"),+ (requests.ConnectionError("boom"), False, "couldn't reach tawk.to"),],)@mock.patch(f"{MODULE}.validate_tawk_to_credentials")- def test_validate_credentials(self, mock_validate, mock_return, expected_valid, expected_message):- mock_validate.return_value = mock_return+ def test_validate_credentials(self, mock_validate, side_effect, expected_valid, expected_message):+ mock_validate.side_effect = side_effectis_valid, error_message = self.source.validate_credentials(self.config, self.team_id)assert is_valid is expected_valid- assert error_message == expected_message+ if expected_message is None:+ assert error_message is None+ else:+ assert expected_message in (error_message or "")mock_validate.assert_called_once_with(self.config.api_key)@pytest.mark.parametrize("raw_property_id", [None, "", " "])