Skip to content

: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.

feat(credentials-service): read the last integration credentials via the service (#122589)

Tom Owersa24e9607ce8e
diff --git a/ee/partners/stripe/api/provisioning/signature.py b/ee/partners/stripe/api/provisioning/signature.py
index 36a57607ca08..d4669015e21e 100644
--- a/ee/partners/stripe/api/provisioning/signature.py
+++ b/ee/partners/stripe/api/provisioning/signature.py
@@ -20,7 +20,6 @@ explicit with ``if error := verify_...(request): return error``.
from __future__ import annotations
-from django.conf import settings
from django.http.request import RawPostDataException
import structlog
@@ -28,6 +27,7 @@ from rest_framework.request import Request
from rest_framework.response import Response
from posthog.exceptions_capture import capture_exception
+from posthog.models.integration.stripe import stripe_signing_secret
from ee.partners.stripe.api.provisioning.analytics import capture_signature_event
from ee.partners.stripe.api.provisioning.constants import MAX_TIMESTAMP_DRIFT_SECONDS, SUPPORTED_VERSIONS
@@ -68,7 +68,7 @@ def verify_stripe_signature(request: Request) -> Response | None:
"""
endpoint = request.path
- secret = settings.STRIPE_SIGNING_SECRET
+ secret = stripe_signing_secret()
if not secret:
_log_and_capture_event("server_error", 500, endpoint)
return Response({"error": {"code": "server_error", "message": "Signing secret not configured"}}, status=500)
diff --git a/posthog/api/integration.py b/posthog/api/integration.py
index e0143d0270c0..1d836e2ae4fe 100644
--- a/posthog/api/integration.py
+++ b/posthog/api/integration.py
@@ -109,6 +109,7 @@ from posthog.models.integration import (
resolve_aliased_oauth_kind,
)
from posthog.models.integration.github_audit import GitHubAudit
+from posthog.models.integration.stripe import stripe_signing_secret
from posthog.models.integration.twitter_ads import TwitterAdsIntegration
from posthog.models.user_integration import UserIntegration
from posthog.permissions import (
@@ -230,7 +231,8 @@ def _verify_stripe_install_signature(state: str, user_id: str, account_id: str,
See: https://docs.stripe.com/stripe-apps/install-links-oauth
"""
- if not install_signature or not settings.STRIPE_SIGNING_SECRET:
+ signing_secret = stripe_signing_secret()
+ if not install_signature or not signing_secret:
return False
import stripe # noqa: PLC0415
@@ -241,7 +243,7 @@ def _verify_stripe_install_signature(state: str, user_id: str, account_id: str,
)
try:
# 300s tolerance matches the Stripe provisioning check at ee/partners/stripe/api/provisioning/signature.py.
- stripe.WebhookSignature.verify_header(payload, install_signature, settings.STRIPE_SIGNING_SECRET, tolerance=300)
+ stripe.WebhookSignature.verify_header(payload, install_signature, signing_secret, tolerance=300)
return True
except stripe.SignatureVerificationError:
return False
diff --git a/posthog/api/test/test_integration.py b/posthog/api/test/test_integration.py
index 83f6e42c2798..eeb70580a3d9 100644
--- a/posthog/api/test/test_integration.py
+++ b/posthog/api/test/test_integration.py
@@ -2,6 +2,7 @@ import hmac
import json
import time
import hashlib
+from collections.abc import Callable
from contextlib import nullcontext
from datetime import timedelta
from typing import Any, cast
@@ -5803,6 +5804,13 @@ class TestOauthIntegrationRevokeOnDisconnect:
mock_post.assert_not_called()
+def _stripe_app_ids(orchestrator: str, settings_mock: MagicMock) -> Callable[[str, object], str]:
+ def resolve(key: str, _caller: object) -> str:
+ return orchestrator if key == "STRIPE_POSTHOG_OAUTH_CLIENT_ID" else getattr(settings_mock, key)
+
+ return resolve
+
+
class TestStripeIntegrationOAuthTokens:
@pytest.fixture(autouse=True)
def setup(self, db):
@@ -5849,7 +5857,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
integration, access_token, refresh_token = self._create_integration_with_tokens()
unlinked_refresh_token = OAuthRefreshToken.objects.create(
@@ -5874,7 +5882,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
integration, _, _ = self._create_integration_with_tokens()
@@ -5912,7 +5920,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = None
integration, access_token, refresh_token = self._create_integration_with_tokens()
stripe_int = StripeIntegration(integration)
@@ -5942,7 +5950,7 @@ class TestStripeIntegrationOAuthTokens:
redirect_uris="https://example.com/callback",
algorithm="RS256",
)
- mock_get_or_settings.return_value = orchestrator_app.client_id
+ mock_get_or_settings.side_effect = _stripe_app_ids(orchestrator_app.client_id, mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = {
"configured": self.oauth_app.client_id,
"unset": "",
@@ -5984,7 +5992,7 @@ class TestStripeIntegrationOAuthTokens:
redirect_uris="https://example.com/callback",
algorithm="RS256",
)
- mock_get_or_settings.return_value = self.oauth_app.client_id
+ mock_get_or_settings.side_effect = _stripe_app_ids(self.oauth_app.client_id, mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = marketplace_app.client_id
integration, legacy_access, legacy_refresh = self._create_integration_with_tokens()
@@ -6018,7 +6026,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
mock_client = MagicMock()
MockStripeClient.return_value = mock_client
@@ -6051,7 +6059,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
MockStripeClient.return_value = MagicMock()
@@ -6085,7 +6093,7 @@ class TestStripeIntegrationOAuthTokens:
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_get_or_settings.side_effect = _stripe_app_ids("", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = None
mock_client = MagicMock()
MockStripeClient.return_value = mock_client
@@ -6125,7 +6133,7 @@ class TestStripeIntegrationOAuthTokens:
mock_get_or_settings: MagicMock,
MockStripeClient: MagicMock,
) -> None:
- mock_get_or_settings.return_value = "orchestrator_client_id"
+ mock_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
MockStripeClient.return_value = MagicMock()
@@ -6157,7 +6165,7 @@ class TestStripeIntegrationOAuthTokens:
MockStripeClient: MagicMock,
mock_capture: MagicMock,
) -> None:
- mock_get_or_settings.return_value = "orchestrator_client_id"
+ mock_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
MockStripeClient.return_value = MagicMock()
@@ -6198,7 +6206,7 @@ class TestStripeIntegrationOAuthTokens:
failing_secrets: tuple[str, ...],
credential_kept: bool,
) -> None:
- mock_get_or_settings.return_value = "orchestrator_client_id"
+ mock_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
def create_secret(params, options):
@@ -6233,7 +6241,7 @@ class TestStripeIntegrationOAuthTokens:
MockStripeClient: MagicMock,
mock_capture: MagicMock,
) -> None:
- mock_get_or_settings.return_value = "orchestrator_client_id"
+ mock_get_or_settings.side_effect = _stripe_app_ids("orchestrator_client_id", mock_settings)
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
MockStripeClient.return_value = MagicMock()
diff --git a/posthog/models/integration/aws.py b/posthog/models/integration/aws.py
index 6978078a7273..92092e958016 100644
--- a/posthog/models/integration/aws.py
+++ b/posthog/models/integration/aws.py
@@ -6,13 +6,14 @@ import secrets
from collections.abc import Mapping
from typing import Any, ClassVar, Literal
-from django.conf import settings
from django.db import transaction
import structlog
from rest_framework.exceptions import ValidationError
from posthog.credentials import AWSKeyPair
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_or_settings
from posthog.models.user import User
from posthog.models.utils import IntegrityError
from posthog.security.url_validation import is_url_allowed
@@ -295,7 +296,7 @@ class AWSRoleBasedIntegration:
# TODO: Make our_aws_role_arn external_id configurable.
validate_aws_role_arn(
aws_role_arn=aws_role_arn,
- our_aws_role_arn=settings.BATCH_EXPORT_S3_EXTERNAL_ROLE_ARN,
+ our_aws_role_arn=get_or_settings("BATCH_EXPORT_S3_EXTERNAL_ROLE_ARN", CredentialsCaller.INTEGRATIONS),
external_id=f"posthog-{organization_id}",
)
diff --git a/posthog/models/integration/stripe.py b/posthog/models/integration/stripe.py
index bb849e094379..5160a5c7e141 100644
--- a/posthog/models/integration/stripe.py
+++ b/posthog/models/integration/stripe.py
@@ -38,6 +38,22 @@ STRIPE_POSTHOG_SECRET_NAMES = (
)
+def stripe_signing_secret() -> str:
+ """The Stripe app signing secret, resolved fresh so rotation and recovery take effect immediately."""
+ return get_or_settings("STRIPE_SIGNING_SECRET", CredentialsCaller.INTEGRATIONS)
+
+
+def _local_oauth_client_id(key: str) -> str:
+ """One of PostHog's own Stripe OAuth application ids, for finding and deleting local tokens.
+
+ Falling back during an outage (or recovery) cannot expose a credential and must not block revocation.
+ """
+ try:
+ return get_or_settings(key, CredentialsCaller.INTEGRATIONS)
+ except CredentialsServiceFailure:
+ return getattr(settings, key, "")
+
+
@frozen
class StripeSecretPublication:
"""What one attempt to publish PostHog's credentials into Stripe's Secret Store produced."""
@@ -263,7 +279,7 @@ 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
+ client_id = get_or_settings("STRIPE_MARKETPLACE_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS)
if not client_id or client_id == get_or_settings(
"STRIPE_POSTHOG_OAUTH_CLIENT_ID", CredentialsCaller.INTEGRATIONS
):
@@ -287,14 +303,14 @@ class StripeIntegration:
Revocation has to span both or tokens issued before the application split survive it.
"""
- 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]
+ client_ids = [
+ cid
+ for cid in (
+ _local_oauth_client_id("STRIPE_MARKETPLACE_OAUTH_CLIENT_ID"),
+ _local_oauth_client_id("STRIPE_POSTHOG_OAUTH_CLIENT_ID"),
+ )
+ if cid
+ ]
if not client_ids:
return []
return list(OAuthApplication.objects.filter(client_id__in=client_ids))
diff --git a/products/batch_exports/backend/temporal/destinations/s3_batch_export.py b/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
index 1c816ebe2f2c..8651c5fe0467 100644
--- a/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/s3_batch_export.py
@@ -26,6 +26,8 @@ from structlog.contextvars import bind_contextvars
from temporalio import activity, exceptions, workflow
from temporalio.common import RetryPolicy
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_or_settings
from posthog.dataclasses import frozen
from posthog.models.integration import (
AWSS3Integration,
@@ -443,14 +445,17 @@ async def get_credentials_using_user_aws_role(
roles and/or policies may not be immediately available.
delay: Initial delay in between connection attempts.
"""
- if not settings.BATCH_EXPORT_S3_EXTERNAL_ROLE_ARN:
+ external_role_arn = await asyncio.to_thread(
+ get_or_settings, "BATCH_EXPORT_S3_EXTERNAL_ROLE_ARN", CredentialsCaller.BATCH_EXPORTS
+ )
+ if not external_role_arn:
raise ExternalRoleArnNotConfiguredError()
for attempt in range(1, max_attempts + 1):
try:
async with SESSION.client("sts") as sts:
first_response = await sts.assume_role(
- RoleArn=settings.BATCH_EXPORT_S3_EXTERNAL_ROLE_ARN,
+ RoleArn=external_role_arn,
RoleSessionName=session_name,
# Hard limit of 1h imposed by AWS
# TODO: Support refreshable credentials so we can support batch
diff --git a/products/conversations/backend/api/slack_oauth.py b/products/conversations/backend/api/slack_oauth.py
index 6c2761b31418..96a63ef4d0c5 100644
--- a/products/conversations/backend/api/slack_oauth.py
+++ b/products/conversations/backend/api/slack_oauth.py
@@ -13,10 +13,9 @@ from rest_framework.views import APIView
from posthog.credentials_service.callers import CredentialsCaller
from posthog.credentials_service.errors import CredentialsServiceFailure
-from posthog.credentials_service.instance_settings import get_many_or_instance_settings
+from posthog.credentials_service.instance_settings import get_many_or_instance_settings, get_or_instance_setting
from posthog.egress.slack.transport import slack_request
from posthog.helpers.impersonation import is_impersonated
-from posthog.models.instance_setting import get_instance_settings
from posthog.models.organization import OrganizationMembership
from posthog.models.team.team import Team
from posthog.models.user import User
@@ -86,8 +85,7 @@ class SupportSlackAuthorizeView(APIView):
permission_classes = [IsAuthenticated, IsConversationsAdmin]
def get(self, request: Request, *args, **kwargs) -> Response:
- support_settings = get_instance_settings(["SUPPORT_SLACK_APP_CLIENT_ID"])
- client_id = str(support_settings.get("SUPPORT_SLACK_APP_CLIENT_ID") or "")
+ client_id = get_or_instance_setting("SUPPORT_SLACK_APP_CLIENT_ID", CredentialsCaller.CONVERSATIONS)
if not client_id:
return Response({"error": "Support Slack OAuth client ID is not configured"}, status=503)
diff --git a/products/conversations/backend/api/tests/test_slack_endpoints.py b/products/conversations/backend/api/tests/test_slack_endpoints.py
index 8b94742e8762..6081fa59c8ce 100644
--- a/products/conversations/backend/api/tests/test_slack_endpoints.py
+++ b/products/conversations/backend/api/tests/test_slack_endpoints.py
@@ -599,8 +599,8 @@ class TestSlackChannelPermissions(BaseTest):
assert response.status_code == 403
@patch(
- "products.conversations.backend.api.slack_oauth.get_instance_settings",
- return_value={"SUPPORT_SLACK_APP_CLIENT_ID": "test-client-id"},
+ "products.conversations.backend.api.slack_oauth.get_or_instance_setting",
+ return_value="test-client-id",
)
def test_admin_can_authorize_slack(self, _mock_settings: MagicMock):
self.organization_membership.level = OrganizationMembership.Level.ADMIN
diff --git a/products/stamphog/backend/logic/github_client.py b/products/stamphog/backend/logic/github_client.py
index 19236f749ce2..b10dec0c5950 100644
--- a/products/stamphog/backend/logic/github_client.py
+++ b/products/stamphog/backend/logic/github_client.py
@@ -26,7 +26,8 @@ import structlog
from posthog.cache_utils import cache_for
from posthog.credentials_service.callers import CredentialsCaller
-from posthog.credentials_service.settings_fallback import get_many_or_settings
+from posthog.credentials_service.errors import CredentialsServiceFailure
+from posthog.credentials_service.settings_fallback import get_many_or_settings, get_or_settings
from posthog.dataclasses import frozen
from posthog.egress.github.limiter import remember_observed_core_limit
from posthog.egress.github.transport import GitHubRateLimitError, github_request, raise_if_github_rate_limited
@@ -44,6 +45,14 @@ _SOURCE = "stamphog"
STICKY_COMMENT_MARKER = "<!-- stamphog:review-status -->"
+def _app_private_key_configured() -> bool:
+ """Whether App credentials exist, only to decide on a warning. A service failure counts as configured."""
+ try:
+ return bool(get_or_settings("STAMPHOG_GITHUB_APP_PRIVATE_KEY", CredentialsCaller.STAMPHOG))
+ except CredentialsServiceFailure:
+ return True
+
+
def expected_app_bot_login() -> str | None:
"""The GitHub login this App acts under (``<slug>[bot]``), or None if the slug is unconfigured.
@@ -891,7 +900,7 @@ class StamphogGitHubClient:
# while every GitHub-side sweep and adopt silently no-ops — quietly disabling the
# orphan-catching half of the stale-approval invariant. The misconfiguration would
# otherwise be invisible until an orphan approval stands over unreviewed commits.
- if settings.STAMPHOG_GITHUB_APP_PRIVATE_KEY:
+ if _app_private_key_configured():
logger.warning(
"stamphog github: STAMPHOG_GITHUB_APP_SLUG unset with App credentials configured — "
"own-approval sweeps and adopt-before-post are disabled",
diff --git a/products/stamphog/backend/presentation/views.py b/products/stamphog/backend/presentation/views.py
index 5e1aef72f29a..df59bc0de45f 100644
--- a/products/stamphog/backend/presentation/views.py
+++ b/products/stamphog/backend/presentation/views.py
@@ -25,6 +25,8 @@ from rest_framework.views import APIView
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.auth import OAuthAccessTokenAuthentication, PersonalAPIKeyAuthentication
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_or_settings
from posthog.models.scoping.manager import resolve_effective_team_id
from posthog.models.team import Team
from posthog.models.user import User
@@ -362,7 +364,7 @@ class StamphogRepoConfigViewSet(_StamphogTeamScopedViewSet, viewsets.GenericView
# current team. Without it, an attacker could send a logged-in member a callback carrying the
# attacker's own installation and bind it to the victim's team.
slug = settings.STAMPHOG_GITHUB_APP_SLUG
- client_id = settings.STAMPHOG_GITHUB_APP_CLIENT_ID
+ client_id = get_or_settings("STAMPHOG_GITHUB_APP_CLIENT_ID", CredentialsCaller.STAMPHOG)
install_url = ""
authorize_url = ""
# One state token backs both URLs — either callback (install Setup URL or authorize Callback URL)