Fix hanging test

This commit is contained in:
twangboy 2024-10-18 14:58:07 -06:00 committed by Daniel Wozniak
parent 4524d8a54a
commit ab9e02a1a9
3 changed files with 13 additions and 11 deletions

View file

@ -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'

View file

@ -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,
)

View file

@ -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,