-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
grpc_client.go
297 lines (251 loc) · 7.67 KB
/
grpc_client.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbplugin
import (
"context"
"errors"
"fmt"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/hashicorp/vault/sdk/database/dbplugin/v5/proto"
"github.com/hashicorp/vault/sdk/helper/pluginutil"
"github.com/hashicorp/vault/sdk/logical"
)
var (
_ Database = gRPCClient{}
_ logical.PluginVersioner = gRPCClient{}
ErrPluginShutdown = errors.New("plugin shutdown")
)
type gRPCClient struct {
client proto.DatabaseClient
versionClient logical.PluginVersionClient
doneCtx context.Context
}
func (c gRPCClient) PluginVersion() logical.PluginVersion {
version, _ := c.versionClient.Version(context.Background(), &logical.Empty{})
if version != nil {
return logical.PluginVersion{Version: version.PluginVersion}
}
return logical.EmptyPluginVersion
}
func (c gRPCClient) Initialize(ctx context.Context, req InitializeRequest) (InitializeResponse, error) {
rpcReq, err := initReqToProto(req)
if err != nil {
return InitializeResponse{}, err
}
rpcResp, err := c.client.Initialize(ctx, rpcReq)
if err != nil {
return InitializeResponse{}, fmt.Errorf("unable to initialize: %s", err.Error())
}
return initRespFromProto(rpcResp)
}
func initReqToProto(req InitializeRequest) (*proto.InitializeRequest, error) {
config, err := mapToStruct(req.Config)
if err != nil {
return nil, fmt.Errorf("unable to marshal config: %w", err)
}
rpcReq := &proto.InitializeRequest{
ConfigData: config,
VerifyConnection: req.VerifyConnection,
}
return rpcReq, nil
}
func initRespFromProto(rpcResp *proto.InitializeResponse) (InitializeResponse, error) {
newConfig := structToMap(rpcResp.GetConfigData())
resp := InitializeResponse{
Config: newConfig,
}
return resp, nil
}
func (c gRPCClient) NewUser(ctx context.Context, req NewUserRequest) (NewUserResponse, error) {
ctx, cancel := context.WithCancel(ctx)
quitCh := pluginutil.CtxCancelIfCanceled(cancel, c.doneCtx)
defer close(quitCh)
defer cancel()
rpcReq, err := newUserReqToProto(req)
if err != nil {
return NewUserResponse{}, err
}
rpcResp, err := c.client.NewUser(ctx, rpcReq)
if err != nil {
if c.doneCtx.Err() != nil {
return NewUserResponse{}, ErrPluginShutdown
}
return NewUserResponse{}, fmt.Errorf("unable to create new user: %w", err)
}
return newUserRespFromProto(rpcResp)
}
func newUserReqToProto(req NewUserRequest) (*proto.NewUserRequest, error) {
switch req.CredentialType {
case CredentialTypePassword:
if req.Password == "" {
return nil, fmt.Errorf("missing password credential")
}
case CredentialTypeRSAPrivateKey:
if len(req.PublicKey) == 0 {
return nil, fmt.Errorf("missing public key credential")
}
case CredentialTypeClientCertificate:
if req.Subject == "" {
return nil, fmt.Errorf("missing certificate subject")
}
default:
return nil, fmt.Errorf("unknown credential type")
}
expiration, err := ptypes.TimestampProto(req.Expiration)
if err != nil {
return nil, fmt.Errorf("unable to marshal expiration date: %w", err)
}
rpcReq := &proto.NewUserRequest{
UsernameConfig: &proto.UsernameConfig{
DisplayName: req.UsernameConfig.DisplayName,
RoleName: req.UsernameConfig.RoleName,
},
CredentialType: int32(req.CredentialType),
Password: req.Password,
PublicKey: req.PublicKey,
Subject: req.Subject,
Expiration: expiration,
Statements: &proto.Statements{
Commands: req.Statements.Commands,
},
RollbackStatements: &proto.Statements{
Commands: req.RollbackStatements.Commands,
},
}
return rpcReq, nil
}
func newUserRespFromProto(rpcResp *proto.NewUserResponse) (NewUserResponse, error) {
resp := NewUserResponse{
Username: rpcResp.GetUsername(),
}
return resp, nil
}
func (c gRPCClient) UpdateUser(ctx context.Context, req UpdateUserRequest) (UpdateUserResponse, error) {
rpcReq, err := updateUserReqToProto(req)
if err != nil {
return UpdateUserResponse{}, err
}
rpcResp, err := c.client.UpdateUser(ctx, rpcReq)
if err != nil {
if c.doneCtx.Err() != nil {
return UpdateUserResponse{}, ErrPluginShutdown
}
return UpdateUserResponse{}, fmt.Errorf("unable to update user: %w", err)
}
return updateUserRespFromProto(rpcResp)
}
func updateUserReqToProto(req UpdateUserRequest) (*proto.UpdateUserRequest, error) {
if req.Username == "" {
return nil, fmt.Errorf("missing username")
}
if (req.Password == nil || req.Password.NewPassword == "") &&
(req.PublicKey == nil || len(req.PublicKey.NewPublicKey) == 0) &&
(req.Expiration == nil || req.Expiration.NewExpiration.IsZero()) {
return nil, fmt.Errorf("missing changes")
}
expiration, err := expirationToProto(req.Expiration)
if err != nil {
return nil, fmt.Errorf("unable to parse new expiration date: %w", err)
}
var password *proto.ChangePassword
if req.Password != nil && req.Password.NewPassword != "" {
password = &proto.ChangePassword{
NewPassword: req.Password.NewPassword,
Statements: &proto.Statements{
Commands: req.Password.Statements.Commands,
},
}
}
var publicKey *proto.ChangePublicKey
if req.PublicKey != nil && len(req.PublicKey.NewPublicKey) > 0 {
publicKey = &proto.ChangePublicKey{
NewPublicKey: req.PublicKey.NewPublicKey,
Statements: &proto.Statements{
Commands: req.PublicKey.Statements.Commands,
},
}
}
rpcReq := &proto.UpdateUserRequest{
Username: req.Username,
CredentialType: int32(req.CredentialType),
Password: password,
PublicKey: publicKey,
Expiration: expiration,
}
return rpcReq, nil
}
func updateUserRespFromProto(rpcResp *proto.UpdateUserResponse) (UpdateUserResponse, error) {
// Placeholder for future conversion if data is returned
return UpdateUserResponse{}, nil
}
func expirationToProto(exp *ChangeExpiration) (*proto.ChangeExpiration, error) {
if exp == nil {
return nil, nil
}
expiration, err := ptypes.TimestampProto(exp.NewExpiration)
if err != nil {
return nil, err
}
changeExp := &proto.ChangeExpiration{
NewExpiration: expiration,
Statements: &proto.Statements{
Commands: exp.Statements.Commands,
},
}
return changeExp, nil
}
func (c gRPCClient) DeleteUser(ctx context.Context, req DeleteUserRequest) (DeleteUserResponse, error) {
rpcReq, err := deleteUserReqToProto(req)
if err != nil {
return DeleteUserResponse{}, err
}
rpcResp, err := c.client.DeleteUser(ctx, rpcReq)
if err != nil {
if c.doneCtx.Err() != nil {
return DeleteUserResponse{}, ErrPluginShutdown
}
return DeleteUserResponse{}, fmt.Errorf("unable to delete user: %w", err)
}
return deleteUserRespFromProto(rpcResp)
}
func deleteUserReqToProto(req DeleteUserRequest) (*proto.DeleteUserRequest, error) {
if req.Username == "" {
return nil, fmt.Errorf("missing username")
}
rpcReq := &proto.DeleteUserRequest{
Username: req.Username,
Statements: &proto.Statements{
Commands: req.Statements.Commands,
},
}
return rpcReq, nil
}
func deleteUserRespFromProto(rpcResp *proto.DeleteUserResponse) (DeleteUserResponse, error) {
// Placeholder for future conversion if data is returned
return DeleteUserResponse{}, nil
}
func (c gRPCClient) Type() (string, error) {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
typeResp, err := c.client.Type(ctx, &proto.Empty{})
if err != nil {
if c.doneCtx.Err() != nil {
return "", ErrPluginShutdown
}
return "", fmt.Errorf("unable to get database plugin type: %w", err)
}
return typeResp.GetType(), nil
}
func (c gRPCClient) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_, err := c.client.Close(ctx, &proto.Empty{})
if err != nil {
if c.doneCtx.Err() != nil {
return ErrPluginShutdown
}
return err
}
return nil
}