diff --git a/mongoctl/commands/common/connect.py b/mongoctl/commands/common/connect.py index 3eb5b0d..51818ac 100644 --- a/mongoctl/commands/common/connect.py +++ b/mongoctl/commands/common/connect.py @@ -27,6 +27,8 @@ "verbose", "ipv6", "port", + "authenticationDatabase", + "authenticationMechanism", "ssl", "sslCAFile", ] @@ -97,9 +99,12 @@ def open_mongo_shell_to_server(server, else: database = "admin" - if username or server.needs_to_auth(database): + login_database = shell_options["authenticationDatabase"] \ + if "authenticationDatabase" in shell_options else database + + if username or server.needs_to_auth(login_database): # authenticate and grab a working username/password - username, password = server.get_working_login(database, username, + username, password = server.get_working_login(login_database, username, password) do_open_mongo_shell_to(server.get_connection_address(), @@ -145,6 +150,13 @@ def open_mongo_shell_to_uri(uri, username = username if username else uri_wrapper.username password = password if password else uri_wrapper.password + if "authsource" in uri_wrapper.options and "authenticationDatabase" not in shell_options: + shell_options["authenticationDatabase"] = uri_wrapper.options["authsource"] + + if "authmechanism" in uri_wrapper.options and "authenticationMechanism" not in shell_options: + shell_options["authenticationMechanism"] = uri_wrapper.options["authmechanism"] + + server_or_cluster = repository.build_server_or_cluster_from_uri(uri) if isinstance(server_or_cluster, Server): diff --git a/mongoctl/mongo_uri_tools.py b/mongoctl/mongo_uri_tools.py index 824395f..8304044 100644 --- a/mongoctl/mongo_uri_tools.py +++ b/mongoctl/mongo_uri_tools.py @@ -80,6 +80,11 @@ def username(self): def password(self): return self._uri_obj["password"] + ########################################################################### + @property + def options(self): + return self._uri_obj["options"] + ########################################################################### def is_cluster_uri(self): return len(self.node_list) > 1 diff --git a/mongoctl/mongoctl_command_config.py b/mongoctl/mongoctl_command_config.py index 9bea8d2..fff7fc7 100644 --- a/mongoctl/mongoctl_command_config.py +++ b/mongoctl/mongoctl_command_config.py @@ -546,6 +546,26 @@ "nargs": 1 }, + { + "name": "authenticationDatabase", + "type": "optional", + "help": "user source (defaults to dbname)", + "cmd_arg": [ + "--authenticationDatabase" + ], + "nargs": 1 + }, + + { + "name": "authenticationMechanism", + "type": "optional", + "help": "authentication mechanism", + "cmd_arg": [ + "--authenticationMechanism" + ], + "nargs": 1 + }, + { "name": "ssl", "type": "optional",