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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package dbplugin
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/hashicorp/go-plugin"
    10  	"github.com/hashicorp/vault/sdk/helper/pluginutil"
    11  )
    12  
    13  // Serve is called from within a plugin and wraps the provided
    14  // Database implementation in a databasePluginRPCServer object and starts a
    15  // RPC server.
    16  func Serve(db Database) {
    17  	plugin.Serve(ServeConfig(db))
    18  }
    19  
    20  func ServeConfig(db Database) *plugin.ServeConfig {
    21  	err := pluginutil.OptionallyEnableMlock()
    22  	if err != nil {
    23  		fmt.Println(err)
    24  		return nil
    25  	}
    26  
    27  	// pluginSets is the map of plugins we can dispense.
    28  	pluginSets := map[int]plugin.PluginSet{
    29  		5: {
    30  			"database": &GRPCDatabasePlugin{
    31  				Impl: db,
    32  			},
    33  		},
    34  	}
    35  
    36  	conf := &plugin.ServeConfig{
    37  		HandshakeConfig:  HandshakeConfig,
    38  		VersionedPlugins: pluginSets,
    39  		GRPCServer:       plugin.DefaultGRPCServer,
    40  	}
    41  
    42  	return conf
    43  }
    44  
    45  func ServeMultiplex(factory Factory) {
    46  	plugin.Serve(ServeConfigMultiplex(factory))
    47  }
    48  
    49  func ServeConfigMultiplex(factory Factory) *plugin.ServeConfig {
    50  	err := pluginutil.OptionallyEnableMlock()
    51  	if err != nil {
    52  		fmt.Println(err)
    53  		return nil
    54  	}
    55  
    56  	db, err := factory()
    57  	if err != nil {
    58  		fmt.Println(err)
    59  		return nil
    60  	}
    61  
    62  	database := db.(Database)
    63  
    64  	// pluginSets is the map of plugins we can dispense.
    65  	pluginSets := map[int]plugin.PluginSet{
    66  		5: {
    67  			"database": &GRPCDatabasePlugin{
    68  				Impl: database,
    69  			},
    70  		},
    71  		6: {
    72  			"database": &GRPCDatabasePlugin{
    73  				FactoryFunc: factory,
    74  			},
    75  		},
    76  	}
    77  
    78  	conf := &plugin.ServeConfig{
    79  		HandshakeConfig:  HandshakeConfig,
    80  		VersionedPlugins: pluginSets,
    81  		GRPCServer:       plugin.DefaultGRPCServer,
    82  	}
    83  
    84  	return conf
    85  }