: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(upstash): tell an unauthorized account apart from a bad key (#102116)
Tom Owersfe6b32772ced
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/source.pyindex 552c8f1f0099..3f7c5abc85d0 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/source.py@@ -22,6 +22,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.upstash.seUPSTASH_ENDPOINTS,)from products.warehouse_sources.backend.temporal.data_imports.sources.upstash.upstash import (+ INVALID_CREDENTIALS_ERROR,upstash_source,validate_credentials as validate_upstash_credentials,)@@ -89,7 +90,7 @@ Create a management API key in the [Upstash console](https://console.upstash.com# 401/403 surface as a requests HTTPError when `_fetch` calls raise_for_status(). Retrying# never satisfies a credential problem, so stop the sync. Match the stable status text and# base host, not the per-request path.- "401 Client Error: Unauthorized for url: https://api.upstash.com": "Your Upstash email or management API key is invalid or has been revoked. Create a new key in the Upstash console, then reconnect.",+ "401 Client Error: Unauthorized for url: https://api.upstash.com": INVALID_CREDENTIALS_ERROR,"403 Client Error: Forbidden for url: https://api.upstash.com": "Your Upstash management API key is not authorized for this resource. Check the key, then reconnect.",}diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/tests/test_upstash.py b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/tests/test_upstash.pyindex c12f821b11f4..42168825bb18 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/tests/test_upstash.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/tests/test_upstash.py@@ -13,6 +13,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.upstash.seUPSTASH_ENDPOINTS,UPSTASH_ROOT_BASE_URL,)+from products.warehouse_sources.backend.temporal.data_imports.sources.upstash.source import UpstashSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.upstash.upstash import (upstash_source,validate_credentials,@@ -225,6 +226,32 @@ class TestValidateCredentials:assert ok is expected_okassert (error is None) is expected_ok+ # A 403 means the whole account can't use the management API, so it must not read as a bad key+ # and send the customer off to generate another one that fails the same way.+ @parameterized.expand(+ [+ (401, "invalid or has been revoked"),+ (403, "isn't authorized to use the management API"),+ ]+ )+ def test_a_bad_key_and_an_unauthorized_account_read_differently(self, status: int, expected: str) -> None:+ session = mock.MagicMock()+ session.get.return_value = mock.MagicMock(status_code=status)+ with mock.patch(SESSION_PATCH, lambda *a, **k: session):+ ok, error = validate_credentials("e", "k")+ assert ok is False+ assert expected in (error or "")++ def test_a_rejected_credential_reads_the_same_during_setup_and_during_a_sync(self) -> None:+ # Setup and sync reach the message by different routes, so a divergence between them is+ # invisible unless the two are compared.+ session = mock.MagicMock()+ session.get.return_value = mock.MagicMock(status_code=401)+ with mock.patch(SESSION_PATCH, lambda *a, **k: session):+ _ok, setup_error = validate_credentials("e", "k")+ sync_errors = UpstashSource().get_non_retryable_errors()+ assert setup_error == sync_errors["401 Client Error: Unauthorized for url: https://api.upstash.com"]+def test_probes_teams_endpoint_with_basic_auth(self) -> None:session = mock.MagicMock()session.get.return_value = mock.MagicMock(status_code=200)diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/upstash.py b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/upstash.pyindex 94b4ef6aea46..63206ab95edf 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/upstash.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/upstash/upstash.py@@ -29,6 +29,22 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.upstash.se# fan-out. Named distinctly from the public `redis_databases` endpoint so the two never collide._DATABASES_PARENT = "redis_databases_parent"+# Shared with the source's 401 entry in `get_non_retryable_errors` so a rejected credential reads+# the same whether it surfaces during setup or during a sync.+INVALID_CREDENTIALS_ERROR = (+ "Your Upstash email or management API key is invalid or has been revoked. Create a new key in "+ "the Upstash console, then reconnect."+)++# A 403 on the probe is not a per-resource permission problem: /v2/teams is the account-level+# endpoint every native account can read. The Developer API is closed to marketplace accounts+# (Vercel, Fly.io), so naming the key sends those customers round a loop of generating another one+# that cannot work either.+_UNAUTHORIZED_ACCOUNT_ERROR = (+ "Your Upstash account isn't authorized to use the management API. Marketplace accounts (Vercel "+ "or Fly.io) can't use it, so connect with a native Upstash account."+)+def validate_credentials(email: str, api_key: str) -> tuple[bool, str | None]:"""Confirm the email + management API key are genuine via GET /v2/teams.@@ -44,8 +60,10 @@ def validate_credentials(email: str, api_key: str) -> tuple[bool, str | None]:f"{UPSTASH_API_BASE_URL}/teams",auth=HTTPBasicAuth(email, api_key),)- if status in (401, 403):- return False, "Invalid Upstash email or management API key"+ if status == 401:+ return False, INVALID_CREDENTIALS_ERROR+ if status == 403:+ return False, _UNAUTHORIZED_ACCOUNT_ERRORreturn True, None