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(report): output plugin #4863

Merged
merged 11 commits into from
Dec 4, 2023
Merged

Conversation

knqyf263
Copy link
Collaborator

@knqyf263 knqyf263 commented Jul 24, 2023

Description

This PR adds support for output plugins.

$ trivy plugin install <plugin_url>
$ trivy <target> [--format <format>] --output plugin=<plugin_name> [--output-plugin-arg <plugin_args>] <target_name>

Examples

$ trivy plugin install github.com/aquasecurity/trivy-output-plugin-count
$ trivy image -f json -o plugin=count --output-plugin-arg "--publish-after 2023-10-01" debian:12
trivy image -f json -o plugin=webhook --output-plugin-arg "--url=http://localhost:8080" debian:12
trivy image -f cyclonedx -o plugin=referrer --output-plugin-arg "put --insecure" debian:12

Related issues

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).

[skip ci]
@knqyf263 knqyf263 self-assigned this Jul 24, 2023
@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label Sep 23, 2023
@knqyf263 knqyf263 removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label Sep 27, 2023
@github-actions github-actions bot added the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label Nov 27, 2023
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Signed-off-by: knqyf263 <knqyf263@gmail.com>
Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263
Copy link
Collaborator Author

@itaysk After all, I added another flag to take the plugin arguments as is, like --output-plugin-arg "put --insecure" (see the examples above). Please let me know if you have any feedback.

@github-actions github-actions bot removed the lifecycle/stale Denotes an issue or PR has remained open with no activity and will be auto-closed. label Nov 28, 2023
Signed-off-by: knqyf263 <knqyf263@gmail.com>
@knqyf263 knqyf263 marked this pull request as ready for review November 28, 2023 13:25
Copy link
Contributor

@DmitriyLewen DmitriyLewen left a comment

Choose a reason for hiding this comment

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

I left few small comments.

Also i found 1 case:
We don't stop Trivy, if plugin argument is invalid:

➜  trivy git:(output_plugin) ✗ ./trivy image --format json --output plugin=count --output-plugin-arg "--published-afterrrrr 2022-10-01" debian:12
...
flag provided but not defined: -published-afterrrrr
Usage of /Users/work/.trivy/plugins/count/count:
  -published-after string
        take vulnerabilities published after the specified timestamp (ex. 2019-11-04)
  -published-before string
        take vulnerabilities published before the specified timestamp (ex. 2019-11-04)

pkg/flag/options.go Outdated Show resolved Hide resolved
pkg/flag/options.go Outdated Show resolved Hide resolved
pkg/flag/report_flags.go Show resolved Hide resolved
pkg/flag/report_flags.go Outdated Show resolved Hide resolved
@itaysk
Copy link
Contributor

itaysk commented Nov 29, 2023

IMO it's worth adding at least the kind: output field to the plugin yaml, to differentiate output plugins from other plugins. we don't need to implement anything with it, but it can serve us in the future for things like discovery (find me an output plugin for html), error handling (user tried to use an output plugin as an execution plugin), improve ux and more.

@knqyf263
Copy link
Collaborator Author

We don't stop Trivy, if plugin argument is invalid:

Do you mean we should exit early? Any ideas on implementation?

@knqyf263
Copy link
Collaborator Author

IMO it's worth adding at least the kind: output field to the plugin yaml, to differentiate output plugins from other plugins. we don't need to implement anything with it, but it can serve us in the future for things like discovery (find me an output plugin for html), error handling (user tried to use an output plugin as an execution plugin), improve ux and more.

I'm supposed to do that in another PR.

@DmitriyLewen
Copy link
Contributor

Do you mean we should exit early? Any ideas on implementation?

No. Trivy simply freezes when plugin returns error (e.g. wrong flag or incorrect data format).

I checked this case and found a pattern:
cmd waits when we read os.stdin (if i understand correctly Pipe reader blocks channel and cmd can't finish work without closing this channel.)

e.g. i moved json decoding for trivy-output-plugin-count to the beginning of the run function:

	var report types.Report
	if err := json.NewDecoder(os.Stdin).Decode(&report); err != nil {
		return err
	}

	publishedBefore := flag.String("published-before", "", "take vulnerabilities published before the specified timestamp (ex. 2019-11-04)")
	publishedAfter := flag.String("published-after", "", "take vulnerabilities published after the specified timestamp (ex. 2019-11-04)")
	flag.Parse()
...

In this case Trivy works correctly:

➜  trivy git:(output_plugin) ✗ ./trivy image --format json --output plugin=count --output-plugin-arg "--published-after 20222-10-01" debian:12
2023-11-30T12:00:14.895+0600    INFO    Vulnerability scanning is enabled
2023-11-30T12:00:14.895+0600    INFO    Secret scanning is enabled
2023-11-30T12:00:14.895+0600    INFO    If your scanning is slow, please try '--scanners vuln' to disable secret scanning
2023-11-30T12:00:14.895+0600    INFO    Please see also https://aquasecurity.github.io/trivy/dev/docs/scanner/secret/#recommendation for faster secret detection
2023-11-30T12:00:22.221+0600    INFO    Detected OS: debian
2023-11-30T12:00:22.221+0600    INFO    Detecting Debian vulnerabilities...
2023-11-30T12:00:22.232+0600    INFO    Number of language-specific files: 0
2023/11/30 12:00:22 parsing time "20222-10-01" as "2006-01-02": cannot parse "2-10-01" as "-"
2023-11-30T12:00:22.252+0600    FATAL   report error: unable to write results: plugin error: exit status 1

It looks like we can add a timeout (I think we need to add a timeout for the plugin as output anyway) or add information to docs that Stdin should be read anyway.

@knqyf263 knqyf263 added this pull request to the merge queue Dec 4, 2023
Merged via the queue into aquasecurity:main with commit 99c04c4 Dec 4, 2023
17 checks passed
@knqyf263 knqyf263 deleted the output_plugin branch December 4, 2023 11:50
@knqyf263 knqyf263 mentioned 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.

plugin as output option
3 participants