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(clerk): stop pointing a forbidden sync at key permissions (#109851)

Tom Owers14ceb96ce4b6
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
index a7ad7805e205..782ecb7c2509 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/clerk.py
@@ -234,9 +234,11 @@ _INVALID_KEY_MESSAGE = (
"Your Clerk secret key is invalid or has been revoked. Please update the secret key in your "
"Clerk dashboard and reconnect."
)
-_FORBIDDEN_KEY_MESSAGE = (
- "Your Clerk secret key does not have permission to access this endpoint. Please check the "
- "key's permissions in your Clerk dashboard."
+# The users list has no feature gate, and Clerk secret keys carry no per-key permissions, so a 403
+# there is Clerk refusing the key for its instance.
+FORBIDDEN_KEY_MESSAGE = (
+ "Clerk refused access with your secret key. Copy the secret key of your active Clerk instance "
+ "from your Clerk dashboard, then update it here."
)
_UNSUPPORTED_CHARACTER_MESSAGE = (
"Your Clerk secret key contains a character that can't be sent to Clerk, such as an invisible "
@@ -278,7 +280,7 @@ def validate_credentials(secret_key: str) -> tuple[bool, str | None]:
# revoked key, so explain it the same way instead of filing an error.
return False, _INVALID_KEY_MESSAGE
if response.status_code == 403:
- return False, _FORBIDDEN_KEY_MESSAGE
+ return False, FORBIDDEN_KEY_MESSAGE
if response.status_code == 404:
# The users list exists on every Clerk instance, so a 404 here is Clerk refusing to resolve
# the instance the key belongs to rather than a missing record. That is user input like the
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
index 1aa0d2208165..16a16745d103 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/source.py
@@ -8,6 +8,7 @@ from products.warehouse_sources.backend.facade.source_config import (
SourceFieldInputConfigType,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.clerk.clerk import (
+ FORBIDDEN_KEY_MESSAGE,
INSTANCE_NOT_FOUND_MESSAGE,
ClerkResumeConfig,
clerk_source,
@@ -103,7 +104,14 @@ The secret key starts with `sk_live_`.
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
"401 Client Error: Unauthorized for url: https://api.clerk.com": "Your Clerk secret key is invalid or has been revoked. Please update the secret key in your Clerk dashboard and reconnect.",
- "403 Client Error: Forbidden for url: https://api.clerk.com": "Your Clerk secret key does not have permission to access this endpoint. Please check the key's permissions in your Clerk dashboard.",
+ # Listed before the host-wide 403 below, since the first matching key supplies the message.
+ "403 Client Error: Forbidden for url: https://api.clerk.com/v1/users": FORBIDDEN_KEY_MESSAGE,
+ # Clerk secret keys carry no per-key permissions, so a 403 on any other table usually means
+ # the table's feature is off for the instance. Pointing the customer at key permissions dead-ends.
+ "403 Client Error: Forbidden for url: https://api.clerk.com": (
+ "Clerk denied access to this table. Check that the feature it needs is turned on for "
+ "your Clerk instance, or turn off syncing for this table."
+ ),
# Clerk answers 410 for endpoints it has removed. Schema discovery retires the table
# within a few hours, so this only covers runs that start in between.
"410 Client Error: Gone for url: https://api.clerk.com": "Clerk removed this endpoint from its API, so this table can't sync any more. Turn off syncing for this table.",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
index 18a972a905b7..485850993083 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk.py
@@ -596,7 +596,7 @@ class TestClerkValidateCredentials:
[
(400, "invalid or has been revoked"),
(401, "invalid or has been revoked"),
- (403, "does not have permission"),
+ (403, "active Clerk instance"),
(404, "active Clerk instance"),
(500, "Couldn't validate your Clerk secret key"),
],
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
index 342236d10b55..6bde876700e0 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/clerk/tests/test_clerk_source.py
@@ -47,6 +47,19 @@ class TestClerkSource:
non_retryable_errors = self.source.get_non_retryable_errors()
assert any(key in observed_error for key in non_retryable_errors)
+ @pytest.mark.parametrize(
+ ("observed_error", "expected_substring"),
+ [
+ ("403 Client Error: Forbidden for url: https://api.clerk.com/v1/users?limit=100", "secret key"),
+ ("403 Client Error: Forbidden for url: https://api.clerk.com/v1/role_sets?limit=100", "feature it needs"),
+ ],
+ )
+ def test_403_message_depends_on_the_endpoint(self, observed_error, expected_substring):
+ non_retryable_errors = self.source.get_non_retryable_errors()
+ first_match = next(message for key, message in non_retryable_errors.items() if key in observed_error)
+
+ assert first_match is not None and expected_substring in first_match
+
def test_non_retryable_errors_does_not_match_400_on_other_clerk_endpoints(self):
# A 400 from a different endpoint is a genuinely bad request worth investigating, not the
# known api_keys limitation — the match must stay scoped to `api_keys`.