diff --git a/gatewayd_plugins.yaml b/gatewayd_plugins.yaml index 23453432..1d5e252e 100644 --- a/gatewayd_plugins.yaml +++ b/gatewayd_plugins.yaml @@ -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 diff --git a/plugin/plugin.go b/plugin/plugin.go index cb81d68f..26adccd0 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -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 diff --git a/plugin/registry.go b/plugin/registry.go index c0adf32f..4085e64d 100644 --- a/plugin/registry.go +++ b/plugin/registry.go @@ -2,7 +2,6 @@ package plugin import ( "context" - "os/exec" gerr "github.com/gatewayd-io/gatewayd/errors" "github.com/gatewayd-io/gatewayd/logging" @@ -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") @@ -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, }, diff --git a/plugin/utils.go b/plugin/utils.go index aa8ae736..c500d73f 100644 --- a/plugin/utils.go +++ b/plugin/utils.go @@ -7,6 +7,7 @@ import ( "fmt" "io" "os" + "os/exec" gerr "github.com/gatewayd-io/gatewayd/errors" "github.com/google/go-cmp/cmp" @@ -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 +}