From f3b72c341e123287595f730d4f51d3a3fdc3bfde Mon Sep 17 00:00:00 2001 From: Dylan Semler Date: Fri, 6 Apr 2018 14:12:34 -0400 Subject: [PATCH] Fix username clobbering in mongodb_uri --- mongo_connector/connector.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/mongo_connector/connector.py b/mongo_connector/connector.py index 7465b6d4..0330c816 100644 --- a/mongo_connector/connector.py +++ b/mongo_connector/connector.py @@ -311,10 +311,19 @@ def copy_uri_options(hosts, mongodb_uri): """Returns a MongoDB URI to hosts with the options from mongodb_uri. """ if '?' in mongodb_uri: - options = mongodb_uri.split('?', 1)[1] + base_url, options = mongodb_uri.split('?', 1) else: - options = None - uri = 'mongodb://' + hosts + base_url, options = '', None + + # Parse out username and/or passwords from mongodb uri + if '@' in base_url: + auth_key = base_url.split('@')[0] + '@' + if auth_key.startswith('mongodb://'): + auth_key = auth_key[10:] + else: + auth_key = '' + + uri = 'mongodb://' + auth_key + hosts if options: uri += '/?' + options return uri