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(e2b): say what to do when the API key is rejected at setup (#102301)

Tom Owersda1dea8dd625
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/e2b.py b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/e2b.py
index a58cdcc9a83b..61ce6ebce3f1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/e2b.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/e2b.py
@@ -27,6 +27,17 @@ NEXT_TOKEN_PARAM = "nextToken"
# read credentials they can't see in the protected source config, so we drop it before ingesting.
SENSITIVE_FIELDS = ("metadata",)
+# Shared with the source's 401 and 403 entries in `get_non_retryable_errors` so a rejected key reads
+# the same whether it surfaces while the source is being set up or during a later sync.
+INVALID_CREDENTIALS_ERROR = (
+ "Your E2B API key is invalid or has been revoked. Create a new team-scoped API key in your E2B "
+ "dashboard, then reconnect."
+)
+NO_ACCESS_ERROR = (
+ "Your E2B API key does not have access to this data. Check the key's team scope in your E2B "
+ "dashboard, then reconnect."
+)
+
def _scrub(item: dict[str, Any]) -> dict[str, Any]:
return {key: value for key, value in item.items() if key not in SENSITIVE_FIELDS}
@@ -168,11 +179,13 @@ def e2b_source(
)
-def validate_credentials(api_key: str) -> bool:
- # Cheapest authenticated probe: list a single sandbox. 200 means the team-scoped key is genuine,
- # 401/403 means it isn't. Anything else — a timeout, connection error, rate limit, or 5xx — is a
- # transient upstream problem that says nothing about the key, so raise rather than mislabel a valid
- # key "invalid" and send the user down the wrong recovery path.
+def validate_credentials(api_key: str) -> tuple[bool, str | None]:
+ # Cheapest authenticated probe: list a single sandbox. 200 means the team-scoped key is genuine.
+ # A 401 and a 403 need different next steps — a revoked key has to be replaced, a key scoped to
+ # the wrong team does not — so they map to their own messages. Anything else — a timeout,
+ # connection error, rate limit, or 5xx — is a transient upstream problem that says nothing about
+ # the key, so raise rather than mislabel a valid key "invalid" and send the user down the wrong
+ # recovery path.
# `redact_values` masks the key from tracked HTTP samples (the `X-API-Key` header isn't on the
# generic scrubber's denylist); `allow_redirects=False` keeps the key from replaying to another host;
# `capture=False` keeps the raw response body out of sample storage, since a sandbox's user-set
@@ -183,7 +196,9 @@ def validate_credentials(api_key: str) -> bool:
headers={"X-API-Key": api_key, "Accept": "application/json"},
)
if ok:
- return True
- if status in (401, 403):
- return False
+ return True, None
+ if status == 401:
+ return False, INVALID_CREDENTIALS_ERROR
+ if status == 403:
+ return False, NO_ACCESS_ERROR
raise E2BRetryableError(f"E2B credential probe failed (retryable): status={status}")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/source.py
index 5006f0ff6340..586a52787ff5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/source.py
@@ -16,6 +16,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.common.res
from products.warehouse_sources.backend.temporal.data_imports.sources.common.schema import SourceSchema
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs, SourceResponse
from products.warehouse_sources.backend.temporal.data_imports.sources.e2b.e2b import (
+ INVALID_CREDENTIALS_ERROR,
+ NO_ACCESS_ERROR,
E2BResumeConfig,
e2b_source,
validate_credentials as validate_e2b_credentials,
@@ -75,8 +77,8 @@ You can create a team-scoped API key (prefixed `e2b_`) in your [E2B dashboard](h
return {
# An invalid or revoked API key surfaces as an HTTPError when `_fetch_page` calls
# `raise_for_status()`. Retrying can never satisfy a credential problem, so stop the sync.
- "401 Client Error: Unauthorized for url: https://api.e2b.app": "Your E2B API key is invalid or has been revoked. Create a new team-scoped API key in your E2B dashboard, then reconnect.",
- "403 Client Error: Forbidden for url: https://api.e2b.app": "Your E2B API key does not have access to this data. Check the key's team scope in your E2B dashboard, then reconnect.",
+ "401 Client Error: Unauthorized for url: https://api.e2b.app": INVALID_CREDENTIALS_ERROR,
+ "403 Client Error: Forbidden for url: https://api.e2b.app": NO_ACCESS_ERROR,
}
def get_schemas(
@@ -108,9 +110,7 @@ You can create a team-scoped API key (prefixed `e2b_`) in your [E2B dashboard](h
self, config: E2BSourceConfig, team_id: int, schema_name: Optional[str] = None, api_version: str | None = None
) -> tuple[bool, str | None]:
try:
- if validate_e2b_credentials(config.api_key):
- return True, None
- return False, "Invalid E2B API key"
+ return validate_e2b_credentials(config.api_key)
except Exception:
# The probe couldn't reach E2B (timeout, rate limit, or a 5xx). That says nothing about the
# key, so point the user at retrying rather than at their credentials.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b.py b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b.py
index 7875745ce4af..bfcd52aa01b5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b.py
@@ -191,7 +191,9 @@ class TestValidateCredentials:
def test_status_code_maps_to_validity(self, _name: str, status: int, expected: bool, MockSession) -> None:
MockSession.return_value.get.return_value = mock.MagicMock(status_code=status)
- assert validate_credentials("e2b_test") is expected
+ ok, error = validate_credentials("e2b_test")
+ assert ok is expected
+ assert (error is None) is expected
# The key rides in the X-API-Key header, which the generic scrubber's denylist doesn't cover, so
# the probe must redact it, pin redirects off to stop it replaying elsewhere, and disable capture
# so the sandbox response body never reaches sample storage.
@@ -201,6 +203,24 @@ class TestValidateCredentials:
"capture": False,
}
+ # A key scoped to another team has to be re-scoped, not replaced, so a 403 must not read as a
+ # revoked key and send someone off to generate another one that fails the same way.
+ @parameterized.expand(
+ [
+ (401, "invalid or has been revoked"),
+ (403, "does not have access to this data"),
+ ]
+ )
+ @mock.patch(E2B_SESSION_PATCH)
+ def test_a_revoked_key_and_a_key_without_access_read_differently(
+ self, status: int, expected: str, MockSession
+ ) -> None:
+ MockSession.return_value.get.return_value = mock.MagicMock(status_code=status)
+
+ ok, error = validate_credentials("e2b_test")
+ assert ok is False
+ assert expected in (error or "")
+
@parameterized.expand([("rate_limited", 429), ("server_error", 503)])
@mock.patch(E2B_SESSION_PATCH)
def test_transient_status_raises_rather_than_reporting_invalid(self, _name: str, status: int, MockSession) -> None:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b_source.py
index d5e03ce388ff..cfd226ca28a6 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/e2b/tests/test_e2b_source.py
@@ -2,6 +2,10 @@ from unittest.mock import MagicMock, patch
from parameterized import parameterized
+from products.warehouse_sources.backend.temporal.data_imports.sources.e2b.e2b import (
+ INVALID_CREDENTIALS_ERROR,
+ NO_ACCESS_ERROR,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.e2b.settings import ENDPOINTS
from products.warehouse_sources.backend.temporal.data_imports.sources.e2b.source import E2BSource
from products.warehouse_sources.backend.temporal.data_imports.sources.generated_configs.e2b import E2BSourceConfig
@@ -26,14 +30,33 @@ class TestE2BSource:
schemas = self.source.get_schemas(MagicMock(spec=E2BSourceConfig), team_id=self.team_id, names=["templates"])
assert [s.name for s in schemas] == ["templates"]
- @parameterized.expand([("valid", True, (True, None)), ("invalid", False, (False, "Invalid E2B API key"))])
- def test_validate_credentials_delegates_to_transport(self, _name: str, transport_ok: bool, expected) -> None:
+ @parameterized.expand(
+ [
+ ("valid", (True, None)),
+ ("invalid", (False, INVALID_CREDENTIALS_ERROR)),
+ ("no_access", (False, NO_ACCESS_ERROR)),
+ ]
+ )
+ def test_validate_credentials_delegates_to_transport(self, _name: str, transport_result) -> None:
config = E2BSourceConfig(api_key="e2b_test")
with patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.e2b.source.validate_e2b_credentials",
- return_value=transport_ok,
+ return_value=transport_result,
):
- assert self.source.validate_credentials(config, self.team_id) == expected
+ assert self.source.validate_credentials(config, self.team_id) == transport_result
+
+ @parameterized.expand(
+ [
+ ("unauthorized", "401 Client Error: Unauthorized for url: https://api.e2b.app", INVALID_CREDENTIALS_ERROR),
+ ("forbidden", "403 Client Error: Forbidden for url: https://api.e2b.app", NO_ACCESS_ERROR),
+ ]
+ )
+ def test_a_rejected_key_reads_the_same_during_setup_and_during_a_sync(
+ self, _name: str, error_key: str, expected: str
+ ) -> None:
+ # Setup and sync reach the message by different routes, so a divergence between them is
+ # invisible unless the two are compared.
+ assert self.source.get_non_retryable_errors()[error_key] == expected
def test_validate_credentials_transient_error_is_not_reported_as_invalid(self) -> None:
# A probe that can't reach E2B must not brand a possibly-valid key "invalid" and send the user