github.com/hashicorp/vault/sdk@v0.13.0/database/dbplugin/server.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package dbplugin
     5  
     6  import (
     7  	"crypto/tls"
     8  	fmt "fmt"
     9  
    10  	plugin "github.com/hashicorp/go-plugin"
    11  	"github.com/hashicorp/vault/sdk/helper/pluginutil"
    12  )
    13  
    14  // Serve is called from within a plugin and wraps the provided
    15  // Database implementation in a databasePluginRPCServer object and starts a
    16  // RPC server.
    17  func Serve(db Database, tlsProvider func() (*tls.Config, error)) {
    18  	plugin.Serve(ServeConfig(db, tlsProvider))
    19  }
    20  
    21  func ServeConfig(db Database, tlsProvider func() (*tls.Config, error)) *plugin.ServeConfig {
    22  	err := pluginutil.OptionallyEnableMlock()
    23  	if err != nil {
    24  		fmt.Println(err)
    25  		return nil
    26  	}
    27  
    28  	// pluginSets is the map of plugins we can dispense.
    29  	pluginSets := map[int]plugin.PluginSet{
    30  		// Version 3 used to supports both protocols. We want to keep it around
    31  		// since it's possible old plugins built against this version will still
    32  		// work with gRPC. There is currently no difference between version 3
    33  		// and version 4.
    34  		3: {
    35  			"database": &GRPCDatabasePlugin{
    36  				Impl: db,
    37  			},
    38  		},
    39  		4: {
    40  			"database": &GRPCDatabasePlugin{
    41  				Impl: db,
    42  			},
    43  		},
    44  	}
    45  
    46  	conf := &plugin.ServeConfig{
    47  		HandshakeConfig:  handshakeConfig,
    48  		VersionedPlugins: pluginSets,
    49  		TLSProvider:      tlsProvider,
    50  		GRPCServer:       plugin.DefaultGRPCServer,
    51  	}
    52  
    53  	return conf
    54  }