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(mssql): treat Azure SQL firewall block as non-retryable (#71708)

Tom Owers25448106caa4
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py
index 94f7d9efcf21..bce77b0a7732 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/source.py
@@ -30,12 +30,21 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssq
)
from products.warehouse_sources.backend.types import ExternalDataSourceType
+_FIREWALL_BLOCKED_ERROR = (
+ "Your SQL Server's firewall is blocking PostHog. Add a firewall rule allowing PostHog's IP "
+ "addresses to reach the server (on Azure SQL, use the portal or sp_set_firewall_rule), then "
+ "try again. New rules can take a few minutes to take effect."
+)
+
MSSQLErrors = {
# SQL Server error 18456 is an authentication failure (wrong username/password, or the login is
# disabled), not a problem with the database field. Surface the same wording the sibling SQL
# sources use and match the stable prefix, not the volatile "'<username>'." that follows it.
"Login failed for user": "Invalid user or password",
"Adaptive Server is unavailable or does not exist": "Could not connect to SQL server - check server host and port",
+ # Azure SQL error 40615 — the server-level firewall rejected the connecting client IP. The full
+ # message echoes the server name and client IP, so match the stable, distinctive phrase instead.
+ "is not allowed to access the server": _FIREWALL_BLOCKED_ERROR,
"connection timed out": "Could not connect to SQL server - check server firewall settings",
}
@@ -54,6 +63,11 @@ class MSSQLSource(SQLSource[MSSQLSourceConfig], SSHTunnelMixin, ValidateDatabase
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
+ # Azure SQL error 40615 — the server-level firewall rejected PostHog's client IP. This
+ # also surfaces "Adaptive Server connection failed" below, but that generic entry has no
+ # message; keep this first so the actionable firewall guidance wins. Match the stable
+ # phrase, not the volatile server name / client IP the rest of the message echoes.
+ "is not allowed to access the server": _FIREWALL_BLOCKED_ERROR,
"Adaptive Server connection failed": None,
# pymssql DB-Lib error 20009 — the server host can't be reached for the whole
# connection attempt. On a managed instance this is a persistent connectivity issue
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
index f223c1f980d0..e210f01dc0ae 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mssql/tests/test_mssql.py
@@ -21,7 +21,10 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.mssq
retry_on_deadlock,
retry_on_transient_connection_error,
)
-from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.source import MSSQLSource
+from products.warehouse_sources.backend.temporal.data_imports.sources.mssql.source import (
+ _FIREWALL_BLOCKED_ERROR,
+ MSSQLSource,
+)
from products.warehouse_sources.backend.types import IncrementalFieldType
@@ -587,6 +590,23 @@ class TestMSSQLSourceNonRetryableErrors:
non_retryable = MSSQLSource().get_non_retryable_errors()
assert any(pattern in error_msg for pattern in non_retryable.keys()), error_msg
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ # Azure SQL error 40615 — the server firewall rejected the client IP. Redacted shape;
+ # the server name / client IP are volatile, the matched phrase is not.
+ "Cannot open server 'example' requested by the login. Client with IP address "
+ "'203.0.113.10' is not allowed to access the server.",
+ ],
+ )
+ def test_firewall_block_is_non_retryable(self, error_msg):
+ non_retryable = MSSQLSource().get_non_retryable_errors()
+ matches = [msg for pattern, msg in non_retryable.items() if pattern in error_msg]
+ assert matches, error_msg
+ # The actionable firewall guidance must win over the generic catch-all, so the first match
+ # can't be the message-less entry.
+ assert matches[0] is not None
+
@pytest.mark.parametrize(
"error_msg",
[
@@ -662,6 +682,30 @@ class TestMSSQLSourceNonRetryableErrors:
assert any(pattern in str(exc_info.value) for pattern in non_retryable.keys())
+class TestMSSQLSourceValidateCredentials:
+ @pytest.fixture
+ def source(self):
+ return MSSQLSource()
+
+ def test_firewall_block_returns_actionable_message_without_capturing(self, source, mocker):
+ capture = mocker.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.mssql.source.capture_exception"
+ )
+ mocker.patch.object(source, "is_database_host_valid", return_value=(True, None))
+ # Redacted Azure SQL error 40615 — real server name and client IP are volatile.
+ firewall_error = pymssql.OperationalError(
+ "Cannot open server 'example' requested by the login. Client with IP address "
+ "'203.0.113.10' is not allowed to access the server."
+ )
+ mocker.patch.object(source, "get_schemas", side_effect=firewall_error)
+
+ valid, error = source.validate_credentials(_make_config(), team_id=1)
+
+ assert valid is False
+ assert error == _FIREWALL_BLOCKED_ERROR
+ capture.assert_not_called()
+
+
class TestIsTransientConnectionError:
@pytest.mark.parametrize(
"error",