: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(mongodb): exclude reserved system.* collections from import (#67341)
Tom Owers8ed3eda3575e
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.pyindex 115d2a79837c..ac70a0efede8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/mongo.py@@ -15,6 +15,7 @@ from bson import Binary, DatetimeMS, ObjectIdfrom bson.codec_options import DatetimeConversionfrom pymongo import MongoClientfrom pymongo.collection import Collection+from pymongo.database import Databasefrom pymongo.errors import PyMongoErrorfrom pymongo.server_description import ServerDescriptionfrom structlog.types import FilteringBoundLogger@@ -430,6 +431,20 @@ def _determine_field_type_from_bson_types(bson_types: list[str]) -> str:return "string"+# MongoDB reserves the `system.*` namespace for internal collections (system.keys,+# system.views, system.profile, ...). They're never user data and reads against them fail with+# an "Unauthorized" OperationFailure, so they must never be offered for import.+_RESERVED_COLLECTION_PREFIX = "system."+++def _list_importable_collection_names(db: Database) -> list[str]:+ return [+ name+ for name in db.list_collection_names(authorizedCollections=True)+ if not name.startswith(_RESERVED_COLLECTION_PREFIX)+ ]++def get_schemas(config: MongoDBSourceConfig, team_id: int, names: list[str] | None = None) -> dict[str, list[tuple[str, str]]]:@@ -445,7 +460,7 @@ def get_schemas(schema_list: dict[str, list[tuple[str, str]]] = collections.defaultdict(list)# Get collection names- collection_names = db.list_collection_names(authorizedCollections=True)+ collection_names = _list_importable_collection_names(db)if names is not None:names_set = set(names)@@ -470,7 +485,7 @@ def get_collection_names(config: MongoDBSourceConfig, team_id: int) -> list[str]if not connection_params["database"]:raise ValueError(DATABASE_NAME_REQUIRED_ERROR)db = client[connection_params["database"]]- return db.list_collection_names(authorizedCollections=True)+ return _list_importable_collection_names(db)def _get_primary_keys(collection: Collection, collection_name: str) -> list[str] | None:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.pyindex b07e3210e8a8..190fa80fb3fb 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/mongodb/test_mongo.py@@ -15,6 +15,7 @@ from pymongo.server_description import ServerDescriptionfrom products.warehouse_sources.backend.temporal.data_imports.sources.mongodb.mongo import (_build_query,_get_rows_to_sync,+ _list_importable_collection_names,_make_safe_server_selector,_process_doc_with_field_logging,_process_nested_value,@@ -487,3 +488,17 @@ class TestGetRowsToSync(SimpleTestCase):) as capture:assert _get_rows_to_sync(coll, {}, MagicMock()) == 0capture.assert_called_once()+++class TestListImportableCollectionNames(SimpleTestCase):+ def test_excludes_reserved_system_collections(self):+ db = MagicMock()+ db.list_collection_names.return_value = ["users", "system.keys", "orders", "system.views"]++ assert _list_importable_collection_names(db) == ["users", "orders"]++ def test_keeps_collections_that_merely_contain_system(self):+ db = MagicMock()+ db.list_collection_names.return_value = ["system_events", "billing.system", "systematic"]++ assert _list_importable_collection_names(db) == ["system_events", "billing.system", "systematic"]