: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(data-imports): normalize zendesk subdomain to avoid doubled host (#64172)
Tom Owers0b133e939b20
diff --git a/posthog/temporal/data_imports/sources/zendesk/source.py b/posthog/temporal/data_imports/sources/zendesk/source.pyindex 42858308220c..8ae8a9ada1f0 100644--- a/posthog/temporal/data_imports/sources/zendesk/source.py+++ b/posthog/temporal/data_imports/sources/zendesk/source.py@@ -20,7 +20,11 @@ from posthog.temporal.data_imports.sources.zendesk.settings import (PARTITION_FIELDS,SUPPORT_ENDPOINTS,)-from posthog.temporal.data_imports.sources.zendesk.zendesk import validate_credentials, zendesk_source+from posthog.temporal.data_imports.sources.zendesk.zendesk import (+ normalize_subdomain,+ validate_credentials,+ zendesk_source,+)from products.data_warehouse.backend.types import ExternalDataSourceType@@ -64,11 +68,12 @@ class ZendeskSource(SimpleSource[ZendeskSourceConfig]):def validate_credentials(self, config: ZendeskSourceConfig, team_id: int, schema_name: Optional[str] = None) -> tuple[bool, str | None]:+ subdomain = normalize_subdomain(config.subdomain)subdomain_regex = re.compile("^[a-zA-Z0-9-]+$")- if not subdomain_regex.match(config.subdomain):+ if not subdomain_regex.match(subdomain):return False, "Zendesk subdomain is incorrect"- if validate_credentials(config.subdomain, config.api_key, config.email_address):+ if validate_credentials(subdomain, config.api_key, config.email_address):return True, Nonereturn False, "Invalid credentials"diff --git a/posthog/temporal/data_imports/sources/zendesk/tests/test_zendesk.py b/posthog/temporal/data_imports/sources/zendesk/tests/test_zendesk.pyindex bf32c8566c21..f24ec7b617dc 100644--- a/posthog/temporal/data_imports/sources/zendesk/tests/test_zendesk.py+++ b/posthog/temporal/data_imports/sources/zendesk/tests/test_zendesk.py@@ -5,7 +5,10 @@ import pytestfrom requests import Request, Response-from posthog.temporal.data_imports.sources.zendesk.zendesk import ZendeskTicketsCursorIncrementalPaginator+from posthog.temporal.data_imports.sources.zendesk.zendesk import (+ ZendeskTicketsCursorIncrementalPaginator,+ normalize_subdomain,+)def _make_response(json_body: dict[str, Any] | None = None) -> Response:@@ -16,6 +19,29 @@ def _make_response(json_body: dict[str, Any] | None = None) -> Response:return resp+class TestNormalizeSubdomain:+ @pytest.mark.parametrize(+ "raw,expected",+ [+ pytest.param("nibbles", "nibbles", id="bare_subdomain"),+ pytest.param("nibbles.zendesk.com", "nibbles", id="full_host"),+ pytest.param("https://nibbles.zendesk.com", "nibbles", id="https_url"),+ pytest.param("https://nibbles.zendesk.com/", "nibbles", id="https_url_trailing_slash"),+ pytest.param("http://nibbles.zendesk.com/api/v2", "nibbles", id="url_with_path"),+ pytest.param(" nibbles.zendesk.com ", "nibbles", id="whitespace"),+ pytest.param("nibbles.ZENDESK.com", "nibbles", id="mixed_case_host"),+ pytest.param("multi-word-team", "multi-word-team", id="hyphenated_subdomain"),+ ],+ )+ def test_collapses_to_subdomain_label(self, raw: str, expected: str) -> None:+ assert normalize_subdomain(raw) == expected++ def test_full_host_does_not_double_when_building_base_url(self) -> None:+ # Regression: a pasted full host previously produced "nibbles.zendesk.com.zendesk.com",+ # whose TLS handshake the Zendesk edge rejects.+ assert f"https://{normalize_subdomain('nibbles.zendesk.com')}.zendesk.com/" == "https://nibbles.zendesk.com/"++class TestZendeskTicketsCursorIncrementalPaginator:def test_advances_to_next_cursor(self) -> None:p = ZendeskTicketsCursorIncrementalPaginator()diff --git a/posthog/temporal/data_imports/sources/zendesk/zendesk.py b/posthog/temporal/data_imports/sources/zendesk/zendesk.pyindex d7d313ccd78a..294c7cc68f78 100644--- a/posthog/temporal/data_imports/sources/zendesk/zendesk.py+++ b/posthog/temporal/data_imports/sources/zendesk/zendesk.py@@ -1,3 +1,4 @@+import reimport base64from typing import Any, Optional@@ -304,6 +305,23 @@ class ZendeskIncrementalEndpointPaginator(BasePaginator):request.params = {}+def normalize_subdomain(subdomain: str) -> str:+ """Reduce whatever the user entered to the bare Zendesk subdomain label.++ Users frequently paste the full host ("nibbles.zendesk.com") or a URL+ ("https://nibbles.zendesk.com/") into the subdomain field. Without normalizing,+ the base URL becomes "https://nibbles.zendesk.com.zendesk.com/", whose doubled+ host the TLS handshake rejects (SSLV3_ALERT_HANDSHAKE_FAILURE) and never recovers.+ """+ subdomain = subdomain.strip()+ if "://" in subdomain:+ subdomain = subdomain.split("://", 1)[1]+ # Drop any path/query left over from a pasted URL.+ subdomain = subdomain.split("/", 1)[0]+ # Strip a trailing ".zendesk.com" so a full host collapses to the subdomain label.+ return re.sub(r"\.zendesk\.com$", "", subdomain, flags=re.IGNORECASE)++def zendesk_source(subdomain: str,api_key: str,@@ -316,7 +334,7 @@ def zendesk_source():config: RESTAPIConfig = {"client": {- "base_url": f"https://{subdomain}.zendesk.com/",+ "base_url": f"https://{normalize_subdomain(subdomain)}.zendesk.com/","auth": {"type": "http_basic","username": f"{email_address}/token",@@ -340,7 +358,7 @@ def zendesk_source(def validate_credentials(subdomain: str, api_key: str, email_address: str) -> bool:basic_token = base64.b64encode(f"{email_address}/token:{api_key}".encode("ascii")).decode("ascii")res = make_tracked_session().get(- f"https://{subdomain}.zendesk.com/api/v2/tickets/count",+ f"https://{normalize_subdomain(subdomain)}.zendesk.com/api/v2/tickets/count",headers={"Authorization": f"Basic {basic_token}"},)