mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-23 07:31:09 +02:00
51 lines
1.3 KiB
Go
51 lines
1.3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
//go:build !enterprise
|
|
|
|
package dbplugin
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"github.com/hashicorp/vault/sdk/database/dbplugin/v5/proto"
|
|
"github.com/hashicorp/vault/sdk/helper/pluginutil"
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
)
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Request the plugin
|
|
raw, err := pluginClient.Dispense("database")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// We should have a database type now. This feels like a normal interface
|
|
// implementation but is in fact over an RPC connection.
|
|
var db Database
|
|
switch c := raw.(type) {
|
|
case gRPCClient:
|
|
// This is an abstraction leak from go-plugin but it is necessary in
|
|
// order to enable multiplexing on multiplexed plugins
|
|
c.client = proto.NewDatabaseClient(pluginClient.Conn())
|
|
c.versionClient = logical.NewPluginVersionClient(pluginClient.Conn())
|
|
|
|
db = c
|
|
default:
|
|
return nil, errors.New("unsupported client type")
|
|
}
|
|
|
|
return &DatabasePluginClient{
|
|
client: pluginClient,
|
|
Database: db,
|
|
}, nil
|
|
}
|