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 integration app keys from the service (#120995)

Tom Owersa035d40d240a
diff --git a/posthog/api/test/test_integration.py b/posthog/api/test/test_integration.py
index b53bb16ab09d..7c4e125a15a1 100644
--- a/posthog/api/test/test_integration.py
+++ b/posthog/api/test/test_integration.py
@@ -46,6 +46,7 @@ from posthog.api.github_callback.team_services import (
from posthog.api.github_callback.types import FlowKind, GitHubAuthorizeState
from posthog.api.integration import IntegrationSerializer, IntegrationViewSet
from posthog.constants import AvailableFeature
+from posthog.credentials_service.errors import CredentialsServiceUnreachableError, SecretInRecoveryError
from posthog.egress.github.transport import GitHubEgressBudgetExhausted
from posthog.egress.limiter.policies import Priority
from posthog.models.activity_logging.activity_log import ActivityLog, apply_activity_visibility_restrictions
@@ -5919,11 +5920,11 @@ class TestStripeIntegrationOAuthTokens:
assert OAuthRefreshToken.objects.filter(pk=refresh_token.pk).exists()
@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.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
def test_marketplace_token_never_mints_on_the_orchestrator_application(
- self, mock_settings, mock_oauth_settings, MockStripeClient, marketplace_setting
+ self, mock_settings, MockStripeClient, marketplace_setting
):
orchestrator_app = OAuthApplication.objects.create(
name="Stripe orchestrator",
@@ -5939,8 +5940,6 @@ class TestStripeIntegrationOAuthTokens:
"unset": "",
"equal_to_orchestrator": orchestrator_app.client_id,
}[marketplace_setting]
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_test"
MockStripeClient.return_value = MagicMock()
integration = Integration.objects.create(
@@ -6001,14 +6000,12 @@ class TestStripeIntegrationOAuthTokens:
locked_pairs = {(call.kwargs["user_id"], call.kwargs["application_id"]) for call in mock_lock.call_args_list}
assert locked_pairs == {(self.user.pk, marketplace_app.id), (self.user.pk, self.oauth_app.id)}
+ @override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
- def test_write_posthog_secrets_uses_account_scope(self, mock_settings, mock_oauth_settings, MockStripeClient):
+ def test_write_posthog_secrets_uses_account_scope(self, mock_settings, MockStripeClient):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_test"
mock_client = MagicMock()
MockStripeClient.return_value = mock_client
@@ -6033,14 +6030,12 @@ class TestStripeIntegrationOAuthTokens:
assert secret_payloads["posthog_project_id"] == str(self.team.pk)
assert secret_payloads["posthog_oauth_client_id"] == self.oauth_app.client_id
+ @override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
- def test_write_posthog_secrets_mints_read_only_token(self, mock_settings, mock_oauth_settings, MockStripeClient):
+ def test_write_posthog_secrets_mints_read_only_token(self, mock_settings, MockStripeClient):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_test"
MockStripeClient.return_value = MagicMock()
integration = Integration.objects.create(
@@ -6066,12 +6061,10 @@ class TestStripeIntegrationOAuthTokens:
assert token.scoped_teams == [self.team.pk]
assert OAuthRefreshToken.objects.get(access_token=token).scoped_teams == [self.team.pk]
+ @override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
- def test_clear_posthog_secrets_uses_account_scope(self, mock_settings, mock_oauth_settings, MockStripeClient):
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_test"
+ def test_clear_posthog_secrets_uses_account_scope(self, mock_settings, MockStripeClient):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = None
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = None
mock_client = MagicMock()
@@ -6100,16 +6093,12 @@ class TestStripeIntegrationOAuthTokens:
("clear", "clear_posthog_secrets"),
]
)
+ @override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_live")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
- def test_stripe_client_uses_live_secret(
- self, _name, method_name, mock_settings, mock_oauth_settings, MockStripeClient
- ):
+ def test_stripe_client_uses_live_secret(self, _name, method_name, mock_settings, MockStripeClient):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_live"
MockStripeClient.return_value = MagicMock()
integration = Integration.objects.create(
@@ -6128,17 +6117,13 @@ class TestStripeIntegrationOAuthTokens:
MockStripeClient.assert_called_once_with("sk_live")
+ @override_settings(STRIPE_APP_CLIENT_ID="", STRIPE_APP_SECRET_KEY="")
@patch("posthog.models.integration.stripe.capture_exception")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
- def test_write_posthog_secrets_skips_when_keys_missing(
- self, mock_settings, mock_oauth_settings, MockStripeClient, mock_capture
- ):
+ 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"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = None
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = None
MockStripeClient.return_value = MagicMock()
integration = Integration.objects.create(
@@ -6166,16 +6151,14 @@ class TestStripeIntegrationOAuthTokens:
],
ids=["every_write_fails", "refresh_token_write_fails"],
)
+ @override_settings(STRIPE_APP_CLIENT_ID="stripe_app_client_id", STRIPE_APP_SECRET_KEY="sk_test")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
def test_write_posthog_secrets_keeps_the_credential_only_when_a_secret_reaches_stripe(
- self, mock_settings, mock_oauth_settings, MockStripeClient, failing_secrets, credential_kept
+ self, mock_settings, MockStripeClient, failing_secrets, credential_kept
):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = "stripe_app_client_id"
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = "sk_test"
def create_secret(params, options):
if params["name"] in failing_secrets:
@@ -6197,17 +6180,15 @@ class TestStripeIntegrationOAuthTokens:
assert OAuthAccessToken.objects.filter(scoped_teams__contains=[self.team.pk]).exists() is credential_kept
assert OAuthRefreshToken.objects.filter(scoped_teams__contains=[self.team.pk]).exists() is credential_kept
+ @override_settings(STRIPE_APP_CLIENT_ID="", STRIPE_APP_SECRET_KEY="")
@patch("posthog.models.integration.stripe.capture_exception")
@patch("stripe.StripeClient")
- @patch("posthog.models.integration.oauth.settings")
@patch("posthog.models.integration.stripe.settings")
def test_clear_posthog_secrets_skips_and_revokes_tokens_when_keys_missing(
- self, mock_settings, mock_oauth_settings, MockStripeClient, mock_capture
+ self, mock_settings, MockStripeClient, mock_capture
):
mock_settings.STRIPE_POSTHOG_OAUTH_CLIENT_ID = "orchestrator_client_id"
mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
- mock_oauth_settings.STRIPE_APP_CLIENT_ID = None
- mock_oauth_settings.STRIPE_APP_SECRET_KEY = None
MockStripeClient.return_value = MagicMock()
integration, access_token, refresh_token = self._create_integration_with_tokens()
@@ -6220,6 +6201,30 @@ class TestStripeIntegrationOAuthTokens:
assert not OAuthAccessToken.objects.filter(pk=access_token.pk).exists()
assert not OAuthRefreshToken.objects.filter(pk=refresh_token.pk).exists()
+ @parameterized.expand(
+ [
+ ("unreachable", CredentialsServiceUnreachableError("credentials service down")),
+ ("in_recovery", SecretInRecoveryError("STRIPE_APP_SECRET_KEY")),
+ ]
+ )
+ @patch("stripe.StripeClient")
+ @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"
+ mock_settings.STRIPE_MARKETPLACE_OAUTH_CLIENT_ID = self.oauth_app.client_id
+
+ integration, access_token, refresh_token = self._create_integration_with_tokens()
+
+ with patch("posthog.models.integration.oauth.OauthIntegration.client_credentials_for_kind", side_effect=error):
+ with pytest.raises(type(error)):
+ StripeIntegration(integration).clear_posthog_secrets()
+
+ MockStripeClient.assert_not_called()
+ assert not OAuthAccessToken.objects.filter(pk=access_token.pk).exists()
+ assert not OAuthRefreshToken.objects.filter(pk=refresh_token.pk).exists()
+
def _make_github_branches_response(names: list[str], has_next: bool = False) -> MagicMock:
"""Build a mock requests.Response for the GitHub branches API."""
diff --git a/posthog/credentials_service/callers.py b/posthog/credentials_service/callers.py
index f893721336ef..37b769c87ff4 100644
--- a/posthog/credentials_service/callers.py
+++ b/posthog/credentials_service/callers.py
@@ -24,3 +24,6 @@ class CredentialsCaller(StrEnum):
MESSAGING = "messaging"
TASKS = "tasks"
WEB_ANALYTICS = "web-analytics"
+ # Shared integration plumbing (OAuth connect, token refresh, account listing) that runs on
+ # behalf of many products. The call sites cannot tell which product asked, so they say so.
+ INTEGRATIONS = "integrations"
diff --git a/posthog/credentials_service/settings_fallback.py b/posthog/credentials_service/settings_fallback.py
new file mode 100644
index 000000000000..d3995cea74aa
--- /dev/null
+++ b/posthog/credentials_service/settings_fallback.py
@@ -0,0 +1,136 @@
+"""Read a platform credential from the credentials service, falling back per key to settings.
+
+`client.get` is strict: a key the service does not hold raises `SecretMissingError`. That is right
+once a key has moved, and wrong while call sites and keys are still on their way into the service.
+The functions here are the strict read plus one temporary bridge — a key the service does not hold
+is read from settings instead, and every time that happens it is counted.
+
+**Sequencing.** Call sites and keys cannot move at the same instant, and the service is already live
+in production — so `_resolve_one` below keeps reading settings for a key that has not reached the
+service yet, counting every time it does. That makes the order of the two moves not matter, and
+makes "is the rollout finished?" a metric rather than a belief. It is temporary; the removal
+condition is on `_resolve_one`.
+
+Reading through here changes nothing until a key is actually moved into the service. With the
+service off (self-hosted, local development, the flag disabled) the client reads the same
+`os.environ`/settings value the call site read before, so a migrated call site behaves identically.
+"""
+
+import os
+from collections.abc import Callable
+
+from django.conf import settings
+
+import structlog
+from prometheus_client import Counter
+
+from .callers import CredentialsCaller
+from .client import CredentialsServiceClient
+from .errors import SecretMissingError
+
+logger = structlog.get_logger(__name__)
+
+SettingsLookup = Callable[[str], str | None]
+
+SETTINGS_FALLBACK_COUNTER = Counter(
+ "posthog_credentials_service_settings_fallback_total",
+ "Platform credential reads the credentials service could not serve, by what happened instead",
+ # outcome="settings": the key is still only in settings, so the rollout isn't finished for it.
+ # outcome="unset": the credential isn't configured anywhere, which is the normal state of an
+ # integration a deployment doesn't use. Separated because only the first is a rollout gap.
+ labelnames=["key", "outcome"],
+)
+
+_default_client = CredentialsServiceClient()
+
+
+def from_django_settings(key: str) -> str | None:
+ """The value this key had before the service existed, or None."""
+ return (os.environ.get(key) or getattr(settings, key, "")) or None
+
+
+def _resolve_one(
+ key: str,
+ caller: CredentialsCaller,
+ client: CredentialsServiceClient,
+ settings_lookup: SettingsLookup,
+) -> str:
+ """One credential, falling back to settings while the key is still on its way into the service.
+
+ TEMPORARY, and the removal condition is exact: when
+ `posthog_credentials_service_settings_fallback_total{outcome="settings"}` has been zero across a
+ full cycle of every caller (a warehouse sync cycle, the OAuth refresh sweep), every key has moved
+ and this function collapses back to `client.get`.
+
+ It exists because the call sites and the keys cannot move at the same instant. The service is
+ already live, so without this, migrating a call site whose key is not in the service yet fails
+ every read of that credential the moment it deploys. The same code also runs where the service
+ is not configured at all.
+
+ It is not the silent fallback the client refuses to have. That one is dangerous because it
+ hides a half-finished rollout behind apparent success; this one names the key, counts it, and
+ is the thing you watch to know when the rollout is finished.
+
+ Only a MISSING key falls back, and the two exclusions are the point:
+
+ - A key in **recovery** must not fall back. Recovery means the credential is known-burned; the
+ settings copy is the very value that was burned, so serving it would turn the kill switch
+ into a no-op — the one failure mode worse than the caller stopping.
+ - An **unreachable service** must not fall back. There is no last known good to fall back to,
+ and treating an outage as "the key isn't there" would mask it as a rollout gap.
+ """
+ try:
+ return client.get(key, caller)
+ except SecretMissingError:
+ value = settings_lookup(key)
+ if value:
+ SETTINGS_FALLBACK_COUNTER.labels(key=key, outcome="settings").inc()
+ logger.warning("credentials_service.settings_fallback", key=key, caller=str(caller))
+ return value
+
+ # Configured nowhere. Before this migration the call site read `settings.X` and got the
+ # empty string its default provides, and an unusable credential surfaced later as a
+ # rejection from the vendor or a "not configured" guard. Keep exactly that: this change
+ # moves where a credential comes from, and must not also change what happens when there
+ # isn't one. Raising here instead would alter behaviour for every deployment that doesn't
+ # use a given integration — self-hosted, local development, and every test that never set
+ # it — which is a much wider blast radius than the migration itself.
+ SETTINGS_FALLBACK_COUNTER.labels(key=key, outcome="unset").inc()
+ logger.debug("credentials_service.unset", key=key, caller=str(caller))
+ return ""
+
+
+def get_or_settings(
+ key: str,
+ caller: CredentialsCaller,
+ *,
+ client: CredentialsServiceClient | None = None,
+ settings_lookup: SettingsLookup = from_django_settings,
+) -> str:
+ """One PostHog-owned credential, by the name it has in the service and in settings.
+
+ `client` swaps the transport (see `CredentialsServiceClient`). `settings_lookup` is where a key
+ the service does not hold is read from instead; the default is the Django setting or
+ environment variable of the same name.
+ """
+ return _resolve_one(key, caller, client or _default_client, settings_lookup)
+
+
+def get_many_or_settings(
+ keys: list[str],
+ caller: CredentialsCaller,
+ *,
+ client: CredentialsServiceClient | None = None,
+ settings_lookup: SettingsLookup = from_django_settings,
+) -> dict[str, str]:
+ """Several at once, in a single request — a caller usually needs an id and a secret together.
+
+ The batch resolves in one request. If any key in it is missing, the batch cannot say which,
+ so the keys are re-resolved one at a time to let the fallback above apply per key. That costs
+ extra round trips only while a key is mid-migration, and disappears with the fallback.
+ """
+ resolved_client = client or _default_client
+ try:
+ return resolved_client.get_many(keys, caller)
+ except SecretMissingError:
+ return {key: _resolve_one(key, caller, resolved_client, settings_lookup) for key in keys}
diff --git a/posthog/credentials_service/test/test_settings_fallback.py b/posthog/credentials_service/test/test_settings_fallback.py
new file mode 100644
index 000000000000..ba53796fe0d2
--- /dev/null
+++ b/posthog/credentials_service/test/test_settings_fallback.py
@@ -0,0 +1,159 @@
+from typing import Any
+
+import pytest
+from unittest import mock
+
+from django.test import SimpleTestCase, override_settings
+
+import jwt
+import requests
+from parameterized import parameterized
+
+from posthog.credentials_service import settings_fallback
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.errors import CredentialsServiceUnreachableError, SecretInRecoveryError
+from posthog.jwt import PosthogJwtAudience
+
+SERVICE_SETTINGS: dict[str, Any] = {
+ "CREDENTIALS_SERVICE_URL": "http://credentials-service.credentials-service.svc.cluster.local",
+ "CREDENTIALS_SERVICE_JWT_SECRET": "signing-key",
+}
+
+KEY = "GOOGLE_ADS_DEVELOPER_TOKEN"
+OTHER = "GOOGLE_ADS_APP_CLIENT_ID"
+CALLER = CredentialsCaller.INTEGRATIONS
+FLAG = "posthog.credentials_service.client.posthoganalytics.feature_enabled"
+POST = "posthog.credentials_service.client.internal_requests.post"
+
+
+class _FakeResponse:
+ status_code = 200
+
+ def __init__(self, payload: dict[str, Any]) -> None:
+ self._payload = payload
+
+ def raise_for_status(self) -> None:
+ pass
+
+ def json(self) -> dict[str, Any]:
+ return self._payload
+
+
+def _steady(value: str) -> dict[str, Any]:
+ return {"state": "steady", "value": value, "version_id": "v1", "fetched_at": "now"}
+
+
+def _missing(*keys: str) -> _FakeResponse:
+ return _FakeResponse({"secrets": {}, "missing": list(keys)})
+
+
+def _keys_asked_for(authorization: str) -> list[str]:
+ claims = jwt.decode(
+ authorization.removeprefix("Bearer "),
+ "signing-key",
+ algorithms=["HS256"],
+ audience=PosthogJwtAudience.CREDENTIALS_SERVICE.value,
+ )
+ return list(claims["keys"])
+
+
+@override_settings(**SERVICE_SETTINGS)
+class TestSettingsFallback(SimpleTestCase):
+ def setUp(self) -> None:
+ flag = mock.patch(FLAG, return_value=True)
+ flag.start()
+ self.addCleanup(flag.stop)
+ settings_fallback.SETTINGS_FALLBACK_COUNTER.clear()
+
+ def _count(self, outcome: str) -> float:
+ return settings_fallback.SETTINGS_FALLBACK_COUNTER.labels(key=KEY, outcome=outcome)._value.get()
+
+ @parameterized.expand(
+ [
+ # The service is live, so a call site whose key has not moved yet must keep working.
+ ("still_in_settings", {KEY: "from-settings"}, None, "from-settings", "settings"),
+ # Configured nowhere is what `settings.X` already returned: an empty string, not an error.
+ ("configured_nowhere", {}, None, "", "unset"),
+ # A caller with its own settings store (Slack instance settings) must not read Django's.
+ ("custom_lookup_wins", {KEY: "django-copy"}, "instance-setting", "instance-setting", "settings"),
+ ]
+ )
+ def test_a_key_not_in_the_service(
+ self, _name: str, environ: dict[str, str], custom_value: str | None, expected: str, outcome: str
+ ) -> None:
+ kwargs: dict[str, Any] = {}
+ if custom_value is not None:
+ kwargs["settings_lookup"] = lambda key: custom_value
+
+ with mock.patch(POST, return_value=_missing(KEY)):
+ with mock.patch.dict("os.environ", environ, clear=True):
+ with mock.patch.object(settings_fallback, "logger") as log:
+ assert settings_fallback.get_or_settings(KEY, CALLER, **kwargs) == expected
+
+ assert self._count(outcome) == 1
+ # Only a value still in settings is a rollout gap, so only that one is counted and logged as one.
+ other_outcome = "unset" if outcome == "settings" else "settings"
+ assert self._count(other_outcome) == 0
+ if outcome == "settings":
+ assert log.warning.call_args.kwargs["key"] == KEY
+
+ # A key the service holds is read from the service, not from the environment it also still sits
+ # in. Otherwise the migration would look complete while changing nothing.
+ def test_the_service_wins_over_a_value_still_in_settings(self) -> None:
+ with mock.patch(POST, return_value=_FakeResponse({"secrets": {KEY: _steady("from-service")}, "missing": []})):
+ with mock.patch.dict("os.environ", {KEY: "stale-copy"}):
+ assert settings_fallback.get_or_settings(KEY, CALLER) == "from-service"
+
+ assert self._count("settings") == 0
+
+ @parameterized.expand(
+ [
+ # Recovery means the credential is known-burned. The settings copy is the burned value, so
+ # falling back would turn the kill switch into a no-op.
+ (
+ "recovery",
+ {"return_value": _FakeResponse({"secrets": {KEY: {"state": "recovery"}}, "missing": []})},
+ SecretInRecoveryError,
+ ),
+ # An outage is not a rollout gap: there is no last known good to fall back to.
+ ("unreachable", {"side_effect": requests.ConnectionError("refused")}, CredentialsServiceUnreachableError),
+ ]
+ )
+ def test_does_not_fall_back(self, _name: str, post: dict[str, Any], error: type[Exception]) -> None:
+ for read in (
+ lambda: settings_fallback.get_or_settings(KEY, CALLER),
+ lambda: settings_fallback.get_many_or_settings([KEY, OTHER], CALLER),
+ ):
+ with mock.patch(POST, **post):
+ with mock.patch.dict("os.environ", {KEY: "still-in-settings", OTHER: "still-in-settings"}):
+ with pytest.raises(error):
+ read()
+
+ assert self._count("settings") == 0
+
+ # A part-migrated batch is the normal state during the rollout: the batch can't say which key was
+ # missing, so it re-resolves per key and each one takes the path it needs.
+ def test_a_batch_with_one_missing_key_resolves_the_rest_per_key(self) -> None:
+ def post(*args: Any, **kwargs: Any) -> _FakeResponse:
+ asked = _keys_asked_for(kwargs["headers"]["Authorization"])
+ if asked == [OTHER]:
+ return _FakeResponse({"secrets": {OTHER: _steady("from-service")}, "missing": []})
+ return _FakeResponse({"secrets": {OTHER: _steady("from-service")}, "missing": [KEY]})
+
+ with mock.patch(POST, side_effect=post) as mock_post:
+ with mock.patch.dict("os.environ", {KEY: "still-in-settings"}):
+ assert settings_fallback.get_many_or_settings([KEY, OTHER], CALLER) == {
+ KEY: "still-in-settings",
+ OTHER: "from-service",
+ }
+
+ assert mock_post.call_count == 3
+ assert self._count("settings") == 1
+
+ def test_a_complete_batch_is_one_request(self) -> None:
+ payload = {"secrets": {KEY: _steady("a"), OTHER: _steady("b")}, "missing": []}
+ with mock.patch(POST, return_value=_FakeResponse(payload)) as mock_post:
+ assert settings_fallback.get_many_or_settings([KEY, OTHER], CALLER) == {KEY: "a", OTHER: "b"}
+
+ assert mock_post.call_count == 1
+ assert _keys_asked_for(mock_post.call_args.kwargs["headers"]["Authorization"]) == sorted([KEY, OTHER])
diff --git a/posthog/models/integration/__init__.py b/posthog/models/integration/__init__.py
index 3c492e2c52fd..2fb1659d2e69 100644
--- a/posthog/models/integration/__init__.py
+++ b/posthog/models/integration/__init__.py
@@ -30,7 +30,13 @@ from .aws import (
)
from .azure_blob import AzureBlobIntegration, AzureBlobIntegrationError
from .clickup import ClickUpIntegration
-from .common import ERROR_TOKEN_REFRESH_FAILED, META_GRAPH_API_VERSION, IntegrationError, dot_get
+from .common import (
+ ERROR_TOKEN_REFRESH_FAILED,
+ META_GRAPH_API_VERSION,
+ IntegrationError,
+ OauthClientCredentials,
+ dot_get,
+)
from .databricks import DatabricksIntegration, DatabricksIntegrationError
from .email import EmailIntegration, cleanup_ses_identity_on_integration_delete
from .external_issues import (
@@ -71,6 +77,7 @@ from .model import (
)
from .oauth import (
INSTAGRAM_OAUTH_SCOPE,
+ OAUTH_CLIENT_CREDENTIAL_KEYS,
POSTHOG_CONNECT_ALLOWED_REGIONS,
POSTHOG_CONNECT_DEFAULT_SCOPES,
POSTHOG_CONNECT_GRANTABLE_SCOPES,
@@ -176,8 +183,10 @@ __all__ = [
"FirebaseIntegration",
"ApplePushIntegration",
"INSTAGRAM_OAUTH_SCOPE",
+ "OauthClientCredentials",
"OauthConfig",
"POSTHOG_SLACK_SCOPE",
+ "OAUTH_CLIENT_CREDENTIAL_KEYS",
"SALESFORCE_OAUTH_KINDS",
"POSTHOG_CONNECT_KIND",
"POSTHOG_CONNECT_ALLOWED_REGIONS",
diff --git a/posthog/models/integration/apple_ads.py b/posthog/models/integration/apple_ads.py
index c4aa4a1056ad..452181dd2933 100644
--- a/posthog/models/integration/apple_ads.py
+++ b/posthog/models/integration/apple_ads.py
@@ -8,17 +8,24 @@ endpoint, presenting it as `client_secret`. The claims are fixed by Apple: `iss`
import time
-from django.conf import settings
-
import jwt
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_many_or_settings
+
+from .common import OauthClientCredentials
+
APPLE_OAUTH_AUDIENCE = "https://appleid.apple.com"
# Apple accepts a client secret of up to 180 days. A short life is enough here because one is
-# signed per token request, and it keeps a leaked assertion near-worthless. It must outlive the
-# OauthConfig cache (5 minutes), which hands the same signed value to every caller in that window.
+# signed per token request, and it keeps a leaked assertion near-worthless.
CLIENT_SECRET_TTL_SECONDS = 30 * 60
+SERVICE_PROVIDER_CLIENT_ID_KEY = "APPLE_ADS_APP_CLIENT_ID"
+SERVICE_PROVIDER_TEAM_ID_KEY = "APPLE_ADS_APP_TEAM_ID"
+SERVICE_PROVIDER_KEY_ID_KEY = "APPLE_ADS_APP_KEY_ID"
+SERVICE_PROVIDER_PRIVATE_KEY_KEY = "APPLE_ADS_APP_PRIVATE_KEY"
+
class AppleAdsOauthNotConfigured(Exception):
pass
@@ -52,21 +59,27 @@ def build_client_secret(
)
-def service_provider_client_secret() -> str:
- """Sign a client secret for PostHog's own Apple Ads service provider registration."""
- if not all(
- (
- settings.APPLE_ADS_APP_CLIENT_ID,
- settings.APPLE_ADS_APP_TEAM_ID,
- settings.APPLE_ADS_APP_KEY_ID,
- settings.APPLE_ADS_APP_PRIVATE_KEY,
- )
- ):
+def service_provider_client_credentials() -> OauthClientCredentials:
+ """The client id and a freshly signed client secret for PostHog's own service provider registration."""
+ values = get_many_or_settings(
+ [
+ SERVICE_PROVIDER_CLIENT_ID_KEY,
+ SERVICE_PROVIDER_TEAM_ID_KEY,
+ SERVICE_PROVIDER_KEY_ID_KEY,
+ SERVICE_PROVIDER_PRIVATE_KEY_KEY,
+ ],
+ CredentialsCaller.INTEGRATIONS,
+ )
+ if not all(values.values()):
raise AppleAdsOauthNotConfigured("Apple Ads service provider app not configured")
- return build_client_secret(
- client_id=settings.APPLE_ADS_APP_CLIENT_ID,
- team_id=settings.APPLE_ADS_APP_TEAM_ID,
- key_id=settings.APPLE_ADS_APP_KEY_ID,
- private_key=settings.APPLE_ADS_APP_PRIVATE_KEY,
+ client_id = values[SERVICE_PROVIDER_CLIENT_ID_KEY]
+ return OauthClientCredentials(
+ client_id=client_id,
+ client_secret=build_client_secret(
+ client_id=client_id,
+ team_id=values[SERVICE_PROVIDER_TEAM_ID_KEY],
+ key_id=values[SERVICE_PROVIDER_KEY_ID_KEY],
+ private_key=values[SERVICE_PROVIDER_PRIVATE_KEY_KEY],
+ ),
)
diff --git a/posthog/models/integration/common.py b/posthog/models/integration/common.py
index 49074c5df0aa..98696ad3a571 100644
--- a/posthog/models/integration/common.py
+++ b/posthog/models/integration/common.py
@@ -3,8 +3,11 @@
import json
import base64
from collections.abc import Mapping
+from dataclasses import field
from typing import Any
+from posthog.dataclasses import frozen
+
class IntegrationError(Exception):
"""Error raised when an integration or its inputs are not valid."""
@@ -49,6 +52,14 @@ def dot_get(d: Any, path: str, default: Any = None) -> Any:
ERROR_TOKEN_REFRESH_FAILED = "TOKEN_REFRESH_FAILED"
+@frozen
+class OauthClientCredentials:
+ """PostHog's own OAuth app credentials for one provider, as resolved for a single use."""
+
+ client_id: str
+ client_secret: str = field(repr=False)
+
+
def _return_non_empty_str_from_config(
config: Mapping,
key: str,
diff --git a/posthog/models/integration/google_ads.py b/posthog/models/integration/google_ads.py
index d860bd9c5554..81f07f3d3df3 100644
--- a/posthog/models/integration/google_ads.py
+++ b/posthog/models/integration/google_ads.py
@@ -2,13 +2,13 @@
from typing import Any
-from django.conf import settings
-
import requests
import structlog
from rest_framework.exceptions import ValidationError
from tenacity import retry, retry_if_exception_type, stop_after_attempt, wait_fixed
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_or_settings
from posthog.egress.slack.client import SlackWebClient as WebClient
from posthog.exceptions_capture import capture_exception
@@ -16,6 +16,8 @@ from . import common, model
logger = structlog.get_logger(__name__)
+DEVELOPER_TOKEN_KEY = "GOOGLE_ADS_DEVELOPER_TOKEN"
+
def google_ads_hierarchy_level(account: dict) -> int:
"""Depth of an account below the manager the walk started from. Google's REST responses omit proto3
@@ -56,6 +58,8 @@ class GoogleAdsIntegration:
return WebClient(self.integration.sensitive_config["access_token"])
def list_google_ads_conversion_actions(self, customer_id, parent_id=None) -> list[dict]:
+ # Only CDP destinations list conversion actions.
+ developer_token = get_or_settings(DEVELOPER_TOKEN_KEY, CredentialsCaller.CDP)
response = requests.request(
"POST",
f"https://googleads.googleapis.com/v24/customers/{customer_id}/googleAds:searchStream",
@@ -65,7 +69,7 @@ class GoogleAdsIntegration:
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.integration.sensitive_config['access_token']}",
- "developer-token": settings.GOOGLE_ADS_DEVELOPER_TOKEN,
+ "developer-token": developer_token,
**({"login-customer-id": parent_id} if parent_id else {}),
},
timeout=10,
@@ -101,13 +105,15 @@ class GoogleAdsIntegration:
# Google Ads manager accounts can have access to other accounts (including other manager accounts).
# Filter out duplicates where a user has direct access and access through a manager account, while prioritizing direct access.
def list_google_ads_accessible_accounts(self) -> list[dict[str, Any]]:
+ # Read once for the whole walk below, which can make many requests.
+ developer_token = get_or_settings(DEVELOPER_TOKEN_KEY, CredentialsCaller.INTEGRATIONS)
response = _google_ads_request(
"GET",
"https://googleads.googleapis.com/v24/customers:listAccessibleCustomers",
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.integration.sensitive_config['access_token']}",
- "developer-token": settings.GOOGLE_ADS_DEVELOPER_TOKEN,
+ "developer-token": developer_token,
},
timeout=10,
)
@@ -155,7 +161,7 @@ class GoogleAdsIntegration:
headers={
"Content-Type": "application/json",
"Authorization": f"Bearer {self.integration.sensitive_config['access_token']}",
- "developer-token": settings.GOOGLE_ADS_DEVELOPER_TOKEN,
+ "developer-token": developer_token,
**({"login-customer-id": parent_id} if parent_id else {}),
},
timeout=10,
diff --git a/posthog/models/integration/meta.py b/posthog/models/integration/meta.py
index e4006edc442b..b49bacff4aff 100644
--- a/posthog/models/integration/meta.py
+++ b/posthog/models/integration/meta.py
@@ -38,11 +38,13 @@ class MetaGraphIntegration:
):
return
+ client = oauth.OauthIntegration.client_credentials_for_kind(self.integration.kind)
+
res = requests.post(
oauth_config.token_url,
data={
- "client_id": oauth_config.client_id,
- "client_secret": oauth_config.client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"fb_exchange_token": self.integration.sensitive_config["access_token"],
"grant_type": "fb_exchange_token",
"set_token_expires_in_60_days": True,
diff --git a/posthog/models/integration/oauth.py b/posthog/models/integration/oauth.py
index aefb418ef77d..72dee88270bf 100644
--- a/posthog/models/integration/oauth.py
+++ b/posthog/models/integration/oauth.py
@@ -5,7 +5,6 @@ import time
import base64
import hashlib
import secrets
-from dataclasses import field, replace
from datetime import timedelta
from typing import Any, NoReturn
from urllib.parse import parse_qs, urlencode, urlparse
@@ -19,6 +18,9 @@ from requests.auth import HTTPBasicAuth
from rest_framework.exceptions import ValidationError
from posthog.cache_utils import cache_for
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.client import credentials_service_enabled
+from posthog.credentials_service.settings_fallback import get_many_or_settings
from posthog.dataclasses import frozen
from posthog.egress.slack.observability import record_slack_api_response
from posthog.models.instance_setting import get_instance_settings
@@ -103,10 +105,14 @@ APPLE_ADS_ACL_URL = "https://api.ads.apple.com/v1/acls"
@frozen
class OauthConfig:
+ """The static part of a provider's OAuth setup.
+
+ It holds no client credentials: this is cached for five minutes, and a cached secret would
+ outlive a rotation. Get those from `OauthIntegration.client_credentials_for_kind` at use time.
+ """
+
authorize_url: str
token_url: str
- client_id: str
- client_secret: str = field(repr=False)
scope: str
id_path: str
name_path: str
@@ -114,8 +120,6 @@ class OauthConfig:
token_info_graphql_query: str | None = None
token_info_config_fields: list[str] | None = None
additional_authorize_params: dict[str, str] | None = None
- client_id_fallback: str | None = None
- client_secret_fallback: str | None = field(default=None, repr=False)
# When true, the authorize/token-exchange flow uses PKCE (RFC 7636, S256)
pkce: bool = False
# When set, disconnecting the integration also revokes the grant at the provider
@@ -274,13 +278,22 @@ POSTHOG_CONNECT_IDENTITY_SCOPES = ("openid", "email")
POSTHOG_CONNECT_GRANTABLE_SCOPES = frozenset(get_oauth_scopes_supported())
-# nosemgrep: tuple-return-prefer-dataclass -- private helper, both call sites a few lines below in this file
-def _posthog_connect_target(region: str | None) -> tuple[str, str, str]:
- """Resolve (base_url, client_id, client_secret) for a remote target cell.
+@frozen
+class _PosthogConnectTarget:
+ region: str
+ base_url: str
+ # Names of the credentials, not their values: they resolve at use time, never in a cache.
+ client_id_key: str
+ client_secret_key: str
+
+
+def _posthog_connect_target(region: str | None) -> _PosthogConnectTarget:
+ """Resolve the base URL and the client credential names for a remote target cell.
Raises NotImplementedError for an unknown or unconfigured region so the connect/refresh
paths fail closed (surfaced to the user as a reconnect error) rather than silently hitting
- the wrong cell.
+ the wrong cell. The client credentials are checked where they are resolved, in
+ `OauthIntegration.client_credentials_for_kind`.
"""
normalized = (region or "").upper()
if normalized == "DEV" and not _POSTHOG_CONNECT_ALLOW_DEV:
@@ -290,33 +303,202 @@ def _posthog_connect_target(region: str | None) -> tuple[str, str, str]:
targets = {
"US": (
settings.POSTHOG_CONNECT_BASE_URL_US,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_ID_US,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_US,
+ "POSTHOG_CONNECT_OAUTH_CLIENT_ID_US",
+ "POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_US",
),
"EU": (
settings.POSTHOG_CONNECT_BASE_URL_EU,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_ID_EU,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_EU,
+ "POSTHOG_CONNECT_OAUTH_CLIENT_ID_EU",
+ "POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_EU",
),
"DEV": (
settings.POSTHOG_CONNECT_BASE_URL_DEV,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_ID_DEV,
- settings.POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_DEV,
+ "POSTHOG_CONNECT_OAUTH_CLIENT_ID_DEV",
+ "POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_DEV",
),
}
if normalized not in targets:
raise NotImplementedError(f"PostHog connect OAuth not supported for region {region!r}")
- base_url, client_id, client_secret = targets[normalized]
- if not base_url or not client_id or not client_secret:
+ base_url, client_id_key, client_secret_key = targets[normalized]
+ if not base_url:
raise NotImplementedError(f"PostHog connect app not configured for region {normalized}")
- return base_url.rstrip("/"), client_id, client_secret
+ return _PosthogConnectTarget(
+ region=normalized,
+ base_url=base_url.rstrip("/"),
+ client_id_key=client_id_key,
+ client_secret_key=client_secret_key,
+ )
def posthog_connect_base_url(region: str | None) -> str:
"""Public base URL of a remote target cell (e.g. https://eu.posthog.com), for callers that
need to reach its API with a `posthog` integration token. Raises for unknown/unconfigured regions."""
- base_url, _, _ = _posthog_connect_target(region)
- return base_url
+ return _posthog_connect_target(region).base_url
+
+
+CREDENTIALS_CALLER = CredentialsCaller.INTEGRATIONS
+
+
+@frozen
+class _ClientCredentialKeys:
+ # Names in the credentials service, and the Django settings that hold them until they move.
+ client_id: str
+ client_secret: str
+ not_configured: str
+
+
+_SALESFORCE_CLIENT_KEYS = _ClientCredentialKeys(
+ client_id="SALESFORCE_CONSUMER_KEY",
+ client_secret="SALESFORCE_CONSUMER_SECRET",
+ not_configured="Salesforce app not configured",
+)
+
+# Every kind whose client credentials are a plain id/secret pair. `posthog` (per region), `slack`
+# (instance settings) and `apple-ads` (a signed secret) resolve in their own branches.
+OAUTH_CLIENT_CREDENTIAL_KEYS: dict[str, _ClientCredentialKeys] = {
+ "salesforce": _SALESFORCE_CLIENT_KEYS,
+ "salesforce-sandbox": _SALESFORCE_CLIENT_KEYS,
+ "pardot": _SALESFORCE_CLIENT_KEYS,
+ "hubspot": _ClientCredentialKeys(
+ client_id="HUBSPOT_APP_CLIENT_ID",
+ client_secret="HUBSPOT_APP_CLIENT_SECRET",
+ not_configured="Hubspot app not configured",
+ ),
+ "google-ads": _ClientCredentialKeys(
+ client_id="GOOGLE_ADS_APP_CLIENT_ID",
+ client_secret="GOOGLE_ADS_APP_CLIENT_SECRET",
+ not_configured="Google Ads app not configured",
+ ),
+ "google-analytics": _ClientCredentialKeys(
+ client_id="GOOGLE_ANALYTICS_APP_CLIENT_ID",
+ client_secret="GOOGLE_ANALYTICS_APP_CLIENT_SECRET",
+ not_configured="Google Analytics app not configured",
+ ),
+ "google-calendar": _ClientCredentialKeys(
+ client_id="GOOGLE_CALENDAR_APP_CLIENT_ID",
+ client_secret="GOOGLE_CALENDAR_APP_CLIENT_SECRET",
+ not_configured="Google Calendar app not configured",
+ ),
+ "google-search-console": _ClientCredentialKeys(
+ client_id="GOOGLE_SEARCH_CONSOLE_APP_CLIENT_ID",
+ client_secret="GOOGLE_SEARCH_CONSOLE_APP_CLIENT_SECRET",
+ not_configured="Google Search Console app not configured",
+ ),
+ "google-sheets": _ClientCredentialKeys(
+ client_id="SOCIAL_AUTH_GOOGLE_OAUTH2_KEY",
+ client_secret="SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET",
+ not_configured="Google Sheets app not configured",
+ ),
+ "youtube-analytics": _ClientCredentialKeys(
+ client_id="YOUTUBE_ANALYTICS_APP_CLIENT_ID",
+ client_secret="YOUTUBE_ANALYTICS_APP_CLIENT_SECRET",
+ not_configured="YouTube Analytics app not configured",
+ ),
+ "snapchat": _ClientCredentialKeys(
+ client_id="SNAPCHAT_APP_CLIENT_ID",
+ client_secret="SNAPCHAT_APP_CLIENT_SECRET",
+ not_configured="Snapchat app not configured",
+ ),
+ "linkedin-ads": _ClientCredentialKeys(
+ client_id="LINKEDIN_APP_CLIENT_ID",
+ client_secret="LINKEDIN_APP_CLIENT_SECRET",
+ not_configured="LinkedIn Ads app not configured",
+ ),
+ "bing-ads": _ClientCredentialKeys(
+ client_id="BING_ADS_CLIENT_ID",
+ client_secret="BING_ADS_CLIENT_SECRET",
+ not_configured="Bing Ads app not configured",
+ ),
+ "intercom": _ClientCredentialKeys(
+ client_id="INTERCOM_APP_CLIENT_ID",
+ client_secret="INTERCOM_APP_CLIENT_SECRET",
+ not_configured="Intercom app not configured",
+ ),
+ "helpscout": _ClientCredentialKeys(
+ client_id="HELPSCOUT_APP_CLIENT_ID",
+ client_secret="HELPSCOUT_APP_CLIENT_SECRET",
+ not_configured="Help Scout app not configured",
+ ),
+ "linear": _ClientCredentialKeys(
+ client_id="LINEAR_APP_CLIENT_ID",
+ client_secret="LINEAR_APP_CLIENT_SECRET",
+ not_configured="Linear app not configured",
+ ),
+ "meta-ads": _ClientCredentialKeys(
+ client_id="META_ADS_APP_CLIENT_ID",
+ client_secret="META_ADS_APP_CLIENT_SECRET",
+ not_configured="Meta Ads app not configured",
+ ),
+ "instagram": _ClientCredentialKeys(
+ client_id="INSTAGRAM_APP_CLIENT_ID",
+ client_secret="INSTAGRAM_APP_CLIENT_SECRET",
+ not_configured="Instagram app not configured",
+ ),
+ "reddit-ads": _ClientCredentialKeys(
+ client_id="REDDIT_ADS_CLIENT_ID",
+ client_secret="REDDIT_ADS_CLIENT_SECRET",
+ not_configured="Reddit Ads app not configured",
+ ),
+ "tiktok-ads": _ClientCredentialKeys(
+ client_id="TIKTOK_ADS_CLIENT_ID",
+ client_secret="TIKTOK_ADS_CLIENT_SECRET",
+ not_configured="TikTok Ads app not configured",
+ ),
+ "clickup": _ClientCredentialKeys(
+ client_id="CLICKUP_APP_CLIENT_ID",
+ client_secret="CLICKUP_APP_CLIENT_SECRET",
+ not_configured="ClickUp app not configured",
+ ),
+ "jira": _ClientCredentialKeys(
+ client_id="ATLASSIAN_APP_CLIENT_ID",
+ client_secret="ATLASSIAN_APP_CLIENT_SECRET",
+ not_configured="Atlassian/Jira app not configured",
+ ),
+ "pinterest-ads": _ClientCredentialKeys(
+ client_id="PINTEREST_ADS_CLIENT_ID",
+ client_secret="PINTEREST_ADS_CLIENT_SECRET",
+ not_configured="Pinterest Ads app not configured",
+ ),
+ "stripe": _ClientCredentialKeys(
+ client_id="STRIPE_APP_CLIENT_ID",
+ client_secret="STRIPE_APP_SECRET_KEY",
+ not_configured="Stripe app not configured",
+ ),
+ "resend": _ClientCredentialKeys(
+ client_id="RESEND_APP_CLIENT_ID",
+ client_secret="RESEND_APP_CLIENT_SECRET",
+ not_configured="Resend app not configured",
+ ),
+}
+
+# Legacy OAuth client credentials kept alive during an app or secret rotation. Refreshes fall back
+# to these when the primary credentials fail, so tokens issued by a since-migrated app keep working
+# until users reconnect. A fallback is used only when its secret is set; an unset id reuses the
+# primary one.
+OAUTH_CLIENT_FALLBACK_KEYS: dict[str, tuple[str, str]] = {
+ "bing-ads": ("BING_ADS_CLIENT_ID_FALLBACK", "BING_ADS_CLIENT_SECRET_FALLBACK"),
+}
+
+SLACK_APP_CLIENT_CREDENTIAL_KEYS = ["SLACK_APP_CLIENT_ID", "SLACK_APP_CLIENT_SECRET"]
+
+
+def _slack_instance_setting(key: str) -> str | None:
+ return get_instance_settings([key])[key] or None
+
+
+def _slack_app_client_credentials() -> dict[str, str]:
+ """Slack's app credentials, with instance settings in place of Django settings.
+
+ Self-hosted deployments edit these in the instance settings UI, and the value there wins over
+ the environment variable it defaults from. With the service off, the client would read the
+ environment variable directly and ignore that edit, so the service is only asked when it is on.
+ """
+ if not credentials_service_enabled():
+ values = get_instance_settings(SLACK_APP_CLIENT_CREDENTIAL_KEYS)
+ return {key: values[key] or "" for key in SLACK_APP_CLIENT_CREDENTIAL_KEYS}
+ return get_many_or_settings(
+ SLACK_APP_CLIENT_CREDENTIAL_KEYS, CREDENTIALS_CALLER, settings_lookup=_slack_instance_setting
+ )
class OauthIntegration:
@@ -363,28 +545,73 @@ class OauthIntegration:
# `region` only applies to the `posthog` remote kind, whose endpoints depend on the
# target cell. cache_for keys on all args, so each (kind, region) pair caches separately;
# every other kind is called without region and keeps its single cached entry.
- config = cls._build_oauth_config(kind, region)
- fallback = settings.OAUTH_CLIENT_FALLBACKS.get(kind)
- if fallback and fallback.get("client_secret"):
- config = replace(
- config,
- client_secret_fallback=fallback["client_secret"],
- client_id_fallback=fallback.get("client_id") or config.client_id,
- )
- return config
+ return cls._build_oauth_config(kind, region)
+
+ @classmethod
+ def client_credentials_for_kind(cls, kind: str, region: str | None = None) -> common.OauthClientCredentials:
+ """PostHog's OAuth client id and secret for `kind`, read from the credentials service.
+
+ Not cached, on purpose: a rotation must land on the next call, and the service can only tell
+ when an old value is safe to retire if every use reads it again. So call this only where the
+ credentials are sent, never where only the static config is needed.
+
+ Raises NotImplementedError when either value is unset, as the static config used to.
+ """
+ if kind == POSTHOG_CONNECT_KIND:
+ target = _posthog_connect_target(region)
+ values = get_many_or_settings([target.client_id_key, target.client_secret_key], CREDENTIALS_CALLER)
+ client_id, client_secret = values[target.client_id_key], values[target.client_secret_key]
+ not_configured = f"PostHog connect app not configured for region {target.region}"
+ elif kind == "slack":
+ values = _slack_app_client_credentials()
+ client_id, client_secret = values["SLACK_APP_CLIENT_ID"], values["SLACK_APP_CLIENT_SECRET"]
+ not_configured = "Slack app not configured"
+ elif kind == "apple-ads":
+ try:
+ return apple_ads.service_provider_client_credentials()
+ except apple_ads.AppleAdsOauthNotConfigured:
+ raise NotImplementedError("Apple Ads service provider app not configured")
+ elif kind in OAUTH_CLIENT_CREDENTIAL_KEYS:
+ keys = OAUTH_CLIENT_CREDENTIAL_KEYS[kind]
+ values = get_many_or_settings([keys.client_id, keys.client_secret], CREDENTIALS_CALLER)
+ client_id, client_secret = values[keys.client_id], values[keys.client_secret]
+ not_configured = keys.not_configured
+ else:
+ raise NotImplementedError(f"Oauth config for kind {kind} not implemented")
+
+ if not client_id or not client_secret:
+ raise NotImplementedError(not_configured)
+ return common.OauthClientCredentials(client_id=client_id, client_secret=client_secret)
+
+ @classmethod
+ def fallback_client_credentials_for_kind(
+ cls, kind: str, primary: common.OauthClientCredentials
+ ) -> common.OauthClientCredentials | None:
+ """The legacy credentials of a rotated app, or None when `kind` has none configured.
+
+ Read only after the primary credentials fail, so a kind with no fallback costs no reads.
+ """
+ keys = OAUTH_CLIENT_FALLBACK_KEYS.get(kind)
+ if keys is None:
+ return None
+ client_id_key, client_secret_key = keys
+ values = get_many_or_settings([client_id_key, client_secret_key], CREDENTIALS_CALLER)
+ if not values[client_secret_key]:
+ return None
+ return common.OauthClientCredentials(
+ client_id=values[client_id_key] or primary.client_id, client_secret=values[client_secret_key]
+ )
@classmethod
def _build_oauth_config(cls, kind: str, region: str | None = None) -> OauthConfig:
if kind == "posthog":
- base_url, client_id, client_secret = _posthog_connect_target(region)
+ base_url = _posthog_connect_target(region).base_url
return OauthConfig(
authorize_url=f"{base_url}/oauth/authorize",
token_url=f"{base_url}/oauth/token",
token_info_url=f"{base_url}/oauth/userinfo",
token_info_config_fields=["sub", "email"],
token_revoke_url=f"{base_url}/oauth/revoke",
- client_id=client_id,
- client_secret=client_secret,
# Default only; authorize_url overrides with the user-selected scopes (plus the
# identity scopes). Token exchange/refresh don't send scope, so this is unused there.
scope=" ".join([*POSTHOG_CONNECT_DEFAULT_SCOPES, *POSTHOG_CONNECT_IDENTITY_SCOPES]),
@@ -393,60 +620,34 @@ class OauthIntegration:
pkce=True,
)
if kind == "slack":
- from_settings = get_instance_settings(
- [
- "SLACK_APP_CLIENT_ID",
- "SLACK_APP_CLIENT_SECRET",
- "SLACK_APP_SIGNING_SECRET",
- ]
- )
-
- if not from_settings["SLACK_APP_CLIENT_ID"] or not from_settings["SLACK_APP_CLIENT_SECRET"]:
- raise NotImplementedError("Slack app not configured")
-
return OauthConfig(
authorize_url="https://slack.com/oauth/v2/authorize",
token_url="https://slack.com/api/oauth.v2.access",
- client_id=from_settings["SLACK_APP_CLIENT_ID"],
- client_secret=from_settings["SLACK_APP_CLIENT_SECRET"],
scope=POSTHOG_SLACK_SCOPE,
id_path="team.id",
name_path="team.name",
)
elif kind == "salesforce":
- if not settings.SALESFORCE_CONSUMER_KEY or not settings.SALESFORCE_CONSUMER_SECRET:
- raise NotImplementedError("Salesforce app not configured")
-
return OauthConfig(
authorize_url="https://login.salesforce.com/services/oauth2/authorize",
token_url="https://login.salesforce.com/services/oauth2/token",
token_revoke_url="https://login.salesforce.com/services/oauth2/revoke",
- client_id=settings.SALESFORCE_CONSUMER_KEY,
- client_secret=settings.SALESFORCE_CONSUMER_SECRET,
scope="full refresh_token",
id_path="instance_url",
name_path="instance_url",
pkce=True,
)
elif kind == "salesforce-sandbox":
- if not settings.SALESFORCE_CONSUMER_KEY or not settings.SALESFORCE_CONSUMER_SECRET:
- raise NotImplementedError("Salesforce app not configured")
-
return OauthConfig(
authorize_url="https://test.salesforce.com/services/oauth2/authorize",
token_url="https://test.salesforce.com/services/oauth2/token",
token_revoke_url="https://test.salesforce.com/services/oauth2/revoke",
- client_id=settings.SALESFORCE_CONSUMER_KEY,
- client_secret=settings.SALESFORCE_CONSUMER_SECRET,
scope="full refresh_token",
id_path="instance_url",
name_path="instance_url",
pkce=True,
)
elif kind == "pardot":
- if not settings.SALESFORCE_CONSUMER_KEY or not settings.SALESFORCE_CONSUMER_SECRET:
- raise NotImplementedError("Salesforce app not configured")
-
# Account Engagement (formerly Pardot) authorizes against Salesforce, so this reuses the
# Salesforce connected app rather than registering a second one. It needs its own kind
# because `pardot_api` is not covered by the `full` scope the `salesforce` kind requests:
@@ -456,24 +657,17 @@ class OauthIntegration:
authorize_url="https://login.salesforce.com/services/oauth2/authorize",
token_url="https://login.salesforce.com/services/oauth2/token",
token_revoke_url="https://login.salesforce.com/services/oauth2/revoke",
- client_id=settings.SALESFORCE_CONSUMER_KEY,
- client_secret=settings.SALESFORCE_CONSUMER_SECRET,
scope="pardot_api refresh_token",
id_path="instance_url",
name_path="instance_url",
pkce=True,
)
elif kind == "hubspot":
- if not settings.HUBSPOT_APP_CLIENT_ID or not settings.HUBSPOT_APP_CLIENT_SECRET:
- raise NotImplementedError("Hubspot app not configured")
-
return OauthConfig(
authorize_url="https://app.hubspot.com/oauth/authorize",
token_url="https://api.hubapi.com/oauth/v1/token",
token_info_url="https://api.hubapi.com/oauth/v1/access-tokens/:access_token",
token_info_config_fields=["hub_id", "hub_domain", "user", "user_id", "scopes"],
- client_id=settings.HUBSPOT_APP_CLIENT_ID,
- client_secret=settings.HUBSPOT_APP_CLIENT_SECRET,
scope="tickets crm.objects.contacts.write sales-email-read crm.objects.companies.read crm.objects.deals.read crm.objects.contacts.read crm.objects.quotes.read crm.objects.companies.write",
additional_authorize_params={
# NOTE: these scopes are only available on certain hubspot plans and as such are optional.
@@ -493,9 +687,6 @@ class OauthIntegration:
name_path="hub_domain",
)
elif kind == "google-ads":
- if not settings.GOOGLE_ADS_APP_CLIENT_ID or not settings.GOOGLE_ADS_APP_CLIENT_SECRET:
- raise NotImplementedError("Google Ads app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
# forces the consent screen, otherwise we won't receive a refresh token
@@ -503,16 +694,11 @@ class OauthIntegration:
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.GOOGLE_ADS_APP_CLIENT_ID,
- client_secret=settings.GOOGLE_ADS_APP_CLIENT_SECRET,
scope="https://www.googleapis.com/auth/adwords https://www.googleapis.com/auth/userinfo.email",
id_path="sub",
name_path="email",
)
elif kind == "google-analytics":
- if not settings.GOOGLE_ANALYTICS_APP_CLIENT_ID or not settings.GOOGLE_ANALYTICS_APP_CLIENT_SECRET:
- raise NotImplementedError("Google Analytics app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
# forces the consent screen, otherwise we won't receive a refresh token
@@ -520,16 +706,11 @@ class OauthIntegration:
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.GOOGLE_ANALYTICS_APP_CLIENT_ID,
- client_secret=settings.GOOGLE_ANALYTICS_APP_CLIENT_SECRET,
scope="https://www.googleapis.com/auth/analytics.readonly https://www.googleapis.com/auth/userinfo.email",
id_path="sub",
name_path="email",
)
elif kind == "google-calendar":
- if not settings.GOOGLE_CALENDAR_APP_CLIENT_ID or not settings.GOOGLE_CALENDAR_APP_CLIENT_SECRET:
- raise NotImplementedError("Google Calendar app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
# forces the consent screen, otherwise we won't receive a refresh token
@@ -537,32 +718,22 @@ class OauthIntegration:
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.GOOGLE_CALENDAR_APP_CLIENT_ID,
- client_secret=settings.GOOGLE_CALENDAR_APP_CLIENT_SECRET,
scope="https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/gmail.readonly https://www.googleapis.com/auth/userinfo.email",
id_path="sub",
name_path="email",
)
elif kind == "google-search-console":
- if not settings.GOOGLE_SEARCH_CONSOLE_APP_CLIENT_ID or not settings.GOOGLE_SEARCH_CONSOLE_APP_CLIENT_SECRET:
- raise NotImplementedError("Google Search Console app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
additional_authorize_params={"access_type": "offline", "prompt": "consent"},
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.GOOGLE_SEARCH_CONSOLE_APP_CLIENT_ID,
- client_secret=settings.GOOGLE_SEARCH_CONSOLE_APP_CLIENT_SECRET,
scope="https://www.googleapis.com/auth/webmasters.readonly https://www.googleapis.com/auth/userinfo.email",
id_path="sub",
name_path="email",
)
elif kind == "google-sheets":
- if not settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY or not settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET:
- raise NotImplementedError("Google Sheets app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
# forces the consent screen, otherwise we won't receive a refresh token
@@ -570,16 +741,11 @@ class OauthIntegration:
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_KEY,
- client_secret=settings.SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET,
scope="https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/userinfo.email",
id_path="sub",
name_path="email",
)
elif kind == "youtube-analytics":
- if not settings.YOUTUBE_ANALYTICS_APP_CLIENT_ID or not settings.YOUTUBE_ANALYTICS_APP_CLIENT_SECRET:
- raise NotImplementedError("YouTube Analytics app not configured")
-
return OauthConfig(
authorize_url="https://accounts.google.com/o/oauth2/v2/auth",
# forces the consent screen, otherwise we won't receive a refresh token
@@ -587,8 +753,6 @@ class OauthIntegration:
token_info_url="https://openidconnect.googleapis.com/v1/userinfo",
token_info_config_fields=["sub", "email"],
token_url="https://oauth2.googleapis.com/token",
- client_id=settings.YOUTUBE_ANALYTICS_APP_CLIENT_ID,
- client_secret=settings.YOUTUBE_ANALYTICS_APP_CLIENT_SECRET,
# `yt-analytics.readonly` reads the reports; `youtube.readonly` lists the account's
# channels so the user picks one instead of hunting for its ID. Channel reports carry
# no revenue metrics, so `yt-analytics-monetary.readonly` is deliberately not asked for.
@@ -601,74 +765,49 @@ class OauthIntegration:
name_path="email",
)
elif kind == "snapchat":
- if not settings.SNAPCHAT_APP_CLIENT_ID or not settings.SNAPCHAT_APP_CLIENT_SECRET:
- raise NotImplementedError("Snapchat app not configured")
-
return OauthConfig(
authorize_url="https://accounts.snapchat.com/accounts/oauth2/auth",
token_url="https://accounts.snapchat.com/accounts/oauth2/token",
token_info_url="https://adsapi.snapchat.com/v1/me",
token_info_config_fields=["me.id", "me.email"],
- client_id=settings.SNAPCHAT_APP_CLIENT_ID,
- client_secret=settings.SNAPCHAT_APP_CLIENT_SECRET,
scope="snapchat-offline-conversions-api snapchat-marketing-api",
id_path="me.id",
name_path="me.email",
)
elif kind == "linkedin-ads":
- if not settings.LINKEDIN_APP_CLIENT_ID or not settings.LINKEDIN_APP_CLIENT_SECRET:
- raise NotImplementedError("LinkedIn Ads app not configured")
-
# Note: We extract user info from id_token JWT instead of calling token_info_url
# because LinkedIn's /v2/userinfo endpoint has intermittent issues returning
# REVOKED_ACCESS_TOKEN errors for valid tokens. See JWT extraction below.
return OauthConfig(
authorize_url="https://www.linkedin.com/oauth/v2/authorization",
token_url="https://www.linkedin.com/oauth/v2/accessToken",
- client_id=settings.LINKEDIN_APP_CLIENT_ID,
- client_secret=settings.LINKEDIN_APP_CLIENT_SECRET,
scope="r_ads rw_conversions r_ads_reporting openid profile email",
id_path="sub",
name_path="email",
)
elif kind == "bing-ads":
- if not settings.BING_ADS_CLIENT_ID or not settings.BING_ADS_CLIENT_SECRET:
- raise NotImplementedError("Bing Ads app not configured")
-
return OauthConfig(
authorize_url="https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
token_url="https://login.microsoftonline.com/common/oauth2/v2.0/token",
- client_id=settings.BING_ADS_CLIENT_ID,
- client_secret=settings.BING_ADS_CLIENT_SECRET,
scope="https://ads.microsoft.com/msads.manage offline_access openid profile",
id_path="id",
name_path="userPrincipalName",
)
elif kind == "intercom":
- if not settings.INTERCOM_APP_CLIENT_ID or not settings.INTERCOM_APP_CLIENT_SECRET:
- raise NotImplementedError("Intercom app not configured")
-
return OauthConfig(
authorize_url="https://app.intercom.com/oauth",
token_url="https://api.intercom.io/auth/eagle/token",
token_info_url="https://api.intercom.io/me",
token_info_config_fields=["id", "email", "app.region"],
- client_id=settings.INTERCOM_APP_CLIENT_ID,
- client_secret=settings.INTERCOM_APP_CLIENT_SECRET,
scope="",
id_path="id",
name_path="email",
)
elif kind == "helpscout":
- if not settings.HELPSCOUT_APP_CLIENT_ID or not settings.HELPSCOUT_APP_CLIENT_SECRET:
- raise NotImplementedError("Help Scout app not configured")
-
return OauthConfig(
authorize_url="https://secure.helpscout.net/authentication/authorizeClientApplication",
token_url="https://api.helpscout.net/v2/oauth2/token",
token_info_url="https://api.helpscout.net/v2/users/me",
- client_id=settings.HELPSCOUT_APP_CLIENT_ID,
- client_secret=settings.HELPSCOUT_APP_CLIENT_SECRET,
# Help Scout grants the authorizing user's full Mailbox API access and takes no
# scope parameter on the authorize URL.
scope="",
@@ -680,9 +819,6 @@ class OauthIntegration:
opaque_state=True,
)
elif kind == "linear":
- if not settings.LINEAR_APP_CLIENT_ID or not settings.LINEAR_APP_CLIENT_SECRET:
- raise NotImplementedError("Linear app not configured")
-
return OauthConfig(
authorize_url="https://linear.app/oauth/authorize",
# Linear skips its approval screen once the app is authorized, and that screen holds
@@ -697,31 +833,21 @@ class OauthIntegration:
"data.viewer.organization.name",
"data.viewer.organization.urlKey",
],
- client_id=settings.LINEAR_APP_CLIENT_ID,
- client_secret=settings.LINEAR_APP_CLIENT_SECRET,
scope="read issues:create",
id_path="data.viewer.organization.id",
name_path="data.viewer.organization.name",
)
elif kind == "meta-ads":
- if not settings.META_ADS_APP_CLIENT_ID or not settings.META_ADS_APP_CLIENT_SECRET:
- raise NotImplementedError("Meta Ads app not configured")
-
return OauthConfig(
authorize_url=f"https://www.facebook.com/{common.META_GRAPH_API_VERSION}/dialog/oauth",
token_url=f"https://graph.facebook.com/{common.META_GRAPH_API_VERSION}/oauth/access_token",
token_info_url=f"https://graph.facebook.com/{common.META_GRAPH_API_VERSION}/me",
token_info_config_fields=["id", "name", "email"],
- client_id=settings.META_ADS_APP_CLIENT_ID,
- client_secret=settings.META_ADS_APP_CLIENT_SECRET,
scope="ads_read",
id_path="id",
name_path="name",
)
elif kind == "instagram":
- if not settings.INSTAGRAM_APP_CLIENT_ID or not settings.INSTAGRAM_APP_CLIENT_SECRET:
- raise NotImplementedError("Instagram app not configured")
-
# Same Facebook Login endpoints as Meta Ads, and the credentials may even be the same
# Meta app, but the two grants request different scopes so they stay separate kinds.
# The token response carries no account identifier, so id/name come from `/me`.
@@ -730,21 +856,14 @@ class OauthIntegration:
token_url=f"https://graph.facebook.com/{common.META_GRAPH_API_VERSION}/oauth/access_token",
token_info_url=f"https://graph.facebook.com/{common.META_GRAPH_API_VERSION}/me",
token_info_config_fields=["id", "name"],
- client_id=settings.INSTAGRAM_APP_CLIENT_ID,
- client_secret=settings.INSTAGRAM_APP_CLIENT_SECRET,
scope=INSTAGRAM_OAUTH_SCOPE,
id_path="id",
name_path="name",
)
elif kind == "reddit-ads":
- if not settings.REDDIT_ADS_CLIENT_ID or not settings.REDDIT_ADS_CLIENT_SECRET:
- raise NotImplementedError("Reddit Ads app not configured")
-
return OauthConfig(
authorize_url="https://www.reddit.com/api/v1/authorize",
token_url="https://www.reddit.com/api/v1/access_token",
- client_id=settings.REDDIT_ADS_CLIENT_ID,
- client_secret=settings.REDDIT_ADS_CLIENT_SECRET,
scope="read adsread adsconversions history adsedit",
id_path="reddit_user_id", # We'll extract this from JWT
# ads-api /me returns the human-readable username under the granted ads scopes
@@ -756,87 +875,56 @@ class OauthIntegration:
additional_authorize_params={"duration": "permanent"},
)
elif kind == "tiktok-ads":
- if not settings.TIKTOK_ADS_CLIENT_ID or not settings.TIKTOK_ADS_CLIENT_SECRET:
- raise NotImplementedError("TikTok Ads app not configured")
-
return OauthConfig(
authorize_url="https://business-api.tiktok.com/portal/auth",
token_url="https://business-api.tiktok.com/open_api/v1.3/oauth2/access_token/",
- client_id=settings.TIKTOK_ADS_CLIENT_ID,
- client_secret=settings.TIKTOK_ADS_CLIENT_SECRET,
scope="",
id_path="data.advertiser_ids",
name_path="data.advertiser_ids",
)
elif kind == "clickup":
- if not settings.CLICKUP_APP_CLIENT_ID or not settings.CLICKUP_APP_CLIENT_SECRET:
- raise NotImplementedError("ClickUp app not configured")
-
return OauthConfig(
authorize_url="https://app.clickup.com/api",
token_url="https://api.clickup.com/api/v2/oauth/token",
token_info_url="https://api.clickup.com/api/v2/user",
token_info_config_fields=["user.id", "user.email"],
- client_id=settings.CLICKUP_APP_CLIENT_ID,
- client_secret=settings.CLICKUP_APP_CLIENT_SECRET,
scope="",
id_path="user.id",
name_path="user.email",
)
elif kind == "jira":
- if not settings.ATLASSIAN_APP_CLIENT_ID or not settings.ATLASSIAN_APP_CLIENT_SECRET:
- raise NotImplementedError("Atlassian/Jira app not configured")
-
return OauthConfig(
authorize_url="https://auth.atlassian.com/authorize",
additional_authorize_params={"audience": "api.atlassian.com", "prompt": "consent"},
token_url="https://auth.atlassian.com/oauth/token",
token_info_url="https://api.atlassian.com/oauth/token/accessible-resources",
token_info_config_fields=[], # Handled specially in integration_from_oauth_response
- client_id=settings.ATLASSIAN_APP_CLIENT_ID,
- client_secret=settings.ATLASSIAN_APP_CLIENT_SECRET,
scope="read:jira-work write:jira-work read:jira-user offline_access",
id_path="cloud_id",
name_path="site_name",
)
elif kind == "pinterest-ads":
- if not settings.PINTEREST_ADS_CLIENT_ID or not settings.PINTEREST_ADS_CLIENT_SECRET:
- raise NotImplementedError("Pinterest Ads app not configured")
-
return OauthConfig(
authorize_url="https://www.pinterest.com/oauth/",
token_url="https://api.pinterest.com/v5/oauth/token",
token_info_url="https://api.pinterest.com/v5/user_account",
token_info_config_fields=["id", "username", "business_name"],
- client_id=settings.PINTEREST_ADS_CLIENT_ID,
- client_secret=settings.PINTEREST_ADS_CLIENT_SECRET,
scope="ads:read user_accounts:read",
id_path="id",
name_path="username",
)
elif kind == "stripe":
- if not settings.STRIPE_APP_CLIENT_ID or not settings.STRIPE_APP_SECRET_KEY:
- raise NotImplementedError("Stripe app not configured")
-
- client_id = settings.STRIPE_APP_CLIENT_ID
- client_secret = settings.STRIPE_APP_SECRET_KEY
-
authorize_url = (
settings.STRIPE_APP_OVERRIDE_AUTHORIZE_URL or "https://marketplace.stripe.com/oauth/v2/authorize"
)
return OauthConfig(
authorize_url=authorize_url,
token_url="https://api.stripe.com/v1/oauth/token",
- client_id=client_id,
- client_secret=client_secret,
scope="",
id_path="stripe_user_id",
name_path="account_name",
)
elif kind == "resend":
- if not settings.RESEND_APP_CLIENT_ID or not settings.RESEND_APP_CLIENT_SECRET:
- raise NotImplementedError("Resend app not configured")
-
# Resend implements OAuth 2.1: PKCE is required, and access tokens are short-lived
# (~15m) JWTs while refresh tokens rotate on every use. We register as a confidential
# client (token_endpoint_auth_method=client_secret_post), so the standard client_secret
@@ -853,8 +941,6 @@ class OauthIntegration:
authorize_url="https://api.resend.com/oauth/authorize",
token_url="https://api.resend.com/oauth/token",
token_revoke_url="https://api.resend.com/oauth/revoke",
- client_id=settings.RESEND_APP_CLIENT_ID,
- client_secret=settings.RESEND_APP_CLIENT_SECRET,
scope="full_access",
pkce=True,
id_path="resend_account_id",
@@ -862,32 +948,19 @@ class OauthIntegration:
)
elif kind == "apple-ads":
- if not all(
- (
- settings.APPLE_ADS_APP_CLIENT_ID,
- settings.APPLE_ADS_APP_TEAM_ID,
- settings.APPLE_ADS_APP_KEY_ID,
- settings.APPLE_ADS_APP_PRIVATE_KEY,
- )
- ):
- raise NotImplementedError("Apple Ads service provider app not configured")
-
# Apple's service provider flow, where a PostHog registration acts on behalf of an
# Apple Ads user who grants access. The first-party flow the warehouse source also
# supports is a different thing: it uses the customer's own key pair with
# `client_credentials` and the `searchadsorg` scope, and never reaches this code.
#
- # The client secret is signed, not stored: Apple issues no static secret, so each
- # build mints a fresh ES256 assertion. `oauth_config_for_kind` caches the config for
- # five minutes, well inside the assertion's own lifetime.
+ # The client secret is signed, not stored: Apple issues no static secret, so
+ # `client_credentials_for_kind` mints a fresh ES256 assertion on every call.
#
# The token response carries no account identifier, so id/name come from Apple's ACL
# endpoint below (see the apple-ads branch in integration_from_oauth_response).
return OauthConfig(
authorize_url="https://appleid.apple.com/auth/oauth2/v2/authorize",
token_url=APPLE_ADS_TOKEN_URL,
- client_id=settings.APPLE_ADS_APP_CLIENT_ID,
- client_secret=apple_ads.service_provider_client_secret(),
scope="searchads",
id_path="apple_ads_account_id",
name_path="apple_ads_account_name",
@@ -915,6 +988,9 @@ class OauthIntegration:
team_id: int | None = None,
) -> str:
oauth_config = cls.oauth_config_for_kind(kind, region)
+ # Only the id goes into the URL, but resolving the pair keeps the "not configured" check on
+ # the secret too, so an unusable app fails here instead of after the user has consented.
+ client = cls.client_credentials_for_kind(kind, region)
# Carry the initiating team through the OAuth round-trip. The fixed callback URL is not
# project-scoped, so without this the SPA re-resolves to the user's default team on
@@ -943,13 +1019,13 @@ class OauthIntegration:
if kind == "tiktok-ads":
# TikTok uses different parameter names
query_params = {
- "app_id": oauth_config.client_id,
+ "app_id": client.client_id,
"redirect_uri": cls.redirect_uri(kind),
"state": state,
}
else:
query_params = {
- "client_id": oauth_config.client_id,
+ "client_id": client.client_id,
"scope": scope,
"redirect_uri": cls.redirect_uri(kind),
"response_type": "code",
@@ -981,6 +1057,7 @@ class OauthIntegration:
region = (parse_qs(params.get("state", "")).get("region", [""])[0] or "").upper()
oauth_config = cls.oauth_config_for_kind(kind, region)
+ client = cls.client_credentials_for_kind(kind, region)
code_verifier: str | None = None
if oauth_config.pkce:
@@ -1002,7 +1079,7 @@ class OauthIntegration:
if kind == "reddit-ads":
res = requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(oauth_config.client_id, oauth_config.client_secret),
+ auth=HTTPBasicAuth(client.client_id, client.client_secret),
data={
"code": params["code"],
"redirect_uri": OauthIntegration.redirect_uri(kind),
@@ -1015,7 +1092,7 @@ class OauthIntegration:
elif kind == "pinterest-ads":
res = requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(oauth_config.client_id, oauth_config.client_secret),
+ auth=HTTPBasicAuth(client.client_id, client.client_secret),
data={
"code": params["code"],
"redirect_uri": OauthIntegration.redirect_uri(kind),
@@ -1028,8 +1105,8 @@ class OauthIntegration:
res = requests.post(
oauth_config.token_url,
json={
- "app_id": oauth_config.client_id,
- "secret": oauth_config.client_secret,
+ "app_id": client.client_id,
+ "secret": client.client_secret,
"auth_code": params["code"],
},
headers={"Content-Type": "application/json"},
@@ -1041,7 +1118,7 @@ class OauthIntegration:
# Connect OAuth (client_id+client_secret in body) is a different system.
res = requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(oauth_config.client_secret, ""),
+ auth=HTTPBasicAuth(client.client_secret, ""),
data={
"code": params["code"],
"grant_type": "authorization_code",
@@ -1053,8 +1130,8 @@ class OauthIntegration:
res = requests.post(
oauth_config.token_url,
data={
- "client_id": oauth_config.client_id,
- "client_secret": oauth_config.client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"code": params["code"],
"redirect_uri": redirect_uri,
"grant_type": "authorization_code",
@@ -1094,11 +1171,12 @@ class OauthIntegration:
# Hack to try getting sandbox auth token instead of their salesforce production account
if kind in SALESFORCE_OAUTH_KINDS:
oauth_config = cls.oauth_config_for_kind("salesforce-sandbox")
+ client = cls.client_credentials_for_kind("salesforce-sandbox")
res = requests.post(
oauth_config.token_url,
data={
- "client_id": oauth_config.client_id,
- "client_secret": oauth_config.client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"code": params["code"],
"redirect_uri": OauthIntegration.redirect_uri(kind),
"grant_type": "authorization_code",
@@ -1119,7 +1197,7 @@ class OauthIntegration:
f"Oauth error for {kind}",
response=res.text,
status_code=res.status_code,
- client_id=oauth_config.client_id,
+ client_id=client.client_id,
redirect_uri=OauthIntegration.redirect_uri(kind),
code_prefix=str(params.get("code", ""))[:12],
exchange_host="sandbox",
@@ -1133,7 +1211,7 @@ class OauthIntegration:
f"Oauth error for {kind}",
response=res.text,
status_code=res.status_code,
- client_id=oauth_config.client_id,
+ client_id=client.client_id,
redirect_uri=OauthIntegration.redirect_uri(kind),
code_prefix=str(params.get("code", ""))[:12],
)
@@ -1291,7 +1369,7 @@ class OauthIntegration:
try:
from stripe import StripeClient # noqa: PLC0415
- stripe_client = StripeClient(oauth_config.client_secret)
+ stripe_client = StripeClient(client.client_secret)
account = stripe_client.accounts.retrieve(str(integration_id))
business_profile = getattr(account, "business_profile", None)
business_name = getattr(business_profile, "name", None) if business_profile else None
@@ -1447,8 +1525,9 @@ class OauthIntegration:
# client_secret_post) and requires client authentication on revocation. Without it
# the endpoint rejects the request and the grant survives the disconnect. The hint
# tells the provider which token type it received (RFC 7009).
- data["client_id"] = oauth_config.client_id
- data["client_secret"] = oauth_config.client_secret
+ client = self.client_credentials_for_kind(self.integration.kind, region)
+ data["client_id"] = client.client_id
+ data["client_secret"] = client.client_secret
data["token_type_hint"] = "refresh_token" if refresh_token else "access_token"
# allow_redirects=False so a misconfigured/compromised provider can't 30x us into
@@ -1487,7 +1566,9 @@ class OauthIntegration:
return time.time() > refreshed_at + expires_in - time_threshold.total_seconds()
- def _post_token_refresh(self, oauth_config: OauthConfig, client_id: str, client_secret: str) -> requests.Response:
+ def _post_token_refresh(
+ self, oauth_config: OauthConfig, client: common.OauthClientCredentials
+ ) -> requests.Response:
# Via the property, so a token that picked up an extra encryption layer is peeled back to
# the real one instead of being posted to the provider as ciphertext.
refresh_token = self.integration.refresh_token
@@ -1501,7 +1582,7 @@ class OauthIntegration:
if kind == "reddit-ads":
return requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(client_id, client_secret),
+ auth=HTTPBasicAuth(client.client_id, client.client_secret),
data={"refresh_token": refresh_token, "grant_type": "refresh_token"},
# If I use a standard User-Agent, it will throw a 429 too many requests error
headers={"User-Agent": "PostHog/1.0 by PostHogTeam"},
@@ -1511,7 +1592,7 @@ class OauthIntegration:
elif kind == "pinterest-ads":
return requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(client_id, client_secret),
+ auth=HTTPBasicAuth(client.client_id, client.client_secret),
data={"refresh_token": refresh_token, "grant_type": "refresh_token"},
timeout=10,
)
@@ -1523,8 +1604,8 @@ class OauthIntegration:
return requests.post(
"https://business-api.tiktok.com/open_api/v1.3/oauth2/refresh_token/",
json={
- "app_id": client_id,
- "secret": client_secret,
+ "app_id": client.client_id,
+ "secret": client.client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token",
},
@@ -1536,8 +1617,8 @@ class OauthIntegration:
return requests.post(
oauth_config.token_url,
data={
- "client_id": client_id,
- "client_secret": client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token",
"scope": oauth_config.scope,
@@ -1551,7 +1632,7 @@ class OauthIntegration:
# A key derived from the token makes the next attempt replay Stripe's saved response.
return requests.post(
oauth_config.token_url,
- auth=HTTPBasicAuth(client_secret, ""),
+ auth=HTTPBasicAuth(client.client_secret, ""),
data={"refresh_token": refresh_token, "grant_type": "refresh_token"},
headers={"Idempotency-Key": hashlib.sha256(refresh_token.encode()).hexdigest()},
timeout=10,
@@ -1562,8 +1643,8 @@ class OauthIntegration:
# one, so a wrong guess in either direction costs one extra request on failure instead
# of a dead integration. Drop the fallback once Apple's behavior is confirmed.
body = {
- "client_id": client_id,
- "client_secret": client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token",
}
@@ -1587,8 +1668,8 @@ class OauthIntegration:
return requests.post(
token_url,
data={
- "client_id": client_id,
- "client_secret": client_secret,
+ "client_id": client.client_id,
+ "client_secret": client.client_secret,
"refresh_token": refresh_token,
"grant_type": "refresh_token",
},
@@ -1635,6 +1716,9 @@ class OauthIntegration:
"""
region = self.integration.config.get("region") if self.integration.kind == "posthog" else None
oauth_config = self.oauth_config_for_kind(self.integration.kind, region)
+ # Resolved before anything changes on the integration: a credentials service failure is
+ # never this integration's fault, so it must not leave the integration marked as failed.
+ client = self.client_credentials_for_kind(self.integration.kind, region)
# Clear out previous token refreshing errors, as they'll be re-set below if another error occurs
self.integration.errors = ""
@@ -1643,7 +1727,7 @@ class OauthIntegration:
config: dict = {}
used_fallback = False
try:
- res = self._post_token_refresh(oauth_config, oauth_config.client_id, oauth_config.client_secret)
+ res = self._post_token_refresh(oauth_config, client)
config = self._parse_token_refresh_response(res)
except model.UndecryptedIntegrationSecretError:
# The stored refresh token can't be read, so there is nothing to refresh with and no
@@ -1660,15 +1744,14 @@ class OauthIntegration:
# A rotated or migrated OAuth app leaves users with refresh tokens only the previous
# credentials can refresh. Retry with the fallback pair — including when the primary hit a
# network error — so they keep working until they reconnect.
- if (
- res is None or res.status_code != 200 or not config.get("access_token")
- ) and oauth_config.client_secret_fallback:
+ fallback_client = (
+ self.fallback_client_credentials_for_kind(self.integration.kind, client)
+ if res is None or res.status_code != 200 or not config.get("access_token")
+ else None
+ )
+ if fallback_client is not None:
try:
- res = self._post_token_refresh(
- oauth_config,
- oauth_config.client_id_fallback or oauth_config.client_id,
- oauth_config.client_secret_fallback,
- )
+ res = self._post_token_refresh(oauth_config, fallback_client)
config = self._parse_token_refresh_response(res)
used_fallback = True
except requests.RequestException as e:
diff --git a/posthog/models/integration/stripe.py b/posthog/models/integration/stripe.py
index ae5894bbed44..b7de1224a967 100644
--- a/posthog/models/integration/stripe.py
+++ b/posthog/models/integration/stripe.py
@@ -121,7 +121,7 @@ class StripeIntegration:
from stripe import StripeClient # noqa: PLC0415
try:
- oauth_config = oauth.OauthIntegration.oauth_config_for_kind("stripe")
+ client = oauth.OauthIntegration.client_credentials_for_kind("stripe")
except NotImplementedError as e:
capture_exception(
e,
@@ -130,7 +130,7 @@ class StripeIntegration:
},
)
return None
- return StripeClient(oauth_config.client_secret)
+ return StripeClient(client.client_secret)
def write_posthog_secrets(self, team_id: int, created_by: "User") -> StripeSecretPublication:
"""Write PostHog OAuth tokens to Stripe's Secret Store so the Stripe App can call PostHog APIs."""
@@ -219,30 +219,31 @@ class StripeIntegration:
if not stripe_user_id:
raise ValueError("Missing stripe_user_id on integration")
- client = self._stripe_client()
- if client is None:
+ try:
+ client = self._stripe_client()
+ if client is None:
+ return
+
+ for name in STRIPE_POSTHOG_SECRET_NAMES:
+ try:
+ client.apps.secrets.delete_where(
+ params={
+ "scope": {"type": "account"},
+ "name": name,
+ },
+ options={"stripe_account": stripe_user_id},
+ )
+ except Exception as e:
+ capture_exception(
+ e,
+ {
+ "secret_name": name,
+ "stripe_user_id": stripe_user_id,
+ },
+ )
+ finally:
+ # Revoke locally even when the credentials service lookup raises.
self._destroy_posthog_oauth_tokens()
- return
-
- for name in STRIPE_POSTHOG_SECRET_NAMES:
- try:
- client.apps.secrets.delete_where(
- params={
- "scope": {"type": "account"},
- "name": name,
- },
- options={"stripe_account": stripe_user_id},
- )
- except Exception as e:
- capture_exception(
- e,
- {
- "secret_name": name,
- "stripe_user_id": stripe_user_id,
- },
- )
-
- self._destroy_posthog_oauth_tokens()
def _destroy_posthog_oauth_tokens(self) -> None:
"""Delete the local OAuth access and refresh tokens created for this Stripe integration."""
diff --git a/posthog/models/integration/twitter_ads.py b/posthog/models/integration/twitter_ads.py
index bdf611840247..d1f675d977cf 100644
--- a/posthog/models/integration/twitter_ads.py
+++ b/posthog/models/integration/twitter_ads.py
@@ -2,7 +2,6 @@ import hashlib
from typing import TYPE_CHECKING
from urllib.parse import parse_qs, urlencode
-from django.conf import settings
from django.core.cache import cache
from django.utils.http import url_has_allowed_host_and_scheme
@@ -10,6 +9,9 @@ import requests
from requests_oauthlib import OAuth1
from rest_framework.exceptions import ValidationError
+from posthog.credentials_service.callers import CredentialsCaller
+from posthog.credentials_service.settings_fallback import get_many_or_settings
+from posthog.models.integration.common import OauthClientCredentials
from posthog.models.integration.model import Integration
from posthog.models.integration.oauth import OauthIntegration
@@ -25,9 +27,16 @@ class TwitterAdsIntegration:
return f"twitter_ads_oauth:{hashlib.sha256(token.encode()).hexdigest()}"
@staticmethod
- def require_configured() -> None:
- if not settings.TWITTER_ADS_CONSUMER_KEY or not settings.TWITTER_ADS_CONSUMER_SECRET:
+ def consumer_credentials() -> OauthClientCredentials:
+ """PostHog's X app consumer key and secret, read at use time and never cached."""
+ values = get_many_or_settings(
+ ["TWITTER_ADS_CONSUMER_KEY", "TWITTER_ADS_CONSUMER_SECRET"], CredentialsCaller.INTEGRATIONS
+ )
+ if not values["TWITTER_ADS_CONSUMER_KEY"] or not values["TWITTER_ADS_CONSUMER_SECRET"]:
raise ValidationError("Kind not configured")
+ return OauthClientCredentials(
+ client_id=values["TWITTER_ADS_CONSUMER_KEY"], client_secret=values["TWITTER_ADS_CONSUMER_SECRET"]
+ )
@staticmethod
def _exchange(endpoint: str, auth: OAuth1, required: tuple[str, ...]) -> dict[str, str]:
@@ -50,7 +59,7 @@ class TwitterAdsIntegration:
@classmethod
def authorize_url(cls, team_id: int, user_id: int, next_url: str) -> str:
- cls.require_configured()
+ consumer = cls.consumer_credentials()
if next_url and (
not next_url.startswith("/") or not url_has_allowed_host_and_scheme(next_url, allowed_hosts=set())
):
@@ -58,8 +67,8 @@ class TwitterAdsIntegration:
tokens = cls._exchange(
"request_token",
OAuth1(
- settings.TWITTER_ADS_CONSUMER_KEY,
- settings.TWITTER_ADS_CONSUMER_SECRET,
+ consumer.client_id,
+ consumer.client_secret,
callback_uri=OauthIntegration.redirect_uri("twitter-ads"),
),
("oauth_token", "oauth_token_secret", "oauth_callback_confirmed"),
@@ -80,7 +89,7 @@ class TwitterAdsIntegration:
@classmethod
def integration_from_callback(cls, team_id: int, user: "User", config: dict[str, object]) -> Integration:
- cls.require_configured()
+ consumer = cls.consumer_credentials()
token = config.get("oauth_token")
verifier = config.get("oauth_verifier")
if not isinstance(token, str) or not token or not isinstance(verifier, str) or not verifier:
@@ -97,8 +106,8 @@ class TwitterAdsIntegration:
tokens = cls._exchange(
"access_token",
OAuth1(
- settings.TWITTER_ADS_CONSUMER_KEY,
- settings.TWITTER_ADS_CONSUMER_SECRET,
+ consumer.client_id,
+ consumer.client_secret,
token,
binding["oauth_token_secret"],
verifier=verifier,
diff --git a/posthog/models/test/integration/test_meta.py b/posthog/models/test/integration/test_meta.py
index b20dc21a7054..e7be7439f31e 100644
--- a/posthog/models/test/integration/test_meta.py
+++ b/posthog/models/test/integration/test_meta.py
@@ -29,8 +29,8 @@ class TestInstagramIntegrationModel(BaseTest):
assert config.authorize_url == "https://www.facebook.com/v25.0/dialog/oauth"
assert config.token_url == "https://graph.facebook.com/v25.0/oauth/access_token"
assert config.token_info_url == "https://graph.facebook.com/v25.0/me"
- assert config.client_id == "instagram-client-id"
- assert config.client_secret == "instagram-client-secret"
+ client = OauthIntegration.client_credentials_for_kind("instagram")
+ assert (client.client_id, client.client_secret) == ("instagram-client-id", "instagram-client-secret")
assert config.id_path == "id"
assert config.name_path == "name"
# Instagram is reached through the Facebook page it is linked to, so the page scopes
@@ -46,7 +46,7 @@ class TestInstagramIntegrationModel(BaseTest):
@override_settings(INSTAGRAM_APP_CLIENT_ID="", INSTAGRAM_APP_CLIENT_SECRET="")
def test_oauth_config_unconfigured_raises(self):
with pytest.raises(NotImplementedError, match="Instagram app not configured"):
- OauthIntegration.oauth_config_for_kind("instagram")
+ OauthIntegration.client_credentials_for_kind("instagram")
def test_the_instagram_grant_is_separate_from_the_meta_ads_one(self):
# Both ride the same Meta app, but an ads grant carries none of the Instagram scopes,
diff --git a/posthog/models/test/integration/test_oauth.py b/posthog/models/test/integration/test_oauth.py
index ef0fe3ebd832..d053df2c0535 100644
--- a/posthog/models/test/integration/test_oauth.py
+++ b/posthog/models/test/integration/test_oauth.py
@@ -13,6 +13,7 @@ import time_machine
from posthog.test.base import BaseTest
from unittest.mock import MagicMock, patch
+from django.conf import settings
from django.core.cache import cache
from django.db import connection
from django.test import SimpleTestCase, override_settings
@@ -24,11 +25,15 @@ from cryptography.hazmat.primitives.asymmetric import ec
from parameterized import parameterized
from rest_framework.exceptions import ValidationError
+from posthog.jwt import PosthogJwtAudience
+from posthog.models.instance_setting import set_instance_setting
from posthog.models.integration import (
CONFIG_LEGACY_OAUTH_CLIENT,
+ OAUTH_CLIENT_CREDENTIAL_KEYS,
POSTHOG_CONNECT_DEFAULT_SCOPES,
POSTHOG_CONNECT_IDENTITY_SCOPES,
Integration,
+ OauthClientCredentials,
OauthIntegration,
oauth_refresh_failure_reason,
oauth_refresh_terminal_counter,
@@ -645,7 +650,7 @@ class TestOauthIntegrationModel(BaseTest):
initial_legacy_flag,
expected_legacy_flag,
):
- fallbacks = {"bing-ads": {"client_id": "old-app-id", "client_secret": "old-app-secret"}} if has_fallback else {}
+ fallback_secret = "old-app-secret" if has_fallback else ""
config = {"expires_in": 1000}
if initial_legacy_flag:
config[CONFIG_LEGACY_OAUTH_CLIENT] = True
@@ -655,7 +660,8 @@ class TestOauthIntegrationModel(BaseTest):
self.settings(
BING_ADS_CLIENT_ID="new-app-id",
BING_ADS_CLIENT_SECRET="new-app-secret",
- OAUTH_CLIENT_FALLBACKS=fallbacks,
+ BING_ADS_CLIENT_ID_FALLBACK="old-app-id",
+ BING_ADS_CLIENT_SECRET_FALLBACK=fallback_secret,
),
patch("posthog.models.integration.oauth.reload_integrations_on_workers"),
patch(
@@ -698,7 +704,8 @@ class TestOauthIntegrationModel(BaseTest):
with self.settings(
BING_ADS_CLIENT_ID="new-app-id",
BING_ADS_CLIENT_SECRET="new-app-secret",
- OAUTH_CLIENT_FALLBACKS={"bing-ads": {"client_id": "old-app-id", "client_secret": "old-app-secret"}},
+ BING_ADS_CLIENT_ID_FALLBACK="old-app-id",
+ BING_ADS_CLIENT_SECRET_FALLBACK="old-app-secret",
):
OauthIntegration(integration).refresh_access_token()
@@ -1429,8 +1436,9 @@ class TestResendIntegrationModel(BaseTest):
assert config.authorize_url == "https://api.resend.com/oauth/authorize"
assert config.token_url == "https://api.resend.com/oauth/token"
assert config.token_revoke_url == "https://api.resend.com/oauth/revoke"
- assert config.client_id == "resend-client-id"
- assert config.client_secret == "resend-client-secret"
+ assert OauthIntegration.client_credentials_for_kind("resend") == OauthClientCredentials(
+ client_id="resend-client-id", client_secret="resend-client-secret"
+ )
assert config.scope == "full_access"
assert config.pkce is True
assert config.id_path == "resend_account_id"
@@ -1438,7 +1446,7 @@ class TestResendIntegrationModel(BaseTest):
@override_settings(RESEND_APP_CLIENT_ID="", RESEND_APP_CLIENT_SECRET="")
def test_oauth_config_unconfigured_raises(self):
with pytest.raises(NotImplementedError, match="Resend app not configured"):
- OauthIntegration.oauth_config_for_kind("resend")
+ OauthIntegration.client_credentials_for_kind("resend")
@patch("posthog.models.integration.oauth.requests.post")
def test_integration_from_oauth_response_extracts_account_from_jwt(self, mock_post):
@@ -1553,8 +1561,9 @@ class TestPardotIntegrationModel(BaseTest):
assert config.authorize_url == "https://login.salesforce.com/services/oauth2/authorize"
assert config.token_url == "https://login.salesforce.com/services/oauth2/token"
assert config.token_revoke_url == "https://login.salesforce.com/services/oauth2/revoke"
- assert config.client_id == "salesforce-client-id"
- assert config.client_secret == "salesforce-client-secret"
+ assert OauthIntegration.client_credentials_for_kind("pardot") == OauthClientCredentials(
+ client_id="salesforce-client-id", client_secret="salesforce-client-secret"
+ )
assert config.pkce is True
assert config.id_path == "instance_url"
# Salesforce's `full` scope does not cover the Account Engagement API, so a token
@@ -1611,7 +1620,7 @@ class TestPardotIntegrationModel(BaseTest):
@override_settings(SALESFORCE_CONSUMER_KEY="", SALESFORCE_CONSUMER_SECRET="")
def test_oauth_config_unconfigured_raises(self):
with pytest.raises(NotImplementedError, match="Salesforce app not configured"):
- OauthIntegration.oauth_config_for_kind("pardot")
+ OauthIntegration.client_credentials_for_kind("pardot")
@patch("posthog.models.integration.oauth.reload_integrations_on_workers")
@patch("posthog.models.integration.oauth.requests.post")
@@ -1667,8 +1676,9 @@ class TestYouTubeAnalyticsIntegrationModel(SimpleTestCase):
assert config.authorize_url == "https://accounts.google.com/o/oauth2/v2/auth"
assert config.token_url == "https://oauth2.googleapis.com/token"
- assert config.client_id == "youtube-client-id"
- assert config.client_secret == "youtube-client-secret"
+ assert OauthIntegration.client_credentials_for_kind("youtube-analytics") == OauthClientCredentials(
+ client_id="youtube-client-id", client_secret="youtube-client-secret"
+ )
assert config.id_path == "sub"
assert config.name_path == "email"
# A refresh token only comes back when consent is forced, and the sync depends on one.
@@ -1685,7 +1695,7 @@ class TestYouTubeAnalyticsIntegrationModel(SimpleTestCase):
@override_settings(YOUTUBE_ANALYTICS_APP_CLIENT_ID="", YOUTUBE_ANALYTICS_APP_CLIENT_SECRET="")
def test_oauth_config_unconfigured_raises(self):
with pytest.raises(NotImplementedError, match="YouTube Analytics app not configured"):
- OauthIntegration.oauth_config_for_kind("youtube-analytics")
+ OauthIntegration.client_credentials_for_kind("youtube-analytics")
_APPLE_ADS_APP_PRIVATE_KEY = (
@@ -1725,14 +1735,15 @@ class TestAppleAdsIntegrationModel(BaseTest):
# Apple issues no static secret: it reads `client_secret` as an ES256 assertion naming the
# registration. Posting a literal setting value instead fails every grant at the exchange.
config = OauthIntegration.oauth_config_for_kind("apple-ads")
+ client = OauthIntegration.client_credentials_for_kind("apple-ads")
assert config.authorize_url == "https://appleid.apple.com/auth/oauth2/v2/authorize"
assert config.token_url == "https://appleid.apple.com/auth/oauth2/token"
- assert config.client_id == "SEARCHADS.posthog"
+ assert client.client_id == "SEARCHADS.posthog"
assert config.scope == "searchads"
claims = jwt.decode(
- config.client_secret,
+ client.client_secret,
_APPLE_ADS_APP_PRIVATE_KEY,
algorithms=["ES256"],
audience="https://appleid.apple.com",
@@ -1740,7 +1751,7 @@ class TestAppleAdsIntegrationModel(BaseTest):
)
assert claims["iss"] == "SEARCHADS.team"
assert claims["sub"] == "SEARCHADS.posthog"
- assert jwt.get_unverified_header(config.client_secret)["kid"] == "key-1"
+ assert jwt.get_unverified_header(client.client_secret)["kid"] == "key-1"
@parameterized.expand(
["APPLE_ADS_APP_CLIENT_ID", "APPLE_ADS_APP_TEAM_ID", "APPLE_ADS_APP_KEY_ID", "APPLE_ADS_APP_PRIVATE_KEY"]
@@ -1748,7 +1759,7 @@ class TestAppleAdsIntegrationModel(BaseTest):
def test_oauth_config_unconfigured_raises(self, missing_setting: str):
with override_settings(**{missing_setting: ""}):
with pytest.raises(NotImplementedError, match="Apple Ads service provider app not configured"):
- OauthIntegration.oauth_config_for_kind("apple-ads")
+ OauthIntegration.client_credentials_for_kind("apple-ads")
@patch("posthog.models.integration.oauth.requests.get")
@patch("posthog.models.integration.oauth.requests.post")
@@ -1837,3 +1848,120 @@ class TestAppleAdsIntegrationModel(BaseTest):
]
assert integration.sensitive_config["access_token"] == "at_2"
assert integration.errors == ""
+
+
+CREDENTIALS_SERVICE_SETTINGS = {
+ "CREDENTIALS_SERVICE_URL": "http://credentials-service.credentials-service.svc.cluster.local",
+ "CREDENTIALS_SERVICE_JWT_SECRET": "signing-key",
+}
+CREDENTIALS_SERVICE_FLAG = "posthog.credentials_service.client.posthoganalytics.feature_enabled"
+CREDENTIALS_SERVICE_POST = "posthog.credentials_service.client.internal_requests.post"
+# Kinds whose credentials are not a plain id/secret pair, so they resolve in their own branch.
+SPECIALLY_RESOLVED_KINDS = {"posthog", "slack", "apple-ads"}
+
+
+def _credentials_service_response(values: dict[str, str], missing: list[str] | None = None) -> MagicMock:
+ response = MagicMock(status_code=200)
+ response.json.return_value = {
+ "secrets": {key: {"state": "steady", "value": value, "version_id": "v1"} for key, value in values.items()},
+ "missing": missing or [],
+ }
+ return response
+
+
+def _credentials_service_claims(post: MagicMock) -> dict:
+ token = post.call_args.kwargs["headers"]["Authorization"].removeprefix("Bearer ")
+ return jwt.decode(token, "signing-key", algorithms=["HS256"], audience=PosthogJwtAudience.CREDENTIALS_SERVICE.value)
+
+
+class TestOauthClientCredentialsResolution(SimpleTestCase):
+ # A kind with no credential mapping can be connected nowhere, and a mistyped key name reads as
+ # "not configured" on every deployment while the real setting sits unused.
+ @parameterized.expand([(kind,) for kind in [*OauthIntegration.supported_kinds, "salesforce-sandbox"]])
+ def test_every_oauth_kind_maps_to_real_credential_settings(self, kind: str) -> None:
+ if kind in SPECIALLY_RESOLVED_KINDS:
+ return
+ keys = OAUTH_CLIENT_CREDENTIAL_KEYS[kind]
+ assert hasattr(settings, keys.client_id)
+ assert hasattr(settings, keys.client_secret)
+
+ # The static config is cached for five minutes and read on every serializer pass through
+ # `display_name`. A credential read there would hold a secret past its rotation and call the
+ # service for every integration in a list.
+ @parameterized.expand([(kind,) for kind in OauthIntegration.supported_kinds])
+ @override_settings(**CREDENTIALS_SERVICE_SETTINGS)
+ def test_static_config_and_display_name_never_read_credentials(self, kind: str) -> None:
+ region = "US" if kind == "posthog" else None
+ integration = Integration(kind=kind, integration_id="the-id", config={"region": region})
+ with (
+ patch(CREDENTIALS_SERVICE_FLAG, return_value=True),
+ patch(CREDENTIALS_SERVICE_POST, side_effect=AssertionError("read a credential")) as post,
+ ):
+ OauthIntegration.oauth_config_for_kind(kind, region)
+ assert integration.display_name
+
+ post.assert_not_called()
+
+
+@override_settings(**CREDENTIALS_SERVICE_SETTINGS)
+class TestOauthClientCredentialsFromTheService(BaseTest):
+ def setUp(self) -> None:
+ super().setUp()
+ flag = patch(CREDENTIALS_SERVICE_FLAG, return_value=True)
+ flag.start()
+ self.addCleanup(flag.stop)
+
+ @patch("posthog.models.integration.oauth.reload_integrations_on_workers")
+ @patch("posthog.models.integration.oauth.requests.post")
+ def test_token_refresh_sends_the_credentials_the_service_holds(self, mock_post, mock_reload):
+ mock_post.return_value.status_code = 200
+ mock_post.return_value.json.return_value = {"access_token": "REFRESHED", "expires_in": 1000}
+ integration = Integration.objects.create(
+ team=self.team, kind="hubspot", config={"expires_in": 1000}, sensitive_config={"refresh_token": "RT"}
+ )
+ service_values = {"HUBSPOT_APP_CLIENT_ID": "service-id", "HUBSPOT_APP_CLIENT_SECRET": "service-secret"}
+
+ with (
+ # A stale copy still in settings must lose to the value the service holds.
+ self.settings(HUBSPOT_APP_CLIENT_ID="stale-id", HUBSPOT_APP_CLIENT_SECRET="stale-secret"),
+ patch(CREDENTIALS_SERVICE_POST, return_value=_credentials_service_response(service_values)) as service,
+ ):
+ OauthIntegration(integration).refresh_access_token()
+
+ sent = mock_post.call_args.kwargs["data"]
+ assert (sent["client_id"], sent["client_secret"]) == ("service-id", "service-secret")
+ claims = _credentials_service_claims(service)
+ assert claims["caller"] == "integrations"
+ assert claims["keys"] == sorted(service_values)
+ integration.refresh_from_db()
+ assert integration.sensitive_config["access_token"] == "REFRESHED"
+
+ @parameterized.expand(
+ [
+ # Self-hosted edits the Slack app in instance settings, and that edit beats the env var it
+ # defaults from. Reading the env var directly with the service off would ignore the edit.
+ ("service_off", False, {}, ["SLACK_APP_CLIENT_ID", "SLACK_APP_CLIENT_SECRET"], "instance-id"),
+ ("service_missing_the_key", True, {}, ["SLACK_APP_CLIENT_ID", "SLACK_APP_CLIENT_SECRET"], "instance-id"),
+ (
+ "service_holds_the_key",
+ True,
+ {"SLACK_APP_CLIENT_ID": "service-id", "SLACK_APP_CLIENT_SECRET": "service-secret"},
+ [],
+ "service-id",
+ ),
+ ]
+ )
+ def test_slack_falls_back_to_instance_settings(
+ self, _name, service_on, service_values, service_missing, expected_client_id
+ ):
+ set_instance_setting("SLACK_APP_CLIENT_ID", "instance-id")
+ set_instance_setting("SLACK_APP_CLIENT_SECRET", "instance-secret")
+ with (
+ patch(CREDENTIALS_SERVICE_FLAG, return_value=service_on),
+ patch.dict("os.environ", {"SLACK_APP_CLIENT_ID": "env-id", "SLACK_APP_CLIENT_SECRET": "env-secret"}),
+ patch(
+ CREDENTIALS_SERVICE_POST,
+ return_value=_credentials_service_response(service_values, service_missing),
+ ),
+ ):
+ assert OauthIntegration.client_credentials_for_kind("slack").client_id == expected_client_id
diff --git a/posthog/settings/integrations.py b/posthog/settings/integrations.py
index 4c0a438fa999..9485fc4cdf5a 100644
--- a/posthog/settings/integrations.py
+++ b/posthog/settings/integrations.py
@@ -258,13 +258,3 @@ POSTHOG_CONNECT_OAUTH_CLIENT_SECRET_DEV = get_from_env("POSTHOG_CONNECT_OAUTH_CL
POSTHOG_CONNECT_BASE_URL_US = get_from_env("POSTHOG_CONNECT_BASE_URL_US", "https://us.posthog.com")
POSTHOG_CONNECT_BASE_URL_EU = get_from_env("POSTHOG_CONNECT_BASE_URL_EU", "https://eu.posthog.com")
POSTHOG_CONNECT_BASE_URL_DEV = get_from_env("POSTHOG_CONNECT_BASE_URL_DEV", "http://localhost:8000")
-
-# Legacy OAuth client credentials kept alive during an app or secret rotation.
-# Refreshes fall back to these when the primary credentials fail, so tokens issued
-# by a since-migrated app keep working until users reconnect.
-OAUTH_CLIENT_FALLBACKS: dict[str, dict[str, str]] = {
- "bing-ads": {
- "client_id": BING_ADS_CLIENT_ID_FALLBACK,
- "client_secret": BING_ADS_CLIENT_SECRET_FALLBACK,
- },
-}
diff --git a/posthog/test/repo_invariants/dataclass_secret_field_exemptions.txt b/posthog/test/repo_invariants/dataclass_secret_field_exemptions.txt
index 274c52c560ee..01162b9b9e4d 100644
--- a/posthog/test/repo_invariants/dataclass_secret_field_exemptions.txt
+++ b/posthog/test/repo_invariants/dataclass_secret_field_exemptions.txt
@@ -1,5 +1,7 @@
posthog/clickhouse/client/connection.py ClickHouseCredentials.password_file
posthog/ingress/verify/schemes.py HmacSignature.secret_getter
+posthog/models/integration/oauth.py _ClientCredentialKeys.client_secret
+posthog/models/integration/oauth.py _PosthogConnectTarget.client_secret_key
products/managed_warehouse/backend/facade/contracts.py DuckLakeCompiledQuery.s3_secrets
products/warehouse_sources/backend/temporal/data_imports/sources/anthropic/anthropic.py ClaudeCodeActor.api_key_name
products/warehouse_sources/backend/temporal/data_imports/sources/fly_io/settings.py FlyIoEndpointConfig.secret_config_field
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/credentials_service.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/credentials_service.py
index cca46086f5bc..d465afa5f22e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/credentials_service.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/credentials_service.py
@@ -19,44 +19,25 @@ Reading through here changes nothing until a key is actually moved into the serv
service off (self-hosted, local development, the flag disabled) the client reads the same
`os.environ`/settings value the source reads today, so a migrated call site behaves identically.
-**Sequencing.** Call sites and keys cannot move at the same instant, and the service is already
-live on the data-warehouse worker — so `_resolve_one` below keeps reading settings for a key that
-has not reached the service yet, counting every time it does. That makes the order of the two
-moves not matter, and makes "is the rollout finished?" a metric rather than a belief. It is
-temporary; the removal condition is on `_resolve_one`.
+A key the service does not hold yet falls back to settings, counted per key. That bridge, and why
+it never applies to a key in recovery or to an unreachable service, lives in
+`posthog.credentials_service.settings_fallback`.
"""
from __future__ import annotations
-import os
-
-from django.conf import settings
-
import requests
-import structlog
-from prometheus_client import Counter
from urllib3.util.retry import Retry
from posthog.credentials_service import client as credentials_service_client
from posthog.credentials_service.callers import CredentialsCaller
from posthog.credentials_service.client import RotatingSecret
-from posthog.credentials_service.errors import SecretMissingError
+from posthog.credentials_service.settings_fallback import get_many_or_settings, get_or_settings
from products.warehouse_sources.backend.temporal.data_imports.sources.common.http import make_tracked_session
-logger = structlog.get_logger(__name__)
-
CALLER = CredentialsCaller.WAREHOUSE_SOURCES
-SETTINGS_FALLBACK_COUNTER = Counter(
- "posthog_warehouse_credentials_service_settings_fallback_total",
- "Platform credential reads the credentials service could not serve, by what happened instead",
- # outcome="settings": the key is still only in settings, so the rollout isn't finished for it.
- # outcome="unset": the credential isn't configured anywhere, which is the normal state of an
- # integration a deployment doesn't use. Separated because only the first is a rollout gap.
- labelnames=["key", "outcome"],
-)
-
def _build_session() -> requests.Session:
"""The tracked session credential reads go over, with two deliberate departures.
@@ -91,73 +72,14 @@ _session = _build_session()
_client = credentials_service_client.CredentialsServiceClient(session=_session)
-def _from_settings(key: str) -> str | None:
- """The value this key had before the service existed, or None."""
- return (os.environ.get(key) or getattr(settings, key, "")) or None
-
-
-def _resolve_one(key: str) -> str:
- """One credential, falling back to settings while the key is still on its way into the service.
-
- TEMPORARY, and the removal condition is exact: when
- `posthog_warehouse_credentials_service_settings_fallback_total` has been zero across a full sync
- cycle, every key has moved and this function collapses back to `_client.get`.
-
- It exists because the call sites and the keys cannot move at the same instant. The service is
- already live on the data-warehouse worker, so without this, migrating a call site whose key is
- not in the service yet fails every sync for that source the moment it deploys — for Google Ads,
- Analytics, Search Console, Sheets, HubSpot, Salesforce, TikTok and Bing at once. The same code
- also runs in the web app, where the service is not configured at all.
-
- It is not the silent fallback the client refuses to have. That one is dangerous because it
- hides a half-finished rollout behind apparent success; this one names the key, counts it, and
- is the thing you watch to know when the rollout is finished.
-
- Only a MISSING key falls back, and the two exclusions are the point:
-
- - A key in **recovery** must not fall back. Recovery means the credential is known-burned; the
- settings copy is the very value that was burned, so serving it would turn the kill switch
- into a no-op — the one failure mode worse than the sync stopping.
- - An **unreachable service** must not fall back. There is no last known good to fall back to,
- and treating an outage as "the key isn't there" would mask it as a rollout gap.
- """
- try:
- return _client.get(key, CALLER)
- except SecretMissingError:
- value = _from_settings(key)
- if value is not None:
- SETTINGS_FALLBACK_COUNTER.labels(key=key, outcome="settings").inc()
- logger.warning("warehouse_credentials_service.settings_fallback", key=key)
- return value
-
- # Configured nowhere. Before this migration the call site read `settings.X` and got the
- # empty string its default provides, and an unusable credential surfaced later as a
- # rejection from the vendor. Keep exactly that: this change moves where a credential comes
- # from, and must not also change what happens when there isn't one. Raising here instead
- # would alter behaviour for every deployment that doesn't use a given integration —
- # self-hosted, local development, and every test that never set it — which is a much wider
- # blast radius than the migration itself, and no part of what it is for.
- SETTINGS_FALLBACK_COUNTER.labels(key=key, outcome="unset").inc()
- logger.debug("warehouse_credentials_service.unset", key=key)
- return ""
-
-
def get_secret(key: str) -> str:
"""One PostHog-owned credential, by the name it has in the service and in settings."""
- return _resolve_one(key)
+ return get_or_settings(key, CALLER, client=_client)
def get_secrets(keys: list[str]) -> dict[str, str]:
- """Several at once, in a single request — a source usually needs an id and a secret together.
-
- The batch resolves in one request. If any key in it is missing, the batch cannot say which,
- so the keys are re-resolved one at a time to let the fallback above apply per key. That costs
- an extra round trip only while a source is mid-migration, and disappears with the fallback.
- """
- try:
- return _client.get_many(keys, CALLER)
- except SecretMissingError:
- return {key: _resolve_one(key) for key in keys}
+ """Several at once, in a single request — a source usually needs an id and a secret together."""
+ return get_many_or_settings(keys, CALLER, client=_client)
def get_secret_with_incoming(key: str) -> RotatingSecret:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/common/tests/test_credentials_service.py b/products/warehouse_sources/backend/temporal/data_imports/sources/common/tests/test_credentials_service.py
index 30b400927ffd..a6b585b7e742 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/common/tests/test_credentials_service.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/common/tests/test_credentials_service.py
@@ -9,7 +9,6 @@ import jwt
import requests
from posthog.credentials_service.callers import CredentialsCaller
-from posthog.credentials_service.errors import CredentialsServiceUnreachableError, SecretInRecoveryError
from posthog.jwt import PosthogJwtAudience
from products.warehouse_sources.backend.temporal.data_imports.sources.common import credentials_service
@@ -167,107 +166,3 @@ class TestNoRequestsExceptionEscapes(SimpleTestCase):
):
with pytest.raises(CredentialsServiceUnreachableError):
credentials_service.get_secret(KEY)
-
-
-@override_settings(**SERVICE_SETTINGS)
-class TestSettingsFallbackBridge(SimpleTestCase):
- """The temporary bridge that lets call sites move before their keys do.
-
- Every test here is about a boundary of it. The bridge is the risky part of this migration:
- too wide and it hides a broken rollout, too narrow and it breaks eight sources on deploy.
- """
-
- def setUp(self) -> None:
- flag = mock.patch(FLAG, return_value=True)
- flag.start()
- self.addCleanup(flag.stop)
- credentials_service.SETTINGS_FALLBACK_COUNTER.clear()
-
- def _missing(self) -> Any:
- return mock.patch.object(
- credentials_service._session,
- "post",
- return_value=_FakeResponse({"secrets": {}, "missing": [KEY]}),
- )
-
- # The whole point: the service is already live on the worker, so a call site whose key hasn't
- # moved must keep working rather than failing every sync for that source.
- def test_a_key_not_in_the_service_falls_back_to_settings(self) -> None:
- with self._missing():
- with mock.patch.dict("os.environ", {KEY: "still-in-settings"}):
- assert credentials_service.get_secret(KEY) == "still-in-settings"
-
- # The fallback is what you watch to know the rollout is done, so it has to be countable and
- # name the key. A silent one would be the failure mode the client refuses to have.
- def test_the_fallback_is_counted_and_named(self) -> None:
- with self._missing():
- with mock.patch.dict("os.environ", {KEY: "still-in-settings"}):
- with mock.patch.object(credentials_service, "logger") as log:
- credentials_service.get_secret(KEY)
-
- assert credentials_service.SETTINGS_FALLBACK_COUNTER.labels(key=KEY, outcome="settings")._value.get() == 1
- assert log.warning.call_args.kwargs["key"] == KEY
-
- # Recovery means the credential is known-burned. The settings copy is the burned value, so
- # falling back would turn the kill switch into a no-op — worse than the sync stopping.
- def test_recovery_does_not_fall_back(self) -> None:
- payload = {"secrets": {KEY: {"state": "recovery", "version_id": "v1", "fetched_at": "now"}}, "missing": []}
- with mock.patch.object(credentials_service._session, "post", return_value=_FakeResponse(payload)):
- with mock.patch.dict("os.environ", {KEY: "the-burned-value"}):
- with pytest.raises(SecretInRecoveryError):
- credentials_service.get_secret(KEY)
-
- assert credentials_service.SETTINGS_FALLBACK_COUNTER.labels(key=KEY, outcome="settings")._value.get() == 0
-
- # An outage is not a rollout gap. There is no last known good here, and treating "no answer"
- # as "the key isn't there" would quietly serve stale credentials through an incident.
- def test_an_unreachable_service_does_not_fall_back(self) -> None:
- with mock.patch.object(credentials_service._session, "post", side_effect=requests.ConnectionError("refused")):
- with mock.patch.dict("os.environ", {KEY: "still-in-settings"}):
- with pytest.raises(CredentialsServiceUnreachableError):
- credentials_service.get_secret(KEY)
-
- # Configured nowhere is the normal state of an integration a deployment doesn't use, and it
- # is what `settings.X` already returned. This migration moves where a credential comes from;
- # changing what happens when there isn't one would reach far past it — every self-hosted
- # install without this integration, and every test that never set it.
- def test_configured_nowhere_returns_what_settings_returned(self) -> None:
- with self._missing():
- with mock.patch.dict("os.environ", {}, clear=True):
- assert credentials_service.get_secret(KEY) == ""
-
- counter = credentials_service.SETTINGS_FALLBACK_COUNTER
- assert counter.labels(key=KEY, outcome="unset")._value.get() == 1
- # Not a rollout gap, so it must not be counted as one.
- assert counter.labels(key=KEY, outcome="settings")._value.get() == 0
-
- # A part-migrated batch is the normal state during the rollout: the batch can't say which key
- # was missing, so it re-resolves per key and each one takes the path it needs.
- def test_a_batch_with_one_missing_key_resolves_the_rest_per_key(self) -> None:
- other = "GOOGLE_ADS_APP_CLIENT_ID"
- in_service = {"state": "steady", "value": "from-service", "version_id": "v1", "fetched_at": "now"}
-
- def post(*args: Any, **kwargs: Any) -> Any:
- asked = _keys_asked_for(kwargs["headers"]["Authorization"])
- if KEY in asked and other in asked:
- return _FakeResponse({"secrets": {other: in_service}, "missing": [KEY]})
- if asked == [other]:
- return _FakeResponse({"secrets": {other: in_service}, "missing": []})
- return _FakeResponse({"secrets": {}, "missing": [KEY]})
-
- with mock.patch.object(credentials_service._session, "post", side_effect=post):
- with mock.patch.dict("os.environ", {KEY: "still-in-settings"}):
- assert credentials_service.get_secrets([KEY, other]) == {
- KEY: "still-in-settings",
- other: "from-service",
- }
-
-
-def _keys_asked_for(authorization: str) -> list[str]:
- claims = jwt.decode(
- authorization.removeprefix("Bearer "),
- "signing-key",
- algorithms=["HS256"],
- audience=PosthogJwtAudience.CREDENTIALS_SERVICE.value,
- )
- return list(claims["keys"])