: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
feat(credentials-service): read the remaining direct platform keys via the service (#122116)
Tom Owers016659abc10c
diff --git a/ee/partners/stripe/api/provisioning/core.py b/ee/partners/stripe/api/provisioning/core.pyindex 7ba79f3b9e24..e4c03cb1e965 100644--- a/ee/partners/stripe/api/provisioning/core.py+++ b/ee/partners/stripe/api/provisioning/core.py@@ -2,7 +2,7 @@The Stripe orchestrator is a fixed, partner-less identity: auth codes areminted with an empty ``partner_id`` and token exchange binds to the Stripe-Projects OAuth app resolved from ``settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID``.+Projects OAuth app resolved from ``STRIPE_POSTHOG_OAUTH_CLIENT_ID``."""from __future__ import annotations@@ -21,6 +21,8 @@ import structlogfrom posthog.accessible_teams import AccessibleTeamsfrom posthog.api.authentication import password_reset_token_generator+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.event_usage import report_user_signed_upfrom posthog.exceptions_capture import capture_exceptionfrom posthog.helpers.email_utils import EmailLookupHandler@@ -75,7 +77,7 @@ class StripeOAuthAppMissingError(Exception):def get_stripe_oauth_app() -> OAuthApplication:- client_id = settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID+ client_id = get_or_settings("STRIPE_POSTHOG_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS)if not client_id:error = StripeOAuthAppMissingError("STRIPE_POSTHOG_OAUTH_CLIENT_ID is not configured")capture_exception(error)@@ -100,7 +102,7 @@ def is_stripe_oauth_app(app: OAuthApplication) -> bool:Fails closed when ``STRIPE_POSTHOG_OAUTH_CLIENT_ID`` is unset: with noconfigured Stripe identity, no caller can be the Stripe orchestrator."""- client_id = settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID+ client_id = get_or_settings("STRIPE_POSTHOG_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS)return bool(client_id) and app.client_id == client_iddiff --git a/posthog/api/github_callback/types.py b/posthog/api/github_callback/types.pyindex 1a581dc0811f..6427bca8e87b 100644--- a/posthog/api/github_callback/types.py+++ b/posthog/api/github_callback/types.py@@ -11,6 +11,8 @@ from django.conf import settingsfrom pydantic import AliasChoices, BaseModel, ConfigDict, Field, ValidationErrorfrom rest_framework.exceptions import ValidationError as ApiValidationError+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.models.instance_setting import get_instance_settings# Server-side state cache used by the personal GitHub linking flow (the install@@ -168,10 +170,11 @@ def github_app_install_url(state: str) -> str:def github_oauth_authorize_url(state: str) -> str:- if not settings.GITHUB_APP_CLIENT_ID:+ client_id = get_or_settings("GITHUB_APP_CLIENT_ID", CredentialsCaller.INTEGRATIONS)+ if not client_id:raise ApiValidationError("GitHub App client ID is not configured (GITHUB_APP_CLIENT_ID missing).")return "https://github.com/login/oauth/authorize?" + urlencode(- {"client_id": settings.GITHUB_APP_CLIENT_ID, "redirect_uri": github_oauth_redirect_uri(), "state": state}+ {"client_id": client_id, "redirect_uri": github_oauth_redirect_uri(), "state": state})diff --git a/posthog/api/test/test_integration.py b/posthog/api/test/test_integration.pyindex 2432bc9efda4..29033695d9c3 100644--- a/posthog/api/test/test_integration.py+++ b/posthog/api/test/test_integration.py@@ -46,7 +46,11 @@ from posthog.api.github_callback.team_services import (from posthog.api.github_callback.types import FlowKind, GitHubAuthorizeStatefrom posthog.api.integration import IntegrationSerializer, IntegrationViewSetfrom posthog.constants import AvailableFeature-from posthog.credentials_service.errors import CredentialsServiceUnreachableError, SecretInRecoveryError+from posthog.credentials_service.errors import (+ CredentialsServiceFailure,+ CredentialsServiceUnreachableError,+ SecretInRecoveryError,+)from posthog.egress.github.transport import GitHubEgressBudgetExhaustedfrom posthog.egress.limiter.policies import Priorityfrom posthog.models.activity_logging.activity_log import ActivityLog, apply_activity_visibility_restrictions@@ -5840,9 +5844,12 @@ class TestStripeIntegrationOAuthTokens:)return integration, access_token, refresh_token+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_destroy_oauth_tokens_deletes_tokens(self, mock_settings):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_destroy_oauth_tokens_deletes_tokens(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idintegration, access_token, refresh_token = self._create_integration_with_tokens()unlinked_refresh_token = OAuthRefreshToken.objects.create(@@ -5862,9 +5869,12 @@ class TestStripeIntegrationOAuthTokens:assert not OAuthRefreshToken.objects.filter(pk=unlinked_refresh_token.pk).exists()@pytest.mark.parametrize("other_credential", ["other_team_only", "shared_with_this_team"])+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_destroy_oauth_tokens_only_affects_same_team(self, mock_settings, other_credential):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_destroy_oauth_tokens_only_affects_same_team(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock, other_credential: str+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idintegration, _, _ = self._create_integration_with_tokens()@@ -5897,9 +5907,12 @@ class TestStripeIntegrationOAuthTokens:assert other_access_token.scoped_teams == [other_team.pk]assert other_refresh_token.scoped_teams == [other_team.pk]+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_destroy_oauth_tokens_noop_when_no_oauth_app(self, mock_settings):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = None+ def test_destroy_oauth_tokens_noop_when_no_oauth_app(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock+ ) -> None:+ mock_get_or_settings.return_value = ""mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = Noneintegration, access_token, refresh_token = self._create_integration_with_tokens()stripe_int = StripeIntegration(integration)@@ -5912,10 +5925,15 @@ class TestStripeIntegrationOAuthTokens:@pytest.mark.parametrize("marketplace_setting", ["configured", "unset", "equal_to_orchestrator"])@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")def test_marketplace_token_never_mints_on_the_orchestrator_application(- self, mock_settings, MockStripeClient, marketplace_setting- ):+ self,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ marketplace_setting: str,+ ) -> None:orchestrator_app = OAuthApplication.objects.create(name="Stripe orchestrator",client_id="orchestrator_client_id",@@ -5924,7 +5942,7 @@ class TestStripeIntegrationOAuthTokens:redirect_uris="https://example.com/callback",algorithm="RS256",)- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = orchestrator_app.client_id+ mock_get_or_settings.return_value = orchestrator_app.client_idmock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = {"configured": self.oauth_app.client_id,"unset": "",@@ -5953,8 +5971,11 @@ class TestStripeIntegrationOAuthTokens:assert not minted.exists()@patch("posthog.models.integration.stripe.lock_oauth_connection")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_destroy_oauth_tokens_spans_the_pre_split_application(self, mock_settings, mock_lock):+ def test_destroy_oauth_tokens_spans_the_pre_split_application(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock, mock_lock: MagicMock+ ) -> None:marketplace_app = OAuthApplication.objects.create(name="Stripe marketplace",client_id="marketplace_client_id",@@ -5963,7 +5984,7 @@ class TestStripeIntegrationOAuthTokens:redirect_uris="https://example.com/callback",algorithm="RS256",)- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = self.oauth_app.client_id+ mock_get_or_settings.return_value = self.oauth_app.client_idmock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = marketplace_app.client_idintegration, legacy_access, legacy_refresh = self._create_integration_with_tokens()@@ -5992,9 +6013,12 @@ class TestStripeIntegrationOAuthTokens:@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_write_posthog_secrets_uses_account_scope(self, mock_settings, MockStripeClient):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_write_posthog_secrets_uses_account_scope(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock, MockStripeClient: MagicMock+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idmock_client = MagicMock()MockStripeClient.return_value = mock_client@@ -6022,9 +6046,12 @@ class TestStripeIntegrationOAuthTokens:@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_write_posthog_secrets_mints_read_only_token(self, mock_settings, MockStripeClient):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_write_posthog_secrets_mints_read_only_token(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock, MockStripeClient: MagicMock+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idMockStripeClient.return_value = MagicMock()@@ -6053,9 +6080,12 @@ class TestStripeIntegrationOAuthTokens:@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_clear_posthog_secrets_uses_account_scope(self, mock_settings, MockStripeClient):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = None+ def test_clear_posthog_secrets_uses_account_scope(+ self, mock_settings: MagicMock, mock_get_or_settings: MagicMock, MockStripeClient: MagicMock+ ) -> None:+ mock_get_or_settings.return_value = ""mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = Nonemock_client = MagicMock()MockStripeClient.return_value = mock_client@@ -6085,9 +6115,17 @@ class TestStripeIntegrationOAuthTokens:)@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_live")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_stripe_client_uses_live_secret(self, _name, method_name, mock_settings, MockStripeClient):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_stripe_client_uses_live_secret(+ self,+ _name: str,+ method_name: str,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idMockStripeClient.return_value = MagicMock()@@ -6110,9 +6148,16 @@ class TestStripeIntegrationOAuthTokens:@override_settings(STRIPE_APP_CLIENT_ID="", STRIPE_APP_SECRET_KEY="")@patch("posthog.models.integration.stripe.capture_exception")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")- def test_write_posthog_secrets_skips_when_keys_missing(self, mock_settings, MockStripeClient, mock_capture):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ def test_write_posthog_secrets_skips_when_keys_missing(+ self,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ mock_capture: MagicMock,+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idMockStripeClient.return_value = MagicMock()@@ -6143,11 +6188,17 @@ class TestStripeIntegrationOAuthTokens:)@override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")def test_write_posthog_secrets_keeps_the_credential_only_when_a_secret_reaches_stripe(- self, mock_settings, MockStripeClient, failing_secrets, credential_kept- ):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ self,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ failing_secrets: tuple[str, ...],+ credential_kept: bool,+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_iddef create_secret(params, options):@@ -6173,11 +6224,16 @@ class TestStripeIntegrationOAuthTokens:@override_settings(STRIPE_APP_CLIENT_ID="", STRIPE_APP_SECRET_KEY="")@patch("posthog.models.integration.stripe.capture_exception")@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")def test_clear_posthog_secrets_skips_and_revokes_tokens_when_keys_missing(- self, mock_settings, MockStripeClient, mock_capture- ):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ self,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ mock_capture: MagicMock,+ ) -> None:+ mock_get_or_settings.return_value = "orchestrator_client_id"mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_idMockStripeClient.return_value = MagicMock()@@ -6198,12 +6254,19 @@ class TestStripeIntegrationOAuthTokens:])@patch("stripe.StripeClient")+ @patch("posthog.models.integration.stripe.get_or_settings")@patch("posthog.models.integration.stripe.settings")def test_clear_posthog_secrets_revokes_tokens_when_credential_lookup_raises(- self, _name, error, mock_settings, MockStripeClient- ):- mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"+ self,+ _name: str,+ error: CredentialsServiceFailure,+ mock_settings: MagicMock,+ mock_get_or_settings: MagicMock,+ MockStripeClient: MagicMock,+ ) -> None:+ mock_get_or_settings.side_effect = errormock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id+ mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"integration, access_token, refresh_token = self._create_integration_with_tokens()diff --git a/posthog/api/test/test_preflight.py b/posthog/api/test/test_preflight.pyindex f01e292424bc..7e68a970813b 100644--- a/posthog/api/test/test_preflight.py+++ b/posthog/api/test/test_preflight.py@@ -1,22 +1,83 @@import osfrom datetime import datetime-from typing import cast+from functools import partial+from typing import Literal, castimport pytestfrom posthog.test.base import APIBaseTest, QueryMatchingTest, snapshot_postgres_queries-from unittest.mock import patch+from unittest.mock import Mock, patch-from django.test import override_settings+from django.test import SimpleTestCase, override_settingsfrom django.utils import timezonefrom parameterized import parameterizedfrom rest_framework import statusfrom posthog.cloud_utils import TEST_clear_instance_license_cache+from posthog.credentials_service.client import CredentialsServiceClient+from posthog.credentials_service.errors import (+ CredentialsServiceFailure,+ CredentialsServiceUnreachableError,+ SecretInRecoveryError,+ SecretMissingError,+)+from posthog.credentials_service.instance_settings import get_many_or_instance_settings+from posthog.credentials_service.settings_fallback import get_many_or_settingsfrom posthog.helpers.dev_login import is_dev_login_allowedfrom posthog.models.instance_setting import set_instance_setting+from posthog.models.integration import SlackIntegrationfrom posthog.models.organization import Organizationfrom posthog.models.organization_invite import OrganizationInvite+from posthog.views import _preflight_oauth_client_id+++class TestPreflightCredentials(SimpleTestCase):+ @parameterized.expand(+ [+ (f"{key}_{name}", key, result, expected)+ for key in ("HUBSPOT_APP_CLIENT_ID", "SALESFORCE_CONSUMER_KEY", "SLACK_APP_CLIENT_ID")+ for name, result, expected in (+ ("service_value", "service-id", "service-id"),+ ("missing", SecretMissingError(key), "old-id"),+ ("unreachable", CredentialsServiceUnreachableError("service unavailable"), "old-id"),+ ("recovery", SecretInRecoveryError(key), "old-id"),+ )+ ]+ )+ def test_public_client_id(+ self,+ _name: str,+ key: Literal["HUBSPOT_APP_CLIENT_ID", "SALESFORCE_CONSUMER_KEY", "SLACK_APP_CLIENT_ID"],+ result: str | CredentialsServiceFailure,+ expected: str,+ ) -> None:+ client = Mock(spec=CredentialsServiceClient)+ if isinstance(result, CredentialsServiceFailure):+ client.get.side_effect = result+ client.get_many.side_effect = result+ else:+ client.get.return_value = result+ client.get_many.return_value = {key: result}++ with (+ self.settings(HUBSPOT_APP_CLIENT_ID="old-id", SALESFORCE_CONSUMER_KEY="old-id"),+ patch.dict(os.environ, {key: ""}),+ patch("posthog.credentials_service.instance_settings.credentials_service_enabled", return_value=True),+ patch("posthog.credentials_service.instance_settings.get_instance_setting", return_value="old-id"),+ patch(+ "posthog.models.integration.slack.get_instance_settings",+ return_value={"SLACK_APP_CLIENT_ID": "old-id"},+ ),+ patch("posthog.views.get_many_or_settings", side_effect=partial(get_many_or_settings, client=client)),+ patch(+ "posthog.models.integration.slack.get_many_or_instance_settings",+ side_effect=partial(get_many_or_instance_settings, client=client),+ ),+ ):+ actual = (+ SlackIntegration.slack_client_id() if key == "SLACK_APP_CLIENT_ID" else _preflight_oauth_client_id(key)+ )+ assert actual == expectedclass TestPreflight(APIBaseTest, QueryMatchingTest):diff --git a/posthog/api/test/test_user_integration.py b/posthog/api/test/test_user_integration.pyindex 8c29b7adbdf2..d9240429e186 100644--- a/posthog/api/test/test_user_integration.py+++ b/posthog/api/test/test_user_integration.py@@ -16,6 +16,7 @@ from django.utils import timezoneimport requestsfrom parameterized import parameterizedfrom rest_framework import status+from rest_framework.exceptions import ValidationErrorfrom rest_framework.test import APIClientfrom structlog.testing import capture_logs@@ -25,8 +26,9 @@ from posthog.api.github_callback.state import (parse_github_authorize_state_param,store_unified_authorize_state,)-from posthog.api.github_callback.types import FlowKind, GitHubAuthorizeState+from posthog.api.github_callback.types import FlowKind, GitHubAuthorizeState, github_oauth_authorize_urlfrom posthog.api.user_integration import _has_unlinked_github_installations+from posthog.credentials_service.callers import CredentialsCallerfrom posthog.egress.limiter.policies import Priorityfrom posthog.models import Organization, OrganizationMembership, Team, Userfrom posthog.models.integration import (@@ -1497,6 +1499,33 @@ class TestUserGitHubIntegrationFromInstallation(APIBaseTest):self.assertEqual(mock_report.call_count, 0)+class TestGithubOAuthAuthorizeUrl(SimpleTestCase):+ @parameterized.expand(+ [+ ("service_only", "", "service-id"),+ ("service_overrides_settings", "old-id", "service-id"),+ ("missing", "", ""),+ ]+ )+ def test_client_id(self, _name: str, setting_value: str, service_value: str) -> None:+ with (+ self.settings(GITHUB_APP_CLIENT_ID=setting_value),+ patch("posthog.api.github_callback.types.get_or_settings", return_value=service_value) as get_client_id,+ ):+ if service_value:+ query = parse_qs(urlparse(github_oauth_authorize_url("test-state")).query)+ assert query["client_id"] == [service_value]+ assert query["state"] == ["test-state"]+ else:+ with self.assertRaises(ValidationError) as raised:+ github_oauth_authorize_url("test-state")+ assert raised.exception.detail == [+ "GitHub App client ID is not configured (GITHUB_APP_CLIENT_ID missing)."+ ]++ get_client_id.assert_called_once_with("GITHUB_APP_CLIENT_ID", CredentialsCaller.INTEGRATIONS)++class TestGithubUserFromCode(SimpleTestCase):@patch("posthog.egress.transport.transport.requests.request")@patch("posthog.models.integration.github.requests.post")diff --git a/posthog/credentials_service/callers.py b/posthog/credentials_service/callers.pyindex 746584021dfa..57ab07d9f785 100644--- a/posthog/credentials_service/callers.py+++ b/posthog/credentials_service/callers.py@@ -20,6 +20,8 @@ class CredentialsCaller(StrEnum):"""WAREHOUSE_SOURCES = "warehouse-sources"+ # Batch exports use platform credentials to authenticate with export destinations.+ BATCH_EXPORTS = "batch-exports"CDP = "cdp"MESSAGING = "messaging"TASKS = "tasks"diff --git a/posthog/management/commands/flag_legacy_oauth_integrations.py b/posthog/management/commands/flag_legacy_oauth_integrations.pyindex 4bb3981629d2..4bdbdadf214a 100644--- a/posthog/management/commands/flag_legacy_oauth_integrations.py+++ b/posthog/management/commands/flag_legacy_oauth_integrations.py@@ -1,9 +1,10 @@-from django.conf import settingsfrom django.core.management.base import BaseCommand, CommandErrorfrom django.db.models import F, Func, JSONField, Valueimport structlog+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.models.integration import CONFIG_LEGACY_OAUTH_CLIENT, Integration, issuing_oauth_client_idslogger = structlog.get_logger(__name__)@@ -48,7 +49,7 @@ class Command(BaseCommand):kind = options["kind"]dry_run = options["dry_run"]- current_client_id = getattr(settings, CURRENT_CLIENT_ID_SETTING[kind], "")+ current_client_id = get_or_settings(CURRENT_CLIENT_ID_SETTING[kind], CredentialsCaller.INTEGRATIONS)if not current_client_id:raise CommandError(f"{CURRENT_CLIENT_ID_SETTING[kind]} is not set, so nothing can be compared against it")diff --git a/posthog/management/commands/generate_stripe_app_tokens.py b/posthog/management/commands/generate_stripe_app_tokens.pyindex 64e5649230e8..05d8ad0bb426 100644--- a/posthog/management/commands/generate_stripe_app_tokens.py+++ b/posthog/management/commands/generate_stripe_app_tokens.py@@ -20,6 +20,8 @@ from django.utils import timezone as tzfrom oauth2_provider.generators import generate_client_secretfrom oauthlib.common import generate_token+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.models import Team, Userfrom posthog.models.integration import StripeIntegrationfrom posthog.models.oauth import OAuthAccessToken, OAuthApplication, OAuthRefreshToken@@ -149,10 +151,10 @@ class Command(BaseCommand):self.stdout.write("")def _resolve_client_id(self) -> str | None:- """Resolve the OAuth client ID from the Django setting or directly from .env."""- # The Django setting is populated if the env var was in the shell environment at startup- if settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID:- return settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID+ """Read the OAuth client ID from the service, settings, or .env, in that order."""+ client_id = get_or_settings(ENV_KEY, CredentialsCaller.INTEGRATIONS)+ if client_id:+ return client_id# Fall back to reading .env directly — manage.py doesn't load .env into the# process environment, so the setting may be empty even though .env has the valuediff --git a/posthog/models/integration/slack.py b/posthog/models/integration/slack.pyindex f51d54a2dd55..3273a167dcf2 100644--- a/posthog/models/integration/slack.py+++ b/posthog/models/integration/slack.py@@ -3,7 +3,7 @@import timefrom collections.abc import Iterablefrom datetime import timedelta-from typing import TYPE_CHECKING, Literal, Optional+from typing import TYPE_CHECKING, Literal, Optional, castfrom posthog.dataclasses import frozen@@ -21,7 +21,10 @@ from slack_sdk.errors import SlackApiErrorfrom posthog.cache_utils import cache_forfrom posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.client import credentials_service_enabled+from posthog.credentials_service.errors import CredentialsServiceFailurefrom posthog.credentials_service.instance_settings import get_many_or_instance_settings+from posthog.credentials_service.settings_fallback import get_many_or_settingsfrom posthog.egress.slack.client import SlackWebClient as WebClientfrom posthog.ingress.slack.provider import build_slack_signature_schemefrom posthog.ingress.verify.schemes import VerificationOutcome, hmac_sha256_signature@@ -413,17 +416,35 @@ class SlackIntegration:@classmethod@cache_for(timedelta(minutes=5))- def slack_client_id(cls) -> str:+ def slack_client_id(cls, instance_setting: str | None = None) -> str:"""The app's public client id, for preflight.- Preflight runs on every page load. It reads the instance setting and never the credentials- service, so an outage of the service cannot break page loads for a value that is not secret.+ Use the instance setting if the credentials service fails, so page loads still work.+ Callers that resolve credentials in a worker can pass the instance setting read by the+ request thread, keeping database access out of that worker."""# Span only fires on cache miss (cache_for is process-local in-memory).# If preflight.slack_config_main is fast in production traces, this span- # will be absent; if it appears, it tells us the DB hit was slow.+ # will be absent; if it appears, it tells us the credential lookup was slow.with tracer.start_as_current_span("slack_integration.slack_client_id_db"):- return get_instance_settings(["SLACK_APP_CLIENT_ID"])["SLACK_APP_CLIENT_ID"] or ""+ if instance_setting is not None and not credentials_service_enabled():+ return instance_setting++ try:+ if instance_setting is not None:+ return get_many_or_settings(+ ["SLACK_APP_CLIENT_ID"],+ CredentialsCaller.INTEGRATIONS,+ settings_lookup=lambda _key: instance_setting or None,+ )["SLACK_APP_CLIENT_ID"]+ return get_many_or_instance_settings(["SLACK_APP_CLIENT_ID"], CredentialsCaller.INTEGRATIONS)[+ "SLACK_APP_CLIENT_ID"+ ]+ except CredentialsServiceFailure:+ logger.warning("slack_integration.credentials_service_unavailable", key="SLACK_APP_CLIENT_ID")+ if instance_setting is not None:+ return instance_setting+ return cast(str, get_instance_settings(["SLACK_APP_CLIENT_ID"])["SLACK_APP_CLIENT_ID"] or "")@frozendiff --git a/posthog/models/integration/stripe.py b/posthog/models/integration/stripe.pyindex b7de1224a967..bb849e094379 100644--- a/posthog/models/integration/stripe.py+++ b/posthog/models/integration/stripe.py@@ -12,6 +12,9 @@ from django.utils import timezoneimport structlog+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.errors import CredentialsServiceFailure+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.dataclasses import frozenfrom posthog.exceptions_capture import capture_exceptionfrom posthog.models.oauth import OAuthAccessToken, OAuthApplication, OAuthRefreshToken, lock_oauth_connection@@ -261,7 +264,9 @@ class StripeIntegration:Returns None when the setting is unset or names the orchestrator, rather than minting there."""client_id = settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID- if not client_id or client_id == settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID:+ if not client_id or client_id == get_or_settings(+ "STRIPE_POSTHOG_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS+ ):# Minting on the orchestrator's application is the vulnerability, whether the setting# is unset or mapped to the orchestrator's own id, so both fail closed: a new install# gets no credential rather than a privileged one.@@ -282,9 +287,14 @@ class StripeIntegration:Revocation has to span both or tokens issued before the application split survive it."""- client_ids = [- cid for cid in (settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID, settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID) if cid- ]+ try:+ orchestrator_client_id = get_or_settings("STRIPE_POSTHOG_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS)+ except CredentialsServiceFailure:+ # This value is only used to find and delete local tokens. Falling back during an+ # outage (or recovery) cannot expose the credential and must not block revocation.+ orchestrator_client_id = settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID++ client_ids = [cid for cid in (settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID, orchestrator_client_id) if cid]if not client_ids:return []return list(OAuthApplication.objects.filter(client_id__in=client_ids))diff --git a/posthog/views.py b/posthog/views.pyindex c9ba26fcc746..0708a6796419 100644--- a/posthog/views.py+++ b/posthog/views.py@@ -1,9 +1,10 @@import os+from concurrent.futures import ThreadPoolExecutorfrom dataclasses import dataclassfrom datetime import timedeltafrom functools import wrapsfrom html import escape-from typing import Union+from typing import Literal, Union, castfrom urllib.parse import parse_qs, urlencode, urlparse, urlunparsefrom django.apps import apps@@ -28,13 +29,18 @@ from prometheus_client import REGISTRY, CollectorRegistry, generate_latest, multfrom posthog.api.secret_revocation import NON_PERSONAL_SECRET_PREFIXESfrom posthog.auth import AUTH_BRAND_COOKIE, apply_auth_brand_cookie, normalize_auth_brand+from posthog.cache_utils import cache_forfrom posthog.cloud_utils import is_cloud+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.errors import CredentialsServiceFailure+from posthog.credentials_service.settings_fallback import get_many_or_settingsfrom posthog.email import is_email_availablefrom posthog.exceptions_capture import capture_exceptionfrom posthog.health import is_clickhouse_connected, is_kafka_connectedfrom posthog.helpers.dev_login import is_dev_login_allowedfrom posthog.models import Organization, Userfrom posthog.models.activity_logging.activity_log import Detail, log_activity+from posthog.models.instance_setting import get_instance_settingsfrom posthog.models.integration import SlackIntegrationfrom posthog.models.oauth import find_oauth_access_token, find_oauth_refresh_tokenfrom posthog.models.personal_api_key import find_personal_api_key@@ -180,12 +186,46 @@ def render_query(request: HttpRequest) -> HttpResponse:return render_template("render_query.html", request, context={"render_query_payload": payload})+PREFLIGHT_OAUTH_CLIENT_ID_KEYS = ["HUBSPOT_APP_CLIENT_ID", "SALESFORCE_CONSUMER_KEY"]+++@cache_for(timedelta(minutes=5))+def _preflight_oauth_client_ids() -> dict[str, str]:+ try:+ return get_many_or_settings(PREFLIGHT_OAUTH_CLIENT_ID_KEYS, CredentialsCaller.INTEGRATIONS)+ except CredentialsServiceFailure:+ logger.warning("preflight.credentials_service_unavailable", keys=PREFLIGHT_OAUTH_CLIENT_ID_KEYS)+ return {key: cast(str, getattr(settings, key)) for key in PREFLIGHT_OAUTH_CLIENT_ID_KEYS}+++def _preflight_oauth_client_id(key: Literal["HUBSPOT_APP_CLIENT_ID", "SALESFORCE_CONSUMER_KEY"]) -> str:+ return _preflight_oauth_client_ids()[key]+++@cache_for(timedelta(minutes=5))+def _preflight_slack_instance_client_id() -> str:+ return cast(str, get_instance_settings(["SLACK_APP_CLIENT_ID"])["SLACK_APP_CLIENT_ID"] or "")+++def _preflight_slack_client_id(instance_setting: str) -> str:+ with tracer.start_as_current_span("preflight.slack_config_main"):+ return SlackIntegration.slack_client_id(instance_setting)++@never_cachedef preflight_check(request: HttpRequest) -> JsonResponse:- with tracer.start_as_current_span("preflight.slack_config_main"):- slack_client_id = SlackIntegration.slack_client_id()- hubspot_client_id = settings.HUBSPOT_APP_CLIENT_ID- salesforce_client_id = settings.SALESFORCE_CONSUMER_KEY+ # Read the database-backed fallback on the request thread, then resolve independent credential+ # groups concurrently so a service timeout costs at most one request timeout. HubSpot and+ # Salesforce share one batch request.+ slack_instance_client_id = _preflight_slack_instance_client_id()+ with ThreadPoolExecutor(max_workers=2, thread_name_prefix="preflight-credentials") as executor:+ slack_client_id_future = executor.submit(_preflight_slack_client_id, slack_instance_client_id)+ oauth_client_ids_future = executor.submit(_preflight_oauth_client_ids)+ slack_client_id = slack_client_id_future.result()+ oauth_client_ids = oauth_client_ids_future.result()++ hubspot_client_id = oauth_client_ids["HUBSPOT_APP_CLIENT_ID"]+ salesforce_client_id = oauth_client_ids["SALESFORCE_CONSUMER_KEY"]in_cloud = is_cloud()diff --git a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.pyindex 29ce56be5b20..adfad2392378 100644--- a/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py+++ b/products/batch_exports/backend/temporal/destinations/bigquery_batch_export.py@@ -39,6 +39,8 @@ from structlog.contextvars import bind_contextvarsfrom temporalio import activity, exceptions, workflowfrom temporalio.common import RetryPolicy+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.settings_fallback import get_or_settingsfrom posthog.models.integration import GoogleCloudServiceAccountIntegration, Integrationfrom posthog.models.integration.google_cloud import InvalidGoogleTokenUriError, require_google_token_urifrom posthog.models.team import Team@@ -441,7 +443,7 @@ def get_our_google_cloud_credentials() -> google.auth.impersonated_credentials.Caws_security_credentials_supplier=Boto3CredentialsSupplier(),scopes=["https://www.googleapis.com/auth/cloud-platform"],),- target_principal=settings.BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT,+ target_principal=get_or_settings("BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT", CredentialsCaller.BATCH_EXPORTS),target_scopes=["https://www.googleapis.com/auth/cloud-platform"],lifetime=3600,)@@ -458,7 +460,7 @@ class GoogleCloudCredentialsError(Exception):async def ensure_our_google_cloud_credentials_are_valid():"""Raise `GoogleCloudCredentialsError` if we cannot refresh our credentials."""- our_credentials = get_our_google_cloud_credentials()+ our_credentials = await asyncio.to_thread(get_our_google_cloud_credentials)session = _make_requests_session()try:await asyncio.to_thread(our_credentials.refresh, google.auth.transport.requests.Request(session=session))@@ -496,7 +498,7 @@ async def get_service_account_description(Uses our credentials to authenticate."""- our_credentials = get_our_google_cloud_credentials()+ our_credentials = await asyncio.to_thread(get_our_google_cloud_credentials)client = iam_admin_v1.IAMAsyncClient(credentials=our_credentials)retryable_get_service_account = make_retryable_with_exponential_backoff(diff --git a/products/mcp_store/backend/oauth_credentials.py b/products/mcp_store/backend/oauth_credentials.pyindex 80aae7ace3ea..21737fc6e699 100644--- a/products/mcp_store/backend/oauth_credentials.py+++ b/products/mcp_store/backend/oauth_credentials.py@@ -3,7 +3,8 @@ from typing import Literalfrom django.conf import settings-from posthog.models.instance_setting import get_instance_settings+from posthog.credentials_service.callers import CredentialsCaller+from posthog.credentials_service.instance_settings import get_many_or_instance_settingsOAuthCredentialsSource = Literal["slack_app", "slack_dev_app"]@@ -40,9 +41,11 @@ def resolve_oauth_credentials_source(source: str) -> dict[str, str]:raise ValueError(f"Unknown OAuth credential source: {source}")prefix = "SLACK_DEV_APP" if source == "slack_dev_app" else "SLACK_APP"- instance_settings = get_instance_settings([f"{prefix}_CLIENT_ID", f"{prefix}_CLIENT_SECRET"])- client_id = instance_settings.get(f"{prefix}_CLIENT_ID")- client_secret = instance_settings.get(f"{prefix}_CLIENT_SECRET")+ credentials = get_many_or_instance_settings(+ [f"{prefix}_CLIENT_ID", f"{prefix}_CLIENT_SECRET"], CredentialsCaller.INTEGRATIONS+ )+ client_id = credentials.get(f"{prefix}_CLIENT_ID")+ client_secret = credentials.get(f"{prefix}_CLIENT_SECRET")return {"client_id": client_id if isinstance(client_id, str) else "","client_secret": client_secret if isinstance(client_secret, str) else "",diff --git a/products/mcp_store/backend/test/test_catalog_sync.py b/products/mcp_store/backend/test/test_catalog_sync.pyindex 222f746b7dbe..fff4e2027b93 100644--- a/products/mcp_store/backend/test/test_catalog_sync.py+++ b/products/mcp_store/backend/test/test_catalog_sync.py@@ -6,6 +6,8 @@ from django.test import SimpleTestCase, TestCasefrom parameterized import parameterized+from posthog.credentials_service.callers import CredentialsCaller+from products.mcp_store.backend.catalog import MCP_SERVER_CATALOG, CatalogEntryfrom products.mcp_store.backend.catalog_sync import sync_mcp_catalogfrom products.mcp_store.backend.models import (@@ -83,7 +85,7 @@ class TestCatalogEntries(SimpleTestCase):class TestSyncMCPCatalog(TestCase):- @patch("products.mcp_store.backend.oauth_credentials.get_instance_settings")+ @patch("products.mcp_store.backend.oauth_credentials.get_many_or_instance_settings")@patch("products.mcp_store.backend.catalog_sync.probe_mcp_server")def test_slack_dev_uses_separate_credentials_and_resets_after_disable(self, probe, instance_settings):entry = next(entry for entry in MCP_SERVER_CATALOG if entry.oauth_credentials_source == "slack_app")@@ -116,7 +118,9 @@ class TestSyncMCPCatalog(TestCase):assert MCPServerTemplate.available_for_team(42).filter(id=template.id).exists()assert not MCPServerTemplate.available_for_team(43).filter(id=template.id).exists()- instance_settings.assert_called_with(["SLACK_DEV_APP_CLIENT_ID", "SLACK_DEV_APP_CLIENT_SECRET"])+ instance_settings.assert_called_with(+ ["SLACK_DEV_APP_CLIENT_ID", "SLACK_DEV_APP_CLIENT_SECRET"], CredentialsCaller.INTEGRATIONS+ )probe.assert_called_once_with(entry.url, scope_allowlist=expected_scopes, shared_client_id="dev-client")with self.settings(MCP_STORE_SLACK_DEV_ALLOWED_TEAM_IDS=[]):@@ -181,7 +185,7 @@ class TestSyncMCPCatalog(TestCase):assert template.oauth_issuer_url == probe_result.oauth_metadata.get("issuer", "")@patch(- "products.mcp_store.backend.oauth_credentials.get_instance_settings",+ "products.mcp_store.backend.oauth_credentials.get_many_or_instance_settings",return_value={"SLACK_APP_CLIENT_ID": "slack-client", "SLACK_APP_CLIENT_SECRET": "slack-secret"},)def test_existing_shared_oauth_entry_activates_from_instance_credentials(self, _settings):@@ -222,7 +226,7 @@ class TestSyncMCPCatalog(TestCase):probe.assert_called_once_with(template.url, scope_allowlist=None, shared_client_id="slack-client")@patch(- "products.mcp_store.backend.oauth_credentials.get_instance_settings",+ "products.mcp_store.backend.oauth_credentials.get_many_or_instance_settings",return_value={"SLACK_APP_CLIENT_ID": "", "SLACK_APP_CLIENT_SECRET": ""},)def test_instance_credential_source_deactivates_when_settings_are_missing(self, _settings):@@ -333,7 +337,7 @@ class TestSyncMCPCatalog(TestCase):assert MCPServerTemplate.objects.get(url=_entry().url).is_active is False@patch(- "products.mcp_store.backend.oauth_credentials.get_instance_settings",+ "products.mcp_store.backend.oauth_credentials.get_many_or_instance_settings",return_value={"SLACK_APP_CLIENT_ID": "slack-client", "SLACK_APP_CLIENT_SECRET": "slack-secret"},)def test_disabled_entry_remains_inactive_across_syncs(self, _settings):diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.pyindex efe74cf69507..51cca63e0740 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/bigquery/bigquery.py@@ -79,6 +79,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.helpersincremental_type_to_operator,)from products.warehouse_sources.backend.temporal.data_imports.sources.common.base import UNVERSIONED_API_VERSION+from products.warehouse_sources.backend.temporal.data_imports.sources.common.credentials_service import get_secretfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.grpc import make_tracked_channelfrom products.warehouse_sources.backend.temporal.data_imports.sources.common.http import (DEFAULT_RETRY,@@ -681,7 +682,7 @@ def _impersonated_credentials(service_account_email: str, team_id: int) -> googlverify_impersonated_service_account_ownership,)- if not settings.BATCH_EXPORT_BIGQUERY_STS_AUDIENCE_FIELD or not settings.BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT:+ if not settings.BATCH_EXPORT_BIGQUERY_STS_AUDIENCE_FIELD or not get_secret("BATCH_EXPORT_BIGQUERY_SERVICE_ACCOUNT"):raise BigQueryAuthResolutionError(BIGQUERY_IMPERSONATION_UNAVAILABLE_ERROR)# The ownership check below authenticates with PostHog's own credentials. If this worker cannot