: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-warehouse): skip accounts.list check for OAuth Stripe sources (#56555)
Matt Brooker93f40d7e7e0f
diff --git a/posthog/temporal/data_imports/sources/stripe/source.py b/posthog/temporal/data_imports/sources/stripe/source.pyindex 3a6752a80e0b..6324d7a343bc 100644--- a/posthog/temporal/data_imports/sources/stripe/source.py+++ b/posthog/temporal/data_imports/sources/stripe/source.py@@ -294,7 +294,7 @@ If automatic creation failed due to a permissions error and you're using a restr) -> tuple[bool, str | None]:try:api_key = self._get_api_key(config, team_id)- if validate_stripe_credentials(api_key, schema_name):+ if validate_stripe_credentials(api_key, schema_name, auth_method=config.auth_method.selection):return True, Noneelse:return False, "Invalid Stripe credentials"diff --git a/posthog/temporal/data_imports/sources/stripe/stripe.py b/posthog/temporal/data_imports/sources/stripe/stripe.pyindex 046c2a34c3c6..83e1c348573e 100644--- a/posthog/temporal/data_imports/sources/stripe/stripe.py+++ b/posthog/temporal/data_imports/sources/stripe/stripe.py@@ -2,7 +2,7 @@ import osimport reimport dataclassesfrom collections.abc import Callable-from typing import Any, Optional, Union, cast, get_args, get_type_hints+from typing import Any, Literal, Optional, Union, cast, get_args, get_type_hintsimport orjsonimport stripe as stripe_lib@@ -398,7 +398,9 @@ class StripeValidationError(Exception):super().__init__(message)-def validate_credentials(api_key: str, table_name: Optional[str] = None) -> bool:+def validate_credentials(+ api_key: str, table_name: Optional[str] = None, auth_method: Literal["api_key", "oauth"] = "api_key"+) -> bool:"""Validates Stripe API credentials and checks permissions for all required resources.Returns True if the API key is valid and has all required permissions.@@ -429,6 +431,12 @@ def validate_credentials(api_key: str, table_name: Optional[str] = None) -> boolreturn f"{name} ({entry.parent_name})", parent_entryreturn name, entry+ # accounts.list requires Connect platform access — OAuth connected-account tokens can't call it.+ # If a per-table check is requested for Account under OAuth, skip it cleanly: Account is also+ # absent from ENDPOINTS so it can never be synced via OAuth anyway.+ if auth_method == "oauth" and table_name == ACCOUNT_RESOURCE_NAME:+ return True+missing_permissions: dict[str, str] = {}errors: dict[str, str] = {}@@ -445,6 +453,11 @@ def validate_credentials(api_key: str, table_name: Optional[str] = None) -> boolresources_to_check = [(name, resource) for name, resource in all_resources.items() if isinstance(resource, StripeResource)]+ if auth_method == "oauth":+ # accounts.list requires Connect platform access — OAuth connected-account tokens can't call it.+ resources_to_check = [+ (name, resource) for name, resource in resources_to_check if name != ACCOUNT_RESOURCE_NAME+ ]for display_name, resource in resources_to_check:try:diff --git a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.pyindex 88ee08ff1488..2fdb69823dd7 100644--- a/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py+++ b/posthog/temporal/tests/data_imports/stripe/test_stripe_source.py@@ -547,6 +547,59 @@ def test_validate_credentials_with_missing_table_name():assert "bad_table" in str(e)+def test_validate_credentials_oauth_skips_account():+ mock_client = mock.MagicMock()++ mock_client.accounts.list = mock.MagicMock()+ mock_client.balance_transactions.list = mock.MagicMock()+ mock_client.charges.list = mock.MagicMock()+ mock_client.customers.list = mock.MagicMock()+ mock_client.disputes.list = mock.MagicMock()+ mock_client.invoice_items.list = mock.MagicMock()+ mock_client.invoices.list = mock.MagicMock()+ mock_client.payouts.list = mock.MagicMock()+ mock_client.prices.list = mock.MagicMock()+ mock_client.products.list = mock.MagicMock()+ mock_client.subscriptions.list = mock.MagicMock()+ mock_client.refunds.list = mock.MagicMock()+ mock_client.credit_notes.list = mock.MagicMock()++ with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.StripeClient", return_value=mock_client):+ result = validate_credentials("oauth_token", auth_method="oauth")++ assert result is True++ # accounts.list requires Connect platform access — must not be called for OAuth tokens+ mock_client.accounts.list.assert_not_called()++ # All other resources should still be checked+ mock_client.balance_transactions.list.assert_called_once_with(params={"limit": 1})+ mock_client.charges.list.assert_called_once_with(params={"limit": 1})+ mock_client.customers.list.assert_called_once_with(params={"limit": 1})+ mock_client.disputes.list.assert_called_once_with(params={"limit": 1})+ mock_client.invoice_items.list.assert_called_once_with(params={"limit": 1})+ mock_client.invoices.list.assert_called_once_with(params={"limit": 1})+ mock_client.payouts.list.assert_called_once_with(params={"limit": 1})+ mock_client.prices.list.assert_called_once_with(params={"limit": 1})+ mock_client.products.list.assert_called_once_with(params={"limit": 1})+ mock_client.subscriptions.list.assert_called_once_with(params={"limit": 1})+ mock_client.refunds.list.assert_called_once_with(params={"limit": 1})+ mock_client.credit_notes.list.assert_called_once_with(params={"limit": 1})+++def test_validate_credentials_oauth_account_table_name_returns_true():+ mock_client = mock.MagicMock()++ with mock.patch("posthog.temporal.data_imports.sources.stripe.stripe.StripeClient", return_value=mock_client):+ result = validate_credentials("oauth_token", ACCOUNT_RESOURCE_NAME, auth_method="oauth")++ assert result is True++ # No Stripe API calls should be made — Account is skipped for OAuth before any checks run+ mock_client.accounts.list.assert_not_called()+ mock_client.balance_transactions.list.assert_not_called()++class TestGetApiKey:@pytest.fixturedef stripe_source(self):