From 1917ca5f79ce8bf076d73aa884d0d62eb007fddf Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Thu, 7 Jan 2021 10:47:51 -0500 Subject: [PATCH] Allow passing environent variables to StreamProcessors Add support for an 'env' field to the StreamProcessor configuration and append the environment variables found there to the os.Environ() array. The env field takes environment variables in the form of key=value. Signed-off-by: Stefan Berger --- diff/stream.go | 4 ++-- diff/stream_unix.go | 3 ++- diff/stream_windows.go | 3 ++- services/server/config/config.go | 2 ++ services/server/server.go | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/diff/stream.go b/diff/stream.go index 1b625feaa..113e0f787 100644 --- a/diff/stream.go +++ b/diff/stream.go @@ -168,7 +168,7 @@ func (c *compressedProcessor) Close() error { return c.rc.Close() } -func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string, args []string) Handler { +func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string, args, env []string) Handler { set := make(map[string]struct{}, len(mediaTypes)) for _, m := range mediaTypes { set[m] = struct{}{} @@ -177,7 +177,7 @@ func BinaryHandler(id, returnsMediaType string, mediaTypes []string, path string if _, ok := set[mediaType]; ok { return func(ctx context.Context, stream StreamProcessor, payloads map[string]*types.Any) (StreamProcessor, error) { payload := payloads[id] - return NewBinaryProcessor(ctx, mediaType, returnsMediaType, stream, path, args, payload) + return NewBinaryProcessor(ctx, mediaType, returnsMediaType, stream, path, args, env, payload) }, true } return nil, false diff --git a/diff/stream_unix.go b/diff/stream_unix.go index 28f38d998..d79fd7154 100644 --- a/diff/stream_unix.go +++ b/diff/stream_unix.go @@ -33,9 +33,10 @@ import ( ) // NewBinaryProcessor returns a binary processor for use with processing content streams -func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args []string, payload *types.Any) (StreamProcessor, error) { +func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) { cmd := exec.CommandContext(ctx, name, args...) cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, env...) var payloadC io.Closer if payload != nil { diff --git a/diff/stream_windows.go b/diff/stream_windows.go index 8dadd72c9..19dcbacf5 100644 --- a/diff/stream_windows.go +++ b/diff/stream_windows.go @@ -39,9 +39,10 @@ import ( const processorPipe = "STREAM_PROCESSOR_PIPE" // NewBinaryProcessor returns a binary processor for use with processing content streams -func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args []string, payload *types.Any) (StreamProcessor, error) { +func NewBinaryProcessor(ctx context.Context, imt, rmt string, stream StreamProcessor, name string, args, env []string, payload *types.Any) (StreamProcessor, error) { cmd := exec.CommandContext(ctx, name, args...) cmd.Env = os.Environ() + cmd.Env = append(cmd.Env, env...) if payload != nil { data, err := proto.Marshal(payload) diff --git a/services/server/config/config.go b/services/server/config/config.go index 85c72f0d3..29ac6decb 100644 --- a/services/server/config/config.go +++ b/services/server/config/config.go @@ -80,6 +80,8 @@ type StreamProcessor struct { Path string `toml:"path"` // Args to the binary Args []string `toml:"args"` + // Environment variables for the binary + Env []string `toml:"env"` } // GetVersion returns the config file's version diff --git a/services/server/server.go b/services/server/server.go index 129425382..b6171f494 100644 --- a/services/server/server.go +++ b/services/server/server.go @@ -91,7 +91,7 @@ func New(ctx context.Context, config *srvconfig.Config) (*Server, error) { return nil, err } for id, p := range config.StreamProcessors { - diff.RegisterProcessor(diff.BinaryHandler(id, p.Returns, p.Accepts, p.Path, p.Args)) + diff.RegisterProcessor(diff.BinaryHandler(id, p.Returns, p.Accepts, p.Path, p.Args, p.Env)) } serverOpts := []grpc.ServerOption{