Skip to content

Commit

Permalink
feat(plugin): add support for nested archives (#6845)
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <[email protected]>
  • Loading branch information
knqyf263 authored Jun 7, 2024
1 parent 04af59c commit 622c67b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
7 changes: 4 additions & 3 deletions docs/docs/plugin/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ $ trivy plugin install referrer

This command will download the plugin and install it in the plugin cache.



Trivy adheres to the XDG specification, so the location depends on whether XDG_DATA_HOME is set.
Trivy will now search XDG_DATA_HOME for the location of the Trivy plugins cache.
The preference order is as follows:
Expand All @@ -55,7 +53,10 @@ Furthermore, it is possible to download plugins that are not registered in the i
$ trivy plugin install github.com/aquasecurity/trivy-plugin-kubectl
```
```bash
$ trivy plugin install myplugin.tar.gz
$ trivy plugin install https://github.com/aquasecurity/trivy-plugin-kubectl/archive/refs/heads/main.zip
```
```bash
$ trivy plugin install ./myplugin.tar.gz
```

If the plugin's Git repository is [properly tagged](./developer-guide.md#tagging-plugin-repositories), you can specify the version to install like this:
Expand Down
8 changes: 8 additions & 0 deletions pkg/plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ func (m *Manager) install(ctx context.Context, src string, opts Options) (Plugin
}
defer os.RemoveAll(tempDir)

if entries, err := os.ReadDir(tempDir); err != nil {
return Plugin{}, xerrors.Errorf("failed to read %s: %w", tempDir, err)
} else if len(entries) == 1 && entries[0].IsDir() {
// A single directory may be contained within an archive file.
// e.g. https://github.com/aquasecurity/trivy-plugin-referrer/archive/refs/heads/main.zip
tempDir = filepath.Join(tempDir, entries[0].Name())
}

m.logger.DebugContext(ctx, "Loading the plugin metadata...")
plugin, err := m.loadMetadata(tempDir)
if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions pkg/plugin/manager_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ func modifyManifest(t *testing.T, worktree, version string) {
}

func TestManager_Install(t *testing.T) {
gs := setupGitRepository(t, "test_plugin", "testdata/test_plugin")
gs := setupGitRepository(t, "test_plugin", "testdata/test_plugin/test_plugin")
t.Cleanup(gs.Close)

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
zr := zip.NewWriter(w)
require.NoError(t, zr.AddFS(os.DirFS("testdata/test_plugin")))
switch r.URL.Path {
case "/test_plugin.zip":
require.NoError(t, zr.AddFS(os.DirFS("testdata/test_plugin/test_plugin")))
case "/test_nested.zip":
require.NoError(t, zr.AddFS(os.DirFS("testdata/test_plugin")))
}
require.NoError(t, zr.Close())
}))
t.Cleanup(ts.Close)
Expand Down Expand Up @@ -119,6 +124,13 @@ func TestManager_Install(t *testing.T) {
wantFile: ".trivy/plugins/test_plugin/test.sh",
wantLogs: fmt.Sprintf(wantLogs, ts.URL+"/test_plugin.zip", "0.2.0"),
},
{
name: "nested archive",
pluginName: ts.URL + "/test_nested.zip",
want: wantPlugin,
wantFile: ".trivy/plugins/test_plugin/test.sh",
wantLogs: fmt.Sprintf(wantLogs, ts.URL+"/test_nested.zip", "0.2.0"),
},
{
name: "local path",
pluginName: "testdata/test_plugin",
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit 622c67b

Please sign in to comment.