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

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package dbplugin
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/hashicorp/go-plugin"
    10  	"github.com/hashicorp/vault/sdk/database/dbplugin/v5/proto"
    11  	"github.com/hashicorp/vault/sdk/helper/pluginutil"
    12  	"github.com/hashicorp/vault/sdk/logical"
    13  	"google.golang.org/grpc"
    14  )
    15  
    16  // handshakeConfigs are used to just do a basic handshake between
    17  // a plugin and host. If the handshake fails, a user friendly error is shown.
    18  // This prevents users from executing bad plugins or executing a plugin
    19  // directory. It is a UX feature, not a security feature.
    20  var HandshakeConfig = plugin.HandshakeConfig{
    21  	MagicCookieKey:   "VAULT_DATABASE_PLUGIN",
    22  	MagicCookieValue: "926a0820-aea2-be28-51d6-83cdf00e8edb",
    23  }
    24  
    25  // Factory is the factory function to create a dbplugin Database.
    26  type Factory func() (interface{}, error)
    27  
    28  type GRPCDatabasePlugin struct {
    29  	FactoryFunc Factory
    30  	Impl        Database
    31  
    32  	// Embeding this will disable the netRPC protocol
    33  	plugin.NetRPCUnsupportedPlugin
    34  }
    35  
    36  var (
    37  	_ plugin.Plugin     = &GRPCDatabasePlugin{}
    38  	_ plugin.GRPCPlugin = &GRPCDatabasePlugin{}
    39  )
    40  
    41  func (d GRPCDatabasePlugin) GRPCServer(_ *plugin.GRPCBroker, s *grpc.Server) error {
    42  	var server gRPCServer
    43  
    44  	if d.Impl != nil {
    45  		server = gRPCServer{singleImpl: d.Impl}
    46  	} else {
    47  		// multiplexing is supported
    48  		server = gRPCServer{
    49  			factoryFunc: d.FactoryFunc,
    50  			instances:   make(map[string]Database),
    51  		}
    52  
    53  		// Multiplexing is enabled for this plugin, register the server so we
    54  		// can tell the client in Vault.
    55  		pluginutil.RegisterPluginMultiplexingServer(s, pluginutil.PluginMultiplexingServerImpl{
    56  			Supported: true,
    57  		})
    58  	}
    59  
    60  	proto.RegisterDatabaseServer(s, &server)
    61  	logical.RegisterPluginVersionServer(s, &server)
    62  	return nil
    63  }
    64  
    65  func (GRPCDatabasePlugin) GRPCClient(doneCtx context.Context, _ *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
    66  	client := gRPCClient{
    67  		client:        proto.NewDatabaseClient(c),
    68  		versionClient: logical.NewPluginVersionClient(c),
    69  		doneCtx:       doneCtx,
    70  	}
    71  	return client, nil
    72  }