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(spacelift): point an unknown account name at the account field (#108179)

Tom Owersd2ed559b23b2
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/source.py
index f5b66112e92a..c39ab19c5f6b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/source.py
@@ -24,6 +24,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.spacelift.
RUNS_INCREMENTAL_LOOKBACK_SECONDS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.spacelift.spacelift import (
+ ACCOUNT_NOT_FOUND_MESSAGE,
+ INVALID_API_KEY_MESSAGE,
SpaceliftResumeConfig,
spacelift_source,
validate_credentials as validate_spacelift_credentials,
@@ -102,7 +104,9 @@ Your account name is the subdomain you use to access Spacelift (e.g. `my-company
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
# Raised by the apiKeyUser token exchange when the key id/secret is wrong or revoked.
- "Invalid Spacelift API key": "Your Spacelift API key is invalid or has been revoked. Create a new API key in your Spacelift organization settings, then reconnect.",
+ "Invalid Spacelift API key": INVALID_API_KEY_MESSAGE,
+ # Raised by the same exchange when the account subdomain names no Spacelift account.
+ "Spacelift account not found": ACCOUNT_NOT_FOUND_MESSAGE,
# Raised when a query stays unauthorized after a fresh token, i.e. the key lacks space access.
"Spacelift API returned unauthorized": "Your Spacelift API key does not have access to this data. Grant the key read access to the relevant spaces in Spacelift, then reconnect.",
"Invalid Spacelift account name": "The Spacelift account name is invalid. Enter only the subdomain of your Spacelift URL (e.g. `my-company` for `my-company.app.spacelift.io`).",
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/spacelift.py b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/spacelift.py
index d406ee845ccb..9a416f0e85a5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/spacelift.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/spacelift.py
@@ -37,6 +37,18 @@ mutation GetSpaceliftToken($id: ID!, $secret: String!) {
"""
+ACCOUNT_NOT_FOUND_REASON = "account not found"
+
+INVALID_API_KEY_MESSAGE = (
+ "Your Spacelift API key is invalid or has been revoked. Create a new API key in your Spacelift "
+ "organization settings, then reconnect."
+)
+ACCOUNT_NOT_FOUND_MESSAGE = (
+ "Spacelift has no account with that name. Enter only the subdomain of your Spacelift URL, for "
+ "example my-company for my-company.app.spacelift.io."
+)
+
+
class SpaceliftRetryableError(Exception):
pass
@@ -45,6 +57,10 @@ class SpaceliftAuthError(Exception):
pass
+class SpaceliftAccountNotFoundError(SpaceliftAuthError):
+ pass
+
+
class SpaceliftPermissionError(Exception):
pass
@@ -200,6 +216,10 @@ class SpaceliftClient:
errors = payload.get("errors")
if errors:
messages = "; ".join(e.get("message", "") for e in errors)
+ # Spacelift rejects the token exchange with "account not found" when the subdomain names
+ # no account, which says nothing about the key the user typed.
+ if ACCOUNT_NOT_FOUND_REASON in messages.lower():
+ raise SpaceliftAccountNotFoundError(f"Spacelift account not found: token exchange failed ({messages})")
raise SpaceliftAuthError(f"Invalid Spacelift API key: token exchange failed ({messages})")
# Spacelift signals a bad key id/secret with a null user, not a GraphQL error.
@@ -368,8 +388,10 @@ def validate_credentials(account_name: str, api_key_id: str, api_key_secret: str
try:
client._ensure_token()
return True, None
- except SpaceliftAuthError as e:
- return False, str(e)
+ except SpaceliftAccountNotFoundError:
+ return False, ACCOUNT_NOT_FOUND_MESSAGE
+ except SpaceliftAuthError:
+ return False, INVALID_API_KEY_MESSAGE
except Exception as e:
return False, f"Could not verify Spacelift credentials: {e}"
finally:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift.py b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift.py
index cfc5af9ce4b9..1d4184c602f5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift.py
@@ -10,6 +10,9 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.spacelift.
SPACELIFT_ENDPOINTS,
)
from products.warehouse_sources.backend.temporal.data_imports.sources.spacelift.spacelift import (
+ ACCOUNT_NOT_FOUND_MESSAGE,
+ INVALID_API_KEY_MESSAGE,
+ SpaceliftAccountNotFoundError,
SpaceliftAuthError,
SpaceliftClient,
SpaceliftPermissionError,
@@ -389,9 +392,32 @@ class TestSpacelift:
def test_validate_credentials_bad_key(self, mock_make_session):
_mock_session(mock_make_session, [_response({"data": {"apiKeyUser": None}})])
- is_valid, message = validate_credentials("my-company", "key-id", "key-secret")
- assert is_valid is False
- assert message is not None and "Invalid Spacelift API key" in message
+ assert validate_credentials("my-company", "key-id", "key-secret") == (False, INVALID_API_KEY_MESSAGE)
+
+ @mock.patch(f"{_MODULE}.make_tracked_session")
+ def test_validate_credentials_unknown_account_points_at_the_account_name(self, mock_make_session):
+ # Spacelift reports an unknown subdomain as a token exchange error, which otherwise reads as
+ # a rejected API key and sends the user to rotate a key that was never the problem.
+ _mock_session(mock_make_session, [_response({"errors": [{"message": "Account not found"}], "data": None})])
+
+ assert validate_credentials("my-company", "key-id", "key-secret") == (False, ACCOUNT_NOT_FOUND_MESSAGE)
+
+ @mock.patch(f"{_MODULE}.make_tracked_session")
+ def test_validate_credentials_keeps_the_upstream_text_out_of_the_message(self, mock_make_session):
+ _mock_session(
+ mock_make_session,
+ [_response({"errors": [{"message": "signature mismatch for key 01ABC"}], "data": None})],
+ )
+
+ assert validate_credentials("my-company", "key-id", "key-secret") == (False, INVALID_API_KEY_MESSAGE)
+
+ @mock.patch(f"{_MODULE}.make_tracked_session")
+ def test_token_exchange_raises_account_not_found_for_an_unknown_account(self, mock_make_session):
+ _mock_session(mock_make_session, [_response({"errors": [{"message": "Account not found"}], "data": None})])
+
+ client = SpaceliftClient("my-company", "key-id", "key-secret")
+ with pytest.raises(SpaceliftAccountNotFoundError):
+ client.execute("query { x }")
def test_validate_credentials_invalid_account_name_never_hits_network(self):
is_valid, message = validate_credentials("evil.com/x", "key-id", "key-secret")
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift_source.py
index 81a85c681477..1789f9d92849 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/spacelift/tests/test_spacelift_source.py
@@ -29,6 +29,7 @@ class TestSpaceliftSource:
"Invalid Spacelift API key: the API key ID or secret is incorrect",
"Spacelift API returned unauthorized: the API key lacks access to this data (unauthorized)",
"Invalid Spacelift account name: 'evil.com/x'",
+ "Spacelift account not found: token exchange failed (Account not found)",
],
)
def test_non_retryable_errors_match_auth_failures(self, observed_error):