-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
257 lines (224 loc) · 8.21 KB
/
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
package grpc
import (
"context"
"sync/atomic"
"github.com/PumpkinSeed/heimdall/internal/errors"
"github.com/PumpkinSeed/heimdall/pkg/client"
"github.com/PumpkinSeed/heimdall/pkg/structs"
log "github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
const (
authorizationHeader = "authorization"
engineNameHeader = "engineName"
defaultEnginePath = "transit/"
)
type Options struct {
CaFile string
ServerAddr string
ServerHostOverride string
EngineName string
APIKey string
URLs []string
TLS bool
}
func (o Options) Setup() client.Client {
return &proxyClient{
o: o,
connections: buildConnections(o),
nxt: 0,
}
}
func buildConnections(o Options) []grpc.ClientConnInterface {
var res []grpc.ClientConnInterface
for _, u := range o.URLs {
conn, err := grpc.Dial(u, buildDialOptions(o)...)
if err != nil {
log.Debugf("gRPC connection error: %v", err)
log.Error(errors.Wrap(err, "grpc dial error", errors.CodeClientGrpcDial))
continue
}
res = append(res, conn)
}
return res
}
func buildDialOptions(o Options) []grpc.DialOption {
res := []grpc.DialOption{
grpc.WithChainUnaryInterceptor(
buildAuthInterceptor(o),
buildEngineNameInterceptor(o),
),
}
if o.TLS {
creds, err := credentials.NewClientTLSFromFile(o.CaFile, o.ServerHostOverride)
if err != nil {
log.Debugf("TLS error: %v", err)
log.Error(errors.Wrap(err, "grpc TLS file reading error", errors.CodeClientGrpcTLSError))
}
res = append(res, grpc.WithTransportCredentials(creds))
} else {
res = append(res, grpc.WithInsecure())
}
res = append(res, grpc.WithBlock())
return res
}
func buildAuthInterceptor(o Options) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(metadata.AppendToOutgoingContext(ctx, authorizationHeader, o.APIKey), method, req, reply, cc, opts...)
}
}
func buildEngineNameInterceptor(o Options) grpc.UnaryClientInterceptor {
return func(ctx context.Context, method string, req, reply interface{},
cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
return invoker(metadata.AppendToOutgoingContext(ctx, engineNameHeader, getEngineName(o)), method, req, reply, cc, opts...)
}
}
func getEngineName(o Options) string {
en := o.EngineName
if en == "" {
return defaultEnginePath
}
return en
}
type proxyClient struct {
o Options
connections []grpc.ClientConnInterface
nxt uint32
}
func (c *proxyClient) next() structs.EncryptionClient {
n := atomic.AddUint32(&c.nxt, 1)
// TODO check connection state
return structs.NewEncryptionClient(c.connections[(int(n)-1)%len(c.connections)])
}
func (c *proxyClient) CreateKey(ctx context.Context, key *structs.Key) (*structs.KeyResponse, error) {
createKey, err := c.next().CreateKey(ctx, key)
if err != nil {
return nil, errors.Wrap(err, "grpc client create key error", errors.CodeClientGrpcCreateKey)
}
return createKey, nil
}
func (c *proxyClient) ReadKey(ctx context.Context, keyName string) (*structs.KeyResponse, error) {
key, err := c.next().ReadKey(ctx, &structs.KeyName{Name: keyName})
if err != nil {
return nil, errors.Wrap(err, "grpc client read key error", errors.CodeClientGrpcReadKey)
}
return key, nil
}
func (c *proxyClient) DeleteKey(ctx context.Context, keyName string) (*structs.KeyResponse, error) {
key, err := c.next().DeleteKey(ctx, &structs.KeyName{Name: keyName})
if err != nil {
return nil, errors.Wrap(err, "grpc client delete key error", errors.CodeClientGrpcDeleteKey)
}
return key, nil
}
func (c *proxyClient) ListKeys(ctx context.Context) (*structs.KeyListResponse, error) {
keys, err := c.next().ListKeys(ctx, &structs.Empty{})
if err != nil {
return nil, errors.Wrap(err, "grpc client list keys error", errors.CodeClientGrpcListKey)
}
return keys, nil
}
func (c *proxyClient) Encrypt(ctx context.Context, encrypt *structs.EncryptRequest) (*structs.CryptoResult, error) {
result, err := c.next().Encrypt(ctx, encrypt)
if err != nil {
return nil, errors.Wrap(err, "grpc client encrypt error", errors.CodeClientGrpcEncrypt)
}
return result, nil
}
func (c *proxyClient) Decrypt(ctx context.Context, decrypt *structs.DecryptRequest) (*structs.CryptoResult, error) {
result, err := c.next().Decrypt(ctx, decrypt)
if err != nil {
return nil, errors.Wrap(err, "grpc client decrypt error", errors.CodeClientGrpcDecrypt)
}
return result, nil
}
func (c *proxyClient) Hash(ctx context.Context, hash *structs.HashRequest) (*structs.HashResponse, error) {
response, err := c.next().Hash(ctx, hash)
if err != nil {
return nil, errors.Wrap(err, "grpc client hash error", errors.CodeClientGrpcHash)
}
return response, nil
}
func (c *proxyClient) GenerateHMAC(ctx context.Context, hmac *structs.HMACRequest) (*structs.HMACResponse, error) {
generateHMAC, err := c.next().GenerateHMAC(ctx, hmac)
if err != nil {
return nil, errors.Wrap(err, "grpc client hmac error", errors.CodeClientGrpcHmac)
}
return generateHMAC, nil
}
func (c *proxyClient) Sign(ctx context.Context, in *structs.SignParameters) (*structs.SignResponse, error) {
sign, err := c.next().Sign(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client sign error", errors.CodeClientGrpcSign)
}
return sign, nil
}
func (c *proxyClient) VerifySigned(ctx context.Context, in *structs.VerificationRequest) (*structs.VerificationResponse, error) {
signed, err := c.next().VerifySigned(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client verify sign error", errors.CodeClientGrpcVerifySign)
}
return signed, nil
}
func (c *proxyClient) Rewrap(ctx context.Context, in *structs.RewrapRequest) (*structs.CryptoResult, error) {
out, err := c.next().Rewrap(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client rewrap error", errors.CodeClientGrpcRewrap)
}
return out, nil
}
func (c *proxyClient) UpdateKeyConfiguration(ctx context.Context, in *structs.KeyConfig) (*structs.Empty, error) {
out, err := c.next().UpdateKeyConfiguration(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client update key config error", errors.CodeClientGrpcUpdateKeyConfig)
}
return out, nil
}
func (c *proxyClient) RotateKey(ctx context.Context, in *structs.RotateRequest) (*structs.Empty, error) {
out, err := c.next().RotateKey(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client rotate key error", errors.CodeClientGrpcRotateKey)
}
return out, nil
}
func (c *proxyClient) ExportKey(ctx context.Context, in *structs.ExportRequest) (*structs.ExportResult, error) {
out, err := c.next().ExportKey(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client export key error", errors.CodeClientGrpcExportKey)
}
return out, nil
}
func (c *proxyClient) BackupKey(ctx context.Context, in *structs.BackupRequest) (*structs.BackupResult, error) {
out, err := c.next().BackupKey(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client backup key error", errors.CodeClientGrpcBackupKey)
}
return out, nil
}
func (c *proxyClient) RestoreKey(ctx context.Context, in *structs.RestoreRequest) (*structs.Empty, error) {
out, err := c.next().RestoreKey(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client restore key error", errors.CodeClientGrpcRestoreKey)
}
return out, nil
}
func (c *proxyClient) GenerateKey(ctx context.Context, in *structs.GenerateKeyRequest) (*structs.GenerateKeyResponse, error) {
out, err := c.next().GenerateKey(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client generate key error", errors.CodeClientGrpcGenerateKey)
}
return out, nil
}
func (c *proxyClient) GenerateRandomBytes(ctx context.Context, in *structs.GenerateBytesRequest) (*structs.GenerateBytesResponse, error) {
out, err := c.next().GenerateRandomBytes(ctx, in)
if err != nil {
return nil, errors.Wrap(err, "grpc client generate random bytes error", errors.CodeClientGrpcGenerateRandomBytes)
}
return out, nil
}
func (c *proxyClient) Health(ctx context.Context, in *structs.HealthRequest) (*structs.HealthResponse, error) {
return c.next().Health(ctx, in)
}