blackducksoftware / hub-detect

This is now deprecated. Please see synopsys-detect.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The powershell script changed the default way of indicating the result code

ilatypov opened this issue · comments

Expected behavior

  • Running the following CMD.EXE command should return the failure on failing the security policy,
    powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command $VerbosePreference = 'Continue'^; $DebugPreference = 'Continue'^; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12^; irm https://blackducksoftware.github.io/hub-detect/hub-detect.ps1?$(Get-Random) ^| iex^; detect --blackduck.hub.url=\"https://XXX.blackducksoftware.com/\" ... --detect.policy.check.fail.on.severities=\"ALL\" ...

Actual behavior

  • Due to a change in the powershell hub-detect.ps1 script contents which was not reflected in this repository's resources/hub-detect-ps, the default behaviour switched from exiting the script to returning its value. While this change has an advantage of enabling integration with wrapping powershell calls, the basic invokation as shown above suffered. It receives an exit code of 0 when the Detect function returns a non-zero value. Besides, Powershell "return" statement may trick newbies if the code path preceding it included any "echo" statements, according to Manoj Mahalingam,
    https://stacktoheap.com/blog/2013/06/15/things-that-trip-newbies-in-powershell-pipeline-output/

Steps to Reproduce

  • The production change
 # If you do not want to exit with the detect exit code,
 # set DETECT_EXIT_CODE_PASSTHRU to 1 and this script won't exit, but simply return it (pass it thru).
-$EnvDetectExitCodePassthru = Get-EnvironmentVariable -Key "DETECT_EXIT_CODE_PASSTHRU" -DefaultValue "";
+$EnvDetectExitCodePassthru = Get-EnvironmentVariable -Key "DETECT_EXIT_CODE_PASSTHRU" -DefaultValue "1";
  • Behaviour before the change in the production script
C:\Temp>type foo.ps1
function Detect {
    exit 123
}

C:\Temp>powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command $VerbosePreference = 'Continue'^; $DebugPreference = 'Continue'^; . .\foo.ps1^; detect

C:\Temp>echo %errorlevel%
123

C:\Temp>powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command $VerbosePreference = 'Continue'^; $DebugPreference = 'Continue'^; . .\foo.ps1^; exit detect

C:\Temp>echo %errorlevel%
123
  • Behaviour after the change in the production script
C:\Temp>type foo.ps1
function Detect {
    return 123
}

C:\Temp>powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command $VerbosePreference = 'Continue'^; $DebugPreference = 'Continue'^; . .\foo.ps1^; detect
123

C:\Temp>echo %errorlevel%
0

C:\Temp>powershell -NoLogo -NoProfile -ExecutionPolicy Bypass -Command $VerbosePreference = 'Continue'^; $DebugPreference = 'Continue'^; . .\foo.ps1^; exit detect

C:\Temp>echo %errorlevel%
123

Version

OS

  • Windows 7+
commented

@taikuukaits Feel like we have seen this problem before, no?

We did which is why we added that flag. Looks like the default was flipped during the major rewrite that occurred for 5. The default should be corrected back to "".

I believe we have switched to the correct default again.