From ab9e02a1a96f36a7d238ffeaf327a858942c3da9 Mon Sep 17 00:00:00 2001 From: twangboy Date: Fri, 18 Oct 2024 14:58:07 -0600 Subject: [PATCH] Fix hanging test --- salt/modules/cmdmod.py | 10 +++++----- salt/utils/win_runas.py | 10 ++++++++-- tests/pytests/functional/utils/test_win_runas.py | 4 ---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/salt/modules/cmdmod.py b/salt/modules/cmdmod.py index 5ec3a6f6a45..cb7aec28bb9 100644 --- a/salt/modules/cmdmod.py +++ b/salt/modules/cmdmod.py @@ -296,7 +296,7 @@ def _prep_powershell_cmd(win_shell, cmd, encoded_cmd): keywords = ["$", "&", ".", "Configuration", "try"] for keyword in keywords: - if cmd.startswith(keyword): + if cmd.lower().startswith(keyword.lower()): new_cmd.extend(["-Command", f"{cmd.strip()}"]) break else: @@ -4096,16 +4096,16 @@ def powershell( # ConvertTo-JSON is only available on PowerShell 3.0 and later psversion = shell_info("powershell")["psversion"] if salt.utils.versions.version_cmp(psversion, "2.0") == 1: - cmd += " | ConvertTo-JSON" + cmd += " | ConvertTo-JSON " if depth is not None: - cmd += f" -Depth {depth}" + cmd += f"-Depth {depth} " # Put the whole command inside a try / catch block # Some errors in PowerShell are not "Terminating Errors" and will not be # caught in a try/catch block. For example, the `Get-WmiObject` command will # often return a "Non Terminating Error". To fix this, make sure # `-ErrorAction Stop` is set in the powershell command - cmd = "try {" + cmd + '} catch { "{}" }' + cmd = "try { " + cmd + ' } catch { "{}" }' if encode_cmd: # Convert the cmd to UTF-16LE without a BOM and base64 encode. @@ -4117,7 +4117,7 @@ def powershell( cmd = salt.utils.stringutils.to_str(cmd) encoded_cmd = True else: - cmd = f"{{{cmd}}}" + cmd = f"{{ {cmd} }}" encoded_cmd = False # Retrieve the response, while overriding shell with 'powershell' diff --git a/salt/utils/win_runas.py b/salt/utils/win_runas.py index 5faf01d3e80..aa2df51dbfc 100644 --- a/salt/utils/win_runas.py +++ b/salt/utils/win_runas.py @@ -199,6 +199,9 @@ def runas(cmdLine, username, password=None, cwd=None): # Create the environment for the user env = create_env(user_token, False) + if "&&" in cmdLine: + cmdLine = f'cmd /c "{cmdLine}"' + hProcess = None try: # Start the process in a suspended state. @@ -206,7 +209,7 @@ def runas(cmdLine, username, password=None, cwd=None): int(user_token), logonflags=1, applicationname=None, - commandline=f'cmd /c "{cmdLine}"', + commandline=cmdLine, currentdirectory=cwd, creationflags=creationflags, startupinfo=startup_info, @@ -297,6 +300,9 @@ def runas_unpriv(cmd, username, password, cwd=None): hStdError=errwrite, ) + if "&&" in cmd: + cmd = f'cmd /c "{cmd}"' + try: # Run command and return process info structure process_info = salt.platform.win.CreateProcessWithLogonW( @@ -304,7 +310,7 @@ def runas_unpriv(cmd, username, password, cwd=None): domain=domain, password=password, logonflags=salt.platform.win.LOGON_WITH_PROFILE, - commandline=f'cmd /c "{cmd}"', + commandline=cmd, startupinfo=startup_info, currentdirectory=cwd, ) diff --git a/tests/pytests/functional/utils/test_win_runas.py b/tests/pytests/functional/utils/test_win_runas.py index acab35171ed..e06e784df72 100644 --- a/tests/pytests/functional/utils/test_win_runas.py +++ b/tests/pytests/functional/utils/test_win_runas.py @@ -4,8 +4,6 @@ Test the win_runas util import pytest -import salt.modules.win_useradd as win_user -import salt.utils.win_functions as win_functions import salt.utils.win_runas as win_runas pytestmark = [ @@ -32,8 +30,6 @@ def test_compound_runas(user): def test_compound_runas_unpriv(user): cmd = "hostname && whoami" - is_admin = win_functions.is_admin(user.username) - user_info = win_user.info(user.username) result = win_runas.runas_unpriv( cmd=cmd, username=user.username,