: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(data-warehouse): clarify unparseable SSH tunnel private key errors (#69195)
Tom Owersff7a1a957abc
diff --git a/products/data_warehouse/backend/models/test/test_ssh_tunnel.py b/products/data_warehouse/backend/models/test/test_ssh_tunnel.pyindex e357ab1bb19f..8ac4c1cf1633 100644--- a/products/data_warehouse/backend/models/test/test_ssh_tunnel.py+++ b/products/data_warehouse/backend/models/test/test_ssh_tunnel.py@@ -1,8 +1,24 @@import pytest+from cryptography.hazmat.primitives import serialization+from cryptography.hazmat.primitives.asymmetric import ed25519+from products.warehouse_sources.backend.facade.models import SSHTunnel+def _keypair_tunnel(private_key: str | None, passphrase: str | None) -> SSHTunnel:+ return SSHTunnel(+ enabled=True,+ host="host.com",+ port=5432,+ auth_type="keypair",+ username=None,+ password=None,+ private_key=private_key,+ passphrase=passphrase,+ )++@pytest.mark.parametrize("port,expected", [(5432, True), (80, False), (443, False)])def test_valid_port(port, expected):ssh_tunnel = SSHTunnel(@@ -76,6 +92,27 @@ def test_is_auth_valid_key_pair(private_key, passphrase, expected):assert res is expected+def test_is_auth_valid_unparseable_key_suggests_format():+ res, error = _keypair_tunnel(private_key="not a private key", passphrase=None).is_auth_valid()++ assert res is False+ assert "OpenSSH or PEM" in error+++def test_is_auth_valid_wrong_passphrase_suggests_passphrase():+ key = ed25519.Ed25519PrivateKey.generate()+ encrypted = key.private_bytes(+ encoding=serialization.Encoding.PEM,+ format=serialization.PrivateFormat.OpenSSH,+ encryption_algorithm=serialization.BestAvailableEncryption(b"correct-passphrase"),+ ).decode()++ res, error = _keypair_tunnel(private_key=encrypted, passphrase="wrong-passphrase").is_auth_valid()++ assert res is False+ assert "passphrase" in error.lower()++def test_get_tunnel_invalid_auth():ssh_tunnel = SSHTunnel(enabled=True,diff --git a/products/warehouse_sources/backend/models/ssh_tunnel.py b/products/warehouse_sources/backend/models/ssh_tunnel.pyindex 4434928ea50c..1dc478a6c0af 100644--- a/products/warehouse_sources/backend/models/ssh_tunnel.py+++ b/products/warehouse_sources/backend/models/ssh_tunnel.py@@ -10,6 +10,10 @@ from sshtunnel import SSHTunnelForwarderfrom products.warehouse_sources.backend.temporal.data_imports.sources.common import config+# Substrings that mark a private-key parse failure as a wrong/missing passphrase rather than a+# format problem, so both the parser and the validator give the same passphrase-specific guidance.+_PASSPHRASE_ERROR_TERMS = ("checksum", "decrypt", "password", "passphrase")+# Taken from https://stackoverflow.com/questions/60660919/paramiko-ssh-client-is-unable-to-unpack-ed25519-keydef from_private_key(file_obj: IO[str], passphrase: str | None = None) -> PKey:@@ -22,7 +26,12 @@ def from_private_key(file_obj: IO[str], passphrase: str | None = None) -> PKey:password=password,)file_obj.seek(0)- except ValueError:+ except ValueError as ssh_error:+ # A wrong passphrase on an OpenSSH key surfaces as a checksum/decrypt failure. Falling+ # through to the PEM loader masks it with a misleading "no BEGIN/END" error, so re-raise+ # the original — the caller uses it to point the user at the passphrase.+ if any(term in str(ssh_error).lower() for term in _PASSPHRASE_ERROR_TERMS):+ raisekey = crypto_serialization.load_pem_private_key( # type: ignorefile_bytes,password=password if passphrase is not None else None,@@ -140,9 +149,20 @@ class SSHTunnel:try:self.parse_private_key()- except:- # TODO: More helpful error messages- return False, "Private key could not be parsed"+ except Exception as e:+ # A wrong/missing passphrase and an unsupported key format both land here but need+ # different fixes, so point the user at the likely cause. cryptography's message+ # mentions the password/checksum only for passphrase mismatches.+ detail = str(e).lower()+ if any(term in detail for term in _PASSPHRASE_ERROR_TERMS):+ return False, (+ "Private key could not be parsed. Check the passphrase: an encrypted key needs the "+ "correct passphrase, and an unencrypted key needs the passphrase field left blank."+ )+ return False, (+ "Private key could not be parsed. Paste the full key in OpenSSH or PEM format "+ "(RSA, Ed25519, ECDSA, or DSA), including the BEGIN and END lines."+ )return True, ""