vault/builtin/logical/database/dbplugin/client.go

76 lines
2 KiB
Go
Raw Normal View History

2017-04-06 15:20:10 -04:00
package dbplugin
import (
"context"
"errors"
2017-04-06 15:20:10 -04:00
"sync"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/vault/helper/pluginutil"
log "github.com/mgutz/logxi/v1"
2017-04-06 15:20:10 -04:00
)
2017-04-11 14:50:34 -04:00
// DatabasePluginClient embeds a databasePluginRPCClient and wraps it's Close
2017-04-06 15:20:10 -04:00
// method to also call Kill() on the plugin.Client.
type DatabasePluginClient struct {
client *plugin.Client
sync.Mutex
Database
2017-04-06 15:20:10 -04:00
}
// This wraps the Close call and ensures we both close the database connection
// and kill the plugin.
2017-04-06 15:20:10 -04:00
func (dc *DatabasePluginClient) Close() error {
err := dc.Database.Close()
2017-04-06 15:20:10 -04:00
dc.client.Kill()
return err
}
// newPluginClient returns a databaseRPCClient with a connection to a running
// plugin. The client is wrapped in a DatabasePluginClient object to ensure the
// plugin is killed on call of Close().
func newPluginClient(ctx context.Context, sys pluginutil.RunnerUtil, pluginRunner *pluginutil.PluginRunner, logger log.Logger) (Database, error) {
2017-04-06 15:20:10 -04:00
// pluginMap is the map of plugins we can dispense.
var pluginMap = map[string]plugin.Plugin{
"database": new(DatabasePlugin),
}
client, err := pluginRunner.Run(ctx, sys, pluginMap, handshakeConfig, []string{}, logger)
2017-04-06 15:20:10 -04:00
if err != nil {
return nil, err
}
// Connect via RPC
rpcClient, err := client.Client()
if err != nil {
return nil, err
}
// Request the plugin
raw, err := rpcClient.Dispense("database")
if err != nil {
return nil, err
}
2017-04-12 19:41:06 -04:00
// We should have a database type now. This feels like a normal interface
2017-04-06 15:20:10 -04:00
// implementation but is in fact over an RPC connection.
var db Database
switch raw.(type) {
case *gRPCClient:
db = raw.(*gRPCClient)
case *databasePluginRPCClient:
logger.Warn("database: plugin is using deprecated net RPC transport, recompile plugin to upgrade to gRPC", "plugin", pluginRunner.Name)
db = raw.(*databasePluginRPCClient)
default:
return nil, errors.New("unsupported client type")
}
2017-04-06 15:20:10 -04:00
2017-04-12 19:41:06 -04:00
// Wrap RPC implimentation in DatabasePluginClient
2017-04-06 15:20:10 -04:00
return &DatabasePluginClient{
client: client,
Database: db,
2017-04-06 15:20:10 -04:00
}, nil
}