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.

test(oauth): pin OIDC key in oauth admin revoke tests (#62557)

Phil Haack48885eeea34b
diff --git a/ee/api/agentic_provisioning/test/base.py b/ee/api/agentic_provisioning/test/base.py
index e49f42b71988..051e0bb7a494 100644
--- a/ee/api/agentic_provisioning/test/base.py
+++ b/ee/api/agentic_provisioning/test/base.py
@@ -9,10 +9,10 @@ from django.conf import settings
from django.core.cache import cache
from django.test import override_settings
-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import rsa
from rest_framework.test import APIClient
+from posthog.test.oauth_test_utils import TEST_RSA_PRIVATE_KEY as _RSA_KEY
+
from ee.api.agentic_provisioning.signature import compute_signature
from ee.api.agentic_provisioning.views import AUTH_CODE_CACHE_PREFIX
@@ -20,19 +20,6 @@ HMAC_SECRET = "test_hmac_secret"
TEST_STRIPE_OAUTH_CLIENT_ID = "test_stripe_oauth_client_id"
-def _generate_rsa_key() -> str:
- private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
- pem = private_key.private_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PrivateFormat.TraditionalOpenSSL,
- encryption_algorithm=serialization.NoEncryption(),
- )
- return pem.decode("utf-8")
-
-
-_RSA_KEY = _generate_rsa_key()
-
-
@pytest.mark.requires_secrets
@override_settings(
STRIPE_SIGNING_SECRET=HMAC_SECRET,
diff --git a/ee/api/agentic_provisioning/test/test_provisioning_auth.py b/ee/api/agentic_provisioning/test/test_provisioning_auth.py
index 569e658bd9ca..dcb7ae937f5a 100644
--- a/ee/api/agentic_provisioning/test/test_provisioning_auth.py
+++ b/ee/api/agentic_provisioning/test/test_provisioning_auth.py
@@ -13,11 +13,10 @@ from django.conf import settings
from django.core.cache import cache as real_cache
from django.test import override_settings
-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives.asymmetric import rsa
from rest_framework.test import APIClient
from posthog.models.oauth import OAuthApplication
+from posthog.test.oauth_test_utils import TEST_RSA_PRIVATE_KEY as _RSA_KEY
from ee.api.agentic_provisioning.signature import compute_signature
@@ -26,16 +25,6 @@ WIZARD_CLIENT_ID = "test-wizard-client"
TEST_STRIPE_OAUTH_CLIENT_ID = "test_stripe_oauth_client_id"
-def _generate_rsa_key() -> str:
- private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
- pem = private_key.private_bytes(
- encoding=serialization.Encoding.PEM,
- format=serialization.PrivateFormat.TraditionalOpenSSL,
- encryption_algorithm=serialization.NoEncryption(),
- )
- return pem.decode("utf-8")
-
-
def _pkce_pair():
"""Generate a PKCE code_verifier and code_challenge pair."""
verifier = secrets.token_urlsafe(32)
@@ -43,9 +32,6 @@ def _pkce_pair():
return verifier, challenge
-_RSA_KEY = _generate_rsa_key()
-
-
@pytest.mark.requires_secrets
@override_settings(
STRIPE_SIGNING_SECRET=HMAC_SECRET,
diff --git a/posthog/admin/test_oauth_admin.py b/posthog/admin/test_oauth_admin.py
index 76af26fe21b4..d454b7009a67 100644
--- a/posthog/admin/test_oauth_admin.py
+++ b/posthog/admin/test_oauth_admin.py
@@ -4,16 +4,25 @@ from freezegun import freeze_time
from posthog.test.base import BaseTest
from unittest.mock import patch
+from django.conf import settings
from django.contrib.admin import AdminSite
-from django.test import RequestFactory
+from django.test import RequestFactory, override_settings
from django.utils import timezone
from parameterized import parameterized
from posthog.admin.admins.oauth_admin import OAuthApplicationAdmin, OAuthApplicationForm
from posthog.models.oauth import OAuthAccessToken, OAuthApplication, OAuthRefreshToken
+from posthog.test.oauth_test_utils import TEST_RSA_PRIVATE_KEY
+# Creating an RS256 app requires OIDC_RSA_PRIVATE_KEY, which isn't available on fork/external PR CI and
+# can be cleared by other OAuth tests overriding OAUTH2_PROVIDER (django-oauth-toolkit caches its
+# settings). Pin a test key so these tests pass regardless of environment and ordering.
+@override_settings(
+ OIDC_RSA_PRIVATE_KEY=TEST_RSA_PRIVATE_KEY,
+ OAUTH2_PROVIDER={**settings.OAUTH2_PROVIDER, "OIDC_RSA_PRIVATE_KEY": TEST_RSA_PRIVATE_KEY},
+)
class TestOAuthApplicationAdmin(BaseTest):
def setUp(self):
super().setUp()
diff --git a/posthog/test/oauth_test_utils.py b/posthog/test/oauth_test_utils.py
new file mode 100644
index 000000000000..4d12333402f8
--- /dev/null
+++ b/posthog/test/oauth_test_utils.py
@@ -0,0 +1,16 @@
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives.asymmetric import rsa
+
+
+def generate_rsa_private_key_pem() -> str:
+ private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
+ pem = private_key.private_bytes(
+ encoding=serialization.Encoding.PEM,
+ format=serialization.PrivateFormat.TraditionalOpenSSL,
+ encryption_algorithm=serialization.NoEncryption(),
+ )
+ return pem.decode("utf-8")
+
+
+# Generated once so test modules pinning OIDC_RSA_PRIVATE_KEY share a single key.
+TEST_RSA_PRIVATE_KEY = generate_rsa_private_key_pem()