Skip to content

Commit

Permalink
Revert "Slight refactor of system view interface"
Browse files Browse the repository at this point in the history
This reverts commit 73d420e.
  • Loading branch information
Brian Kassouf committed Feb 9, 2022
1 parent 73d420e commit f755270
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 47 deletions.
6 changes: 3 additions & 3 deletions sdk/database/dbplugin/v5/plugin_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type DatabasePluginClient struct {
client pluginutil.PluginInstance
client pluginutil.Multiplexer
Database
}

Expand All @@ -35,8 +35,8 @@ var PluginSets = map[int]plugin.PluginSet{

// NewPluginClient returns a databaseRPCClient with a connection to a running
// plugin.
func NewPluginClient(ctx context.Context, sys pluginutil.RunnerUtil, config pluginutil.PluginClientConfig) (Database, error) {
pluginClient, err := sys.NewPluginClient(ctx, config)
func NewPluginClient(ctx context.Context, sys pluginutil.RunnerUtil, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (Database, error) {
pluginClient, err := sys.NewPluginClient(ctx, pluginRunner, config)
if err != nil {
return nil, err
}
Expand Down
3 changes: 1 addition & 2 deletions sdk/database/dbplugin/v5/plugin_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,14 @@ func PluginFactory(ctx context.Context, pluginName string, sys pluginutil.LookRu
} else {
config := pluginutil.PluginClientConfig{
Name: pluginName,
PluginType: consts.PluginTypeDatabase,
PluginSets: PluginSets,
HandshakeConfig: HandshakeConfig,
Logger: namedLogger,
IsMetadataMode: false,
AutoMTLS: true,
}
// create a DatabasePluginClient instance
db, err = NewPluginClient(ctx, sys, config)
db, err = NewPluginClient(ctx, sys, pluginRunner, config)
if err != nil {
return nil, err
}
Expand Down
2 changes: 0 additions & 2 deletions sdk/helper/pluginutil/run_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import (

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
PluginSets map[int]plugin.PluginSet
HandshakeConfig plugin.HandshakeConfig
Logger log.Logger
Expand Down
4 changes: 2 additions & 2 deletions sdk/helper/pluginutil/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Looker interface {
// configuration and wrapping data in a response wrapped token.
// logical.SystemView implementations satisfy this interface.
type RunnerUtil interface {
NewPluginClient(ctx context.Context, config PluginClientConfig) (PluginInstance, error)
NewPluginClient(ctx context.Context, pluginRunner *PluginRunner, config PluginClientConfig) (Multiplexer, error)
ResponseWrapData(ctx context.Context, data map[string]interface{}, ttl time.Duration, jwt bool) (*wrapping.ResponseWrapInfo, error)
MlockEnabled() bool
}
Expand All @@ -33,7 +33,7 @@ type LookRunnerUtil interface {
RunnerUtil
}

type PluginInstance interface {
type Multiplexer interface {
Conn() grpc.ClientConnInterface
MultiplexingSupport() (bool, error)

Expand Down
4 changes: 2 additions & 2 deletions sdk/logical/system_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type SystemView interface {

// NewPluginClient returns a client for managing the lifecycle of plugin
// processes
NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (pluginutil.PluginInstance, error)
NewPluginClient(ctx context.Context, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (pluginutil.Multiplexer, error)

// MlockEnabled returns the configuration setting for enabling mlock on
// plugins.
Expand Down Expand Up @@ -156,7 +156,7 @@ func (d StaticSystemView) ReplicationState() consts.ReplicationState {
return d.ReplicationStateVal
}

func (d StaticSystemView) NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (pluginutil.PluginInstance, error) {
func (d StaticSystemView) NewPluginClient(ctx context.Context, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (pluginutil.Multiplexer, error) {
return nil, errors.New("NewPluginClient is not implemented in StaticSystemView")
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/plugin/grpc_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (s *gRPCSystemViewClient) ResponseWrapData(ctx context.Context, data map[st
return info, nil
}

func (s *gRPCSystemViewClient) NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (pluginutil.PluginInstance, error) {
func (s *gRPCSystemViewClient) NewPluginClient(ctx context.Context, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (pluginutil.Multiplexer, error) {
return nil, fmt.Errorf("cannot call NewPluginClient from a plugin backend")
}

Expand Down
2 changes: 1 addition & 1 deletion vault/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2028,7 +2028,7 @@ func (s standardUnsealStrategy) unseal(ctx context.Context, logger log.Logger, c
if err := c.handleVersionTimeStamps(ctx); err != nil {
return err
}
if err := c.setupPluginCatalog(ctx, c.enableMlock); err != nil {
if err := c.setupPluginCatalog(ctx); err != nil {
return err
}
if err := c.loadMounts(ctx); err != nil {
Expand Down
11 changes: 2 additions & 9 deletions vault/dynamic_system_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,8 @@ func (d dynamicSystemView) ResponseWrapData(ctx context.Context, data map[string
return resp.WrapInfo, nil
}

func (d dynamicSystemView) NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (pluginutil.PluginInstance, error) {
if d.core == nil {
return nil, fmt.Errorf("system view core is nil")
}
if d.core.pluginCatalog == nil {
return nil, fmt.Errorf("system view core plugin catalog is nil")
}

c, err := d.core.pluginCatalog.NewPluginClient(ctx, config)
func (d dynamicSystemView) NewPluginClient(ctx context.Context, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (pluginutil.Multiplexer, error) {
c, err := d.core.pluginCatalog.NewPluginClient(ctx, d, pluginRunner, config)
if err != nil {
return nil, err
}
Expand Down
32 changes: 7 additions & 25 deletions vault/plugin_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ type PluginCatalog struct {
// configuration using the same plugin will be routed to the existing
// plugin process.
externalPlugins map[string]*externalPlugin
mlockPlugins bool

lock sync.RWMutex
}
Expand Down Expand Up @@ -80,13 +79,12 @@ type externalPlugin struct {
connections map[string]*pluginClient
}

func (c *Core) setupPluginCatalog(ctx context.Context, mlockPlugins bool) error {
func (c *Core) setupPluginCatalog(ctx context.Context) error {
c.pluginCatalog = &PluginCatalog{
builtinRegistry: c.builtinRegistry,
catalogView: NewBarrierView(c.barrier, pluginCatalogPath),
directory: c.pluginDirectory,
logger: c.logger,
mlockPlugins: mlockPlugins,
}

// Run upgrade if untyped plugins exist
Expand Down Expand Up @@ -217,32 +215,18 @@ func (c *PluginCatalog) newExternalPlugin(pluginName string) *externalPlugin {

// NewPluginClient returns a client for managing the lifecycle of a plugin
// process
func (c *PluginCatalog) NewPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (*pluginClient, error) {
func (c *PluginCatalog) NewPluginClient(ctx context.Context, sys pluginutil.RunnerUtil, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (*pluginClient, error) {
c.lock.Lock()
pc, err := c.newPluginClient(ctx, config)
pc, err := c.newPluginClient(ctx, sys, pluginRunner, config)
c.lock.Unlock()
return pc, err
}

// newPluginClient returns a client for managing the lifecycle of a plugin
// process
func (c *PluginCatalog) newPluginClient(ctx context.Context, config pluginutil.PluginClientConfig) (*pluginClient, error) {
if config.Name == "" {
return nil, fmt.Errorf("no name provided for plugin")
}
if config.PluginType == consts.PluginTypeUnknown {
return nil, fmt.Errorf("no plugin type provided")
}

pluginRunner, err := c.get(ctx, config.Name, config.PluginType)
if err != nil {
return nil, fmt.Errorf("failed to lookup plugin: %w", err)
}
if pluginRunner == nil {
return nil, fmt.Errorf("no plugin found")
}

func (c *PluginCatalog) newPluginClient(ctx context.Context, sys pluginutil.RunnerUtil, pluginRunner *pluginutil.PluginRunner, config pluginutil.PluginClientConfig) (*pluginClient, error) {
extPlugin := c.getExternalPlugin(pluginRunner.Name)

id, err := base62.Random(10)
if err != nil {
return nil, err
Expand All @@ -255,17 +239,15 @@ func (c *PluginCatalog) newPluginClient(ctx context.Context, config pluginutil.P
return c.removePluginClient(pluginRunner.Name, id)
},
}

if !pluginRunner.MultiplexingSupport || len(extPlugin.connections) == 0 {
c.logger.Debug("spawning a new plugin process", "id", id)
client, err := pluginRunner.RunConfig(ctx,
pluginutil.Runner(sys),
pluginutil.PluginSets(config.PluginSets),
pluginutil.HandshakeConfig(config.HandshakeConfig),
pluginutil.Logger(config.Logger),
pluginutil.MetadataMode(config.IsMetadataMode),

// NewPluginClient only supports AutoMTLS today
pluginutil.AutoMTLS(true),
pluginutil.AutoMTLS(config.AutoMTLS),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit f755270

Please sign in to comment.