2018-01-18 21:49:20 +00:00
|
|
|
package plugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-12 17:31:03 +00:00
|
|
|
"errors"
|
2018-01-18 21:49:20 +00:00
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-01-18 21:49:20 +00:00
|
|
|
plugin "github.com/hashicorp/go-plugin"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/helper/pluginutil"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/sdk/plugin/pb"
|
2018-01-18 21:49:20 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2019-02-12 17:31:03 +00:00
|
|
|
var ErrServerInMetadataMode = errors.New("plugin server can not perform action while in metadata mode")
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
type backendGRPCPluginServer struct {
|
|
|
|
broker *plugin.GRPCBroker
|
|
|
|
backend logical.Backend
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
factory logical.Factory
|
2018-01-18 21:49:20 +00:00
|
|
|
|
|
|
|
brokeredClient *grpc.ClientConn
|
|
|
|
|
|
|
|
logger log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup dials into the plugin's broker to get a shimmed storage, logger, and
|
|
|
|
// system view of the backend. This method also instantiates the underlying
|
|
|
|
// backend through its factory func for the server side of the plugin.
|
|
|
|
func (b *backendGRPCPluginServer) Setup(ctx context.Context, args *pb.SetupArgs) (*pb.SetupReply, error) {
|
|
|
|
// Dial for storage
|
|
|
|
brokeredClient, err := b.broker.Dial(args.BrokerID)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.SetupReply{}, err
|
|
|
|
}
|
|
|
|
b.brokeredClient = brokeredClient
|
|
|
|
storage := newGRPCStorageClient(brokeredClient)
|
|
|
|
sysView := newGRPCSystemView(brokeredClient)
|
|
|
|
|
|
|
|
config := &logical.BackendConfig{
|
|
|
|
StorageView: storage,
|
|
|
|
Logger: b.logger,
|
|
|
|
System: sysView,
|
|
|
|
Config: args.Config,
|
2018-03-21 19:04:27 +00:00
|
|
|
BackendUUID: args.BackendUUID,
|
2018-01-18 21:49:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call the underlying backend factory after shims have been created
|
|
|
|
// to set b.backend
|
2018-01-19 06:44:44 +00:00
|
|
|
backend, err := b.factory(ctx, config)
|
2018-01-18 21:49:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return &pb.SetupReply{
|
|
|
|
Err: pb.ErrToString(err),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
b.backend = backend
|
|
|
|
|
|
|
|
return &pb.SetupReply{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendGRPCPluginServer) HandleRequest(ctx context.Context, args *pb.HandleRequestArgs) (*pb.HandleRequestReply, error) {
|
2018-04-03 17:36:14 +00:00
|
|
|
if pluginutil.InMetadataMode() {
|
2018-01-18 21:49:20 +00:00
|
|
|
return &pb.HandleRequestReply{}, ErrServerInMetadataMode
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalReq, err := pb.ProtoRequestToLogicalRequest(args.Request)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.HandleRequestReply{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalReq.Storage = newGRPCStorageClient(b.brokeredClient)
|
|
|
|
|
|
|
|
resp, respErr := b.backend.HandleRequest(ctx, logicalReq)
|
|
|
|
|
|
|
|
pbResp, err := pb.LogicalResponseToProtoResponse(resp)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.HandleRequestReply{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &pb.HandleRequestReply{
|
|
|
|
Response: pbResp,
|
|
|
|
Err: pb.ErrToProtoErr(respErr),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-05 23:55:40 +00:00
|
|
|
func (b *backendGRPCPluginServer) Initialize(ctx context.Context, _ *pb.InitializeArgs) (*pb.InitializeReply, error) {
|
|
|
|
if pluginutil.InMetadataMode() {
|
|
|
|
return &pb.InitializeReply{}, ErrServerInMetadataMode
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &logical.InitializationRequest{
|
|
|
|
Storage: newGRPCStorageClient(b.brokeredClient),
|
|
|
|
}
|
|
|
|
|
|
|
|
respErr := b.backend.Initialize(ctx, req)
|
|
|
|
|
|
|
|
return &pb.InitializeReply{
|
|
|
|
Err: pb.ErrToProtoErr(respErr),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-01-18 21:49:20 +00:00
|
|
|
func (b *backendGRPCPluginServer) SpecialPaths(ctx context.Context, args *pb.Empty) (*pb.SpecialPathsReply, error) {
|
|
|
|
paths := b.backend.SpecialPaths()
|
2018-02-01 22:30:17 +00:00
|
|
|
if paths == nil {
|
|
|
|
return &pb.SpecialPathsReply{
|
|
|
|
Paths: nil,
|
|
|
|
}, nil
|
|
|
|
}
|
2018-01-18 21:49:20 +00:00
|
|
|
|
|
|
|
return &pb.SpecialPathsReply{
|
|
|
|
Paths: &pb.Paths{
|
|
|
|
Root: paths.Root,
|
|
|
|
Unauthenticated: paths.Unauthenticated,
|
|
|
|
LocalStorage: paths.LocalStorage,
|
|
|
|
SealWrapStorage: paths.SealWrapStorage,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendGRPCPluginServer) HandleExistenceCheck(ctx context.Context, args *pb.HandleExistenceCheckArgs) (*pb.HandleExistenceCheckReply, error) {
|
2018-04-03 17:36:14 +00:00
|
|
|
if pluginutil.InMetadataMode() {
|
2018-01-18 21:49:20 +00:00
|
|
|
return &pb.HandleExistenceCheckReply{}, ErrServerInMetadataMode
|
|
|
|
}
|
|
|
|
|
|
|
|
logicalReq, err := pb.ProtoRequestToLogicalRequest(args.Request)
|
|
|
|
if err != nil {
|
|
|
|
return &pb.HandleExistenceCheckReply{}, err
|
|
|
|
}
|
|
|
|
logicalReq.Storage = newGRPCStorageClient(b.brokeredClient)
|
|
|
|
|
|
|
|
checkFound, exists, err := b.backend.HandleExistenceCheck(ctx, logicalReq)
|
|
|
|
return &pb.HandleExistenceCheckReply{
|
|
|
|
CheckFound: checkFound,
|
|
|
|
Exists: exists,
|
|
|
|
Err: pb.ErrToProtoErr(err),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendGRPCPluginServer) Cleanup(ctx context.Context, _ *pb.Empty) (*pb.Empty, error) {
|
2018-01-19 06:44:44 +00:00
|
|
|
b.backend.Cleanup(ctx)
|
2018-01-18 21:49:20 +00:00
|
|
|
|
|
|
|
// Close rpc clients
|
|
|
|
b.brokeredClient.Close()
|
|
|
|
return &pb.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendGRPCPluginServer) InvalidateKey(ctx context.Context, args *pb.InvalidateKeyArgs) (*pb.Empty, error) {
|
2018-04-03 17:36:14 +00:00
|
|
|
if pluginutil.InMetadataMode() {
|
2018-01-18 21:49:20 +00:00
|
|
|
return &pb.Empty{}, ErrServerInMetadataMode
|
|
|
|
}
|
|
|
|
|
2018-01-19 06:44:44 +00:00
|
|
|
b.backend.InvalidateKey(ctx, args.Key)
|
2018-01-18 21:49:20 +00:00
|
|
|
return &pb.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *backendGRPCPluginServer) Type(ctx context.Context, _ *pb.Empty) (*pb.TypeReply, error) {
|
|
|
|
return &pb.TypeReply{
|
|
|
|
Type: uint32(b.backend.Type()),
|
|
|
|
}, nil
|
|
|
|
}
|