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.

fix(provisioning): stop handing partners a host their account isn't on (#74880)

Fernando Gomes76fa887616cb
diff --git a/ee/api/agentic_provisioning/regions.py b/ee/api/agentic_provisioning/regions.py
index 45f121195bdd..8ab967fab84c 100644
--- a/ee/api/agentic_provisioning/regions.py
+++ b/ee/api/agentic_provisioning/regions.py
@@ -6,14 +6,24 @@ from posthog.utils import get_instance_region
def current_region_host() -> str:
- """Host of the region this instance serves, for URLs handed to partners."""
- return region_to_host(get_instance_region() or "US")
+ """Host of the region this instance serves, for URLs handed to partners.
+
+ No region means this is not a Cloud deployment (local dev, self-hosted), so the instance's
+ own URL is the only correct answer. Defaulting to a Cloud region instead hands partners a
+ host their freshly provisioned account does not exist on.
+ """
+ region = get_instance_region()
+ if not region:
+ return settings.SITE_URL
+ return region_to_host(region)
def region_to_host(region: str) -> str:
region_lower = region.lower()
if region_lower == "eu":
return "https://eu.posthog.com"
- elif region_lower in ("us", "dev"):
+ elif region_lower == "us":
return "https://us.posthog.com"
+ elif region_lower == "dev":
+ return "https://app.dev.posthog.com"
return settings.SITE_URL
diff --git a/ee/partners/stripe/api/provisioning/core.py b/ee/partners/stripe/api/provisioning/core.py
index d6bc7b5e63b6..0757bec47fbd 100644
--- a/ee/partners/stripe/api/provisioning/core.py
+++ b/ee/partners/stripe/api/provisioning/core.py
@@ -685,8 +685,10 @@ def region_to_host(region: str) -> str:
region_lower = region.lower()
if region_lower == "eu":
return "https://eu.posthog.com"
- elif region_lower in ("us", "dev"):
+ elif region_lower == "us":
return "https://us.posthog.com"
+ elif region_lower == "dev":
+ return "https://app.dev.posthog.com"
return settings.SITE_URL
diff --git a/products/demo/backend/logic/products/hedgebox/matrix.py b/products/demo/backend/logic/products/hedgebox/matrix.py
index 22275620acc2..8aebc609a928 100644
--- a/products/demo/backend/logic/products/hedgebox/matrix.py
+++ b/products/demo/backend/logic/products/hedgebox/matrix.py
@@ -9,7 +9,6 @@ from urllib.parse import urlparse, urlunparse
from django.apps import apps
from django.conf import settings
-from django.core.exceptions import ValidationError
from django.db import IntegrityError
from posthog.schema import (
@@ -51,6 +50,7 @@ from posthog.constants import PAGEVIEW_EVENT
from posthog.exceptions_capture import capture_exception
from posthog.models.event.util import create_event
from posthog.models.oauth import OAuthApplication
+from posthog.models.oauth_provisioning import ProvisioningConfig
from posthog.scopes import UNPRIVILEGED_SCOPES
from posthog.storage import object_storage
@@ -1804,29 +1804,38 @@ class HedgeboxMatrix(Matrix):
if not (settings.OIDC_RSA_PRIVATE_KEY and settings.DEBUG and not is_cloud()):
return
- try:
- OAuthApplication.objects.create(
- name="Demo OAuth Application",
- client_id="DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ",
- client_secret="GQItUP4GqE6t5kjcWIRfWO9c0GXPCY8QDV4eszH4PnxXwCVxIMVSil4Agit7yay249jasnzHEkkVqHnFMxI1YTXSrh8Bj1sl1IDfNi1S95sv208NOc0eoUBP3TdA7vf0",
- redirect_uris="http://localhost:3000/callback http://localhost:8237/callback http://localhost:8239/callback",
- user=user,
- organization=team.organization,
- client_type=OAuthApplication.CLIENT_PUBLIC,
- authorization_grant_type=OAuthApplication.GRANT_AUTHORIZATION_CODE,
- algorithm="RS256",
- is_first_party=True,
+ OAuthApplication.objects.update_or_create(
+ client_id="DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ",
+ defaults={
+ "name": "Demo OAuth Application",
+ "client_secret": "GQItUP4GqE6t5kjcWIRfWO9c0GXPCY8QDV4eszH4PnxXwCVxIMVSil4Agit7yay249jasnzHEkkVqHnFMxI1YTXSrh8Bj1sl1IDfNi1S95sv208NOc0eoUBP3TdA7vf0",
+ "redirect_uris": "http://localhost:3000/callback http://localhost:8237/callback http://localhost:8239/callback",
+ "user": user,
+ "organization": team.organization,
+ "client_type": OAuthApplication.CLIENT_PUBLIC,
+ "authorization_grant_type": OAuthApplication.GRANT_AUTHORIZATION_CODE,
+ "algorithm": "RS256",
+ "is_first_party": True,
# An empty ceiling resolves to UNPRIVILEGED_SCOPES at /authorize, which
# excludes the privileged/hidden scopes the onboarding wizard requests
# (llm_gateway:read, wizard_session:*) — so it failed with invalid_scope.
# Reproduce the broad default and add those so the wizard works locally.
- scopes=sorted(
+ "scopes": sorted(
UNPRIVILEGED_SCOPES
| {"llm_gateway:read", "llm_gateway:write", "wizard_session:read", "wizard_session:write"}
),
- )
- except (IntegrityError, ValidationError):
- pass
+ # The wizard can also provision an account rather than logging into one, which
+ # every provisioning endpoint gates on. Every capability defaults to False, so
+ # without these account creation fails at authentication with "no provisioning
+ # client is registered" and the flow can't be exercised locally at all.
+ "is_provisioning_partner": True,
+ "_provisioning_config": ProvisioningConfig(
+ active=True,
+ can_create_accounts=True,
+ can_provision_resources=True,
+ ).model_dump(mode="json"),
+ },
+ )
def _set_up_error_tracking_demo_data(self, team: "Team") -> None:
issue_specs: list[ErrorTrackingDemoIssueSpec] = [
diff --git a/products/demo/backend/test/test_hedgebox_matrix.py b/products/demo/backend/test/test_hedgebox_matrix.py
index e412fa77fa08..a23a3b70f160 100644
--- a/products/demo/backend/test/test_hedgebox_matrix.py
+++ b/products/demo/backend/test/test_hedgebox_matrix.py
@@ -317,7 +317,7 @@ class TestHedgeboxMatrixDemoOAuthApplication(SimpleTestCase):
("no_oidc_key", "", True, False, True),
]
)
- @patch("products.demo.backend.logic.products.hedgebox.matrix.OAuthApplication.objects.create")
+ @patch("products.demo.backend.logic.products.hedgebox.matrix.OAuthApplication.objects.update_or_create")
def test_demo_oauth_app_only_created_in_local_dev(
self,
_name: str,
@@ -325,7 +325,7 @@ class TestHedgeboxMatrixDemoOAuthApplication(SimpleTestCase):
debug: bool,
cloud: bool,
should_skip: bool,
- mock_create: MagicMock,
+ mock_update_or_create: MagicMock,
) -> None:
matrix = HedgeboxMatrix(seed="oauth-test", n_clusters=0)
team = cast(Any, SimpleNamespace(organization=SimpleNamespace()))
@@ -336,9 +336,10 @@ class TestHedgeboxMatrixDemoOAuthApplication(SimpleTestCase):
matrix._set_up_demo_oauth_application(team, user)
if should_skip:
- mock_create.assert_not_called()
+ mock_update_or_create.assert_not_called()
else:
- mock_create.assert_called_once()
- kwargs = mock_create.call_args.kwargs
- assert kwargs["is_first_party"] is True
- assert "example.com" not in kwargs["redirect_uris"]
+ mock_update_or_create.assert_called_once()
+ call_kwargs = mock_update_or_create.call_args.kwargs
+ assert call_kwargs["client_id"] == "DC5uRLVbGI02YQ82grxgnK6Qn12SXWpCqdPb60oZ"
+ assert call_kwargs["defaults"]["is_first_party"] is True
+ assert "example.com" not in call_kwargs["defaults"]["redirect_uris"]