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(mongodb): treat Atlas bad-auth failures as non-retryable (#63784)

Tom Owers8c18a0671f43
diff --git a/posthog/temporal/data_imports/sources/mongodb/source.py b/posthog/temporal/data_imports/sources/mongodb/source.py
index f1190394b8f3..cf3a02bb1228 100644
--- a/posthog/temporal/data_imports/sources/mongodb/source.py
+++ b/posthog/temporal/data_imports/sources/mongodb/source.py
@@ -51,6 +51,12 @@ class MongoDBSource(SimpleSource[MongoDBSourceConfig], ValidateDatabaseHostMixin
# matched the real message — key off the stable codeName and the capitalised message.
"AuthenticationFailed": auth_failed_msg,
"Authentication failed": auth_failed_msg,
+ # MongoDB Atlas reports bad credentials differently from self-hosted MongoDB: instead of
+ # codeName 'AuthenticationFailed' (code 18) it raises OperationFailure with errmsg
+ # 'bad auth : authentication failed' and codeName 'AtlasError' (code 8000). The lowercase
+ # 'authentication failed' here doesn't match the capitalised entries above (matching is
+ # case-sensitive), so key off the stable Atlas-specific 'bad auth' prefix.
+ "bad auth": auth_failed_msg,
"SSL handshake failed": None,
# pymongo raises ServerSelectionTimeoutError when it can't select a usable cluster node
# for the whole selection timeout. The reason varies — "No servers found yet" / "No
diff --git a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
index 204add38de26..2519a261c8e4 100644
--- a/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
+++ b/posthog/temporal/data_imports/sources/mongodb/test_mongo.py
@@ -337,6 +337,12 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):
"Authentication failed., full error: {'ok': 0.0, 'errmsg': 'Authentication failed.', "
"'code': 18, 'codeName': 'AuthenticationFailed'}",
),
+ # Real pymongo OperationFailure string for bad credentials on MongoDB Atlas (code 8000).
+ (
+ "atlas_bad_auth",
+ "bad auth : authentication failed, full error: {'ok': 0, 'errmsg': 'bad auth : "
+ "authentication failed', 'code': 8000, 'codeName': 'AtlasError'}",
+ ),
("dns_failure", "The DNS query name does not exist: example.mongodb.net."),
("ssl_failure", "SSL handshake failed: certificate verify failed"),
# ServerSelectionTimeoutError variants — cluster unreachable for the whole selection
@@ -390,6 +396,7 @@ class TestMongoDBNonRetryableErrors(SimpleTestCase):
[
("code_name", "AuthenticationFailed", "password"),
("message", "Authentication failed", "password"),
+ ("atlas_bad_auth", "bad auth", "password"),
("unreachable_topology", "Topology Description:", "allowlist"),
]
)