From f81229c85c6573120c188850200cbed1b0e5371a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Subileau?= Date: Mon, 27 Aug 2018 10:20:12 +0200 Subject: [PATCH] win_nssm: use Run-Command instead of Invoke-Expression to prevent interpretation issue Fix #44079 (cherry picked from commit 20a0d90ebeec92fc498e29d57393b280f6e639a5) --- lib/ansible/modules/windows/win_nssm.ps1 | 192 +++++++++++------------ test/sanity/pslint/ignore.txt | 1 - 2 files changed, 90 insertions(+), 103 deletions(-) diff --git a/lib/ansible/modules/windows/win_nssm.ps1 b/lib/ansible/modules/windows/win_nssm.ps1 index c10b6ce2b26..483fe78c7df 100644 --- a/lib/ansible/modules/windows/win_nssm.ps1 +++ b/lib/ansible/modules/windows/win_nssm.ps1 @@ -6,6 +6,7 @@ # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) #Requires -Module Ansible.ModuleUtils.Legacy +#Requires -Module Ansible.ModuleUtils.CommandUtil $ErrorActionPreference = "Stop" @@ -35,8 +36,6 @@ if (($appParameters -ne $null) -and ($appParametersFree -ne $null)) Fail-Json $result "Use either app_parameters or app_parameteres_free_form, but not both" } -#abstract the calling of nssm because some PowerShell environments -#mishandle its stdout(which is Unicode) as UTF8 Function Nssm-Invoke { [CmdletBinding()] @@ -44,21 +43,10 @@ Function Nssm-Invoke [Parameter(Mandatory=$true)] [string]$cmd ) - Try { - $encodingWas = [System.Console]::OutputEncoding - [System.Console]::OutputEncoding = [System.Text.Encoding]::Unicode - $nssmOutput = invoke-expression "nssm $cmd" - return $nssmOutput - } - Catch { - $ErrorMessage = $_.Exception.Message - Fail-Json $result "an exception occurred when invoking NSSM: $ErrorMessage" - } - Finally { - # Set the console encoding back to what it was - [System.Console]::OutputEncoding = $encodingWas - } + $nssm_result = Run-Command -command "nssm $cmd" + + return $nssm_result } Function Service-Exists @@ -84,15 +72,15 @@ Function Nssm-Remove { if ((Get-Service -Name $name).Status -ne "Stopped") { $cmd = "stop ""$name""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd } $cmd = "remove ""$name"" confirm" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error removing service ""$name""" } @@ -122,12 +110,12 @@ Function Nssm-Install if (!(Service-Exists -name $name)) { - $results = Nssm-Invoke "install ""$name"" ""$application""" + $nssm_result = Nssm-Invoke "install ""$name"" ""$application""" - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error installing service ""$name""" } @@ -135,25 +123,25 @@ Function Nssm-Install $result.changed = $true } else { - $results = Nssm-Invoke "get ""$name"" Application" + $nssm_result = Nssm-Invoke "get ""$name"" Application" - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error installing service ""$name""" } - if ($results -cnotlike $application) + if ($nssm_result.stdout -cnotlike $application) { $cmd = "set ""$name"" Application ""$application""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error installing service ""$name""" } $result.application = "$application" @@ -166,14 +154,14 @@ Function Nssm-Install if ($result.changed) { $applicationPath = (Get-Item $application).DirectoryName - $cmd = "nssm set ""$name"" AppDirectory ""$applicationPath""" + $cmd = "set ""$name"" AppDirectory ""$applicationPath""" - $results = invoke-expression $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error installing service ""$name""" } } @@ -207,12 +195,12 @@ Function Nssm-Update-AppParameters ) $cmd = "get ""$name"" AppParameters" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating AppParameters for service ""$name""" } @@ -250,7 +238,7 @@ Function Nssm-Update-AppParameters $result.nssm_app_parameters = $appParameters $result.nssm_single_line_app_parameters = $singleLineParams - if ($results -ne $singleLineParams) + if ($nssm_result.stdout -ne $singleLineParams) { if ($appParameters -or $appParametersFree) { @@ -258,12 +246,12 @@ Function Nssm-Update-AppParameters } else { $cmd = "set ""$name"" AppParameters '""""'" } - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating AppParameters for service ""$name""" } @@ -283,16 +271,16 @@ Function Nssm-Set-Output-Files ) $cmd = "get ""$name"" AppStdout" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error retrieving existing stdout file for service ""$name""" } - if ($results -cnotlike $stdout) + if ($nssm_result.stdout -cnotlike $stdout) { if (!$stdout) { @@ -301,12 +289,12 @@ Function Nssm-Set-Output-Files $cmd = "set ""$name"" AppStdout $stdout" } - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error setting stdout file for service ""$name""" } @@ -315,36 +303,36 @@ Function Nssm-Set-Output-Files } $cmd = "get ""$name"" AppStderr" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error retrieving existing stderr file for service ""$name""" } - if ($results -cnotlike $stderr) + if ($nssm_result.stdout -cnotlike $stderr) { if (!$stderr) { $cmd = "reset ""$name"" AppStderr" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error clearing stderr file setting for service ""$name""" } } else { $cmd = "set ""$name"" AppStderr $stderr" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error setting stderr file for service ""$name""" } } @@ -359,27 +347,27 @@ Function Nssm-Set-Output-Files #set files to overwrite $cmd = "set ""$name"" AppStdoutCreationDisposition 2" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd $cmd = "set ""$name"" AppStderrCreationDisposition 2" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd #enable file rotation $cmd = "set ""$name"" AppRotateFiles 1" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd #don't rotate until the service restarts $cmd = "set ""$name"" AppRotateOnline 0" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd #both of the below conditions must be met before rotation will happen #minimum age before rotating $cmd = "set ""$name"" AppRotateSeconds 86400" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd #minimum size before rotating $cmd = "set ""$name"" AppRotateBytes 104858" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd } Function Nssm-Update-Credentials @@ -395,12 +383,12 @@ Function Nssm-Update-Credentials ) $cmd = "get ""$name"" ObjectName" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating credentials for service ""$name""" } @@ -414,14 +402,14 @@ Function Nssm-Update-Credentials $fullUser = ".\" + $user } - If ($results -ne $fullUser) { + If ($nssm_result.stdout -ne $fullUser) { $cmd = "set ""$name"" ObjectName $fullUser '$password'" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating credentials for service ""$name""" } @@ -443,23 +431,23 @@ Function Nssm-Update-Dependencies ) $cmd = "get ""$name"" DependOnService" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating dependencies for service ""$name""" } - If (($dependencies) -and ($results.Tolower() -ne $dependencies.Tolower())) { + If (($dependencies) -and ($nssm_result.stdout.Tolower() -ne $dependencies.Tolower())) { $cmd = "set ""$name"" DependOnService $dependencies" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating dependencies for service ""$name""" } @@ -479,25 +467,25 @@ Function Nssm-Update-StartMode ) $cmd = "get ""$name"" Start" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating start mode for service ""$name""" } $modes=@{"auto" = "SERVICE_AUTO_START"; "delayed" = "SERVICE_DELAYED_AUTO_START"; "manual" = "SERVICE_DEMAND_START"; "disabled" = "SERVICE_DISABLED"} $mappedMode = $modes.$mode - if ($results -cnotlike $mappedMode) { + if ($nssm_result.stdout -cnotlike $mappedMode) { $cmd = "set ""$name"" Start $mappedMode" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error updating start mode for service ""$name""" } @@ -515,9 +503,9 @@ Function Nssm-Get-Status ) $cmd = "status ""$name""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - return ,$results + return $nssm_result } Function Nssm-Start @@ -530,14 +518,14 @@ Function Nssm-Start $currentStatus = Nssm-Get-Status -name $name - if ($LastExitCode -ne 0) + if ($currentStatus.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $currentStatus.stderr Throw "Error starting service ""$name""" } - switch ($currentStatus) + switch ($currentStatus.stdout) { "SERVICE_RUNNING" { <# Nothing to do #> } "SERVICE_STOPPED" { Nssm-Start-Service-Command -name $name } @@ -560,12 +548,12 @@ Function Nssm-Start-Service-Command $cmd = "start ""$name""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error starting service ""$name""" } @@ -583,12 +571,12 @@ Function Nssm-Stop-Service-Command $cmd = "stop ""$name""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error stopping service ""$name""" } @@ -606,23 +594,23 @@ Function Nssm-Stop $currentStatus = Nssm-Get-Status -name $name - if ($LastExitCode -ne 0) + if ($currentStatus.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $currentStatus.stderr Throw "Error stopping service ""$name""" } - if ($currentStatus -ne "SERVICE_STOPPED") + if ($currentStatus.stdout -ne "SERVICE_STOPPED") { $cmd = "stop ""$name""" - $results = Nssm-Invoke $cmd + $nssm_result = Nssm-Invoke $cmd - if ($LastExitCode -ne 0) + if ($nssm_result.rc -ne 0) { $result.nssm_error_cmd = $cmd - $result.nssm_error_log = "$results" + $result.nssm_error_log = $nssm_result.stderr Throw "Error stopping service ""$name""" } diff --git a/test/sanity/pslint/ignore.txt b/test/sanity/pslint/ignore.txt index 1915f87dd8a..a5052bb086d 100644 --- a/test/sanity/pslint/ignore.txt +++ b/test/sanity/pslint/ignore.txt @@ -63,7 +63,6 @@ lib/ansible/modules/windows/win_iis_website.ps1 PSUseBOMForUnicodeEncodedFile lib/ansible/modules/windows/win_iis_website.ps1 PSUseDeclaredVarsMoreThanAssignments lib/ansible/modules/windows/win_mapped_drive.ps1 PSAvoidUsingConvertToSecureStringWithPlainText lib/ansible/modules/windows/win_nssm.ps1 PSAvoidUsingCmdletAliases -lib/ansible/modules/windows/win_nssm.ps1 PSAvoidUsingInvokeExpression lib/ansible/modules/windows/win_nssm.ps1 PSAvoidUsingPlainTextForPassword lib/ansible/modules/windows/win_nssm.ps1 PSAvoidUsingUserNameAndPassWordParams lib/ansible/modules/windows/win_nssm.ps1 PSUseApprovedVerbs