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: add support for plugin index #6674

Merged
merged 26 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
822a133
feat: add support for plugin index
knqyf263 May 9, 2024
486eb48
feat(plugin): output mode
knqyf263 May 10, 2024
ddbed50
docs: add user-guide and developer-guide
knqyf263 May 10, 2024
b69a5a8
feat: add installed platform
knqyf263 May 10, 2024
bf0847d
test: respect XDG_DATA_HOME
knqyf263 May 13, 2024
a7c71fa
docs: auto-generate references
knqyf263 May 13, 2024
8553252
docs: update a developer guide
knqyf263 May 13, 2024
739c0e9
Use NoArgs
knqyf263 May 13, 2024
9051fef
Update docs/docs/plugin/user-guide.md
knqyf263 May 13, 2024
d41b592
refactor: remove unneeded PersistentRun
knqyf263 May 13, 2024
9dbecdf
fix(plugin): delay initilizing plugin manager
knqyf263 May 13, 2024
2fcd08b
fix: remove MaximumNArgs
knqyf263 May 13, 2024
d2f2e96
refactor: pass one argument as a keyword
knqyf263 May 13, 2024
5a64f2d
feat: replace usage with summary
knqyf263 May 13, 2024
6bcd91b
test: use a common error message
knqyf263 May 13, 2024
39ce40e
docs: mention the keyword matches name or desc
knqyf263 May 13, 2024
466b9f5
chore: add info for successful installation
knqyf263 May 13, 2024
7e62137
chore: fix usage
knqyf263 May 13, 2024
1a5f094
docs: update the developer guide
knqyf263 May 13, 2024
99ab575
Update docs/docs/plugin/user-guide.md
knqyf263 May 14, 2024
c622be1
Update docs/docs/plugin/index.md
knqyf263 May 14, 2024
a240e52
docs: update mkdocs
knqyf263 May 14, 2024
60f4848
fix: use a local logger
knqyf263 May 14, 2024
1835268
fix: add SilenceUsage
knqyf263 May 14, 2024
2fe4f21
chore: add a message on uninstallation
knqyf263 May 14, 2024
7fae791
feat: add version to the index
knqyf263 May 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/semantic-pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ jobs:
k8s
aws
vm
plugin
alpine
wolfi
Expand Down
5 changes: 1 addition & 4 deletions cmd/trivy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ func main() {
func run() error {
// Trivy behaves as the specified plugin.
if runAsPlugin := os.Getenv("TRIVY_RUN_AS_PLUGIN"); runAsPlugin != "" {
if !plugin.IsPredefined(runAsPlugin) {
return xerrors.Errorf("unknown plugin: %s", runAsPlugin)
}
if err := plugin.RunWithURL(context.Background(), runAsPlugin, plugin.RunOptions{Args: os.Args[1:]}); err != nil {
if err := plugin.RunWithURL(context.Background(), runAsPlugin, plugin.Options{Args: os.Args[1:]}); err != nil {
return xerrors.Errorf("plugin error: %w", err)
}
return nil
Expand Down
1 change: 1 addition & 0 deletions docs/community/contribute/pr.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mode:
- server
- aws
- vm
- plugin

os:

Expand Down
236 changes: 0 additions & 236 deletions docs/docs/advanced/plugins.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/docs/configuration/reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ $ trivy <target> [--format <format>] --output plugin=<plugin_name> [--output-plu
```

This is useful for cases where you want to convert the output into a custom format, or when you want to send the output somewhere.
For more details, please check [here](../advanced/plugins.md#output-plugins).
For more details, please check [here](../plugin/plugins.md#output-plugins).

## Converting
To generate multiple reports, you can generate the JSON report first and convert it to other formats with the `convert` subcommand.
Expand Down
124 changes: 124 additions & 0 deletions docs/docs/plugin/developer-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
# Developer Guide

## Introduction
If you are looking to start developing plugins for Trivy, read [the user guide](./user-guide.md) first.

To summarize the documentation, the procedure is to:

- Create a repository for your plugin, named `trivy-plugin-<name>`.
- Create an executable binary that can be invoked as `trivy <name>`.
- Place the executable binary in a repository.
- Create a `plugin.yaml` file that describes the plugin.

After you develop a plugin with a good name following the best practices, you can develop a [trivy-plugin-index][trivy-plugin-index] manifest and submit your plugin.

## Naming
This section describes guidelines for naming your plugins.

### Use `trivy-plugin-` prefix
The name of the plugin repository should be prefixed with `trivy-plugin-`.

### Use lowercase and hyphens
Plugin names must be all lowercase and separate words with hyphens.
Don’t use camelCase, PascalCase, or snake_case; use kebab-case.

- NO: `trivy OpenSvc`
- YES: `trivy open-svc`

### Be specific
Plugin names should not be verbs or nouns that are generic, already overloaded, or likely to be used for broader purposes by another plugin.

- NO: trivy sast (Too broad)
- YES: trivy govulncheck


### Be unique
Find a unique name for your plugin that differentiates it from other plugins that perform a similar function.

- NO: `trivy images` (Unclear how it is different from the builtin “image" command)
- YES: `trivy registry-images` (Unique name).

### Prefix Vendor Identifiers
Use vendor-specific strings as prefix, separated with a dash.
This makes it easier to search/group plugins that are about a specific vendor.

- NO: `trivy security-hub-aws (Makes it harder to search or locate in a plugin list)
- YES: `trivy aws-security-hub (Will show up together with other aws-* plugins)

Each plugin has a top-level directory, and then a plugin.yaml file.

```bash
your-plugin/
|
|- plugin.yaml
|- your-plugin.sh
```

In the example above, the plugin is contained inside of a directory named `your-plugin`.
It has two files: plugin.yaml (required) and an executable script, your-plugin.sh (optional).

The core of a plugin is a simple YAML file named plugin.yaml.
Here is an example YAML of trivy-plugin-kubectl plugin that adds support for Kubernetes scanning.

```yaml
name: "kubectl"
repository: github.com/aquasecurity/trivy-plugin-kubectl
version: "0.1.0"
usage: scan kubectl resources
description: |-
A Trivy plugin that scans the images of a kubernetes resource.
Usage: trivy kubectl TYPE[.VERSION][.GROUP] NAME
platforms:
- selector: # optional
os: darwin
arch: amd64
uri: ./trivy-kubectl # where the execution file is (local file, http, git, etc.)
bin: ./trivy-kubectl # path to the execution file
- selector: # optional
os: linux
arch: amd64
uri: https://github.com/aquasecurity/trivy-plugin-kubectl/releases/download/v0.1.0/trivy-kubectl.tar.gz
bin: ./trivy-kubectl
```

The `plugin.yaml` field should contain the following information:

- name: The name of the plugin. This also determines how the plugin will be made available in the Trivy CLI. For example, if the plugin is named kubectl, you can call the plugin with `trivy kubectl`. (required)
- version: The version of the plugin. (required)
- usage: A short usage description. (required)
- description: A long description of the plugin. This is where you could provide a helpful documentation of your plugin. (required)
- platforms: (required)
- selector: The OS/Architecture specific variations of a execution file. (optional)
- os: OS information based on GOOS (linux, darwin, etc.) (optional)
- arch: The architecture information based on GOARCH (amd64, arm64, etc.) (optional)
- uri: Where the executable file is. Relative path from the root directory of the plugin or remote URL such as HTTP and S3. (required)
- bin: Which file to call when the plugin is executed. Relative path from the root directory of the plugin. (required)

The following rules will apply in deciding which platform to select:

- If both `os` and `arch` under `selector` match the current platform, search will stop and the platform will be used.
- If `selector` is not present, the platform will be used.
- If `os` matches and there is no more specific `arch` match, the platform will be used.
- If no `platform` match is found, Trivy will exit with an error.

After determining platform, Trivy will download the execution file from `uri` and store it in the plugin cache.
When the plugin is called via Trivy CLI, `bin` command will be executed.

The plugin is responsible for handling flags and arguments. Any arguments are passed to the plugin from the `trivy` command.

A plugin should be archived `*.tar.gz`.

```bash
$ tar -czvf myplugin.tar.gz plugin.yaml script.py
plugin.yaml
script.py

$ trivy plugin install myplugin.tar.gz
2023-03-03T19:04:42.026+0600 INFO Installing the plugin from myplugin.tar.gz...
2023-03-03T19:04:42.026+0600 INFO Loading the plugin metadata...

$ trivy myplugin
Hello from Trivy demo plugin!
```

[trivy-plugin-index]: https://github.com/aquasecurity/trivy-plugin-index
Loading
Loading