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(snowflake): treat unparseable key-pair PEM key as user error (#64738)

Tom Owersd2e027df9d42
diff --git a/posthog/temporal/data_imports/sources/snowflake/source.py b/posthog/temporal/data_imports/sources/snowflake/source.py
index aff7aef3402e..67aa462f9d40 100644
--- a/posthog/temporal/data_imports/sources/snowflake/source.py
+++ b/posthog/temporal/data_imports/sources/snowflake/source.py
@@ -23,6 +23,17 @@ from products.data_warehouse.backend.types import ExternalDataSourceType
_SNOWFLAKE_IMPLEMENTATION = SnowflakeImplementation()
+# cryptography raises ValueError("Unable to load PEM file ...") when the key-pair private key can't
+# be parsed — typically the user pasted the bare base64 DER body without the
+# `-----BEGIN/END PRIVATE KEY-----` framing, or a truncated key. The `{action}` placeholder lets each
+# call site supply its own context-appropriate verb ("resync." on the sync path, "try again." during
+# credential validation) while keeping the guidance text defined once.
+_MALFORMED_PEM_MESSAGE = (
+ "Your Snowflake key-pair private key could not be read. Paste the full PEM private key including "
+ "the -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- lines (not just the base64 body), "
+ "then {action}"
+)
+
SnowflakeErrors = {
"No active warehouse selected in the current session": "No warehouse found for selected role",
"or attempt to login with another role": "Role specified doesn't exist or is not authorized",
@@ -217,6 +228,9 @@ class SnowflakeSource(SQLSource[SnowflakeSourceConfig]):
# can never succeed until the customer re-registers the matching public key. The host and
# request id in the message are volatile, so we match the stable phrase.
"JWT token is invalid": "Snowflake rejected key-pair authentication because the signed token is invalid — the private key you configured doesn't match the public key registered on the Snowflake user (often after a key rotation, or if the public key was never set). Re-register the matching public key on your Snowflake user (ALTER USER ... SET RSA_PUBLIC_KEY), or paste the correct private key here, then resync.",
+ # See `_MALFORMED_PEM_MESSAGE`: the key-pair private key can't be parsed, so retrying can
+ # never succeed until the user pastes a valid PEM key.
+ "Unable to load PEM file": _MALFORMED_PEM_MESSAGE.format(action="resync."),
# Snowflake error 002003 (SQLSTATE 42S02 for tables / 02000 for schemas): a table or
# schema the source syncs was dropped or renamed in Snowflake, or the role's grant on it
# was revoked, after the schema was discovered. The driver raises "<object> does not exist
@@ -256,6 +270,13 @@ class SnowflakeSource(SQLSource[SnowflakeSourceConfig]):
capture_exception(e)
return False, "Could not connect to Snowflake. Please check all connection details are valid."
+ except ValueError as e:
+ # A malformed key-pair private key fails to parse in `load_pem_private_key` before we ever
+ # reach Snowflake — a user config error, not an unexpected failure worth capturing.
+ if "Unable to load PEM file" in str(e):
+ return False, _MALFORMED_PEM_MESSAGE.format(action="try again.")
+ capture_exception(e)
+ return False, "Could not connect to Snowflake. Please check all connection details are valid."
except Exception as e:
capture_exception(e)
return False, "Could not connect to Snowflake. Please check all connection details are valid."
diff --git a/posthog/temporal/data_imports/sources/snowflake/tests/test_snowflake.py b/posthog/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
index 299247601ee8..bbb10d76ab19 100644
--- a/posthog/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
+++ b/posthog/temporal/data_imports/sources/snowflake/tests/test_snowflake.py
@@ -758,6 +758,18 @@ class TestSnowflakeSourceNonRetryableErrors:
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert is_non_retryable, f"Incorrect-credentials error should be non-retryable: {error_msg}"
+ @pytest.mark.parametrize(
+ "error_msg",
+ [
+ "Unable to load PEM file. See https://cryptography.io/en/latest/faq/#why-can-t-i-import-my-pem-file for more details. MalformedFraming",
+ "Unable to load PEM file.",
+ ],
+ )
+ def test_unparseable_private_key_is_non_retryable(self, source, error_msg):
+ non_retryable = source.get_non_retryable_errors()
+ is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
+ assert is_non_retryable, f"Unparseable private-key error should be non-retryable: {error_msg}"
+
@pytest.mark.parametrize(
"error_msg",
[
@@ -769,3 +781,34 @@ class TestSnowflakeSourceNonRetryableErrors:
non_retryable = source.get_non_retryable_errors()
is_non_retryable = any(pattern in error_msg for pattern in non_retryable.keys())
assert not is_non_retryable, f"Error should remain retryable: {error_msg}"
+
+
+class TestSnowflakeValidateCredentials:
+ @pytest.fixture
+ def source(self):
+ return SnowflakeSource()
+
+ def test_unparseable_private_key_returns_friendly_message_without_capture(self, source):
+ pem_error = ValueError(
+ "Unable to load PEM file. See https://cryptography.io/en/latest/faq/"
+ "#why-can-t-i-import-my-pem-file for more details. MalformedFraming"
+ )
+ with (
+ patch.object(source, "get_schemas", side_effect=pem_error),
+ patch("posthog.temporal.data_imports.sources.snowflake.source.capture_exception") as mock_capture,
+ ):
+ ok, message = source.validate_credentials(_make_config("keypair"), team_id=1)
+
+ assert ok is False
+ assert message is not None and "PEM private key" in message
+ mock_capture.assert_not_called()
+
+ def test_unexpected_value_error_is_still_captured(self, source):
+ with (
+ patch.object(source, "get_schemas", side_effect=ValueError("something unexpected")),
+ patch("posthog.temporal.data_imports.sources.snowflake.source.capture_exception") as mock_capture,
+ ):
+ ok, message = source.validate_credentials(_make_config("keypair"), team_id=1)
+
+ assert ok is False
+ mock_capture.assert_called_once()