From 9a51bb4f1b6bc97bbeb91fd0c3a5be4c7ad734d4 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Mon, 4 Mar 2024 16:25:27 +0200 Subject: [PATCH 1/2] Raise clear error message on invalid Windows minion WinRM creds. Signed-off-by: Nashwan Azhari --- coriolis/osmorphing/osmount/windows.py | 16 ++++++++++++++++ coriolis/wsman.py | 6 ++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/coriolis/osmorphing/osmount/windows.py b/coriolis/osmorphing/osmount/windows.py index 911f229c0..8bcc6de09 100644 --- a/coriolis/osmorphing/osmount/windows.py +++ b/coriolis/osmorphing/osmount/windows.py @@ -5,6 +5,7 @@ import uuid from oslo_log import log as logging +from winrm import exceptions as winrm_exceptions from coriolis import exception from coriolis.osmorphing.osmount import base @@ -36,7 +37,22 @@ def check_os(self): "(get-ciminstance Win32_OperatingSystem).Caption") LOG.debug("Windows version: %s", version_info) return True + except winrm_exceptions.InvalidCredentialsError as ex: + raise exception.NotAuthorized( + message="The WinRM connection credentials are invalid. " + "If you are using a template with a default " + "pre-baked username/password, please ensure " + "that you have passed the credentials to the " + "destination Coriolis plugin you have selected," + " either via the Target Environment parameters " + "set when creating the Migration/Replica, or " + "by setting it in the destination plugin's " + "dedicated section of the coriolis.conf " + "static configuration file.") from ex except exception.CoriolisException: + LOG.debug( + "Failed Windows OSMount OS check: %s", + utils.get_exception_details()) pass def _run_diskpart_script(self, script): diff --git a/coriolis/wsman.py b/coriolis/wsman.py index 428f269e5..196aa2c46 100644 --- a/coriolis/wsman.py +++ b/coriolis/wsman.py @@ -100,8 +100,9 @@ def set_timeout(self, timeout): def _exec_command(self, cmd, args=[], timeout=None): timeout = int(timeout or self._conn_timeout) self.set_timeout(timeout) - shell_id = self._protocol.open_shell(codepage=CODEPAGE_UTF8) + shell_id = None try: + shell_id = self._protocol.open_shell(codepage=CODEPAGE_UTF8) command_id = self._protocol.run_command(shell_id, cmd, args) try: (std_out, @@ -116,7 +117,8 @@ def _exec_command(self, cmd, args=[], timeout=None): return (std_out, std_err, exit_code) finally: - self._protocol.close_shell(shell_id) + if shell_id: + self._protocol.close_shell(shell_id) def exec_command(self, cmd, args=[], timeout=None): LOG.debug("Executing WSMAN command: %s", str([cmd] + args)) From e633a7a630eb416aefdbf4db6a8fa11c50919143 Mon Sep 17 00:00:00 2001 From: Nashwan Azhari Date: Tue, 5 Mar 2024 17:43:24 +0200 Subject: [PATCH 2/2] Move winrm_exception.InvalidCredentials handling back to wsman.py. Signed-off-by: Nashwan Azhari --- coriolis/osmorphing/osmount/windows.py | 17 ++++------------- coriolis/wsman.py | 12 ++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/coriolis/osmorphing/osmount/windows.py b/coriolis/osmorphing/osmount/windows.py index 8bcc6de09..0a533d0aa 100644 --- a/coriolis/osmorphing/osmount/windows.py +++ b/coriolis/osmorphing/osmount/windows.py @@ -5,7 +5,6 @@ import uuid from oslo_log import log as logging -from winrm import exceptions as winrm_exceptions from coriolis import exception from coriolis.osmorphing.osmount import base @@ -37,23 +36,15 @@ def check_os(self): "(get-ciminstance Win32_OperatingSystem).Caption") LOG.debug("Windows version: %s", version_info) return True - except winrm_exceptions.InvalidCredentialsError as ex: - raise exception.NotAuthorized( - message="The WinRM connection credentials are invalid. " - "If you are using a template with a default " - "pre-baked username/password, please ensure " - "that you have passed the credentials to the " - "destination Coriolis plugin you have selected," - " either via the Target Environment parameters " - "set when creating the Migration/Replica, or " - "by setting it in the destination plugin's " - "dedicated section of the coriolis.conf " - "static configuration file.") from ex + except exception.NotAuthorized: + # NOTE: Unauthorized exceptions should be propagated. + raise except exception.CoriolisException: LOG.debug( "Failed Windows OSMount OS check: %s", utils.get_exception_details()) pass + return False def _run_diskpart_script(self, script): """Executes the given script with diskpart.exe. diff --git a/coriolis/wsman.py b/coriolis/wsman.py index 196aa2c46..adb69cc27 100644 --- a/coriolis/wsman.py +++ b/coriolis/wsman.py @@ -116,6 +116,18 @@ def _exec_command(self, cmd, args=[], timeout=None): self._protocol.cleanup_command(shell_id, command_id) return (std_out, std_err, exit_code) + except winrm_exceptions.InvalidCredentialsError as ex: + raise exception.NotAuthorized( + message="The WinRM connection credentials are invalid. " + "If you are using a template with a default " + "pre-baked username/password, please ensure " + "that you have passed the credentials to the " + "destination Coriolis plugin you have selected," + " either via the Target Environment parameters " + "set when creating the Migration/Replica, or " + "by setting it in the destination plugin's " + "dedicated section of the coriolis.conf " + "static configuration file.") from ex finally: if shell_id: self._protocol.close_shell(shell_id)