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(batch-exports): only connect to databricks workspace hosts (#122545)

Ross1a15c29787d5
diff --git a/posthog/api/test/test_integration.py b/posthog/api/test/test_integration.py
index eeb70580a3d9..357c7463157f 100644
--- a/posthog/api/test/test_integration.py
+++ b/posthog/api/test/test_integration.py
@@ -74,6 +74,7 @@ from posthog.models.integration import (
StripeIntegration,
github_account_type,
)
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE
from posthog.models.integration.github_audit import GitHubAudit, GitHubAuditPayload
from posthog.models.integration.twitter_ads import TwitterAdsIntegration
from posthog.models.oauth import OAuthAccessToken, OAuthApplication, OAuthRefreshToken
@@ -857,7 +858,7 @@ class TestDatabricksIntegration:
{
"kind": "databricks",
"config": {
- "server_hostname": "databricks.com",
+ "server_hostname": "dbc-a1b2c3d4-e5f6.cloud.databricks.com",
"client_id": "client_id",
"client_secret": "client_secret",
},
@@ -873,22 +874,22 @@ class TestDatabricksIntegration:
integration = Integration.objects.get(id=id)
assert integration.kind == "databricks"
assert integration.team == self.team
- assert integration.config == {"server_hostname": "databricks.com"}
+ assert integration.config == {"server_hostname": "dbc-a1b2c3d4-e5f6.cloud.databricks.com"}
assert integration.sensitive_config == {"client_id": "client_id", "client_secret": "client_secret"}
assert integration.created_by == self.user
- assert integration.integration_id == "databricks.com"
+ assert integration.integration_id == "dbc-a1b2c3d4-e5f6.cloud.databricks.com"
@pytest.mark.parametrize(
"invalid_config,expected_error_message",
[
# missing client_secret
(
- {"server_hostname": "databricks.com", "client_id": "client_id"},
+ {"server_hostname": "dbc-a1b2c3d4-e5f6.cloud.databricks.com", "client_id": "client_id"},
"Server hostname, client ID, and client secret must be provided",
),
# missing client_id
(
- {"server_hostname": "databricks.com", "client_secret": "client_secret"},
+ {"server_hostname": "dbc-a1b2c3d4-e5f6.cloud.databricks.com", "client_secret": "client_secret"},
"Server hostname, client ID, and client secret must be provided",
),
# missing server_hostname
@@ -900,7 +901,11 @@ class TestDatabricksIntegration:
({}, "Server hostname, client ID, and client secret must be provided"),
# wrong type for client_secret
(
- {"server_hostname": "databricks.com", "client_id": "client_id", "client_secret": 1},
+ {
+ "server_hostname": "dbc-a1b2c3d4-e5f6.cloud.databricks.com",
+ "client_id": "client_id",
+ "client_secret": 1,
+ },
"Server hostname, client ID, and client secret must be strings",
),
],
@@ -932,17 +937,17 @@ class TestDatabricksIntegration:
"127.0.0.1",
"10.0.0.1",
"192.168.1.1",
+ "evil.example.com",
],
)
# FORCE_URL_VALIDATION exercises the real SSRF guard; otherwise is_url_allowed short-circuits
# in dev/DEBUG, which is on under tests.
@override_settings(FORCE_URL_VALIDATION=True)
- def test_integration_from_config_rejects_internal_host(
+ def test_integration_from_config_rejects_non_databricks_host(
self,
host,
client: HttpClient,
):
- """Member-supplied Databricks hosts that resolve to internal IPs must be rejected (SSRF guard)."""
client.force_login(self.user)
response = client.post(
@@ -959,7 +964,7 @@ class TestDatabricksIntegration:
)
assert response.status_code == status.HTTP_400_BAD_REQUEST
- assert host in response.json()["detail"]
+ assert response.json()["detail"] == NOT_A_DATABRICKS_HOST_MESSAGE
assert not Integration.objects.filter(team=self.team, kind="databricks").exists()
diff --git a/posthog/models/integration/databricks.py b/posthog/models/integration/databricks.py
index eed068b6e436..b49a0363279a 100644
--- a/posthog/models/integration/databricks.py
+++ b/posthog/models/integration/databricks.py
@@ -1,10 +1,38 @@
"""Databricks integration."""
+import re
+
from posthog.models.user import User
from posthog.security.url_validation import is_url_allowed
from . import model
+# Production workspace zones from the Databricks SDK's environment list (databricks/sdk/environments.py).
+# Connections check this list but not the resolved addresses, so add only zones whose DNS Databricks controls.
+DATABRICKS_WORKSPACE_DNS_ZONES: tuple[str, ...] = (
+ ".cloud.databricks.com",
+ ".cloud.databricks.us",
+ ".azuredatabricks.net",
+ ".databricks.azure.us",
+ ".databricks.azure.cn",
+ ".gcp.databricks.com",
+)
+
+NOT_A_DATABRICKS_HOST_MESSAGE = (
+ "This is not a Databricks workspace hostname. Enter the server hostname from your SQL warehouse's "
+ "connection details, for example dbc-a1b2c3d4-e5f6.cloud.databricks.com."
+)
+
+_HOSTNAME_PATTERN = re.compile(r"[a-z0-9-]+(\.[a-z0-9-]+)+")
+
+
+def is_databricks_workspace_host(host: str) -> bool:
+ """Return whether `host` is a bare hostname in one of the Databricks workspace DNS zones."""
+ host = host.lower()
+ if not _HOSTNAME_PATTERN.fullmatch(host):
+ return False
+ return host.endswith(DATABRICKS_WORKSPACE_DNS_ZONES)
+
class DatabricksIntegrationError(Exception):
"""Error raised when the Databricks integration is not valid."""
@@ -39,7 +67,7 @@ class DatabricksIntegration:
self.integration = integration
try:
- self.server_hostname = self.integration.config["server_hostname"]
+ self.server_hostname = self.integration.config["server_hostname"].lower()
self.client_id = self.integration.sensitive_config["client_id"]
self.client_secret = self.integration.sensitive_config["client_secret"]
except KeyError as e:
@@ -51,6 +79,7 @@ class DatabricksIntegration:
) -> model.Integration:
# first, validate the host
cls.validate_host(server_hostname)
+ server_hostname = server_hostname.lower()
config = {
"server_hostname": server_hostname,
@@ -79,18 +108,21 @@ class DatabricksIntegration:
def validate_host(server_hostname: str):
"""Validate the Databricks host.
- We check the value is a bare hostname (not a full URL) and that it passes our shared SSRF
- guard (rejects unresolvable hosts, internal IPs, cloud-metadata hosts, and internal domain
- patterns). This is a quick check (testing connectivity to a SQL warehouse requires a
- warehouse http_path in addition to these parameters so it not possible to perform a full
- test here).
+ We check the value is a bare hostname (not a full URL) in a Databricks workspace DNS zone,
+ and that it passes our shared SSRF guard (rejects unresolvable hosts, internal IPs,
+ cloud-metadata hosts, and internal domain patterns). This is a quick check (testing
+ connectivity to a SQL warehouse requires a warehouse http_path in addition to these
+ parameters so it not possible to perform a full test here).
"""
# we expect a hostname, not a full URL
- if server_hostname.startswith("http"):
+ if server_hostname.lower().startswith(("http://", "https://")):
raise DatabricksIntegrationError(
f"Databricks integration is not valid: 'server_hostname' should not be a full URL"
)
+ if not is_databricks_workspace_host(server_hostname):
+ raise DatabricksIntegrationError(NOT_A_DATABRICKS_HOST_MESSAGE)
+
# Databricks is always https, so reuse the shared URL allowlist as the SSRF guard.
allowed, _reason = is_url_allowed(f"https://{server_hostname}")
if not allowed:
diff --git a/posthog/models/test/integration/test_databricks.py b/posthog/models/test/integration/test_databricks.py
index 47ebffb2116f..b8706c77401f 100644
--- a/posthog/models/test/integration/test_databricks.py
+++ b/posthog/models/test/integration/test_databricks.py
@@ -4,32 +4,73 @@ import pytest
from posthog.test.base import BaseTest
from unittest.mock import patch
+from parameterized import parameterized
+
from posthog.models.integration import DatabricksIntegration, DatabricksIntegrationError
+from posthog.models.integration.databricks import is_databricks_workspace_host
+
+
+ "host,expected",
+ [
+ ("dbc-a1b2c3d4-e5f6.cloud.databricks.com", True),
+ ("dbc-a1b2c3d4-e5f6.cloud.databricks.us", True),
+ ("adb-1234567890123456.7.azuredatabricks.net", True),
+ ("adb-1234567890123456.7.databricks.azure.us", True),
+ ("adb-1234567890123456.7.databricks.azure.cn", True),
+ ("1234567890123456.7.gcp.databricks.com", True),
+ ("DBC-A1B2C3D4-E5F6.Cloud.Databricks.com", True),
+ ("dbc-a1b2c3d4-e5f6.cloud.databricks.com.", False),
+ ("databricks.com", False),
+ ("cloud.databricks.com", False),
+ ("evil.example.com", False),
+ ("evilcloud.databricks.com", False),
+ ("x.cloud.databricks.com.evil.com", False),
+ ("evil.com#.cloud.databricks.com", False),
+ ("evil.com/.cloud.databricks.com", False),
+ ("[email protected]", False),
+ ("x.cloud.databricks.com:8443", False),
+ ("https://x.cloud.databricks.com", False),
+ ("10.0.0.1", False),
+ ("", False),
+ ],
+)
+def test_is_databricks_workspace_host(host: str, expected: bool) -> None:
+ assert is_databricks_workspace_host(host) is expected
class TestDatabricksIntegrationModel(BaseTest):
+ @parameterized.expand(
+ [
+ ("dbc-a1b2c3d4-e5f6.cloud.databricks.com", "dbc-a1b2c3d4-e5f6.cloud.databricks.com"),
+ ("httpteam.cloud.databricks.com", "httpteam.cloud.databricks.com"),
+ ("DBC-A1B2C3D4-E5F6.Cloud.Databricks.com", "dbc-a1b2c3d4-e5f6.cloud.databricks.com"),
+ ]
+ )
@patch("posthog.models.integration.databricks.is_url_allowed", return_value=(True, None))
- def test_integration_from_config_with_valid_config(self, mock_is_url_allowed):
+ def test_integration_from_config_with_valid_config(self, server_hostname, expected_hostname, mock_is_url_allowed):
integration = DatabricksIntegration.integration_from_config(
team_id=self.team.pk,
- server_hostname="databricks.com",
+ server_hostname=server_hostname,
client_id="client_id",
client_secret="client_secret",
created_by=self.user,
)
assert integration.team == self.team
assert integration.created_by == self.user
- assert integration.config == {"server_hostname": "databricks.com"}
+ assert integration.config == {"server_hostname": expected_hostname}
+ assert integration.integration_id == expected_hostname
assert integration.sensitive_config == {"client_id": "client_id", "client_secret": "client_secret"}
@patch("posthog.models.integration.databricks.is_url_allowed", return_value=(False, "Could not resolve host"))
def test_integration_from_config_with_invalid_server_hostname(self, mock_is_url_allowed):
with pytest.raises(
- DatabricksIntegrationError, match="Databricks integration error: could not validate hostname 'invalid'"
+ DatabricksIntegrationError,
+ match="Databricks integration error: could not validate hostname 'dbc-missing.cloud.databricks.com'",
):
DatabricksIntegration.integration_from_config(
team_id=self.team.pk,
- server_hostname="invalid",
+ server_hostname="dbc-missing.cloud.databricks.com",
client_id="client_id",
client_secret="client_secret",
created_by=self.user,
diff --git a/products/batch_exports/backend/temporal/destinations/databricks_batch_export.py b/products/batch_exports/backend/temporal/destinations/databricks_batch_export.py
index 071d4355e37e..bdffa4b36dd6 100644
--- a/products/batch_exports/backend/temporal/destinations/databricks_batch_export.py
+++ b/products/batch_exports/backend/temporal/destinations/databricks_batch_export.py
@@ -5,7 +5,6 @@ import socket
import typing as t
import asyncio
import datetime as dt
-import ipaddress
import contextlib
import dataclasses
import collections.abc
@@ -25,15 +24,8 @@ from structlog.contextvars import bind_contextvars
from temporalio import activity, exceptions, workflow
from temporalio.common import RetryPolicy
-from posthog.cloud_utils import is_cloud
from posthog.models.integration import DatabricksIntegration, Integration
-from posthog.security.oob_domains import matched_oob_domain
-from posthog.security.url_validation import (
- INVALID_HOST_MESSAGE,
- UNREACHABLE_HOST_MESSAGE,
- ShapeError,
- validate_external_host,
-)
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE, is_databricks_workspace_host
from posthog.temporal.common.base import PostHogWorkflow
from posthog.temporal.common.heartbeat import Heartbeater
from posthog.temporal.common.logger import get_logger, get_write_only_logger
@@ -329,41 +321,16 @@ class DatabricksClient:
leaves no stranded thread — the socket terminates on its own timeout and the SDK call is never
started.
- We also resolve and validate the hostname again here, and connect only to the validated
- addresses, to prevent SSRF through DNS rebinding.
+ We also refuse a host outside the Databricks workspace DNS zones, because the SDK and connector
+ send requests to URLs the server returns. Checking here as well as at creation also covers
+ integrations that are already stored.
"""
+ if not is_databricks_workspace_host(self.server_hostname):
+ raise DatabricksConnectionError(NOT_A_DATABRICKS_HOST_MESSAGE, retryable=False)
def probe() -> None:
- if is_cloud() and (oob_domain := matched_oob_domain(self.server_hostname)) is not None:
- # Detection only, so a match is logged and never refused. See posthog/security/crosslang/README.md.
- self.logger.info("security.oob_domain_seen", domain=oob_domain, guard="databricks_probe")
-
- addresses = [
- str(sockaddr[0])
- for *_, sockaddr in socket.getaddrinfo(self.server_hostname, 443, type=socket.SOCK_STREAM)
- ]
- try:
- validate_external_host(
- self.server_hostname, resolved_ips={ipaddress.ip_address(address) for address in addresses}
- )
- except ShapeError as err:
- raise DatabricksConnectionError(INVALID_HOST_MESSAGE, retryable=False) from err
- except ValueError as err:
- self.logger.warning(
- "Refused to connect to a Databricks host that failed the SSRF check",
- reason=str(err),
- addresses=addresses,
- )
- raise DatabricksConnectionError(UNREACHABLE_HOST_MESSAGE, retryable=False) from err
-
- errors: list[OSError] = []
- for address in addresses:
- try:
- with socket.create_connection((address, 443), timeout=self.connect_timeout_seconds):
- return
- except OSError as err:
- errors.append(err)
- raise errors[-1]
+ with socket.create_connection((self.server_hostname, 443), timeout=self.connect_timeout_seconds):
+ pass
try:
await asyncio.to_thread(probe)
diff --git a/products/batch_exports/backend/tests/destination_tests/test_databricks_destination_tests.py b/products/batch_exports/backend/tests/destination_tests/test_databricks_destination_tests.py
index a51a0ace91c0..0960c144c94c 100644
--- a/products/batch_exports/backend/tests/destination_tests/test_databricks_destination_tests.py
+++ b/products/batch_exports/backend/tests/destination_tests/test_databricks_destination_tests.py
@@ -7,6 +7,8 @@ from unittest.mock import patch
from databricks import sql
from databricks.sdk.core import Config, oauth_service_principal
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE
+
from products.batch_exports.backend.destination_tests.base import Status
from products.batch_exports.backend.destination_tests.databricks import (
DatabricksCatalogTestStep,
@@ -117,7 +119,7 @@ class TestDatabricksEstablishConnectionTestStep:
)
result = await test_step.run()
assert result.status == Status.FAILED
- assert result.message == "Failed to connect to Databricks. Please check that your connection details are valid."
+ assert result.message == NOT_A_DATABRICKS_HOST_MESSAGE
async def test_with_wrong_http_path(self, databricks_config):
test_step = DatabricksEstablishConnectionTestStep(
diff --git a/products/batch_exports/backend/tests/temporal/destinations/databricks/test_client.py b/products/batch_exports/backend/tests/temporal/destinations/databricks/test_client.py
index 107fb52f43c7..c18f8619c586 100644
--- a/products/batch_exports/backend/tests/temporal/destinations/databricks/test_client.py
+++ b/products/batch_exports/backend/tests/temporal/destinations/databricks/test_client.py
@@ -32,7 +32,7 @@ skip_without_real_databricks = pytest.mark.requires_vendor_credentials(*REAL_DAT
@pytest.fixture
def client() -> DatabricksClient:
return DatabricksClient(
- server_hostname="test",
+ server_hostname="dbc-test.cloud.databricks.com",
http_path="test",
client_id="test",
client_secret="test",
@@ -183,14 +183,17 @@ class TestConnect:
is left blocking on the hang-prone SDK call.
"""
# 192.0.2.1 is reserved as non-routable (RFC 5737 TEST-NET-1), so the preflight connect fails
- # fast (no DNS, no real host).
- client.server_hostname = "192.0.2.1"
+ # fast (no real host).
+ addrinfo = [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("192.0.2.1", 443))]
client.connect_timeout_seconds = 0.01
client.connect_max_attempts = 1
- with pytest.raises(
- DatabricksConnectionError,
- match="Failed to connect to Databricks. Please check that your connection details are valid.",
+ with (
+ patch("socket.getaddrinfo", return_value=addrinfo),
+ pytest.raises(
+ DatabricksConnectionError,
+ match="Failed to connect to Databricks. Please check that your connection details are valid.",
+ ),
):
async with client.connect():
pass
@@ -204,17 +207,15 @@ class TestConnect:
host becomes reachable, then complete the connection."""
attempts = {"n": 0}
- def resolution_result(*_args, **_kwargs):
+ def reachability_result(*_args, **_kwargs):
attempts["n"] += 1
if attempts["n"] < 3:
raise socket.gaierror(socket.EAI_AGAIN, "Temporary failure in name resolution")
- return [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("8.8.8.8", 443))]
+ return MagicMock()
- with (
- patch("socket.getaddrinfo", side_effect=resolution_result),
- patch(
- "products.batch_exports.backend.temporal.destinations.databricks_batch_export.socket.create_connection",
- ),
+ with patch(
+ "products.batch_exports.backend.temporal.destinations.databricks_batch_export.socket.create_connection",
+ side_effect=reachability_result,
):
async with client.connect():
pass
@@ -239,16 +240,19 @@ class TestConnect:
):
"""A hostname that does not exist is a config error, so we should fail fast with a targeted
message instead of backing off for minutes."""
- getaddrinfo_mock = MagicMock(side_effect=socket.gaierror(errno, "Name or service not known"))
+ create_connection_mock = MagicMock(side_effect=socket.gaierror(errno, "Name or service not known"))
with (
- patch("socket.getaddrinfo", new=getaddrinfo_mock),
+ patch(
+ "products.batch_exports.backend.temporal.destinations.databricks_batch_export.socket.create_connection",
+ new=create_connection_mock,
+ ),
pytest.raises(DatabricksConnectionError, match=expected_match),
):
async with client.connect():
pass
- assert getaddrinfo_mock.call_count == 1
+ assert create_connection_mock.call_count == 1
mock_sql_connect.sql_connect.assert_not_called()
async def test_does_not_retry_invalid_credentials(
@@ -267,57 +271,21 @@ class TestConnect:
assert mock_sql_connect.sql_connect.call_count == 1
- async def test_probe_connects_to_the_address_it_checked(
- self, client: DatabricksClient, settings, mock_sql_connect: MockDatabricksSqlConnect
+ async def test_refuses_a_host_outside_databricks_zones(
+ self, client: DatabricksClient, mock_sql_connect: MockDatabricksSqlConnect
):
- settings.FORCE_URL_VALIDATION = True
client.server_hostname = "dbc.example.com"
- addrinfo = [(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", ("8.8.8.8", 443))]
with (
- patch("socket.getaddrinfo", return_value=addrinfo),
- patch(
- "products.batch_exports.backend.temporal.destinations.databricks_batch_export.socket.create_connection",
- ) as create_connection_mock,
- ):
- async with client.connect():
- pass
-
- create_connection_mock.assert_called_once_with(("8.8.8.8", 443), timeout=client.connect_timeout_seconds)
- mock_sql_connect.sql_connect.assert_called_once()
-
- @pytest.mark.parametrize(
- "addresses",
- [
- ["127.0.0.1"],
- ["169.254.169.254"],
- ["8.8.8.8", "10.0.0.5"],
- ],
- )
- async def test_probe_refuses_a_host_that_resolves_internal(
- self,
- client: DatabricksClient,
- settings,
- fast_backoff,
- mock_sql_connect: MockDatabricksSqlConnect,
- addresses: list[str],
- ):
- settings.FORCE_URL_VALIDATION = True
- client.server_hostname = "dbc.example.com"
- addrinfo = [
- (socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP, "", (address, 443)) for address in addresses
- ]
-
- with (
- patch("socket.getaddrinfo", return_value=addrinfo),
patch(
"products.batch_exports.backend.temporal.destinations.databricks_batch_export.socket.create_connection",
) as create_connection_mock,
- pytest.raises(DatabricksConnectionError, match="Could not reach this host"),
+ pytest.raises(DatabricksConnectionError, match="not a Databricks workspace hostname") as excinfo,
):
async with client.connect():
pass
+ assert excinfo.value.retryable is False
create_connection_mock.assert_not_called()
mock_sql_connect.sql_connect.assert_not_called()
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/databricks.py b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/databricks.py
index bad02e190319..4421b0514648 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/databricks.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/databricks.py
@@ -23,13 +23,17 @@ from databricks.sdk.core import (
)
from structlog.types import FilteringBoundLogger
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE, is_databricks_workspace_host
from posthog.temporal.common.errors import NonReportableError
from products.warehouse_sources.backend.temporal.data_imports.sources.common.deadline import (
DeadlineExceededError,
run_with_deadline,
)
-from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import log_connection_open
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import (
+ HostNotAllowedError,
+ log_connection_open,
+)
from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql import BacktickIdentifierQuoter
from products.warehouse_sources.backend.temporal.data_imports.sources.common.sql.implementation import (
SourceMetadata,
@@ -169,9 +173,9 @@ def clean_databricks_host(host: str) -> str:
"""Normalize a pasted workspace URL down to the bare hostname the connector expects."""
host = host.strip()
for prefix in ("https://", "http://"):
- if host.startswith(prefix):
+ if host.lower().startswith(prefix):
host = host[len(prefix) :]
- return host.rstrip("/")
+ return host.rstrip("/").lower()
def _split_display_name(display_name: str, default_schema: Optional[str]) -> tuple[Optional[str], str]:
@@ -220,6 +224,11 @@ class DatabricksImplementation(SQLSourceImplementation[DatabricksSourceConfig, A
(multi-schema import) leaves the session schema unset.
"""
host = clean_databricks_host(config.host)
+ # The SDK and connector send requests to URLs the server returns, so the server must be one
+ # Databricks runs. `validate_credentials` checks this too, but scheduled syncs never call it.
+ if not is_databricks_workspace_host(host):
+ raise HostNotAllowedError(NOT_A_DATABRICKS_HOST_MESSAGE)
+
auth_connect_args: dict[str, Any] = {}
if config.auth_type.selection == "service_principal":
@@ -232,6 +241,7 @@ class DatabricksImplementation(SQLSourceImplementation[DatabricksSourceConfig, A
host=f"https://{host}",
client_id=client_id,
client_secret=client_secret,
+ auth_type="oauth-m2m",
)
)
@@ -248,6 +258,7 @@ class DatabricksImplementation(SQLSourceImplementation[DatabricksSourceConfig, A
# `schema=""` would try `USE SCHEMA ""`, an invalid identifier, and fail at connect time.
schema=normalize_namespace(config.schema),
user_agent_entry="PostHog",
+ enable_telemetry=False,
**auth_connect_args,
)
try:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/source.py
index 19f23b9ffd27..9326892a61b1 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/source.py
@@ -1,6 +1,7 @@
from typing import Optional, cast
from posthog.exceptions_capture import capture_exception
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE, is_databricks_workspace_host
from products.warehouse_sources.backend.facade.codebase_detection import detect
from products.warehouse_sources.backend.facade.source_config import (
@@ -202,6 +203,8 @@ class DatabricksSource(SQLSource[DatabricksSourceConfig], ValidateDatabaseHostMi
return {
**self.default_non_retryable_errors(),
"Invalid access token": "Databricks rejected the access token. Check that the personal access token is correct and has not expired or been revoked, then resync.",
+ # Raised by `DatabricksImplementation.connect` for a stored host outside the Databricks DNS zones.
+ "is not a Databricks workspace hostname": "This source's server hostname is not a Databricks workspace hostname. Update it to the server hostname from your SQL warehouse's connection details, then resync.",
"invalid_client": "Databricks rejected the service principal credentials. Check the client ID and client secret, and that the service principal still exists, then resync.",
# Unity Catalog error classes are stable identifiers; the object names around them are volatile.
"CATALOG_NOT_FOUND": "The configured catalog no longer exists in Databricks, or your credentials lost USE CATALOG on it. Check the catalog and grants, then resync.",
@@ -274,8 +277,12 @@ class DatabricksSource(SQLSource[DatabricksSourceConfig], ValidateDatabaseHostMi
):
return False, "Missing required parameters: client ID, client secret"
+ host = clean_databricks_host(config.host)
+ if not is_databricks_workspace_host(host):
+ return False, NOT_A_DATABRICKS_HOST_MESSAGE
+
# Block SSRF to internal hosts before any OAuth/SQL request reaches the pasted host.
- valid_host, host_error = self.is_database_host_valid(clean_databricks_host(config.host), team_id)
+ valid_host, host_error = self.is_database_host_valid(host, team_id)
if not valid_host:
return valid_host, host_error
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/tests/test_databricks.py b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/tests/test_databricks.py
index 4800dd612a5d..4164f646b15e 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/tests/test_databricks.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/databricks/tests/test_databricks.py
@@ -6,9 +6,12 @@ from unittest.mock import MagicMock, patch
import pyarrow as pa
from databricks.sql.exc import RequestError
+from posthog.models.integration.databricks import NOT_A_DATABRICKS_HOST_MESSAGE
+
from products.warehouse_sources.backend.temporal.data_imports.discover_schemas_workflow import (
DISCOVER_SCHEMAS_ACTIVITY_TIMEOUT,
)
+from products.warehouse_sources.backend.temporal.data_imports.sources.common.mixins import HostNotAllowedError
from products.warehouse_sources.backend.temporal.data_imports.sources.common.typings import SourceInputs
from products.warehouse_sources.backend.temporal.data_imports.sources.databricks.databricks import (
DATABRICKS_SCHEMA_DISCOVERY_DEADLINE_SECONDS,
@@ -136,11 +139,23 @@ class TestConnect:
kwargs = mock_connect.call_args.kwargs
assert "access_token" not in kwargs
assert callable(kwargs["credentials_provider"])
+ assert kwargs["enable_telemetry"] is False
+
+ def test_refuses_a_host_outside_databricks_zones(self, impl):
+ with patch(_CONNECT_PATH) as mock_connect:
+ with pytest.raises(HostNotAllowedError, match="not a Databricks workspace hostname"):
+ with impl.connect(_make_config(host="dbc-abc123.example.com")):
+ pass
+ mock_connect.assert_not_called()
- def test_pasted_workspace_url_is_cleaned(self, impl):
+ @pytest.mark.parametrize(
+ "pasted_host",
+ ["https://dbc-abc123.cloud.databricks.com/", "HTTPS://DBC-abc123.cloud.databricks.com/"],
+ )
+ def test_pasted_workspace_url_is_cleaned(self, impl, pasted_host):
# A `https://…/` host reaches the connector as a bare hostname, or every request fails.
with patch(_CONNECT_PATH) as mock_connect:
- with impl.connect(_make_config(host="https://dbc-abc123.cloud.databricks.com/")):
+ with impl.connect(_make_config(host=pasted_host)):
pass
assert mock_connect.call_args.kwargs["server_hostname"] == "dbc-abc123.cloud.databricks.com"
@@ -417,6 +432,17 @@ class TestDatabricksSource:
assert message == "Host is not allowed"
mock_get_schemas.assert_not_called()
+ def test_validate_credentials_refuses_a_host_outside_databricks_zones(self, source):
+ with (
+ patch.object(DatabricksSource, "is_database_host_valid") as mock_is_database_host_valid,
+ patch.object(DatabricksSource, "get_schemas") as mock_get_schemas,
+ ):
+ ok, message = source.validate_credentials(_make_config(host="dbc-abc123.example.com"), team_id=1)
+ assert ok is False
+ assert message == NOT_A_DATABRICKS_HOST_MESSAGE
+ mock_is_database_host_valid.assert_not_called()
+ mock_get_schemas.assert_not_called()
+
def test_get_retryable_errors_matches_stopped_warehouse(self, source):
# A command in flight when the SQL warehouse auto-suspends fails with this message;
# Databricks auto-starts the warehouse on the next query, so it must stay retryable