From b74d467188ef8001598a3c1dc3deccec68add69f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Kacs=C3=A1ndi?= Date: Mon, 22 Jul 2024 15:52:24 +0200 Subject: [PATCH] fix(plugin): make output file path configurable by plugin runtime handler --- .../internal/runtimehandler/binary/handler.go | 13 ++++++++++--- .../internal/runtimehandler/docker/handler.go | 4 ++++ plugins/runner/internal/runtimehandler/types.go | 2 +- plugins/runner/runner.go | 7 ++++++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/plugins/runner/internal/runtimehandler/binary/handler.go b/plugins/runner/internal/runtimehandler/binary/handler.go index 81426ed38..59b1f3492 100644 --- a/plugins/runner/internal/runtimehandler/binary/handler.go +++ b/plugins/runner/internal/runtimehandler/binary/handler.go @@ -30,6 +30,7 @@ import ( "sync" "syscall" + "github.com/google/uuid" multierror "github.com/hashicorp/go-multierror" "github.com/openclarity/vmclarity/plugins/runner/internal/runtimehandler" @@ -45,18 +46,20 @@ type binaryRuntimeHandler struct { stdoutPipe io.ReadCloser stderrPipe io.ReadCloser + pluginServerEndpoint string + outputFilePath string pluginDir string inputDirMountPoint string - pluginServerEndpoint string - ready bool imageCleanup func() + ready bool mu sync.Mutex } func New(ctx context.Context, config types.PluginConfig) (runtimehandler.PluginRuntimeHandler, error) { return &binaryRuntimeHandler{ - config: config, + config: config, + outputFilePath: fmt.Sprintf("/tmp/%s.json", uuid.New().String()), }, nil } @@ -196,6 +199,10 @@ func (h *binaryRuntimeHandler) GetPluginServerEndpoint(ctx context.Context) (str return h.pluginServerEndpoint, nil } +func (h *binaryRuntimeHandler) GetOutputFilePath(ctx context.Context) (string, error) { + return h.outputFilePath, nil +} + func (h *binaryRuntimeHandler) Logs(ctx context.Context) (io.ReadCloser, error) { if h.cmd == nil { return nil, errors.New("plugin process is not running") diff --git a/plugins/runner/internal/runtimehandler/docker/handler.go b/plugins/runner/internal/runtimehandler/docker/handler.go index a67f38cf6..ed0d99d6b 100644 --- a/plugins/runner/internal/runtimehandler/docker/handler.go +++ b/plugins/runner/internal/runtimehandler/docker/handler.go @@ -229,6 +229,10 @@ func (h *containerRuntimeHandler) GetPluginServerEndpoint(ctx context.Context) ( return "http://" + net.JoinHostPort("127.0.0.1", hostPorts[0].HostPort), nil } +func (h *containerRuntimeHandler) GetOutputFilePath(ctx context.Context) (string, error) { + return runtimehandler.RemoteScanResultFileOverride, nil +} + func (h *containerRuntimeHandler) Result(ctx context.Context) (io.ReadCloser, error) { // Copy result file from container reader, _, err := h.client.CopyFromContainer(ctx, h.containerID, runtimehandler.RemoteScanResultFileOverride) diff --git a/plugins/runner/internal/runtimehandler/types.go b/plugins/runner/internal/runtimehandler/types.go index 7ebc431b7..3fa26098d 100644 --- a/plugins/runner/internal/runtimehandler/types.go +++ b/plugins/runner/internal/runtimehandler/types.go @@ -33,6 +33,7 @@ type PluginRuntimeHandler interface { Start(ctx context.Context) error Ready() (bool, error) GetPluginServerEndpoint(ctx context.Context) (string, error) + GetOutputFilePath(ctx context.Context) (string, error) Logs(ctx context.Context) (io.ReadCloser, error) Result(ctx context.Context) (io.ReadCloser, error) Remove(ctx context.Context) error @@ -43,7 +44,6 @@ type PluginRuntimeHandler interface { func WithOverrides(c plugintypes.Config) plugintypes.Config { return plugintypes.Config{ InputDir: RemoteScanInputDirOverride, - OutputFile: RemoteScanResultFileOverride, ScannerConfig: c.ScannerConfig, TimeoutSeconds: c.TimeoutSeconds, } diff --git a/plugins/runner/runner.go b/plugins/runner/runner.go index 7ac7ab07c..e691060f1 100644 --- a/plugins/runner/runner.go +++ b/plugins/runner/runner.go @@ -142,9 +142,14 @@ func (r *pluginRunner) Run(ctx context.Context) error { return errors.New("client missing, did not wait for ready state") } - _, err := r.client.PostConfigWithResponse( + outputFilePath, err := r.runtimeHandler.GetOutputFilePath(ctx) + if err != nil { + return fmt.Errorf("failed to get plugin output file path: %w", err) + } + _, err = r.client.PostConfigWithResponse( ctx, runtimehandler.WithOverrides(plugintypes.Config{ + OutputFile: outputFilePath, ScannerConfig: to.Ptr(r.config.ScannerConfig), TimeoutSeconds: int(types.ScanTimeout.Seconds()), }),