Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: respect custom exit code from plugin #6584

Merged
merged 3 commits into from
May 2, 2024

Conversation

knqyf263
Copy link
Collaborator

@knqyf263 knqyf263 commented Apr 30, 2024

Description

Trivy doesn't respect exit codes from plugins now and always exits with 1 if the plugin fails. This PR adds support for it.

Before

The testplugin always exits with 2.

$ trivy testplugin; echo $?
Error: plugin error: plugin exec: exit status 2
Usage:
  trivy testplugin [flags]

Flags:
  -h, --help   help for testplugin

Global Flags:
      --cache-dir string          cache directory (default "/Users/teppei/Library/Caches/trivy")
  -c, --config string             config path (default "trivy.yaml")
  -d, --debug                     debug mode
      --generate-default-config   write the default config to trivy-default.yaml
      --insecure                  allow insecure server connections
  -q, --quiet                     suppress progress bar and log output
      --timeout duration          timeout (default 5m0s)
  -v, --version                   show version

2024-04-30T22:56:13+04:00       FATAL   Fatal error     plugin error: plugin exec: exit status 2
1

After

It exits with the code from a plugin and doesn't show usage and error anymore.

$ trivy testplugin; echo $?
2

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@knqyf263 knqyf263 self-assigned this Apr 30, 2024
@knqyf263 knqyf263 changed the title feat(plugin): support custom exit codes feat: respect custom exit code from plugin Apr 30, 2024
@knqyf263 knqyf263 marked this pull request as ready for review May 1, 2024 18:04
Copy link
Member

@simar7 simar7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, should we consider the case if the plugin panics? I was thinking along the lines of a global panic handler.

@knqyf263
Copy link
Collaborator Author

knqyf263 commented May 2, 2024

lgtm, should we consider the case if the plugin panics? I was thinking along the lines of a global panic handler.

Trivy calls plugins via OS command. IIUC, panic is just converted into exit status 2.

I created a plugin causing panic.

$ cat main.go
package main

func main() {
        panic("error")
}

$ go build -o trivy-panic main.go

$ ./trivy-panic
panic: error

goroutine 1 [running]:
main.main()
        /tmp/panic/main.go:4 +0x2c
$ echo $?
2

Then, run the plugin through Trivy.

$ trivy panic; echo $?
panic: error

goroutine 1 [running]:
main.main()
        /tmp/panic/main.go:4 +0x2c
$ echo $?
2

I hope it answers your question.

@knqyf263 knqyf263 added this pull request to the merge queue May 2, 2024
Merged via the queue into aquasecurity:main with commit f0961d5 May 2, 2024
12 checks passed
@knqyf263 knqyf263 deleted the custom_exit_code branch May 2, 2024 05:29
knqyf263 added a commit to knqyf263/trivy that referenced this pull request May 2, 2024
fl0pp5 pushed a commit to altlinux/trivy that referenced this pull request May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants