Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
feat(plugin): enable caching for binary artifacts (#1924)
Browse files Browse the repository at this point in the history
* feat(plugin): enable caching for binary artifacts

* fix(plugin): fix description of BinaryArtifactsClean

* fix(plugin): fix typo

* fix(plugin): fix linter warning

* fix(plugin): fix test
  • Loading branch information
zsoltkacsandi authored Jul 17, 2024
1 parent 58df361 commit a61acee
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 120 deletions.
2 changes: 2 additions & 0 deletions .families.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ rootkits:
plugins:
enabled: false
binary_mode: false
binary_artifacts_path: ""
binary_artifacts_clean: true
scanners_list:
- "kics"
inputs:
Expand Down
4 changes: 4 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2089,6 +2089,10 @@ components:
type: boolean
binary_mode:
type: boolean
binary_artifacts_path:
type: string
binary_artifacts_clean:
type: boolean
scanners_list:
type: array
items:
Expand Down
197 changes: 99 additions & 98 deletions api/server/internal/server/server.gen.go

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions api/types/types.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions cli/state/testdata/effective-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@
}
},
"plugins": {
"Enabled":false,
"ScannersList":null,
"Inputs":null,
"ScannersConfig":null,
"binary_mode":false
"Enabled": false,
"ScannersList": null,
"Inputs": null,
"ScannersConfig": null,
"BinaryMode": false,
"BinaryArtifactsPath": "",
"BinaryArtifactsClean": false
}
}
23 changes: 16 additions & 7 deletions plugins/runner/internal/runtimehandler/binary/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,19 @@ func (h *binaryRuntimeHandler) Start(ctx context.Context) error {
}
h.imageCleanup = cleanup

home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to determine user's home directory: %w", err)
var binaryArtifactsPath string
if h.config.BinaryArtifactsPath != "" {
binaryArtifactsPath = h.config.BinaryArtifactsPath
} else {
home, err := os.UserHomeDir()
if err != nil {
return fmt.Errorf("unable to determine user's home directory: %w", err)
}

binaryArtifactsPath = filepath.Join(home, ".vmclarity/plugins")
}

h.pluginDir = filepath.Join(home, ".vmclarity/plugins", h.config.Name, image.Metadata.ID)
h.pluginDir = filepath.Join(binaryArtifactsPath, h.config.Name, image.Metadata.ID)

if _, err := os.Stat(h.pluginDir); os.IsNotExist(err) {
err = containerrootfs.ToDirectory(ctx, image, h.pluginDir)
Expand Down Expand Up @@ -232,9 +239,11 @@ func (h *binaryRuntimeHandler) Remove(ctx context.Context) error {
if err := syscall.Unmount(h.inputDirMountPoint, 0); err != nil {
removeErr = multierror.Append(removeErr, fmt.Errorf("failed to kill plugin process: %w", err))
} else {
// Call the cleanup function for the image only after the input directory is unmounted, or else it will also remove
// the root filesystem mounted under input
h.imageCleanup()
if h.config.BinaryArtifactsClean {
// Call the cleanup function for the image only after the input directory is unmounted, or else it will also remove
// the root filesystem mounted under input
h.imageCleanup()
}
}

return removeErr //nolint:wrapcheck
Expand Down
4 changes: 4 additions & 0 deletions plugins/runner/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ type PluginConfig struct {
TimeoutSeconds int `yaml:"timeout_seconds" mapstructure:"timeout_seconds"`
// BinaryMode is a flag to indicate that the plugin should be run as a binary
BinaryMode bool `yaml:"binary_mode" mapstructure:"binary_mode"`
// BinaryArtifactsPath is the location of the extracted container images
BinaryArtifactsPath string `yaml:"binary_artifacts_path" mapstructure:"binary_artifacts_path"`
// BinaryArtifactsClean is a flag to indicate that the downloaded and extracted container image needs to be cleaned up after the plugin execution
BinaryArtifactsClean bool `yaml:"binary_artifacts_clean" mapstructure:"binary_artifacts_clean"`
}

type PluginRunner interface {
Expand Down
6 changes: 5 additions & 1 deletion scanner/families/plugins/runner/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ type Config struct {
// ScannerConfig is a json string that will be passed to the scanner in the plugin
ScannerConfig string `yaml:"scanner_config" mapstructure:"scanner_config"`
// BinaryMode is a flag to indicate that the plugin should be run as a binary
BinaryMode bool `json:"binary_mode,omitempty" mapstructure:"binary_mode,omitempty"`
BinaryMode bool `yaml:"binary_mode,omitempty" mapstructure:"binary_mode,omitempty"`
// BinaryArtifactsPath is the location of the extracted container images
BinaryArtifactsPath string `yaml:"binary_artifacts_path" mapstructure:"binary_artifacts_path"`
// BinaryArtifactsClean is a flag to indicate that the downloaded and extracted container image needs to be cleaned up after the plugin execution
BinaryArtifactsClean bool `yaml:"binary_artifacts_clean" mapstructure:"binary_artifacts_clean"`
}
12 changes: 7 additions & 5 deletions scanner/families/plugins/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import (
)

type Config struct {
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
ScannersList []string `yaml:"scanners_list" mapstructure:"scanners_list"`
Inputs []common.ScanInput `yaml:"inputs" mapstructure:"inputs"`
ScannersConfig ScannersConfig `yaml:"scanners_config" mapstructure:"scanners_config"`
BinaryMode bool `json:"binary_mode" mapstructure:"binary_mode"`
Enabled bool `yaml:"enabled" mapstructure:"enabled"`
ScannersList []string `yaml:"scanners_list" mapstructure:"scanners_list"`
Inputs []common.ScanInput `yaml:"inputs" mapstructure:"inputs"`
ScannersConfig ScannersConfig `yaml:"scanners_config" mapstructure:"scanners_config"`
BinaryMode bool `yaml:"binary_mode" mapstructure:"binary_mode"`
BinaryArtifactsPath string `yaml:"binary_artifacts_path" mapstructure:"binary_artifacts_path"`
BinaryArtifactsClean bool `yaml:"binary_artifacts_clean" mapstructure:"binary_artifacts_clean"`
}

type ScannersConfig map[string]runnerconfig.Config

0 comments on commit a61acee

Please sign in to comment.