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
148 lines
3.8 KiB
Go
148 lines
3.8 KiB
Go
package plugin
|
|
|
|
import (
|
|
"context"
|
|
"net/rpc"
|
|
"sync"
|
|
|
|
uuid "github.com/hashicorp/go-uuid"
|
|
"github.com/hashicorp/vault/sdk/helper/consts"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
"github.com/hashicorp/vault/sdk/plugin"
|
|
bplugin "github.com/hashicorp/vault/sdk/plugin"
|
|
)
|
|
|
|
// Backend returns an instance of the backend, either as a plugin if external
|
|
// or as a concrete implementation if builtin, casted as logical.Backend.
|
|
func Backend(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
|
|
var b backend
|
|
name := conf.Config["plugin_name"]
|
|
pluginType, err := consts.ParsePluginType(conf.Config["plugin_type"])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sys := conf.System
|
|
|
|
raw, err := plugin.NewBackendV5(ctx, name, pluginType, sys, conf)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
b.Backend = raw
|
|
b.config = conf
|
|
|
|
return &b, nil
|
|
}
|
|
|
|
// backend is a thin wrapper around plugin.BackendPluginClientV5
|
|
type backend struct {
|
|
logical.Backend
|
|
mu sync.RWMutex
|
|
|
|
config *logical.BackendConfig
|
|
|
|
// Used to detect if we already reloaded
|
|
canary string
|
|
}
|
|
|
|
func (b *backend) reloadBackend(ctx context.Context) error {
|
|
pluginName := b.config.Config["plugin_name"]
|
|
pluginType, err := consts.ParsePluginType(b.config.Config["plugin_type"])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
b.Logger().Debug("plugin: reloading plugin backend", "plugin", pluginName)
|
|
|
|
// Ensure proper cleanup of the backend
|
|
// Pass a context value so that the plugin client will call the appropriate
|
|
// cleanup method for reloading
|
|
reloadCtx := context.WithValue(ctx, plugin.ContextKeyPluginReload, "reload")
|
|
b.Backend.Cleanup(reloadCtx)
|
|
|
|
nb, err := plugin.NewBackendV5(ctx, pluginName, pluginType, b.config.System, b.config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = nb.Setup(ctx, b.config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b.Backend = nb
|
|
|
|
return nil
|
|
}
|
|
|
|
// HandleRequest is a thin wrapper implementation of HandleRequest that includes automatic plugin reload.
|
|
func (b *backend) HandleRequest(ctx context.Context, req *logical.Request) (*logical.Response, error) {
|
|
b.mu.RLock()
|
|
canary := b.canary
|
|
resp, err := b.Backend.HandleRequest(ctx, req)
|
|
b.mu.RUnlock()
|
|
// Need to compare string value for case were err comes from plugin RPC
|
|
// and is returned as plugin.BasicError type.
|
|
if err != nil &&
|
|
(err.Error() == rpc.ErrShutdown.Error() || err == bplugin.ErrPluginShutdown) {
|
|
// Reload plugin if it's an rpc.ErrShutdown
|
|
b.mu.Lock()
|
|
if b.canary == canary {
|
|
err := b.reloadBackend(ctx)
|
|
if err != nil {
|
|
b.mu.Unlock()
|
|
return nil, err
|
|
}
|
|
b.canary, err = uuid.GenerateUUID()
|
|
if err != nil {
|
|
b.mu.Unlock()
|
|
return nil, err
|
|
}
|
|
}
|
|
b.mu.Unlock()
|
|
|
|
// Try request once more
|
|
b.mu.RLock()
|
|
defer b.mu.RUnlock()
|
|
return b.Backend.HandleRequest(ctx, req)
|
|
}
|
|
return resp, err
|
|
}
|
|
|
|
// HandleExistenceCheck is a thin wrapper implementation of HandleRequest that includes automatic plugin reload.
|
|
func (b *backend) HandleExistenceCheck(ctx context.Context, req *logical.Request) (bool, bool, error) {
|
|
b.mu.RLock()
|
|
canary := b.canary
|
|
checkFound, exists, err := b.Backend.HandleExistenceCheck(ctx, req)
|
|
b.mu.RUnlock()
|
|
if err != nil &&
|
|
(err.Error() == rpc.ErrShutdown.Error() || err == bplugin.ErrPluginShutdown) {
|
|
// Reload plugin if it's an rpc.ErrShutdown
|
|
b.mu.Lock()
|
|
if b.canary == canary {
|
|
err := b.reloadBackend(ctx)
|
|
if err != nil {
|
|
b.mu.Unlock()
|
|
return false, false, err
|
|
}
|
|
b.canary, err = uuid.GenerateUUID()
|
|
if err != nil {
|
|
b.mu.Unlock()
|
|
return false, false, err
|
|
}
|
|
}
|
|
b.mu.Unlock()
|
|
|
|
// Try request once more
|
|
b.mu.RLock()
|
|
defer b.mu.RUnlock()
|
|
return b.Backend.HandleExistenceCheck(ctx, req)
|
|
}
|
|
return checkFound, exists, err
|
|
}
|
|
|
|
// InvalidateKey is a thin wrapper used to ensure we grab the lock for race purposes
|
|
func (b *backend) InvalidateKey(ctx context.Context, key string) {
|
|
b.mu.RLock()
|
|
defer b.mu.RUnlock()
|
|
b.Backend.InvalidateKey(ctx, key)
|
|
}
|