Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion gatewayd_plugins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,13 @@ gatewayd-plugin-test:
localPath: ../gatewayd-plugin-test/gatewayd-plugin-test
# Pass cmdline args to the plugin
args: ["--log-level", "info"]
# Pass environment variables to the plugin
# System-wide environment variables are passed to the plugin normally
# and they can be accessed via os.Environ().
# Defining any environment variables below will override system-wide environment variables.
env:
# The below environment variables are used by the plugin loader to verify the plugin's identity.
- MAGIC_COOKIE_KEY=GATEWAYD_PLUGIN
- MAGIC_COOKIE_VALUE=5712b87aa5d7e9f9e9ab643e6603181c5b796015cb1c09d6f5ada882bf2a1872
# Checksum hash to verify the binary before loading
checksum: a20e3a1529f4460ad24416a055d599f6f55f42ead836345a4bf0a17d056abb45
checksum: 1ce8c55e574491871b86f783000e7cfb82f7253b1d3a33a73dffee5fb8af97fe
1 change: 1 addition & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Impl struct {
ProjectURL string
LocalPath string
Args []string
Env []string
Enabled bool
// internal and external config options
Config map[string]string
Expand Down
7 changes: 5 additions & 2 deletions plugin/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package plugin

import (
"context"
"os/exec"

gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/gatewayd-io/gatewayd/logging"
Expand Down Expand Up @@ -133,6 +132,10 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) {
plugin.Args = args
}

if env := pluginConfig.Strings(name + ".env"); len(env) > 0 {
plugin.Env = append(plugin.Env, env...)
}

if checksum, ok := pluginConfig.Get(name + ".checksum").(string); !ok || checksum == "" {
reg.hooksConfig.Logger.Debug().Str("name", name).Msg(
"Checksum of plugin doesn't exist or is not set")
Expand Down Expand Up @@ -167,7 +170,7 @@ func (reg *RegistryImpl) LoadPlugins(pluginConfig *koanf.Koanf) {
&goplugin.ClientConfig{
HandshakeConfig: pluginV1.Handshake,
Plugins: pluginV1.GetPluginMap(plugin.ID.Name),
Cmd: exec.Command(plugin.LocalPath, plugin.Args...), //nolint:gosec
Cmd: NewCommand(plugin.LocalPath, plugin.Args, plugin.Env),
AllowedProtocols: []goplugin.Protocol{
goplugin.ProtocolGRPC,
},
Expand Down
10 changes: 10 additions & 0 deletions plugin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io"
"os"
"os/exec"

gerr "github.com/gatewayd-io/gatewayd/errors"
"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -55,3 +56,12 @@ func Verify(params, returnVal *structpb.Struct) bool {
cmpopts.EquateEmpty(),
})
}

// NewCommand returns a command with the given arguments and environment variables.
func NewCommand(cmd string, args []string, env []string) *exec.Cmd {
command := exec.Command(cmd, args...)
if env != nil {
command.Env = append(command.Env, env...)
}
return command
}