forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_storage.go
110 lines (96 loc) · 2.81 KB
/
grpc_storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package plugin
import (
"context"
"errors"
"google.golang.org/grpc"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/plugin/pb"
)
func newGRPCStorageClient(conn *grpc.ClientConn) *GRPCStorageClient {
return &GRPCStorageClient{
client: pb.NewStorageClient(conn),
}
}
// GRPCStorageClient is an implementation of logical.Storage that communicates
// over RPC.
type GRPCStorageClient struct {
client pb.StorageClient
}
func (s *GRPCStorageClient) List(ctx context.Context, prefix string) ([]string, error) {
reply, err := s.client.List(ctx, &pb.StorageListArgs{
Prefix: prefix,
}, largeMsgGRPCCallOpts...)
if err != nil {
return []string{}, err
}
if reply.Err != "" {
return reply.Keys, errors.New(reply.Err)
}
return reply.Keys, nil
}
func (s *GRPCStorageClient) Get(ctx context.Context, key string) (*logical.StorageEntry, error) {
reply, err := s.client.Get(ctx, &pb.StorageGetArgs{
Key: key,
}, largeMsgGRPCCallOpts...)
if err != nil {
return nil, err
}
if reply.Err != "" {
return nil, errors.New(reply.Err)
}
return pb.ProtoStorageEntryToLogicalStorageEntry(reply.Entry), nil
}
func (s *GRPCStorageClient) Put(ctx context.Context, entry *logical.StorageEntry) error {
reply, err := s.client.Put(ctx, &pb.StoragePutArgs{
Entry: pb.LogicalStorageEntryToProtoStorageEntry(entry),
}, largeMsgGRPCCallOpts...)
if err != nil {
return err
}
if reply.Err != "" {
return errors.New(reply.Err)
}
return nil
}
func (s *GRPCStorageClient) Delete(ctx context.Context, key string) error {
reply, err := s.client.Delete(ctx, &pb.StorageDeleteArgs{
Key: key,
})
if err != nil {
return err
}
if reply.Err != "" {
return errors.New(reply.Err)
}
return nil
}
// StorageServer is a net/rpc compatible structure for serving
type GRPCStorageServer struct {
impl logical.Storage
}
func (s *GRPCStorageServer) List(ctx context.Context, args *pb.StorageListArgs) (*pb.StorageListReply, error) {
keys, err := s.impl.List(ctx, args.Prefix)
return &pb.StorageListReply{
Keys: keys,
Err: pb.ErrToString(err),
}, nil
}
func (s *GRPCStorageServer) Get(ctx context.Context, args *pb.StorageGetArgs) (*pb.StorageGetReply, error) {
storageEntry, err := s.impl.Get(ctx, args.Key)
return &pb.StorageGetReply{
Entry: pb.LogicalStorageEntryToProtoStorageEntry(storageEntry),
Err: pb.ErrToString(err),
}, nil
}
func (s *GRPCStorageServer) Put(ctx context.Context, args *pb.StoragePutArgs) (*pb.StoragePutReply, error) {
err := s.impl.Put(ctx, pb.ProtoStorageEntryToLogicalStorageEntry(args.Entry))
return &pb.StoragePutReply{
Err: pb.ErrToString(err),
}, nil
}
func (s *GRPCStorageServer) Delete(ctx context.Context, args *pb.StorageDeleteArgs) (*pb.StorageDeleteReply, error) {
err := s.impl.Delete(ctx, args.Key)
return &pb.StorageDeleteReply{
Err: pb.ErrToString(err),
}, nil
}