: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(warehouse-sources): point customer.io keys at their own region (#124846)
Tom Owers193c7ecf498a
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.pyindex c40c5c46ef51..def4bcb8af3c 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/api_client.py@@ -91,13 +91,13 @@ def _format_http_error(error: requests.HTTPError) -> str:status_code = response.status_codeif status_code == 401:return (- "Customer.io rejected the App API Key (401). Generate an App API Key from "- "Settings > API Credentials > App API Keys and check the region."+ "Customer.io rejected your App API key. Create a new key under Settings > API "+ "Credentials > App API Keys, then reconnect.")if status_code == 403:- return "Customer.io denied the request (403). Make sure the App API Key has access to reporting webhooks."+ return "Customer.io denied the request. Make sure your App API key has access to reporting webhooks."if status_code == 429:- return "Customer.io rate-limited the request (429). Try again in a few seconds."+ return "Customer.io rate-limited the request. Wait a few seconds, then try again."return f"Customer.io API error ({status_code})."@@ -112,12 +112,32 @@ def validate_credentials(api_key: str, region: str | None) -> tuple[bool, str |response = _session(api_key).get(url, timeout=REQUEST_TIMEOUT_SECONDS)response.raise_for_status()except requests.HTTPError as e:+ other_region = _other_region(region)+ if e.response.status_code == 401 and _key_works_in_region(api_key, other_region):+ label = other_region.upper()+ return False, (+ f"Your App API key belongs to Customer.io's {label} region. Set Region to {label}, then try again."+ )return False, _format_http_error(e)except requests.RequestException as e:return False, f"Could not reach Customer.io: {e}"return True, None+def _other_region(region: str | None) -> str:+ return "us" if (region or "").lower() == "eu" else "eu"+++def _key_works_in_region(api_key: str, region: str) -> bool:+ # Customer.io keys only work in the region that issued them, and the wrong region answers with+ # the same 401 as a revoked key.+ try:+ response = _session(api_key).get(f"{_base_url(region)}{WORKSPACES_PATH}", timeout=REQUEST_TIMEOUT_SECONDS)+ except requests.RequestException:+ return False+ return response.ok++@retry_with_deadline(retry=retry_if_exception_type((ListEndpointRetryableError, requests.ReadTimeout, requests.ConnectionError)),stop=stop_after_attempt(5),diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.pyindex eb9f8d122906..7ec12cedee66 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/customer_io/tests/test_api_client.py@@ -4,6 +4,11 @@ from unittest.mock import MagicMock, patchimport requestsfrom parameterized import parameterized+from products.warehouse_sources.backend.temporal.data_imports.sources.common.testing import (+ RecordedRequest,+ ScriptedResponse,+ scripted_network,+)from products.warehouse_sources.backend.temporal.data_imports.sources.customer_io import api_clientfrom products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.constants import (CIO_AUTO_WEBHOOK_NAME,@@ -66,6 +71,26 @@ class TestValidateCredentials:assert error is not Noneassert expected_substring.lower() in error.lower()+ @parameterized.expand(+ [+ ("key_from_other_region", "us", CIO_EU_BASE_URL, "Set Region to EU"),+ ("key_from_other_region_eu", "eu", CIO_US_BASE_URL, "Set Region to US"),+ ("key_invalid_everywhere", "us", None, "Create a new key"),+ ]+ )+ def test_rejected_key_names_the_region_it_belongs_to(self, _name, region, accepting_base_url, expected):+ def respond(request: RecordedRequest) -> ScriptedResponse:+ if accepting_base_url and request.url.startswith(accepting_base_url):+ return ScriptedResponse(status=200, json={"workspaces": []})+ return ScriptedResponse(status=401, json={})++ with scripted_network(respond):+ success, error = api_client.validate_credentials("key", region)++ assert success is False+ assert error is not None+ assert expected in error+class TestCreateWebhook:@patch("products.warehouse_sources.backend.temporal.data_imports.sources.customer_io.api_client._session")