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

Commit

Permalink
fix(plugin): make output file path configurable by pluginruntimehandl…
Browse files Browse the repository at this point in the history
…er (#1941)

* fix(plugin): make output file path configurable by plugin runtime handler

* fix(plugin): add github.com/google/uuid

* revert(plugin): add OutputFile back

* fix(plugin): read results from outputFilePath
  • Loading branch information
zsoltkacsandi authored Jul 22, 2024
1 parent 88457ff commit f0eda70
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion plugins/runner/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.22.4
require (
github.com/docker/docker v26.1.4+incompatible
github.com/docker/go-connections v0.5.0
github.com/google/uuid v1.6.0
github.com/hashicorp/go-multierror v1.1.1
github.com/oapi-codegen/oapi-codegen/v2 v2.3.0
github.com/openclarity/vmclarity/core v0.7.2
Expand Down Expand Up @@ -54,7 +55,6 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-containerregistry v0.19.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down
15 changes: 11 additions & 4 deletions plugins/runner/internal/runtimehandler/binary/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}

Expand Down Expand Up @@ -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")
Expand All @@ -216,7 +223,7 @@ func (h *binaryRuntimeHandler) Logs(ctx context.Context) (io.ReadCloser, error)
}

func (h *binaryRuntimeHandler) Result(ctx context.Context) (io.ReadCloser, error) {
f, err := os.Open(filepath.Join(h.pluginDir, runtimehandler.RemoteScanResultFileOverride))
f, err := os.Open(filepath.Join(h.pluginDir, h.outputFilePath))
if err != nil {
return nil, fmt.Errorf("unable to open result file: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions plugins/runner/internal/runtimehandler/docker/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion plugins/runner/internal/runtimehandler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,7 +44,7 @@ type PluginRuntimeHandler interface {
func WithOverrides(c plugintypes.Config) plugintypes.Config {
return plugintypes.Config{
InputDir: RemoteScanInputDirOverride,
OutputFile: RemoteScanResultFileOverride,
OutputFile: c.OutputFile,
ScannerConfig: c.ScannerConfig,
TimeoutSeconds: c.TimeoutSeconds,
}
Expand Down
7 changes: 6 additions & 1 deletion plugins/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
}),
Expand Down

0 comments on commit f0eda70

Please sign in to comment.