Exit Handling and Return Codes
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Exit handling in 2ms is a mechanism designed to communicate the result of a secret scan to the calling environment, such as a shell or a CI/CD pipeline. The tool uses specific non-zero exit codes to distinguish between different types of outcomes, such as identifying secrets versus encountering execution errors. This behavior is highly configurable, allowing users to suppress certain exit codes based on their integration requirements.
The system primary ensures that automated workflows can react appropriately when sensitive information is detected. By default, the presence of secrets or the occurrence of an engine error will trigger a non-zero exit code, but these can be ignored using global flags.
Sources: README.md:144-150, cmd/main.go:102-104
Exit Code Configuration
The behavior of the application upon completion is governed by the --ignore-on-exit flag. This flag defines which conditions that would normally trigger a non-zero exit code should instead be treated as a successful execution (exit code 0).
Ignore Options
| Option | Description |
|---|---|
none | Default behavior. Any findings or errors result in a non-zero exit code. |
all | All non-zero exit codes are ignored; the tool always exits with 0. |
results | Only findings (secrets detected) are ignored. Engine errors still trigger non-zero exits. |
errors | Only engine errors are ignored. Secret findings still trigger non-zero exits. |
Sources: README.md:204-207, cmd/exit_handler_test.go:11-66
Logic Flow for Exit Determination
The tool evaluates whether to return an error code by checking the internal state of the ignoreOnExit variable. The logic distinguishes between "results" (secrets found) and "errors" (runtime issues).
This diagram illustrates the decision logic used to determine the final process exit code based on the scan results and user configuration.
Sources: cmd/exit_handler_test.go:11-96, cmd/main.go:121-125
Implementation Details
The core logic for evaluating exit codes is verified through the exit_handler_test.go suite, which tests the isNeedReturnErrorCodeFor function against various user inputs.
Return Code Significance
While the exact integer values for errorCode and resultsCode are handled internally, the system distinguishes them to allow CI/CD pipelines to differentiate between a "failed scan" (technical failure) and a "vulnerable scan" (secrets found).
- Result-based Exit: Occurs when
report.TotalSecretsFound > 0andignore-on-exitis not set toallorresults. - Error-based Exit: Occurs when a plugin or the engine returns an error and
ignore-on-exitis not set toallorerrors.
Sources: cmd/exit_handler_test.go:98-150, cmd/main.go:132-134, tests/e2e_test.go:23-25
Integration in CI/CD
In automated environments like GitHub Actions or Azure DevOps, these return codes are used to fail the build step. For instance, if a secret is found in a pull request, the non-zero exit code will prevent the PR from being merged if the CI policy requires successful status checks.
- name: Run 2ms Scan
run: docker run -v $(pwd):/repo checkmarx/2ms:2.8.1 git /repo
In the example above, if 2ms identifies a secret, the run step will fail due to the non-zero return code.
Sources: README.md:100-112, tests/e2e_test.go:42-44
Conclusion
The exit handling system in 2ms provides the necessary granularity for both manual and automated secret scanning. By leveraging the --ignore-on-exit flag, users can tailor the tool's feedback to fit specific workflow requirements, ensuring that secret detection is actionable without being disruptive to execution environments when errors are expected or results are handled elsewhere.