open-vault/sdk/database/dbplugin/server.go

55 lines
1.3 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2019-04-15 15:36:10 +00:00
package dbplugin
import (
"crypto/tls"
2019-04-15 17:38:08 +00:00
fmt "fmt"
2019-04-15 15:36:10 +00:00
plugin "github.com/hashicorp/go-plugin"
2019-04-15 17:38:08 +00:00
"github.com/hashicorp/vault/sdk/helper/pluginutil"
2019-04-15 15:36:10 +00:00
)
// Serve is called from within a plugin and wraps the provided
// Database implementation in a databasePluginRPCServer object and starts a
// RPC server.
func Serve(db Database, tlsProvider func() (*tls.Config, error)) {
plugin.Serve(ServeConfig(db, tlsProvider))
}
func ServeConfig(db Database, tlsProvider func() (*tls.Config, error)) *plugin.ServeConfig {
2019-04-15 17:38:08 +00:00
err := pluginutil.OptionallyEnableMlock()
if err != nil {
fmt.Println(err)
return nil
}
2019-04-15 15:36:10 +00:00
// pluginSets is the map of plugins we can dispense.
pluginSets := map[int]plugin.PluginSet{
// Version 3 used to supports both protocols. We want to keep it around
// since it's possible old plugins built against this version will still
// work with gRPC. There is currently no difference between version 3
// and version 4.
3: {
2019-04-15 15:36:10 +00:00
"database": &GRPCDatabasePlugin{
Impl: db,
},
},
4: {
2019-04-15 15:36:10 +00:00
"database": &GRPCDatabasePlugin{
Impl: db,
},
},
}
conf := &plugin.ServeConfig{
HandshakeConfig: handshakeConfig,
VersionedPlugins: pluginSets,
TLSProvider: tlsProvider,
GRPCServer: plugin.DefaultGRPCServer,
}
return conf
}