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(google-sheets): handle a malformed service account key in validation (#115889)

posthog[bot]83429de42b79
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
index 9248e7382da6..a044cc7afdfd 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
@@ -3,6 +3,7 @@ from typing import Optional, cast
from django.conf import settings
import gspread
+import structlog
from google.auth import exceptions as google_auth_exceptions
from products.warehouse_sources.backend.facade.source_config import (
@@ -32,6 +33,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.google_she
)
from products.warehouse_sources.backend.types import ExternalDataSourceType
+logger = structlog.get_logger(__name__)
+
@SourceRegistry.register
class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):
@@ -149,7 +152,17 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- client = google_sheets_client()
+ try:
+ client = google_sheets_client()
+ except ValueError:
+ # google-auth raises ValueError when this instance's service account key is malformed,
+ # for example not valid PEM. The user can't fix that, so return a clear message, not a 500.
+ logger.exception("google_sheets_service_account_key_invalid", team_id=team_id)
+ return (
+ False,
+ "The Google Sheets integration isn't configured correctly on this PostHog instance. "
+ "Ask your instance administrator to check the Google Sheets service account key.",
+ )
try:
client.open_by_url(config.spreadsheet_url)
return True, None
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
index 2b194def49e3..d70aea71ed2b 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
@@ -749,6 +749,18 @@ def test_validate_credentials_maps_google_auth_error(auth_error, expect_retry_hi
assert "try again in a moment" not in (error_message or "").lower()
+def test_validate_credentials_handles_malformed_service_account_key():
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.source.google_sheets_client",
+ side_effect=ValueError("No key could be detected."),
+ ):
+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")
+ is_valid, error_message = GoogleSheetsSource().validate_credentials(config, team_id=1)
+
+ assert is_valid is False
+ assert "isn't configured correctly on this PostHog instance" in (error_message or "")
+
+
@pytest.mark.parametrize(
"pinned_version,expected_version",
[