b6c05fae33
* enable registering backend muxed plugins in plugin catalog * set the sysview on the pluginconfig to allow enabling secrets/auth plugins * store backend instances in map * store single implementations in the instances map cleanup instance map and ensure we don't deadlock * fix system backend unit tests move GetMultiplexIDFromContext to pluginutil package fix pluginutil test fix dbplugin ut * return error(s) if we can't get the plugin client update comments * refactor/move GetMultiplexIDFromContext test * add changelog * remove unnecessary field on pluginClient * add unit tests to PluginCatalog for secrets/auth plugins * fix comment * return pluginClient from TestRunTestPlugin * add multiplexed backend test * honor metadatamode value in newbackend pluginconfig * check that connection exists on cleanup * add automtls to secrets/auth plugins * don't remove apiclientmeta parsing * use formatting directive for fmt.Errorf * fix ut: remove tls provider func * remove tlsproviderfunc from backend plugin tests * use env var to prevent test plugin from running as a unit test * WIP: remove lazy loading * move non lazy loaded backend to new package * use version wrapper for backend plugin factory * remove backendVersionWrapper type * implement getBackendPluginType for plugin catalog * handle backend plugin v4 registration * add plugin automtls env guard * modify plugin factory to determine the backend to use * remove old pluginsets from v5 and log pid in plugin catalog * add reload mechanism via context * readd v3 and v4 to pluginset * call cleanup from reload if non-muxed * move v5 backend code to new package * use context reload for for ErrPluginShutdown case * add wrapper on v5 backend * fix run config UTs * fix unit tests - use v4/v5 mapping for plugin versions - fix test build err - add reload method on fakePluginClient - add multiplexed cases for integration tests * remove comment and update AutoMTLS field in test * remove comment * remove errwrap and unused context * only support metadatamode false for v5 backend plugins * update plugin catalog errors * use const for env variables * rename locks and remove unused * remove unneeded nil check * improvements based on staticcheck recommendations * use const for single implementation string * use const for context key * use info default log level * move pid to pluginClient struct * remove v3 and v4 from multiplexed plugin set * return from reload when non-multiplexed * update automtls env string * combine getBackend and getBrokeredClient * update comments for plugin reload, Backend return val and log * revert Backend return type * allow non-muxed plugins to serve v5 * move v5 code to existing sdk plugin package * do next export sdk fields now that we have removed extra plugin pkg * set TLSProvider in ServeMultiplex for backwards compat * use bool to flag multiplexing support on grpc backend server * revert userpass main.go * refactor plugin sdk - update comments - make use of multiplexing boolean and single implementation ID const * update comment and use multierr * attempt v4 if dispense fails on getPluginTypeForUnknown * update comments on sdk plugin backend
180 lines
4 KiB
Go
180 lines
4 KiB
Go
package pluginutil
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"crypto/tls"
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
log "github.com/hashicorp/go-hclog"
|
|
"github.com/hashicorp/go-plugin"
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
"github.com/hashicorp/vault/sdk/version"
|
|
)
|
|
|
|
type PluginClientConfig struct {
|
|
Name string
|
|
PluginType consts.PluginType
|
|
Version string
|
|
PluginSets map[int]plugin.PluginSet
|
|
HandshakeConfig plugin.HandshakeConfig
|
|
Logger log.Logger
|
|
IsMetadataMode bool
|
|
AutoMTLS bool
|
|
MLock bool
|
|
Wrapper RunnerUtil
|
|
}
|
|
|
|
type runConfig struct {
|
|
// Provided by PluginRunner
|
|
command string
|
|
args []string
|
|
sha256 []byte
|
|
|
|
// Initialized with what's in PluginRunner.Env, but can be added to
|
|
env []string
|
|
|
|
PluginClientConfig
|
|
}
|
|
|
|
func (rc runConfig) makeConfig(ctx context.Context) (*plugin.ClientConfig, error) {
|
|
cmd := exec.Command(rc.command, rc.args...)
|
|
cmd.Env = append(cmd.Env, rc.env...)
|
|
|
|
// Add the mlock setting to the ENV of the plugin
|
|
if rc.MLock || (rc.Wrapper != nil && rc.Wrapper.MlockEnabled()) {
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginMlockEnabled, "true"))
|
|
}
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginVaultVersionEnv, version.GetVersion().Version))
|
|
|
|
if rc.IsMetadataMode {
|
|
rc.Logger = rc.Logger.With("metadata", "true")
|
|
}
|
|
metadataEnv := fmt.Sprintf("%s=%t", PluginMetadataModeEnv, rc.IsMetadataMode)
|
|
cmd.Env = append(cmd.Env, metadataEnv)
|
|
|
|
automtlsEnv := fmt.Sprintf("%s=%t", PluginAutoMTLSEnv, rc.AutoMTLS)
|
|
cmd.Env = append(cmd.Env, automtlsEnv)
|
|
|
|
var clientTLSConfig *tls.Config
|
|
if !rc.AutoMTLS && !rc.IsMetadataMode {
|
|
// Get a CA TLS Certificate
|
|
certBytes, key, err := generateCert()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Use CA to sign a client cert and return a configured TLS config
|
|
clientTLSConfig, err = createClientTLSConfig(certBytes, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Use CA to sign a server cert and wrap the values in a response wrapped
|
|
// token.
|
|
wrapToken, err := wrapServerConfig(ctx, rc.Wrapper, certBytes, key)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Add the response wrap token to the ENV of the plugin
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", PluginUnwrapTokenEnv, wrapToken))
|
|
}
|
|
|
|
secureConfig := &plugin.SecureConfig{
|
|
Checksum: rc.sha256,
|
|
Hash: sha256.New(),
|
|
}
|
|
|
|
clientConfig := &plugin.ClientConfig{
|
|
HandshakeConfig: rc.HandshakeConfig,
|
|
VersionedPlugins: rc.PluginSets,
|
|
Cmd: cmd,
|
|
SecureConfig: secureConfig,
|
|
TLSConfig: clientTLSConfig,
|
|
Logger: rc.Logger,
|
|
AllowedProtocols: []plugin.Protocol{
|
|
plugin.ProtocolNetRPC,
|
|
plugin.ProtocolGRPC,
|
|
},
|
|
AutoMTLS: rc.AutoMTLS,
|
|
}
|
|
return clientConfig, nil
|
|
}
|
|
|
|
func (rc runConfig) run(ctx context.Context) (*plugin.Client, error) {
|
|
clientConfig, err := rc.makeConfig(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client := plugin.NewClient(clientConfig)
|
|
return client, nil
|
|
}
|
|
|
|
type RunOpt func(*runConfig)
|
|
|
|
func Env(env ...string) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.env = append(rc.env, env...)
|
|
}
|
|
}
|
|
|
|
func Runner(wrapper RunnerUtil) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.Wrapper = wrapper
|
|
}
|
|
}
|
|
|
|
func PluginSets(pluginSets map[int]plugin.PluginSet) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.PluginSets = pluginSets
|
|
}
|
|
}
|
|
|
|
func HandshakeConfig(hs plugin.HandshakeConfig) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.HandshakeConfig = hs
|
|
}
|
|
}
|
|
|
|
func Logger(logger log.Logger) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.Logger = logger
|
|
}
|
|
}
|
|
|
|
func MetadataMode(isMetadataMode bool) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.IsMetadataMode = isMetadataMode
|
|
}
|
|
}
|
|
|
|
func AutoMTLS(autoMTLS bool) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.AutoMTLS = autoMTLS
|
|
}
|
|
}
|
|
|
|
func MLock(mlock bool) RunOpt {
|
|
return func(rc *runConfig) {
|
|
rc.MLock = mlock
|
|
}
|
|
}
|
|
|
|
func (r *PluginRunner) RunConfig(ctx context.Context, opts ...RunOpt) (*plugin.Client, error) {
|
|
rc := runConfig{
|
|
command: r.Command,
|
|
args: r.Args,
|
|
sha256: r.Sha256,
|
|
env: r.Env,
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
opt(&rc)
|
|
}
|
|
|
|
return rc.run(ctx)
|
|
}
|