: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(cloudflare): say which list a denied api token can't read (#123918)
Tom Owers3e52cc87f7d4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.pyindex 5136e5747cf5..e8b8cd132936 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/source.py@@ -9,6 +9,7 @@ from products.warehouse_sources.backend.facade.source_config import (SourceFieldInputConfigType,)from products.warehouse_sources.backend.temporal.data_imports.sources.cloudflare.cloudflare import (+ CLOUDFLARE_BASE_URL,DNS_ANALYTICS_ALL_ZONES_FAILED,cloudflare_source,validate_credentials as validate_cloudflare_credentials,@@ -192,6 +193,11 @@ class CloudflareSource(SimpleSource[CloudflareSourceConfig]):def get_non_retryable_errors(self) -> dict[str, str | None]:return {"401 Client Error: Unauthorized for url: https://api.cloudflare.com": "Cloudflare authentication failed. Please check your API token.",+ # Every table fans out from one of these two lists, and a 403 on a single zone or account+ # is skipped, so a 403 on the list itself means the token can't read any of it. Matched+ # before the generic 403 below, because the first match wins.+ f"403 Client Error: Forbidden for url: {CLOUDFLARE_BASE_URL}/zones?": "Your Cloudflare API token can't list your zones. Check that the token is still active and has the Zone Read permission, then re-enable the sync.",+ f"403 Client Error: Forbidden for url: {CLOUDFLARE_BASE_URL}/accounts?": "Your Cloudflare API token can't list your accounts. Check that the token is still active and has the Account Settings Read permission, then re-enable the sync.","403 Client Error: Forbidden for url: https://api.cloudflare.com": "Cloudflare denied access. Please check that your API token has read permissions for this resource.",DNS_ANALYTICS_ALL_ZONES_FAILED: "Your Cloudflare API token can't read analytics for any of your zones. Add the Zone Analytics read permission to the token, then re-enable the sync.",}diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.pyindex 705a9777d291..bba6faa9b4e7 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/cloudflare/tests/test_cloudflare_client.py@@ -23,6 +23,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.cloudflareZONES_PARENT,)from products.warehouse_sources.backend.temporal.data_imports.sources.cloudflare.source import CloudflareSource+from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import error_message_matchesfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.rest_source.rest_client import (RESTClientRetryableError,)@@ -176,12 +177,28 @@ class TestZoneFanout:assert isinstance(result.raised, requests.HTTPError)- def test_forbidden_zone_listing_propagates(self) -> None:- # No zone access at all (the top-level listing) must still fail loudly- # so the schema is flagged non-retryable rather than silently emptied.- result = _driver().run("dns_records", [_error_response(403)])+ @pytest.mark.parametrize(+ "endpoint, expected_message",+ [+ ("zones", "can't list your zones"),+ ("dns_records", "can't list your zones"),+ ("audit_logs", "can't list your accounts"),+ ],+ )+ def test_forbidden_parent_listing_fails_with_the_permission_to_add(+ self, endpoint: str, expected_message: str+ ) -> None:+ # No access to the top-level listing must still fail loudly so the schema is flagged+ # non-retryable rather than silently emptied, and the customer is told which list it was.+ result = _driver().run(endpoint, [_error_response(403)])assert isinstance(result.raised, requests.HTTPError)+ message = next(+ message+ for pattern, message in CloudflareSource().get_non_retryable_errors().items()+ if error_message_matches(str(result.raised), [pattern])+ )+ assert message is not None and expected_message in messageclass TestRetry: